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 "Matrix4x4.h"
|
---|
9 | #include "Vector3.h"
|
---|
10 | #include "Transform3.h"
|
---|
11 | #include "Shape.h"
|
---|
12 | #include "LODInfo.h"
|
---|
13 | #include "RenderState.h"
|
---|
14 | #include "ShaderProgram.h"
|
---|
15 | #include <Cg/cg.h>
|
---|
16 | #include <Cg/cgGL.h>
|
---|
17 |
|
---|
18 |
|
---|
19 | #ifdef _CRT_SET
|
---|
20 | #define _CRTDBG_MAP_ALLOC
|
---|
21 | #include <stdlib.h>
|
---|
22 | #include <crtdbg.h>
|
---|
23 |
|
---|
24 | // redefine new operator
|
---|
25 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
26 | #define new DEBUG_NEW
|
---|
27 | #endif
|
---|
28 |
|
---|
29 |
|
---|
30 | using namespace std;
|
---|
31 |
|
---|
32 |
|
---|
33 | namespace CHCDemoEngine
|
---|
34 | {
|
---|
35 |
|
---|
36 |
|
---|
37 | /////////
|
---|
38 | //-- cg stuff
|
---|
39 |
|
---|
40 | static CGcontext sCgContext = NULL;
|
---|
41 |
|
---|
42 | static CGprofile sCgFragmentProfile;
|
---|
43 | static CGprofile sCgVertexProfile;
|
---|
44 | static ShaderProgram *sTreeAnimation;
|
---|
45 |
|
---|
46 | // only instance of the resource manager
|
---|
47 | ResourceManager *ResourceManager::sResourceManager = NULL;
|
---|
48 |
|
---|
49 |
|
---|
50 | static void cgErrorCallback()
|
---|
51 | {
|
---|
52 | CGerror lastError = cgGetError();
|
---|
53 |
|
---|
54 | if(lastError)
|
---|
55 | {
|
---|
56 | printf("%s\n\n", cgGetErrorString(lastError));
|
---|
57 | printf("%s\n", cgGetLastListing(sCgContext));
|
---|
58 |
|
---|
59 | printf("Cg error, exiting...\n");
|
---|
60 |
|
---|
61 | exit(0);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | ResourceManager::ResourceManager()
|
---|
67 | {
|
---|
68 | InitCg();
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | ResourceManager::~ResourceManager()
|
---|
73 | {
|
---|
74 | // delete all resources.
|
---|
75 | CLEAR_CONTAINER(mSceneEntities);
|
---|
76 | CLEAR_CONTAINER(mGeometry);
|
---|
77 | CLEAR_CONTAINER(mMaterials);
|
---|
78 | CLEAR_CONTAINER(mTextures);
|
---|
79 | CLEAR_CONTAINER(mTrafos);
|
---|
80 | CLEAR_CONTAINER(mShapes);
|
---|
81 | CLEAR_CONTAINER(mLODs);
|
---|
82 | CLEAR_CONTAINER(mGPUParameters);
|
---|
83 | CLEAR_CONTAINER(mShaders);
|
---|
84 |
|
---|
85 | if (sCgContext)
|
---|
86 | cgDestroyContext(sCgContext);
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | SceneEntity *ResourceManager::LoadSceneEntity(igzstream &str)
|
---|
91 | {
|
---|
92 | bool hasTrafo;
|
---|
93 | str.read(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
|
---|
94 |
|
---|
95 | SceneEntity *sceneGeom;
|
---|
96 | Transform3 *trafo;
|
---|
97 |
|
---|
98 | if (!hasTrafo)
|
---|
99 | {
|
---|
100 | // identity
|
---|
101 | trafo = new Transform3();
|
---|
102 | }
|
---|
103 | else
|
---|
104 | {
|
---|
105 | Matrix4x4 m;
|
---|
106 | str.read(reinterpret_cast<char *>(m.x), sizeof(Matrix4x4));
|
---|
107 |
|
---|
108 | trafo = new Transform3(m);
|
---|
109 | }
|
---|
110 |
|
---|
111 | mTrafos.push_back(trafo);
|
---|
112 |
|
---|
113 | sceneGeom = new SceneEntity(trafo);
|
---|
114 |
|
---|
115 |
|
---|
116 | ///////////////
|
---|
117 | //-- load lod levels
|
---|
118 |
|
---|
119 | // note: lods must be ordered from smallest distance to largest
|
---|
120 | int numLODs;
|
---|
121 | str.read(reinterpret_cast<char *>(&numLODs), sizeof(int));
|
---|
122 |
|
---|
123 | for (int i = 0; i < numLODs; ++ i)
|
---|
124 | {
|
---|
125 | float dist;
|
---|
126 | str.read(reinterpret_cast<char *>(&dist), sizeof(float));
|
---|
127 |
|
---|
128 | int numShapes;
|
---|
129 | str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
|
---|
130 |
|
---|
131 | LODLevel *lodLevel = new LODLevel(dist);
|
---|
132 |
|
---|
133 | for (int j = 0; j < numShapes; ++ j)
|
---|
134 | {
|
---|
135 | int shapeId;
|
---|
136 | str.read(reinterpret_cast<char *>(&shapeId), sizeof(int));
|
---|
137 |
|
---|
138 | Geometry *geom = mGeometryTable[shapeId];
|
---|
139 | Material *mat = mMaterialTable[shapeId];
|
---|
140 |
|
---|
141 | // create shape
|
---|
142 | Shape *shape = new Shape(geom, mat, sceneGeom);
|
---|
143 |
|
---|
144 | mShapes.push_back(shape);
|
---|
145 |
|
---|
146 | sceneGeom->AddShape(shape);
|
---|
147 | lodLevel->AddShape(shape);
|
---|
148 | }
|
---|
149 |
|
---|
150 | mLODs.push_back(lodLevel);
|
---|
151 | sceneGeom->AddLODLevel(lodLevel);
|
---|
152 | }
|
---|
153 |
|
---|
154 | ///////////
|
---|
155 | //-- hack: tree animation
|
---|
156 |
|
---|
157 | if (numLODs > 1)
|
---|
158 | {
|
---|
159 | for (int i = 0; i < min(numLODs, 2); ++ i)
|
---|
160 | {
|
---|
161 | ShapeContainer::iterator sstart, send;
|
---|
162 | sceneGeom->GetLODLevel(i, sstart, send);
|
---|
163 |
|
---|
164 | for (ShapeContainer::iterator it = sstart; it != send; ++ it)
|
---|
165 | {
|
---|
166 | Shape *shape = *it;
|
---|
167 |
|
---|
168 | Material *mat = shape->GetMaterial();
|
---|
169 |
|
---|
170 | // add the vertex animation program
|
---|
171 | for (int i = 0; i < 2; ++ i)
|
---|
172 | {
|
---|
173 | Technique *tech = mat->GetTechnique(i);
|
---|
174 | tech->SetVertexProgram(sTreeAnimation);
|
---|
175 |
|
---|
176 | GPUProgramParameters *vtxParams = tech->GetVertexProgramParameters();
|
---|
177 |
|
---|
178 | vtxParams->SetTimerParam(0);
|
---|
179 | // wind direction
|
---|
180 | static Vector3 windDir = Normalize(Vector3(0.8f, 0.2f, 0.0f));
|
---|
181 | vtxParams->SetValue3f(1, windDir.x, windDir.y, windDir.z);
|
---|
182 | // amplitude
|
---|
183 | vtxParams->SetValue1f(2, 0.3f);
|
---|
184 |
|
---|
185 | AxisAlignedBox3 box = sceneGeom->GetBoundingBox();
|
---|
186 | vtxParams->SetValue2f(3, box.Min().z, box.Max().z);
|
---|
187 | // frequency
|
---|
188 | vtxParams->SetValue1f(4, 0.1f);
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | return sceneGeom;
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | void ResourceManager::LoadTextures(igzstream &str)
|
---|
199 | {
|
---|
200 | int numTextures;
|
---|
201 | str.read(reinterpret_cast<char *>(&numTextures), sizeof(int));
|
---|
202 |
|
---|
203 | for (int i = 0; i < numTextures; ++ i)
|
---|
204 | {
|
---|
205 | // load texture name
|
---|
206 | int texnameSize;
|
---|
207 | str.read(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
208 |
|
---|
209 | char *texname = new char[texnameSize];
|
---|
210 | str.read(texname, sizeof(char) * texnameSize);
|
---|
211 |
|
---|
212 | Texture *tex = new Texture(model_path + texname);
|
---|
213 |
|
---|
214 | int boundS, boundT;
|
---|
215 |
|
---|
216 | str.read(reinterpret_cast<char *>(&boundS), sizeof(int));
|
---|
217 | str.read(reinterpret_cast<char *>(&boundT), sizeof(int));
|
---|
218 |
|
---|
219 | tex->SetBoundaryModeS(boundS);
|
---|
220 | tex->SetBoundaryModeT(boundT);
|
---|
221 |
|
---|
222 | tex->Create();
|
---|
223 |
|
---|
224 | mTextureTable[i] = tex;
|
---|
225 | mTextures.push_back(tex);
|
---|
226 |
|
---|
227 | delete [] texname;
|
---|
228 | }
|
---|
229 |
|
---|
230 | cout << "loaded " << (int)mTextureTable.size() << " textures" << endl;
|
---|
231 | }
|
---|
232 |
|
---|
233 |
|
---|
234 | void ResourceManager::LoadShapes(igzstream &str)
|
---|
235 | {
|
---|
236 | int numShapes;
|
---|
237 | str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
|
---|
238 |
|
---|
239 | for (int i = 0; i < numShapes; ++ i)
|
---|
240 | {
|
---|
241 | Geometry *geom = LoadGeometry(str);
|
---|
242 | Material *mat = LoadMaterial(str);
|
---|
243 |
|
---|
244 | mGeometryTable[i] = geom;
|
---|
245 | mMaterialTable[i] = mat;
|
---|
246 |
|
---|
247 | mGeometry.push_back(geom);
|
---|
248 | mMaterials.push_back(mat);
|
---|
249 | }
|
---|
250 |
|
---|
251 | cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl;
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | Material *ResourceManager::LoadMaterial(igzstream &str)
|
---|
256 | {
|
---|
257 | // default material
|
---|
258 | Material *mat = new Material();
|
---|
259 |
|
---|
260 | // set default fragment + vertex programs
|
---|
261 | Technique *tech = mat->GetDefaultTechnique();
|
---|
262 |
|
---|
263 | // texture
|
---|
264 | int texId;
|
---|
265 | str.read(reinterpret_cast<char *>(&texId), sizeof(int));
|
---|
266 |
|
---|
267 | if (texId >= 0)
|
---|
268 | tech->SetTexture(mTextureTable[texId]);
|
---|
269 |
|
---|
270 | str.read(reinterpret_cast<char *>(&tech->mAlphaTestEnabled), sizeof(bool));
|
---|
271 | str.read(reinterpret_cast<char *>(&tech->mCullFaceEnabled), sizeof(bool));
|
---|
272 |
|
---|
273 | // gl material
|
---|
274 | bool hasGlMaterial;
|
---|
275 | str.read(reinterpret_cast<char *>(&hasGlMaterial), sizeof(bool));
|
---|
276 |
|
---|
277 | if (hasGlMaterial)
|
---|
278 | {
|
---|
279 | // only write rgb part of the material
|
---|
280 | str.read(reinterpret_cast<char *>(&tech->mAmbientColor), sizeof(Vector3));
|
---|
281 | str.read(reinterpret_cast<char *>(&tech->mDiffuseColor), sizeof(Vector3));
|
---|
282 | str.read(reinterpret_cast<char *>(&tech->mEmmisiveColor), sizeof(Vector3));
|
---|
283 | str.read(reinterpret_cast<char *>(&tech->mSpecularColor), sizeof(Vector3));
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | ///////////////
|
---|
288 | //-- add technique for deferred shading
|
---|
289 |
|
---|
290 | Technique *deferred = new Technique(*tech);
|
---|
291 |
|
---|
292 | if (deferred->GetTexture())
|
---|
293 | {
|
---|
294 | deferred->SetFragmentProgram(mMrtDefaultFragmentTexProgram);
|
---|
295 | deferred->GetFragmentProgramParameters()->SetViewMatrixParam(0);
|
---|
296 | deferred->GetFragmentProgramParameters()->SetTexture(1, tech->GetTexture()->GetId());
|
---|
297 | }
|
---|
298 | else
|
---|
299 | {
|
---|
300 | deferred->SetFragmentProgram(mMrtDefaultFragmentProgram);
|
---|
301 | deferred->GetFragmentProgramParameters()->SetViewMatrixParam(0);
|
---|
302 | }
|
---|
303 |
|
---|
304 | deferred->SetVertexProgram(mMrtDefaultVertexProgram);
|
---|
305 |
|
---|
306 | mat->AddTechnique(deferred);
|
---|
307 |
|
---|
308 | return mat;
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | Geometry *ResourceManager::LoadGeometry(igzstream &str)
|
---|
313 | {
|
---|
314 | Vector3 *vertices;
|
---|
315 | Vector3 *normals;
|
---|
316 | Texcoord2 *texcoords;
|
---|
317 |
|
---|
318 |
|
---|
319 | //////////////
|
---|
320 | //-- read in vertices
|
---|
321 |
|
---|
322 | int vertexCount;
|
---|
323 | str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
324 |
|
---|
325 | // end of file reached
|
---|
326 | if (str.eof()) return NULL;
|
---|
327 |
|
---|
328 | //cout << "vertexcount: " << vertexCount << endl;
|
---|
329 |
|
---|
330 | vertices = new Vector3[vertexCount];
|
---|
331 | str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
|
---|
332 |
|
---|
333 | normals = new Vector3[vertexCount];
|
---|
334 | str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
|
---|
335 |
|
---|
336 | int texCoordCount;
|
---|
337 | str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
338 |
|
---|
339 | if (texCoordCount)
|
---|
340 | {
|
---|
341 | texcoords = new Texcoord2[texCoordCount];
|
---|
342 | str.read(reinterpret_cast<char *>(texcoords), sizeof(Texcoord2) * texCoordCount);
|
---|
343 | }
|
---|
344 | else
|
---|
345 | {
|
---|
346 | texcoords = NULL;
|
---|
347 | }
|
---|
348 |
|
---|
349 | return new Geometry(vertices, normals, texcoords, vertexCount, true);
|
---|
350 | //return new Geometry(vertices, normals, texcoords, vertexCount, false);
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 | void ResourceManager::LoadSceneEntities(igzstream &str, SceneEntityContainer &entities)
|
---|
355 | {
|
---|
356 | int entityCount;
|
---|
357 | str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
358 |
|
---|
359 | entities.reserve(entityCount);
|
---|
360 |
|
---|
361 | for (int i = 0; i < entityCount; ++ i)
|
---|
362 | {
|
---|
363 | SceneEntity *ent = LoadSceneEntity(str);
|
---|
364 |
|
---|
365 | // return loaded entities
|
---|
366 | entities.push_back(ent);
|
---|
367 | // also store internally
|
---|
368 | mSceneEntities.push_back(ent);
|
---|
369 | }
|
---|
370 |
|
---|
371 | cout << "loaded " << entityCount << " scene entities" << endl;
|
---|
372 | }
|
---|
373 |
|
---|
374 |
|
---|
375 | bool ResourceManager::Load(const std::string &filename, SceneEntityContainer &entities)
|
---|
376 | {
|
---|
377 | igzstream istr(filename.c_str());
|
---|
378 |
|
---|
379 | if (!istr.is_open())
|
---|
380 | return false;
|
---|
381 |
|
---|
382 | cout << "loading textures" << endl;
|
---|
383 |
|
---|
384 | // load the texture table
|
---|
385 | LoadTextures(istr);
|
---|
386 |
|
---|
387 | cout << "loading shapes (geometry + materials)" << endl;
|
---|
388 |
|
---|
389 | // load the shapees
|
---|
390 | LoadShapes(istr);
|
---|
391 |
|
---|
392 | cout << "loading scene entites" << endl;
|
---|
393 | LoadSceneEntities(istr, entities);
|
---|
394 |
|
---|
395 | cout << "bin loading finished" << endl;
|
---|
396 |
|
---|
397 | // clean up
|
---|
398 | mTextureTable.clear();
|
---|
399 | mGeometryTable.clear();
|
---|
400 | mMaterialTable.clear();
|
---|
401 |
|
---|
402 | return true;
|
---|
403 | }
|
---|
404 |
|
---|
405 |
|
---|
406 |
|
---|
407 | ShaderProgram *ResourceManager::CreateFragmentProgram(const std::string &filename,
|
---|
408 | const std::string &funcName)
|
---|
409 | {
|
---|
410 | const string fullName = "src/shaders/" + filename + ".cg";
|
---|
411 |
|
---|
412 | ShaderProgram *p = new ShaderProgram(sCgContext, fullName, sCgFragmentProfile, funcName);
|
---|
413 |
|
---|
414 | mShaders.push_back(p);
|
---|
415 |
|
---|
416 | if (!p->IsValid())
|
---|
417 | {
|
---|
418 | DEL_PTR(p);
|
---|
419 | cerr << "Program " << funcName << " in " << fullName << " failed to load" << endl;
|
---|
420 | }
|
---|
421 |
|
---|
422 | cout << "Program " << funcName << " in " << fullName << " loaded" << endl;
|
---|
423 |
|
---|
424 | return p;
|
---|
425 | }
|
---|
426 |
|
---|
427 |
|
---|
428 | ShaderProgram *ResourceManager::CreateVertexProgram(const std::string &filename, const std::string &funcName)
|
---|
429 | {
|
---|
430 | const string fullName = "src/shaders/" + filename + ".cg";
|
---|
431 |
|
---|
432 | ShaderProgram *p = new ShaderProgram(sCgContext, fullName, sCgVertexProfile, funcName);
|
---|
433 | mShaders.push_back(p);
|
---|
434 |
|
---|
435 | if (!p->IsValid())
|
---|
436 | {
|
---|
437 | DEL_PTR(p);
|
---|
438 | cerr << "Program " << funcName << " in " << fullName << " failed to load" << endl;
|
---|
439 | }
|
---|
440 |
|
---|
441 | cout << "Program " << funcName << " in " << fullName << " loaded" << endl;
|
---|
442 |
|
---|
443 | return p;
|
---|
444 | }
|
---|
445 |
|
---|
446 |
|
---|
447 | void ResourceManager::InitCg()
|
---|
448 | {
|
---|
449 | // setup cg
|
---|
450 | cgSetErrorCallback(cgErrorCallback);
|
---|
451 | // create context.
|
---|
452 | sCgContext = cgCreateContext();
|
---|
453 | // get the optimal fragment profile
|
---|
454 | sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
|
---|
455 | cgGLSetOptimalOptions(sCgFragmentProfile);
|
---|
456 | // get the optimal vertex profile
|
---|
457 | sCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
|
---|
458 | cgGLSetOptimalOptions(sCgVertexProfile);
|
---|
459 |
|
---|
460 | // set textures to auto load
|
---|
461 | cgGLSetManageTextureParameters(sCgContext, false);
|
---|
462 |
|
---|
463 | mMrtDefaultVertexProgram = CreateVertexProgram("mrt", "vtx");
|
---|
464 | mMrtDefaultFragmentProgram = CreateFragmentProgram("mrt", "frag");
|
---|
465 | mMrtDefaultFragmentTexProgram = CreateFragmentProgram("mrt", "fragtex");
|
---|
466 |
|
---|
467 | // provide the current view matrix
|
---|
468 | mMrtDefaultFragmentProgram->AddParameter("viewMatrix", 0);
|
---|
469 | mMrtDefaultFragmentTexProgram->AddParameter("viewMatrix", 0);
|
---|
470 |
|
---|
471 | // add a texture parameter
|
---|
472 | mMrtDefaultFragmentTexProgram->AddParameter("tex", 1);
|
---|
473 |
|
---|
474 | sTreeAnimation = CreateVertexProgram("treeanimation", "animateVtxMrt");
|
---|
475 |
|
---|
476 | sTreeAnimation->AddParameter("timer", 0);
|
---|
477 | sTreeAnimation->AddParameter("windDir", 1);
|
---|
478 | sTreeAnimation->AddParameter("windStrength", 2);
|
---|
479 | sTreeAnimation->AddParameter("minMaxPos", 3);
|
---|
480 | sTreeAnimation->AddParameter("frequency", 4);
|
---|
481 |
|
---|
482 | cout << "cg initialization successful" << endl;
|
---|
483 | }
|
---|
484 |
|
---|
485 |
|
---|
486 | void ResourceManager::EnableFragmentProfile()
|
---|
487 | {
|
---|
488 | cgGLEnableProfile(sCgFragmentProfile);
|
---|
489 | }
|
---|
490 |
|
---|
491 |
|
---|
492 | void ResourceManager::EnableVertexProfile()
|
---|
493 | {
|
---|
494 | cgGLEnableProfile(sCgVertexProfile);
|
---|
495 | }
|
---|
496 |
|
---|
497 |
|
---|
498 | void ResourceManager::DisableFragmentProfile()
|
---|
499 | {
|
---|
500 | cgGLDisableProfile(sCgFragmentProfile);
|
---|
501 | }
|
---|
502 |
|
---|
503 |
|
---|
504 | void ResourceManager::DisableVertexProfile()
|
---|
505 | {
|
---|
506 | cgGLDisableProfile(sCgVertexProfile);
|
---|
507 |
|
---|
508 | }
|
---|
509 |
|
---|
510 |
|
---|
511 | }
|
---|
512 |
|
---|