source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp @ 3274

Revision 3274, 11.2 KB checked in by mattausch, 15 years ago (diff)
Line 
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
28using namespace std;
29
30
31namespace CHCDemoEngine
32{
33
34
35// only instance of the resource manager
36ResourceManager *ResourceManager::sResourceManager = NULL;
37
38
39ResourceManager::ResourceManager()
40{
41        mUseNormalMapping = false;
42        mUseSpecialColors = false;
43}
44
45
46ResourceManager::~ResourceManager()
47{
48        // delete all resources.
49        CLEAR_CONTAINER(mSceneEntities);
50        CLEAR_CONTAINER(mGeometry);
51        CLEAR_CONTAINER(mMaterials);
52        CLEAR_CONTAINER(mTextures);
53        CLEAR_CONTAINER(mTrafos);
54        CLEAR_CONTAINER(mShapes);
55}
56
57
58void ResourceManager::DelSingleton()
59{
60        DEL_PTR(sResourceManager);
61}
62
63
64SceneEntity *ResourceManager::LoadSceneEntity(igzstream &str)
65{       
66        bool hasTrafo;
67        str.read(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
68       
69        SceneEntity *sceneGeom;
70        Transform3 *trafo;
71
72        if (!hasTrafo)
73        {       
74                trafo = new Transform3(); // identity
75        }
76        else
77        {
78                Matrix4x4 m;
79                str.read(reinterpret_cast<char *>(m.x), sizeof(Matrix4x4));
80                trafo = new Transform3(m);
81        }
82
83        mTrafos.push_back(trafo);
84
85        sceneGeom = new SceneEntity(trafo);
86
87
88        ///////////////
89        //-- load LOD levels
90
91        // note: lods must be ordered from smallest distance to largest
92        int numLODs;
93        str.read(reinterpret_cast<char *>(&numLODs), sizeof(int));
94
95        for (int i = 0; i < numLODs; ++ i)
96        {
97                // the distance until the next LOD level is used
98                float dist;
99                str.read(reinterpret_cast<char *>(&dist), sizeof(float));
100
101                int numShapes;
102                str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
103
104                //LODLevel *lodLevel = new LODLevel(dist);
105                LODLevel lodLevel(dist);
106
107                for (int j = 0; j < numShapes; ++ j)
108                {
109                        int shapeId;
110                        str.read(reinterpret_cast<char *>(&shapeId), sizeof(int));
111
112                        Geometry *geom = mGeometryTable[shapeId];
113                        Material *mat = mMaterialTable[shapeId];
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
193int 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        return numTextures;
226}
227
228// todo: split up geometry and materials!!
229int 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        return numShapes;
247}
248
249
250Material *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        if (mUseSpecialColors)
282        {
283                tech->mAmbientColor.r = tech->mAmbientColor.g = tech->mAmbientColor.b = 0.2f;
284                tech->mDiffuseColor.r = 0.7f; tech->mDiffuseColor.g = 0.5f; tech->mDiffuseColor.b = 0.2f;
285        }
286
287
288        ///////////////
289        //-- add technique for deferred shading
290
291        static ShaderProgram *sDefaultFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultFragmentMrt");
292        static ShaderProgram *sDefaultFragmentTexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultFragmentTexMrt");
293        static ShaderProgram *sDefaultVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultVertexMrt");
294
295        static ShaderProgram *sNormalMappingVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingVertexMrt");
296        static ShaderProgram *sNormalMappingFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingFragmentMrt");
297
298
299        Technique *deferred = new Technique(*tech);
300
301        if (mUseNormalMapping)
302        {
303                deferred->SetFragmentProgram(sNormalMappingFragmentProgramMrt);
304                deferred->SetVertexProgram(sNormalMappingVertexProgramMrt);
305        }
306        else
307        {
308                if (deferred->GetTexture())
309                        deferred->SetFragmentProgram(sDefaultFragmentTexProgramMrt);
310                else
311                        deferred->SetFragmentProgram(sDefaultFragmentProgramMrt);
312
313                deferred->SetVertexProgram(sDefaultVertexProgramMrt);
314        }
315
316        deferred->GetFragmentProgramParameters()->SetViewMatrixParam(0);
317        deferred->GetVertexProgramParameters()->SetModelMatrixParam(1);
318        deferred->GetVertexProgramParameters()->SetOldModelMatrixParam(2);
319
320        mat->AddTechnique(deferred);
321       
322
323        ///////////
324        //-- add depth pass
325
326        Technique *depthPass = new Technique(*tech);
327
328        depthPass->SetColorWriteEnabled(false);
329        depthPass->SetLightingEnabled(false);
330        mat->AddTechnique(depthPass);
331
332        return mat;
333}
334
335
336Geometry *ResourceManager::LoadGeometry(igzstream &str)
337{
338        Vector3 *vertices;
339        Vector3 *normals;
340        Texcoord2 *texcoords;
341
342
343        //////////////
344        //-- read in vertices
345
346        int vertexCount;
347        str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
348       
349        // end of file reached
350        if (str.eof()) return NULL;
351
352        vertices = new Vector3[vertexCount];
353    str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
354       
355        normals = new Vector3[vertexCount];
356        str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
357
358        Vector3 *tangents;
359
360        if (mUseNormalMapping)
361        {
362                tangents = new Vector3[vertexCount];
363                str.read(reinterpret_cast<char *>(tangents), sizeof(Vector3) * vertexCount);
364        }
365        else
366        {
367                tangents = NULL;
368        }
369
370        int texCoordCount;
371        str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
372
373
374        if (texCoordCount)
375        {
376                texcoords = new Texcoord2[texCoordCount];
377                str.read(reinterpret_cast<char *>(texcoords), sizeof(Texcoord2) * texCoordCount);
378        }
379        else
380        {
381                texcoords = NULL;
382        }
383
384        const bool delGeometry = true;
385        //const bool delGeometry = false;
386        return new Geometry(vertices, normals, texcoords, vertexCount, delGeometry, NULL);//tangents);
387}
388
389
390int ResourceManager::LoadSceneEntities(igzstream &str,
391                                                                           SceneEntityContainer &entities)
392{
393        int entityCount;
394        str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
395
396        entities.reserve(entityCount);
397
398        for (int i = 0; i < entityCount; ++ i)
399        {
400                SceneEntity *ent = LoadSceneEntity(str);
401
402                // return loaded entities
403                entities.push_back(ent);
404                // also store internally
405                mSceneEntities.push_back(ent);
406        }
407
408        return entityCount;
409}
410
411
412int ResourceManager::Load(const std::string &filename,
413                                                  SceneEntityContainer &entities)
414{
415        igzstream istr(filename.c_str());
416       
417        if (!istr.is_open())
418                return 0;
419
420        cout << "loading textures" << endl;
421       
422        // load the texture table
423        int numTextures = LoadTextures(istr);
424
425        cout << "loaded " << numTextures << " textures" << endl;
426        cout << "loading shapes (geometry + materials)" << endl;
427
428        // load the shapees
429        int numShapes = LoadShapes(istr);
430
431        cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl;
432        cout << "loading scene entites" << endl;
433       
434        int numEntities = LoadSceneEntities(istr, entities);
435       
436        cout << "loaded " << numEntities << " scene entities" << endl;
437        cout << "bin loading finished" << endl;
438
439        // clean up temporary tables
440        mTextureTable.clear();
441        mGeometryTable.clear();
442        mMaterialTable.clear();
443
444        return numEntities;
445}
446
447
448void ResourceManager::AddSceneEntity(SceneEntity *ent)
449{
450        mSceneEntities.push_back(ent);
451}
452
453
454Transform3 *ResourceManager::CreateTransform(const Matrix4x4 &m)
455{
456        Transform3 *t = new Transform3(m);
457
458        mTrafos.push_back(t);
459        return t;
460}
461
462
463
464Material *ResourceManager::CreateMaterial()
465{
466        Material *mat = new Material();
467        mMaterials.push_back(mat);
468
469        return mat;
470}
471
472}
Note: See TracBrowser for help on using the repository browser.