Repository navigation
Expand file tree
/
Copy pathModelContainer.cpp
More file actions
426 lines (300 loc) · 11 KB
/
Copy pathModelContainer.cpp
File metadata and controls
426 lines (300 loc) · 11 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// +------------------------------------------------------------+
// | University Racer |
// | Projekt do PGR a GMU, FIT VUT v Brne, 2011 |
// +------------------------------------------------------------+
// | Autori: Tomáš Kimer, xkimer00@stud.fit.vutbr.cz |
// | Tomáš Sychra, xsychr03@stud.fit.vutbr.cz |
// | David Šabata, xsabat01@stud.fit.vutbr.cz |
// +------------------------------------------------------------+
#include "ModelContainer.h"
#include <stdexcept>
#ifdef _DEBUG
#define new MYDEBUG_NEW
#endif
#define TEX2_SUFFIX "_normal"
#define TEX3_SUFFIX "_height"
#define TEX_EXT ".bmp"
using namespace std;
ModelContainer::ModelContainer(void)
{
totalFacesCount = 0;
}
ModelContainer::~ModelContainer(void)
{
// uvolnit vsechny modely
for (vector<BaseModel*>::iterator it = models.begin(); it != models.end(); it++)
{
delete (*it);
}
}
vector<BaseModel*> const &ModelContainer::getModels()
{
return models;
}
vector<ModelContainer::DRAWINGQUEUEITEM> const &ModelContainer::getDrawingQueue()
{
return drawingQueue;
}
vector<ModelContainer::MESHDRAWINGQUEUEITEM> const &ModelContainer::getMeshDrawingQueue()
{
return meshDrawingQueue;
}
void ModelContainer::optimizeDrawingQueue()
{
/**
* [material] => [
* [Mesh*] => index do kreslici fronty
* [Mesh*] => ...
* ],
* [material] => ...
*/
typedef map<string, vector<pair<Mesh*, unsigned int>>> MATERIALMAP;
// asociativni pole materialu ve scene a meshi (s vazbou na puvodni model) ktere je pouzivaji
MATERIALMAP materials;
// rozskatulkovat meshe podle pouziteho materialu
for (unsigned int i = 0; i < drawingQueue.size(); i++)
{
BaseModel* model = drawingQueue[i].model;
for (vector<Mesh*>::iterator meshIt = model->getMeshes().begin(); meshIt != model->getMeshes().end(); meshIt++)
{
Mesh* mesh = (*meshIt);
// najit material
MATERIALMAP::iterator matIt = materials.find( mesh->getMaterialName() );
// pokud material neexistuje, vyrobit novy zaznam
if (matIt == materials.end()) {
// vlozi prazdny zaznam a vraci jeho iterator
matIt = materials.insert(
pair<string, vector<pair<Mesh*, unsigned int>>>( mesh->getMaterialName(), vector<pair<Mesh*, unsigned int>>() )
).first;
}
// pridat mesh do spravne 'skatulky' a zapamatovat si index puvodni polozky
(*matIt).second.push_back(pair<Mesh*, unsigned int>(mesh, i));
}
}
// prochazet postupne materialy
for (MATERIALMAP::iterator matIt = materials.begin(); matIt != materials.end(); matIt++)
{
// a jimi kreslene meshe
for (vector<pair<Mesh*, unsigned int>>::iterator meshIt = (*matIt).second.begin(); meshIt != (*matIt).second.end(); meshIt++)
{
Mesh* mesh = (*meshIt).first;
unsigned int drawingQueueIndex = (*meshIt).second;
// pridat do kreslici fronty meshi
MESHDRAWINGQUEUEITEM item = { mesh, drawingQueue[drawingQueueIndex].matrix };
meshDrawingQueue.push_back(item);
// najit model v mapovani
map<unsigned int, vector<unsigned int>>::iterator mappingIt = drawingQueuesMapping.find(drawingQueueIndex);
// vytvorit zaznam pokud neni
if (mappingIt == drawingQueuesMapping.end()) {
mappingIt = drawingQueuesMapping.insert( pair<unsigned int, vector<unsigned int>>( drawingQueueIndex, vector<unsigned int>() ) ).first;
}
// zapamatovat si, kteremu modelu tato mesh patrila (pro naslednou upravu matic) - index do meshDrawingQueue
(*mappingIt).second.push_back( meshDrawingQueue.size() - 1 );
}
}
}
void ModelContainer::addModel(string name, BaseModel *model)
{
// pridame model
models.push_back(model);
modelNames.insert( pair<string, unsigned int>(name, models.size()) );
// pridame info o offsetu jeho indexu
modelsIndexOffsets.insert(pair<BaseModel*, unsigned int>(model, totalFacesCount * 3));
// info o indexech meshi
unsigned int offset = totalFacesCount * 3;
for (vector<Mesh*>::iterator it = model->getMeshes().begin(); it != model->getMeshes().end(); it++)
{
meshesIndexOffsets.insert(pair<Mesh*, unsigned int>((*it), offset));
offset += (*it)->getFaces().size() * 3;
}
// a aktualizujeme pocet facu v kontejneru
totalFacesCount += model->facesCount();
}
unsigned int ModelContainer::queueDraw(string modelName)
{
return queueDraw(modelName, glm::mat4(1.0));
}
unsigned int ModelContainer::queueDraw(string modelName, glm::mat4 mat)
{
map<string, unsigned int>::iterator it = modelNames.find(modelName);
if (it == modelNames.end())
runtime_error("Model queued for drawing has not been loaded into container");
return queueDraw(models.at((*it).second), mat);
}
unsigned int ModelContainer::queueDraw(BaseModel* model)
{
return queueDraw(model, glm::mat4(1.0));
}
unsigned int ModelContainer::queueDraw(BaseModel* model, glm::mat4 mat)
{
// zaradit do modelove fronty
DRAWINGQUEUEITEM item = {model, mat};
drawingQueue.push_back(item);
return drawingQueue.size() - 1;
}
void ModelContainer::updateDrawingMatrix(unsigned int drawingQueueIndex, glm::mat4 matrix)
{
// najit indexy ve fronte meshi
vector<unsigned int> meshIndices = drawingQueuesMapping[drawingQueueIndex];
// aktualizovat vsem matice
for (vector<unsigned int>::iterator it = meshIndices.begin(); it != meshIndices.end(); it++)
{
meshDrawingQueue[(*it)].matrix = matrix;
}
}
unsigned int ModelContainer::getModelIndexOffset(BaseModel* model)
{
return modelsIndexOffsets[model];
}
unsigned int ModelContainer::getMeshIndexOffset(Mesh* mesh)
{
return meshesIndexOffsets[mesh];
}
unsigned int ModelContainer::verticesCount()
{
unsigned int total = 0;
for (vector<BaseModel*>::iterator it = models.begin(); it != models.end(); it++)
{
for (vector<Mesh*>::iterator mit = (*it)->getMeshes().begin(); mit != (*it)->getMeshes().end(); mit++)
{
total += (*mit)->getVertices().size();
}
}
return total;
}
unsigned int ModelContainer::facesCount()
{
return totalFacesCount;
}
unsigned int ModelContainer::modelsCount()
{
return models.size();
}
void ModelContainer::addLight(Light light)
{
lights.push_back(light);
}
vector<Light> &ModelContainer::getLights()
{
return lights;
}
bool fileExists(string filename)
{
ifstream ifile(filename);
return (bool)ifile;
}
BaseModel* ModelContainer::load3DS(string filename)
{
// parsovani souboru
Scene3DS *scene = new Scene3DS(filename.c_str());
vector<Mesh3DSObject> meshes = scene->Meshes();
// vytvorime novy model
BaseModel* model = new BaseModel;
vector<Mesh*> modelMeshes;
for (vector<Mesh3DSObject>::iterator it = meshes.begin(); it != meshes.end(); it++)
{
Mesh3DSObject mesh = (*it);
// nazev
string name = mesh.Name();
// nazev materialu (jen prvni, pokud je vice)
string material = "";
if (mesh.Materials().size() > 0)
{
material = (*mesh.Materials().begin()).first;
}
// vrcholy
vector<glm::vec3> vertices;
for (unsigned int i = 0; i < mesh.Vertices().size(); i++)
{
Mesh3DSVertex vert = mesh.Vertices()[i];
glm::vec4 glmvert(vert.x, vert.y, vert.z, 1);
// 3DS modely jsou Z-up; transformovat na Y-up
glm::mat4 rotate = glm::rotate(glm::mat4(), 90.0f, glm::vec3(1.0f, 0.0f, 0.0f));
glmvert = glmvert * rotate;
vertices.push_back(glm::vec3(glmvert));
}
// indexy
vector<glm::ivec3> faces;
for (unsigned int i = 0; i < mesh.Faces().size(); i++)
{
Mesh3DSFace face = mesh.Faces()[i];
faces.push_back( glm::ivec3(face.a, face.b, face.c) );
}
// texturovaci souradnice
vector<glm::vec2> texcoords;
for (unsigned int i = 0; i < mesh.TextureCoords().size(); i++)
{
Mesh3DSTextureCoord coord = mesh.TextureCoords()[i];
texcoords.push_back( glm::vec2(coord.u, coord.v) );
}
if (faces.size() > 0) {
Mesh* m = new Mesh(name, material, vertices, faces, texcoords);
modelMeshes.push_back(m);
}
}
// ulozit materialy
vector<Material3DSObject> materials = scene->Materials();
for (vector<Material3DSObject>::iterator it = materials.begin(); it != materials.end(); it++) {
Material3DSObject m = (*it);
ShaderManager::MATERIALPARAMS params;
params.ambient = glm::vec4( m.AmbientColor().r, m.AmbientColor().g, m.AmbientColor().b, 1.0f );
params.diffuse = glm::vec4( m.DiffuseColor().r, m.DiffuseColor().g, m.DiffuseColor().b, 1.0f );
params.specular = glm::vec4( m.SpecularColor().r, m.SpecularColor().g, m.SpecularColor().b, 1.0f );
params.shininess = m.Shininess();
// nacist textury do GL, pokud odpovidajici soubory existuji
// hleda se automaticky zakladni textura, normalova a vyskova
string path = ShaderManager::getTexturesPath();
if (fileExists(path + m.Texture())) {
params.textures.push_back( ShaderManager::loadTexture(path + m.Texture()) );
string::size_type pos = m.Texture().find_last_of(".");
string baseName = m.Texture().substr(0, pos);
string normalTexture = path + baseName + TEX2_SUFFIX + TEX_EXT;
if (fileExists(normalTexture))
params.textures.push_back( ShaderManager::loadTexture(normalTexture) );
string heightTexture = path + baseName + TEX3_SUFFIX + TEX_EXT;
if (fileExists(heightTexture))
params.textures.push_back( ShaderManager::loadTexture(heightTexture) );
}
// za shader povazujeme vse pred podtrzitkem, pokud se v nazvu vyskytuje
string::size_type pos = m.Name().find("_");
if (pos != string::npos)
ShaderManager::addMaterial(m.Name().substr(0, pos));
else
ShaderManager::addMaterial(m.Name());
// priradit parametry materialu vsem meshim ktere ho pouzivaji
for (vector<Mesh*>::iterator meshIt = modelMeshes.begin(); meshIt != modelMeshes.end(); meshIt++)
{
if ((*meshIt)->getMaterialName() == m.Name())
(*meshIt)->setMaterialParams(params);
}
#if 0
cout << "--- in " << filename << " ---------------------------------------------" << endl;
cout << "Material: " << m.Name() << endl;
cout << "Ambient: " << m.AmbientColor().r << " / " << m.AmbientColor().g << " / " << m.AmbientColor().b << endl;
cout << "Diffuse: " << m.DiffuseColor().r << " / " << m.DiffuseColor().g << " / " << m.DiffuseColor().b << endl;
cout << "Specular: " << m.SpecularColor().r << " / " << m.SpecularColor().g << " / " << m.SpecularColor().b << endl;
cout << "Shininess percentage: " << m.Shininess() << endl;
cout << "Texture: " << m.Texture() << endl;
#endif
}
// ulozit svetla
vector<Light3DSObject> parsedLights = scene->Lights();
for (vector<Light3DSObject>::iterator it = parsedLights.begin(); it != parsedLights.end(); it++) {
// vse v 3DS je Z-up; transformovat na Y-up
Light3DSObject::POSITION pos = (*it).Position();
glm::vec4 glmvert(pos.x, pos.y, pos.z, 1);
glm::mat4 rotate = glm::rotate(glm::mat4(), 90.0f, glm::vec3(1.0f, 0.0f, 0.0f));
glmvert = glmvert * rotate;
// TMP: scalovat svetlo stejne jako se bude scalovat ucebna
// TODO: globalni matice kontejneru ktera ovlivni i svetla
glm::mat4 modelmat = glm::scale(glm::mat4(), glm::vec3(0.05));
glmvert = glmvert * modelmat;
// pridat svetla s defaultnimi barevnymi vlastnostmi (bile)
Light l = Light(glmvert);
addLight(l);
}
delete scene;
// ulozit a vratit model
model->setMeshes(modelMeshes);
return model;
}