-
Notifications
You must be signed in to change notification settings - Fork 7
/
ModelRenderer.cpp
208 lines (181 loc) · 6.04 KB
/
ModelRenderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* The Simple Earth Project
* Copyright (C) 2022 HueSoft LLC
* Author: Hector Mendoza, hector.mendoza@huesoftllc.com
*
* This file is part of the Simple Earth Project.
*
* The Simple Earth Project is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version
* 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <QtOpenGL>
#include "ModelRenderer.h"
#include "IconModelManager.h"
#include "WorldObject.h"
#ifdef USING_ASSIMP
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#endif
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/**
* Constructor. Initializes attributes and calls parent constructor.
*/
ModelRenderer::ModelRenderer(const QString& filePath) : MeshRenderer()
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
mFilePath = filePath;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/**
* Destructor.
*/
ModelRenderer::~ModelRenderer()
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/**
* OVERRIDE FOR MeshRenderer. Sets appropriate transformations and calls
* recursiveRender to render Assimp scene.
*/
void ModelRenderer::render()
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
glPushMatrix();
//first rotate and translate coordinate system to object
//coordinate system
SimpleVector position = mWorldObject->getPosition();
GeodeticPosition geoPosition = mWorldObject->getGeodeticPosition();
glRotatef(geoPosition.longitude - 180.0f, 0.0f, 0.0f, 1.0f);
glRotatef(-1.0f * (90.0f - geoPosition.latitude), 0.0f, 1.0f, 0.0f);
float radius = sqrt(position.x*position.x + position.y*position.y + position.z*position.z);
glTranslatef(0.0f, 0.0f, radius);
//rotate around X, Y and Z axis, note rotations
//are counterclockwise in OpenGL, that is why we
//give the negative rotation values
SimpleVector rotation = mWorldObject->getRotation();
glRotatef(-rotation.x, 1.0f, 0.0f, 0.0f);
glRotatef(-rotation.y, 0.0f, 1.0f, 0.0f);
glRotatef(-rotation.z, 0.0f, 0.0f, 1.0f);
//set scale
SimpleVector scale = mWorldObject->getScale();
glScalef(scale.x, scale.y, scale.z);
void* modelData = NULL;
int texture = -1;
bool modelLoaded = IconModelManager::getInstance()->getModel(mFilePath, modelData, texture);
if (modelLoaded)
{
#ifdef USING_ASSIMP
//cast model data to assimp's scene struct in order to access root node
struct aiScene* scene = (struct aiScene*)modelData;
recursiveRender((void*)scene, (void*)scene->mRootNode, texture);
#endif
}
glPopMatrix();
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/**
* Returns the file path if the model associated with this renderer.
*
* @return The file path if the model associated with this renderer
*/
const QString& ModelRenderer::getFilePath() const
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
return mFilePath;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/**
* Sets the file path of the model associated with this renderer.
*
* @param filePath File path to model
*/
void ModelRenderer::setFilePath(const QString& filePath)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
mFilePath = filePath;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/**
* This function is a slight modification of the recursive_render function in
* Assimp's SimpleOpenGL example. It renders the given scene recursively.
*
* @param scene Handle to scene (root node)
* @param node Handle to node
* @param texture Texture index
*/
void ModelRenderer::recursiveRender(void* scene, void* node, int texture)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
#ifdef USING_ASSIMP
struct aiScene* sc = (struct aiScene*)scene;
struct aiNode* nd = (struct aiNode*)node;
unsigned int i;
unsigned int n = 0, t;
aiMatrix4x4 m = nd->mTransformation;
//update transform
aiTransposeMatrix4(&m);
glPushMatrix();
glMultMatrixf((float*)&m);
//draw all meshes assigned to this node
for (n = 0; n < nd->mNumMeshes; ++n)
{
const struct aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]];
for (t = 0; t < mesh->mNumFaces; ++t)
{
const struct aiFace* face = &mesh->mFaces[t];
GLenum face_mode;
switch(face->mNumIndices)
{
case 1: face_mode = GL_POINTS; break;
case 2: face_mode = GL_LINES; break;
case 3: face_mode = GL_TRIANGLES; break;
default: face_mode = GL_POLYGON; break;
}
//bind texture
if (texture >= 0)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
}
glBegin(face_mode);
for (i = 0; i < face->mNumIndices; i++)
{
int index = face->mIndices[i];
if (texture >= 0)
{
if (mesh->HasTextureCoords(0))
{
const aiVector3D* pTexCoord = &(mesh->mTextureCoords[0][index]);
glTexCoord2f(pTexCoord->x, pTexCoord->y);
}
}
glVertex3fv(&mesh->mVertices[index].x);
}
glEnd();
if (texture >= 0)
{
glDisable(GL_TEXTURE_2D);
}
}
}
//draw children nodes
for (n = 0; n < nd->mNumChildren; ++n)
{
recursiveRender(sc, nd->mChildren[n], texture);
}
glPopMatrix();
#endif
}