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

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