1 | #include "ResourceManager.h"
|
---|
2 | #include "Matrix4x4.h"
|
---|
3 | #include "Geometry.h"
|
---|
4 | #include "SceneEntity.h"
|
---|
5 | #include "Material.h"
|
---|
6 | #include "Texture.h"
|
---|
7 | #include "gzstream.h"
|
---|
8 | #include "Vector3.h"
|
---|
9 | #include "Transform3.h"
|
---|
10 | #include "Shape.h"
|
---|
11 | #include "LODInfo.h"
|
---|
12 | #include "RenderState.h"
|
---|
13 | #include "ShaderProgram.h"
|
---|
14 | #include "ShaderManager.h"
|
---|
15 |
|
---|
16 |
|
---|
17 | #ifdef _CRT_SET
|
---|
18 | #define _CRTDBG_MAP_ALLOC
|
---|
19 | #include <stdlib.h>
|
---|
20 | #include <crtdbg.h>
|
---|
21 |
|
---|
22 | // redefine new operator
|
---|
23 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
24 | #define new DEBUG_NEW
|
---|
25 | #endif
|
---|
26 |
|
---|
27 |
|
---|
28 | using namespace std;
|
---|
29 |
|
---|
30 |
|
---|
31 | namespace CHCDemoEngine
|
---|
32 | {
|
---|
33 |
|
---|
34 |
|
---|
35 | // only instance of the resource manager
|
---|
36 | ResourceManager *ResourceManager::sResourceManager = NULL;
|
---|
37 |
|
---|
38 |
|
---|
39 | ResourceManager::ResourceManager()
|
---|
40 | {
|
---|
41 | mUseNormalMapping = false;
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | ResourceManager::~ResourceManager()
|
---|
46 | {
|
---|
47 | // delete all resources.
|
---|
48 | CLEAR_CONTAINER(mSceneEntities);
|
---|
49 | CLEAR_CONTAINER(mGeometry);
|
---|
50 | CLEAR_CONTAINER(mMaterials);
|
---|
51 | CLEAR_CONTAINER(mTextures);
|
---|
52 | CLEAR_CONTAINER(mTrafos);
|
---|
53 | CLEAR_CONTAINER(mShapes);
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | void ResourceManager::DelSingleton()
|
---|
58 | {
|
---|
59 | DEL_PTR(sResourceManager);
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | SceneEntity *ResourceManager::LoadSceneEntity(igzstream &str)
|
---|
64 | {
|
---|
65 | bool hasTrafo;
|
---|
66 | str.read(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
|
---|
67 |
|
---|
68 | SceneEntity *sceneGeom;
|
---|
69 | Transform3 *trafo;
|
---|
70 |
|
---|
71 | if (!hasTrafo)
|
---|
72 | {
|
---|
73 | trafo = new Transform3(); // identity
|
---|
74 | }
|
---|
75 | else
|
---|
76 | {
|
---|
77 | Matrix4x4 m;
|
---|
78 | str.read(reinterpret_cast<char *>(m.x), sizeof(Matrix4x4));
|
---|
79 | trafo = new Transform3(m);
|
---|
80 | }
|
---|
81 |
|
---|
82 | mTrafos.push_back(trafo);
|
---|
83 |
|
---|
84 | sceneGeom = new SceneEntity(trafo);
|
---|
85 |
|
---|
86 |
|
---|
87 | ///////////////
|
---|
88 | //-- load LOD levels
|
---|
89 |
|
---|
90 | // note: lods must be ordered from smallest distance to largest
|
---|
91 | int numLODs;
|
---|
92 | str.read(reinterpret_cast<char *>(&numLODs), sizeof(int));
|
---|
93 |
|
---|
94 | for (int i = 0; i < numLODs; ++ i)
|
---|
95 | {
|
---|
96 | // the distance until the next LOD level is used
|
---|
97 | float dist;
|
---|
98 | str.read(reinterpret_cast<char *>(&dist), sizeof(float));
|
---|
99 |
|
---|
100 | int numShapes;
|
---|
101 | str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
|
---|
102 |
|
---|
103 | //LODLevel *lodLevel = new LODLevel(dist);
|
---|
104 | LODLevel lodLevel(dist);
|
---|
105 |
|
---|
106 | for (int j = 0; j < numShapes; ++ j)
|
---|
107 | {
|
---|
108 | int shapeId;
|
---|
109 | str.read(reinterpret_cast<char *>(&shapeId), sizeof(int));
|
---|
110 |
|
---|
111 | Geometry *geom = mGeometryTable[shapeId];
|
---|
112 | Material *mat = mMaterialTable[shapeId];
|
---|
113 |
|
---|
114 | // create shape
|
---|
115 | Shape *shape = new Shape(geom, mat);
|
---|
116 |
|
---|
117 | mShapes.push_back(shape);
|
---|
118 |
|
---|
119 | sceneGeom->AddShape(shape);
|
---|
120 | lodLevel.AddShape(shape);
|
---|
121 | }
|
---|
122 |
|
---|
123 | //mLODs.push_back(lodLevel);
|
---|
124 | sceneGeom->AddLODLevel(lodLevel);
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | static ShaderProgram *sTreeAnimationProgram =
|
---|
129 | ShaderManager::GetSingleton()->GetShaderProgram("treeAnimation");
|
---|
130 | static ShaderProgram *sTreeAnimationProgramMrt =
|
---|
131 | ShaderManager::GetSingleton()->GetShaderProgram("treeAnimationMrt");
|
---|
132 |
|
---|
133 |
|
---|
134 | ///////////
|
---|
135 | //-- hack: add tree animation (should be done per material script!)
|
---|
136 |
|
---|
137 | if (numLODs > 1)
|
---|
138 | {
|
---|
139 | ShapeContainer::iterator sstart, send;
|
---|
140 |
|
---|
141 | // only use shader for the first two lod levels (the non-billboards)
|
---|
142 | for (int i = 0; i < min(numLODs, 2); ++ i)
|
---|
143 | //for (int i = 0; i < numLODs; ++ i)
|
---|
144 | {
|
---|
145 | sceneGeom->GetLODLevel(i, sstart, send);
|
---|
146 |
|
---|
147 | for (ShapeContainer::iterator it = sstart; it != send; ++ it)
|
---|
148 | {
|
---|
149 | Shape *shape = *it;
|
---|
150 |
|
---|
151 | Material *mat = shape->GetMaterial();
|
---|
152 |
|
---|
153 | // add the vertex animation program
|
---|
154 | for (int i = 0; i < 3; ++ i)
|
---|
155 | {
|
---|
156 | Technique *tech = mat->GetTechnique(i);
|
---|
157 |
|
---|
158 | GPUProgramParameters *vtxParams = tech->GetVertexProgramParameters();
|
---|
159 |
|
---|
160 | if (i == 0)
|
---|
161 | {
|
---|
162 | tech->SetVertexProgram(sTreeAnimationProgram);
|
---|
163 | vtxParams->SetLightDirParam(5);
|
---|
164 | }
|
---|
165 | else
|
---|
166 | {
|
---|
167 | tech->SetVertexProgram(sTreeAnimationProgramMrt);
|
---|
168 | vtxParams->SetOldTimerParam(5);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /// use a timer to simulate the moving of the tree in the wind
|
---|
172 | vtxParams->SetTimerParam(0);
|
---|
173 |
|
---|
174 | // wind direction
|
---|
175 | static Vector3 windDir = Normalize(Vector3(0.8f, 0.2f, 0.0f));
|
---|
176 | vtxParams->SetValue3f(1, windDir.x, windDir.y, windDir.z);
|
---|
177 | // amplitude
|
---|
178 | vtxParams->SetValue1f(2, 0.3f);
|
---|
179 |
|
---|
180 | AxisAlignedBox3 box = sceneGeom->GetBoundingBox();
|
---|
181 | vtxParams->SetValue2f(3, box.Min().z, box.Max().z);
|
---|
182 | // frequency
|
---|
183 | vtxParams->SetValue1f(4, 0.1f);
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | return sceneGeom;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | void ResourceManager::LoadTextures(igzstream &str)
|
---|
194 | {
|
---|
195 | int numTextures;
|
---|
196 | str.read(reinterpret_cast<char *>(&numTextures), sizeof(int));
|
---|
197 |
|
---|
198 | for (int i = 0; i < numTextures; ++ i)
|
---|
199 | {
|
---|
200 | // load texture name
|
---|
201 | int texnameSize;
|
---|
202 | str.read(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
203 |
|
---|
204 | char *texname = new char[texnameSize];
|
---|
205 | str.read(texname, sizeof(char) * texnameSize);
|
---|
206 |
|
---|
207 | Texture *tex = new Texture(model_path + texname);
|
---|
208 |
|
---|
209 | int boundS, boundT;
|
---|
210 |
|
---|
211 | str.read(reinterpret_cast<char *>(&boundS), sizeof(int));
|
---|
212 | str.read(reinterpret_cast<char *>(&boundT), sizeof(int));
|
---|
213 |
|
---|
214 | tex->SetBoundaryModeS(boundS);
|
---|
215 | tex->SetBoundaryModeT(boundT);
|
---|
216 |
|
---|
217 | tex->Create();
|
---|
218 |
|
---|
219 | mTextureTable[i] = tex;
|
---|
220 | mTextures.push_back(tex);
|
---|
221 |
|
---|
222 | delete [] texname;
|
---|
223 | }
|
---|
224 |
|
---|
225 | cout << "loaded " << (int)mTextureTable.size() << " textures" << endl;
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | void ResourceManager::LoadShapes(igzstream &str)
|
---|
230 | {
|
---|
231 | int numShapes;
|
---|
232 | str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
|
---|
233 |
|
---|
234 | for (int i = 0; i < numShapes; ++ i)
|
---|
235 | {
|
---|
236 | Geometry *geom = LoadGeometry(str);
|
---|
237 | Material *mat = LoadMaterial(str);
|
---|
238 |
|
---|
239 | mGeometryTable[i] = geom;
|
---|
240 | mMaterialTable[i] = mat;
|
---|
241 |
|
---|
242 | mGeometry.push_back(geom);
|
---|
243 | mMaterials.push_back(mat);
|
---|
244 | }
|
---|
245 |
|
---|
246 | cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl;
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | Material *ResourceManager::LoadMaterial(igzstream &str)
|
---|
251 | {
|
---|
252 | // default material
|
---|
253 | Material *mat = new Material();
|
---|
254 |
|
---|
255 | // set default fragment + vertex programs
|
---|
256 | Technique *tech = mat->GetDefaultTechnique();
|
---|
257 |
|
---|
258 | // texture
|
---|
259 | int texId;
|
---|
260 | str.read(reinterpret_cast<char *>(&texId), sizeof(int));
|
---|
261 |
|
---|
262 | if (texId >= 0)
|
---|
263 | tech->SetTexture(mTextureTable[texId]);
|
---|
264 |
|
---|
265 | str.read(reinterpret_cast<char *>(&tech->mAlphaTestEnabled), sizeof(bool));
|
---|
266 | str.read(reinterpret_cast<char *>(&tech->mCullFaceEnabled), sizeof(bool));
|
---|
267 |
|
---|
268 | // gl material
|
---|
269 | bool hasGlMaterial;
|
---|
270 | str.read(reinterpret_cast<char *>(&hasGlMaterial), sizeof(bool));
|
---|
271 |
|
---|
272 | if (hasGlMaterial)
|
---|
273 | {
|
---|
274 | // only write rgb part of the material
|
---|
275 | str.read(reinterpret_cast<char *>(&tech->mAmbientColor), sizeof(Vector3));
|
---|
276 | str.read(reinterpret_cast<char *>(&tech->mDiffuseColor), sizeof(Vector3));
|
---|
277 | str.read(reinterpret_cast<char *>(&tech->mEmmisiveColor), sizeof(Vector3));
|
---|
278 | str.read(reinterpret_cast<char *>(&tech->mSpecularColor), sizeof(Vector3));
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | ///////////////
|
---|
283 | //-- add technique for deferred shading
|
---|
284 |
|
---|
285 | static ShaderProgram *sDefaultFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultFragmentMrt");
|
---|
286 | static ShaderProgram *sDefaultFragmentTexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultFragmentTexMrt");
|
---|
287 | static ShaderProgram *sDefaultVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultVertexMrt");
|
---|
288 |
|
---|
289 | //static ShaderProgram *sNormalMappingVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingVertexMrt");
|
---|
290 | //static ShaderProgram *sNormalMappingFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingFragmentMrt");
|
---|
291 |
|
---|
292 |
|
---|
293 | Technique *deferred = new Technique(*tech);
|
---|
294 |
|
---|
295 | if (deferred->GetTexture())
|
---|
296 | deferred->SetFragmentProgram(sDefaultFragmentTexProgramMrt);
|
---|
297 | else
|
---|
298 | deferred->SetFragmentProgram(sDefaultFragmentProgramMrt);
|
---|
299 |
|
---|
300 | deferred->GetFragmentProgramParameters()->SetViewMatrixParam(0);
|
---|
301 |
|
---|
302 | deferred->SetVertexProgram(sDefaultVertexProgramMrt);
|
---|
303 |
|
---|
304 | deferred->GetVertexProgramParameters()->SetModelMatrixParam(1);
|
---|
305 | deferred->GetVertexProgramParameters()->SetOldModelMatrixParam(2);
|
---|
306 |
|
---|
307 | mat->AddTechnique(deferred);
|
---|
308 |
|
---|
309 |
|
---|
310 | ///////////
|
---|
311 | //-- add depth pass
|
---|
312 |
|
---|
313 | Technique *depthPass = new Technique(*tech);
|
---|
314 |
|
---|
315 | depthPass->SetColorWriteEnabled(false);
|
---|
316 | depthPass->SetLightingEnabled(false);
|
---|
317 | mat->AddTechnique(depthPass);
|
---|
318 |
|
---|
319 | return mat;
|
---|
320 | }
|
---|
321 |
|
---|
322 |
|
---|
323 | Geometry *ResourceManager::LoadGeometry(igzstream &str)
|
---|
324 | {
|
---|
325 | Vector3 *vertices;
|
---|
326 | Vector3 *normals;
|
---|
327 | Texcoord2 *texcoords;
|
---|
328 |
|
---|
329 |
|
---|
330 | //////////////
|
---|
331 | //-- read in vertices
|
---|
332 |
|
---|
333 | int vertexCount;
|
---|
334 | str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
335 |
|
---|
336 | // end of file reached
|
---|
337 | if (str.eof()) return NULL;
|
---|
338 |
|
---|
339 | //cout << "vertexcount: " << vertexCount << endl;
|
---|
340 |
|
---|
341 | vertices = new Vector3[vertexCount];
|
---|
342 | str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
|
---|
343 |
|
---|
344 | normals = new Vector3[vertexCount];
|
---|
345 | str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
|
---|
346 |
|
---|
347 | Vector3 *tangents;
|
---|
348 |
|
---|
349 | if (mUseNormalMapping)
|
---|
350 | {
|
---|
351 | tangents = new Vector3[vertexCount];
|
---|
352 | str.read(reinterpret_cast<char *>(tangents), sizeof(Vector3) * vertexCount);
|
---|
353 | }
|
---|
354 | else
|
---|
355 | {
|
---|
356 | tangents = NULL;
|
---|
357 | }
|
---|
358 |
|
---|
359 | int texCoordCount;
|
---|
360 | str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
361 |
|
---|
362 | if (texCoordCount)
|
---|
363 | {
|
---|
364 | texcoords = new Texcoord2[texCoordCount];
|
---|
365 | str.read(reinterpret_cast<char *>(texcoords), sizeof(Texcoord2) * texCoordCount);
|
---|
366 | }
|
---|
367 | else
|
---|
368 | {
|
---|
369 | texcoords = NULL;
|
---|
370 | }
|
---|
371 |
|
---|
372 | const bool delGeometry = false;
|
---|
373 | return new Geometry(vertices, normals, texcoords, vertexCount, delGeometry, tangents);
|
---|
374 | }
|
---|
375 |
|
---|
376 |
|
---|
377 | void ResourceManager::LoadSceneEntities(igzstream &str,
|
---|
378 | SceneEntityContainer &entities)
|
---|
379 | {
|
---|
380 | int entityCount;
|
---|
381 | str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
382 |
|
---|
383 | entities.reserve(entityCount);
|
---|
384 |
|
---|
385 | for (int i = 0; i < entityCount; ++ i)
|
---|
386 | {
|
---|
387 | SceneEntity *ent = LoadSceneEntity(str);
|
---|
388 |
|
---|
389 | // return loaded entities
|
---|
390 | entities.push_back(ent);
|
---|
391 | // also store internally
|
---|
392 | mSceneEntities.push_back(ent);
|
---|
393 | }
|
---|
394 |
|
---|
395 | cout << "loaded " << entityCount << " scene entities" << endl;
|
---|
396 | }
|
---|
397 |
|
---|
398 |
|
---|
399 | bool ResourceManager::Load(const std::string &filename,
|
---|
400 | SceneEntityContainer &entities)
|
---|
401 | {
|
---|
402 | igzstream istr(filename.c_str());
|
---|
403 |
|
---|
404 | if (!istr.is_open())
|
---|
405 | return false;
|
---|
406 |
|
---|
407 | cout << "loading textures" << endl;
|
---|
408 |
|
---|
409 | // load the texture table
|
---|
410 | LoadTextures(istr);
|
---|
411 |
|
---|
412 | cout << "loading shapes (geometry + materials)" << endl;
|
---|
413 |
|
---|
414 | // load the shapees
|
---|
415 | LoadShapes(istr);
|
---|
416 |
|
---|
417 | cout << "loading scene entites" << endl;
|
---|
418 | LoadSceneEntities(istr, entities);
|
---|
419 |
|
---|
420 | cout << "bin loading finished" << endl;
|
---|
421 |
|
---|
422 | // clean up
|
---|
423 | mTextureTable.clear();
|
---|
424 | mGeometryTable.clear();
|
---|
425 | mMaterialTable.clear();
|
---|
426 |
|
---|
427 | return true;
|
---|
428 | }
|
---|
429 |
|
---|
430 |
|
---|
431 | void ResourceManager::AddSceneEntity(SceneEntity *ent)
|
---|
432 | {
|
---|
433 | mSceneEntities.push_back(ent);
|
---|
434 | }
|
---|
435 |
|
---|
436 |
|
---|
437 | Transform3 *ResourceManager::CreateTransform(const Matrix4x4 &m)
|
---|
438 | {
|
---|
439 | Transform3 *t = new Transform3(m);
|
---|
440 |
|
---|
441 | mTrafos.push_back(t);
|
---|
442 | return t;
|
---|
443 | }
|
---|
444 |
|
---|
445 | } |
---|