-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelManager.cpp
274 lines (218 loc) · 7.85 KB
/
ModelManager.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* File: ModelManager.cpp
* Author: bdi_usr
*
* Created on 10 de mayo de 2012, 4:10
*/
#include "ModelManager.h"
#define aisgl_min(x,y) (x<y?x:y)
#define aisgl_max(x,y) (y>x?y:x)
ModelManager* ModelManager::m_inst(0);
ModelManager* ModelManager::Inst() {
if (!m_inst)
m_inst = new ModelManager();
return m_inst;
}
void get_bounding_box_for_node(const aiScene* scene, const struct aiNode* nd,
aiVector3D* min,
aiVector3D* max,
aiMatrix4x4* trafo
) {
struct aiMatrix4x4 prev;
unsigned int n = 0, t;
prev = *trafo;
aiMultiplyMatrix4(trafo, &nd->mTransformation);
for (; n < nd->mNumMeshes; ++n) {
const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
for (t = 0; t < mesh->mNumVertices; ++t) {
struct aiVector3D tmp = mesh->mVertices[t];
aiTransformVecByMatrix4(&tmp, trafo);
min->x = aisgl_min(min->x, tmp.x);
min->y = aisgl_min(min->y, tmp.y);
min->z = aisgl_min(min->z, tmp.z);
max->x = aisgl_max(max->x, tmp.x);
max->y = aisgl_max(max->y, tmp.y);
max->z = aisgl_max(max->z, tmp.z);
}
}
for (n = 0; n < nd->mNumChildren; ++n) {
get_bounding_box_for_node(scene, nd->mChildren[n], min, max, trafo);
}
*trafo = prev;
}
void get_bounding_box(const aiScene* scene, aiVector3D* min, aiVector3D* max) {
aiMatrix4x4 trafo;
aiIdentityMatrix4(&trafo);
min->x = min->y = min->z = 1e10f;
max->x = max->y = max->z = -1e10f;
get_bounding_box_for_node(scene, scene->mRootNode, min, max, &trafo);
}
// ----------------------------------------------------------------------------
void color4_to_float4(const struct aiColor4D *c, float f[4]) {
f[0] = c->r;
f[1] = c->g;
f[2] = c->b;
f[3] = c->a;
}
// ----------------------------------------------------------------------------
void set_float4(float f[4], float a, float b, float c, float d) {
f[0] = a;
f[1] = b;
f[2] = c;
f[3] = d;
}
// ----------------------------------------------------------------------------
void apply_material(const struct aiMaterial *mtl) {
float c[4];
GLenum fill_mode;
int ret1, ret2;
struct aiColor4D diffuse;
struct aiColor4D specular;
struct aiColor4D ambient;
struct aiColor4D emission;
float shininess, strength;
int two_sided;
int wireframe;
unsigned int max;
set_float4(c, 0.8f, 0.8f, 0.8f, 1.0f);
if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse))
color4_to_float4(&diffuse, c);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, c);
set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f);
if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &specular))
color4_to_float4(&specular, c);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, c);
set_float4(c, 0.2f, 0.2f, 0.2f, 1.0f);
if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &ambient))
color4_to_float4(&ambient, c);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, c);
set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f);
if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_EMISSIVE, &emission))
color4_to_float4(&emission, c);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, c);
max = 1;
ret1 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS, &shininess, &max);
if (ret1 == AI_SUCCESS) {
max = 1;
ret2 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS_STRENGTH, &strength, &max);
if (ret2 == AI_SUCCESS)
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess * strength);
else
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
} else {
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0.0f);
set_float4(c, 0.0f, 0.0f, 0.0f, 0.0f);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, c);
}
max = 1;
if (AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_ENABLE_WIREFRAME, &wireframe, &max))
fill_mode = wireframe ? GL_LINE : GL_FILL;
else
fill_mode = GL_FILL;
//glPolygonMode(GL_FRONT_AND_BACK, fill_mode);
max = 1;
if ((AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_TWOSIDED, &two_sided, &max)) && two_sided)
glDisable(GL_CULL_FACE);
else
glEnable(GL_CULL_FACE);
}
// ----------------------------------------------------------------------------
void recursive_render(const struct aiScene *scene, const struct aiNode* nd)
{
unsigned int i;
unsigned int n = 0, t;
struct aiMatrix4x4 m = nd->mTransformation;
// update transform
aiTransposeMatrix4(&m);
glPushMatrix();
glMultMatrixf((float*) &m);
// draw all meshes assigned to this node
for (; n < nd->mNumMeshes; ++n) {
const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
apply_material(scene->mMaterials[mesh->mMaterialIndex]);
//if (mesh->mNormals == NULL) {
//glDisable(GL_LIGHTING);
//} else {
//glEnable(GL_LIGHTING);
//}
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;
}
glBegin(face_mode);
for (i = 0; i < face->mNumIndices; i++) {
int index = face->mIndices[i];
if (mesh->mColors[0] != NULL)
glColor4fv((GLfloat*) & mesh->mColors[0][index]);
if (mesh->mNormals != NULL)
glNormal3fv(&mesh->mNormals[index].x);
glVertex3fv(&mesh->mVertices[index].x);
}
glEnd();
}
}
// draw all children
for (n = 0; n < nd->mNumChildren; ++n) {
recursive_render(scene, nd->mChildren[n]);
}
glPopMatrix();
}
Modelo* ModelManager::loadasset(const char* path)
{
const aiScene* scene = aiImportFile(path, aiProcessPreset_TargetRealtime_MaxQuality);
Modelo* nuevo_modelo = new Modelo(scene);
aiVector3D* scene_min = new aiVector3D();
aiVector3D* scene_max = new aiVector3D();
aiVector3D* scene_center = new aiVector3D();
if (scene) {
get_bounding_box(scene, scene_min, scene_max);
scene_center->x = (scene_min->x + scene_max->x) / 2.0f;
scene_center->y = (scene_min->y + scene_max->y) / 2.0f;
scene_center->z = (scene_min->z + scene_max->z) / 2.0f;
}
nuevo_modelo->SetAiVector3d(scene_min, scene_max, scene_center);
nuevo_modelo->SetFactorEscalamiento(8.0);
GLuint scene_list = glGenLists(1);
glNewList(scene_list, GL_COMPILE);
// now begin at the root node of the imported data and traverse
// the scenegraph by multiplying subsequent local transforms
// together on GL's matrix stack.
recursive_render(scene, scene->mRootNode);
glEndList();
nuevo_modelo->SetDisplayList(scene_list);
return nuevo_modelo;
}
bool ModelManager::LoadMesh(const char* path, const unsigned int texID) {
m_texID[texID] = this->loadasset(path);
}
bool ModelManager::Render(const unsigned int texID)
{
bool result(true);
//if this texture ID mapped, bind it's texture as current
if (m_texID.find(texID) != m_texID.end())
m_texID[texID]->Render();
//otherwise, binding failed
else
result = false;
return result;
}
Modelo* ModelManager::GetModelo(const unsigned int texID)
{
if (m_texID.find(texID) != m_texID.end())
return m_texID[texID];
else
return NULL;
}
ModelManager::ModelManager()
{}
ModelManager::~ModelManager() {
}