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

Revision 3237, 11.2 KB checked in by mattausch, 15 years ago (diff)

debug 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));
111cout<<"x";
112
113                        Geometry *geom = mGeometryTable[shapeId];
114cout<<"y";
115
116                        Material *mat = mMaterialTable[shapeId];
117cout<<"z";
118                        // create shape
119                        Shape *shape = new Shape(geom, mat);
120cout<<"w";
121
122                        mShapes.push_back(shape);
123
124                        sceneGeom->AddShape(shape);
125                        lodLevel.AddShape(shape);
126                }
127
128                //mLODs.push_back(lodLevel);
129                sceneGeom->AddLODLevel(lodLevel);
130        }
131
132
133        static ShaderProgram *sTreeAnimationProgram =
134                ShaderManager::GetSingleton()->GetShaderProgram("treeAnimation");
135        static ShaderProgram *sTreeAnimationProgramMrt =
136                ShaderManager::GetSingleton()->GetShaderProgram("treeAnimationMrt");
137
138
139        ///////////
140        //-- hack: add tree animation (should be done per material script!)
141
142        if (numLODs > 1)
143        {
144                ShapeContainer::iterator sstart, send;
145
146                // only use shader for the first two lod levels (the non-billboards)
147                for (int i = 0; i < min(numLODs, 2); ++ i)
148                //for (int i = 0; i < numLODs; ++ i)
149                {
150                        sceneGeom->GetLODLevel(i, sstart, send);
151
152                        for (ShapeContainer::iterator it = sstart; it != send; ++ it)
153                        {
154                                Shape *shape = *it;
155
156                                Material *mat = shape->GetMaterial();
157
158                                // add the vertex animation program
159                                for (int i = 0; i < 3; ++ i)
160                                {
161                                        Technique *tech = mat->GetTechnique(i);
162
163                                        GPUProgramParameters *vtxParams = tech->GetVertexProgramParameters();
164
165                                        if (i == 0)
166                                        {
167                                                tech->SetVertexProgram(sTreeAnimationProgram);
168                                                vtxParams->SetLightDirParam(5);
169                                        }
170                                        else
171                                        {
172                                                tech->SetVertexProgram(sTreeAnimationProgramMrt);
173                                                vtxParams->SetOldTimerParam(5);
174                                        }
175
176                                        /// use a timer to simulate the moving of the tree in the wind
177                                        vtxParams->SetTimerParam(0);
178
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
198int 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        return numTextures;
231}
232
233// todo: split up geometry and materials!!
234int 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        return numShapes;
252}
253
254
255Material *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        if (mUseSpecialColors)
287        {
288                tech->mAmbientColor.r = tech->mAmbientColor.g = tech->mAmbientColor.b = 0.2f;
289                tech->mDiffuseColor.r = 0.7f; tech->mDiffuseColor.g = 0.5f; tech->mDiffuseColor.b = 0.2f;
290        }
291
292
293        ///////////////
294        //-- add technique for deferred shading
295
296        static ShaderProgram *sDefaultFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultFragmentMrt");
297        static ShaderProgram *sDefaultFragmentTexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultFragmentTexMrt");
298        static ShaderProgram *sDefaultVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultVertexMrt");
299
300        static ShaderProgram *sNormalMappingVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingVertexMrt");
301        static ShaderProgram *sNormalMappingFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingFragmentMrt");
302
303
304        Technique *deferred = new Technique(*tech);
305
306        if (mUseNormalMapping)
307        {
308                deferred->SetFragmentProgram(sNormalMappingFragmentProgramMrt);
309                deferred->SetVertexProgram(sNormalMappingVertexProgramMrt);
310        }
311        else
312        {
313                if (deferred->GetTexture())
314                        deferred->SetFragmentProgram(sDefaultFragmentTexProgramMrt);
315                else
316                        deferred->SetFragmentProgram(sDefaultFragmentProgramMrt);
317
318                deferred->SetVertexProgram(sDefaultVertexProgramMrt);
319        }
320
321        deferred->GetFragmentProgramParameters()->SetViewMatrixParam(0);
322       
323        deferred->GetVertexProgramParameters()->SetModelMatrixParam(1);
324        deferred->GetVertexProgramParameters()->SetOldModelMatrixParam(2);
325
326        mat->AddTechnique(deferred);
327       
328
329        ///////////
330        //-- add depth pass
331
332        Technique *depthPass = new Technique(*tech);
333
334        depthPass->SetColorWriteEnabled(false);
335        depthPass->SetLightingEnabled(false);
336        mat->AddTechnique(depthPass);
337
338        return mat;
339}
340
341
342Geometry *ResourceManager::LoadGeometry(igzstream &str)
343{
344        Vector3 *vertices;
345        Vector3 *normals;
346        Texcoord2 *texcoords;
347
348
349        //////////////
350        //-- read in vertices
351
352        int vertexCount;
353        str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
354       
355        // end of file reached
356        if (str.eof()) return NULL;
357
358        vertices = new Vector3[vertexCount];
359    str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
360       
361        normals = new Vector3[vertexCount];
362        str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
363
364        Vector3 *tangents;
365
366        if (mUseNormalMapping)
367        {
368                tangents = new Vector3[vertexCount];
369                str.read(reinterpret_cast<char *>(tangents), sizeof(Vector3) * vertexCount);
370        }
371        else
372        {
373                tangents = NULL;
374        }
375
376        int texCoordCount;
377        str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
378
379
380        if (texCoordCount)
381        {
382                texcoords = new Texcoord2[texCoordCount];
383                str.read(reinterpret_cast<char *>(texcoords), sizeof(Texcoord2) * texCoordCount);
384        }
385        else
386        {
387                texcoords = NULL;
388        }
389
390        const bool delGeometry = false;
391        return new Geometry(vertices, normals, texcoords, vertexCount, delGeometry, NULL);//tangents);
392}
393
394
395int ResourceManager::LoadSceneEntities(igzstream &str,
396                                                                           SceneEntityContainer &entities)
397{
398        int entityCount;
399        str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
400
401        entities.reserve(entityCount);
402
403        for (int i = 0; i < entityCount; ++ i)
404        {
405                SceneEntity *ent = LoadSceneEntity(str);
406
407                // return loaded entities
408                entities.push_back(ent);
409                // also store internally
410                mSceneEntities.push_back(ent);
411        }
412
413        return entityCount;
414}
415
416
417int ResourceManager::Load(const std::string &filename,
418                                                  SceneEntityContainer &entities)
419{
420        igzstream istr(filename.c_str());
421       
422        if (!istr.is_open())
423                return 0;
424
425        cout << "loading textures" << endl;
426       
427        // load the texture table
428        int numTextures = LoadTextures(istr);
429
430        cout << "loaded " << numTextures << " textures" << endl;
431
432        cout << "loading shapes (geometry + materials)" << endl;
433
434        // load the shapees
435        int numShapes = LoadShapes(istr);
436
437        cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl;
438
439        cout << "loading scene entites" << endl;
440       
441        int numEntities = LoadSceneEntities(istr, entities);
442       
443        cout << "loaded " << numEntities << " scene entities" << endl;
444
445        cout << "bin loading finished" << endl;
446
447        // clean up temporary tables
448        mTextureTable.clear();
449        mGeometryTable.clear();
450        mMaterialTable.clear();
451
452        return numEntities;
453}
454
455
456void ResourceManager::AddSceneEntity(SceneEntity *ent)
457{
458        mSceneEntities.push_back(ent);
459}
460
461
462Transform3 *ResourceManager::CreateTransform(const Matrix4x4 &m)
463{
464        Transform3 *t = new Transform3(m);
465
466        mTrafos.push_back(t);
467        return t;
468}
469
470
471
472Material *ResourceManager::CreateMaterial()
473{
474        Material *mat = new Material();
475        mMaterials.push_back(mat);
476
477        return mat;
478}
479
480}
Note: See TracBrowser for help on using the repository browser.