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

Revision 3372, 11.3 KB checked in by mattausch, 15 years ago (diff)

reverted but now a good version

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                if (i && (i % 1000 == 0)) cout << "loaded " << i << " shapes" << endl;
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        return numShapes;
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        if (mUseSpecialColors)
284        {
285                tech->mAmbientColor.r = tech->mAmbientColor.g = tech->mAmbientColor.b = 0.2f;
286                tech->mDiffuseColor.r = 0.7f; tech->mDiffuseColor.g = 0.5f; tech->mDiffuseColor.b = 0.2f;
287        }
288
289
290        ///////////////
291        //-- add technique for deferred shading
292
293        static ShaderProgram *sDefaultFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultFragmentMrt");
294        static ShaderProgram *sDefaultFragmentTexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultFragmentTexMrt");
295        static ShaderProgram *sDefaultVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultVertexMrt");
296
297        static ShaderProgram *sNormalMappingVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingVertexMrt");
298        static ShaderProgram *sNormalMappingFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingFragmentMrt");
299
300
301        Technique *deferred = new Technique(*tech);
302
303        if (mUseNormalMapping)
304        {
305                deferred->SetFragmentProgram(sNormalMappingFragmentProgramMrt);
306                deferred->SetVertexProgram(sNormalMappingVertexProgramMrt);
307        }
308        else
309        {
310                if (deferred->GetTexture())
311                        deferred->SetFragmentProgram(sDefaultFragmentTexProgramMrt);
312                else
313                        deferred->SetFragmentProgram(sDefaultFragmentProgramMrt);
314
315                deferred->SetVertexProgram(sDefaultVertexProgramMrt);
316        }
317
318        deferred->GetFragmentProgramParameters()->SetViewMatrixParam(0);
319        deferred->GetVertexProgramParameters()->SetModelMatrixParam(1);
320        deferred->GetVertexProgramParameters()->SetOldModelMatrixParam(2);
321
322        mat->AddTechnique(deferred);
323       
324
325        ///////////
326        //-- add depth pass
327
328        Technique *depthPass = new Technique(*tech);
329
330        depthPass->SetColorWriteEnabled(false);
331        depthPass->SetLightingEnabled(false);
332        mat->AddTechnique(depthPass);
333
334        return mat;
335}
336
337
338Geometry *ResourceManager::LoadGeometry(igzstream &str)
339{
340        Vector3 *vertices;
341        Vector3 *normals;
342        Texcoord2 *texcoords;
343
344
345        //////////////
346        //-- read in vertices
347
348        int vertexCount;
349        str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
350       
351
352        // end of file reached
353        if (str.eof()) return NULL;
354
355        vertices = new Vector3[vertexCount];
356    str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
357       
358        normals = new Vector3[vertexCount];
359        str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
360
361        Vector3 *tangents;
362
363        if (mUseNormalMapping)
364        {
365                tangents = new Vector3[vertexCount];
366                str.read(reinterpret_cast<char *>(tangents), sizeof(Vector3) * vertexCount);
367        }
368        else
369        {
370                tangents = NULL;
371        }
372
373        int texCoordCount;
374        str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
375
376
377        if (texCoordCount)
378        {
379                texcoords = new Texcoord2[texCoordCount];
380                str.read(reinterpret_cast<char *>(texcoords), sizeof(Texcoord2) * texCoordCount);
381        }
382        else
383        {
384                texcoords = NULL;
385        }
386
387        const bool delGeometry = true;
388        //const bool delGeometry = false;
389        return new Geometry(vertices, normals, texcoords, vertexCount, delGeometry, NULL);//tangents);
390}
391
392
393int ResourceManager::LoadSceneEntities(igzstream &str,
394                                                                           SceneEntityContainer &entities)
395{
396        int entityCount;
397        str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
398
399        entities.reserve(entityCount);
400
401        for (int i = 0; i < entityCount; ++ i)
402        {
403                SceneEntity *ent = LoadSceneEntity(str);
404
405                // return loaded entities
406                entities.push_back(ent);
407                // also store internally
408                mSceneEntities.push_back(ent);
409        }
410
411        return entityCount;
412}
413
414
415int ResourceManager::Load(const std::string &filename,
416                                                  SceneEntityContainer &entities)
417{
418        igzstream istr(filename.c_str());
419       
420        if (!istr.is_open()) return 0;
421
422        cout << "loading textures" << endl;
423       
424        // load the texture table
425        int numTextures = LoadTextures(istr);
426
427        cout << "loaded " << numTextures << " textures" << endl;
428        cout << "loading shapes (geometry + materials)" << endl;
429
430        // load the shapees
431        int numShapes = LoadShapes(istr);
432
433        cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl;
434        cout << "loading scene entites" << endl;
435       
436        int numEntities = LoadSceneEntities(istr, entities);
437       
438        cout << "loaded " << numEntities << " scene entities" << endl;
439        cout << "bin loading finished" << endl;
440
441        // clean up temporary tables
442        mTextureTable.clear();
443        mGeometryTable.clear();
444        mMaterialTable.clear();
445
446        return numEntities;
447}
448
449
450void ResourceManager::AddSceneEntity(SceneEntity *ent)
451{
452        mSceneEntities.push_back(ent);
453}
454
455
456Transform3 *ResourceManager::CreateTransform(const Matrix4x4 &m)
457{
458        Transform3 *t = new Transform3(m);
459
460        mTrafos.push_back(t);
461        return t;
462}
463
464
465
466Material *ResourceManager::CreateMaterial()
467{
468        Material *mat = new Material();
469        mMaterials.push_back(mat);
470
471        return mat;
472}
473
474}
Note: See TracBrowser for help on using the repository browser.