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

Revision 3128, 10.5 KB checked in by mattausch, 16 years ago (diff)
RevLine 
[2795]1#include "ResourceManager.h"
[2755]2#include "Matrix4x4.h"
3#include "Geometry.h"
[2756]4#include "SceneEntity.h"
[2755]5#include "Material.h"
[2756]6#include "Texture.h"
[2795]7#include "gzstream.h"
[2823]8#include "Vector3.h"
[2840]9#include "Transform3.h"
[2842]10#include "Shape.h"
[2844]11#include "LODInfo.h"
[3034]12#include "RenderState.h"
13#include "ShaderProgram.h"
[3057]14#include "ShaderManager.h"
[2747]15
16
[3021]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
[2747]28using namespace std;
29
30
[2776]31namespace CHCDemoEngine
[2755]32{
[2747]33
[3038]34
[3037]35// only instance of the resource manager
36ResourceManager *ResourceManager::sResourceManager = NULL;
[2755]37
[3038]38
39ResourceManager::ResourceManager()
40{
[3127]41        mUseNormalMapping = false;
[3038]42}
43
44
[2795]45ResourceManager::~ResourceManager()
[2781]46{
[2795]47        // delete all resources.
48        CLEAR_CONTAINER(mSceneEntities);
49        CLEAR_CONTAINER(mGeometry);
50        CLEAR_CONTAINER(mMaterials);
51        CLEAR_CONTAINER(mTextures);
52        CLEAR_CONTAINER(mTrafos);
[2842]53        CLEAR_CONTAINER(mShapes);
[2781]54}
55
[2795]56
[3052]57void ResourceManager::DelSingleton()
58{
59        DEL_PTR(sResourceManager);
60}
61
62
[2795]63SceneEntity *ResourceManager::LoadSceneEntity(igzstream &str)
[2747]64{       
[2764]65        bool hasTrafo;
66        str.read(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
67       
[2823]68        SceneEntity *sceneGeom;
[2957]69        Transform3 *trafo;
[2823]70
[2764]71        if (!hasTrafo)
[3128]72        {       
73                trafo = new Transform3(); // identity
[2764]74        }
75        else
76        {
[2957]77                Matrix4x4 m;
78                str.read(reinterpret_cast<char *>(m.x), sizeof(Matrix4x4));
79                trafo = new Transform3(m);
[2764]80        }
81
[2840]82        mTrafos.push_back(trafo);
[2844]83
[2839]84        sceneGeom = new SceneEntity(trafo);
[2825]85
[2852]86
[2844]87        ///////////////
[3048]88        //-- load LOD levels
[2842]89
[2853]90        // note: lods must be ordered from smallest distance to largest
[2844]91        int numLODs;
92        str.read(reinterpret_cast<char *>(&numLODs), sizeof(int));
[2842]93
[2844]94        for (int i = 0; i < numLODs; ++ i)
95        {
[3048]96                // the distance until the next LOD level is used
[2853]97                float dist;
98                str.read(reinterpret_cast<char *>(&dist), sizeof(float));
[2844]99
100                int numShapes;
101                str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
102
[3070]103                //LODLevel *lodLevel = new LODLevel(dist);
104                LODLevel lodLevel(dist);
[2844]105
106                for (int j = 0; j < numShapes; ++ j)
107                {
108                        int shapeId;
109                        str.read(reinterpret_cast<char *>(&shapeId), sizeof(int));
110
111                        Geometry *geom = mGeometryTable[shapeId];
112                        Material *mat = mMaterialTable[shapeId];
113
114                        // create shape
[3071]115                        Shape *shape = new Shape(geom, mat);
[3041]116
[2844]117                        mShapes.push_back(shape);
118
119                        sceneGeom->AddShape(shape);
[3070]120                        lodLevel.AddShape(shape);
[2844]121                }
122
[3070]123                //mLODs.push_back(lodLevel);
[2844]124                sceneGeom->AddLODLevel(lodLevel);
125        }
126
[3057]127
[3113]128        static ShaderProgram *sTreeAnimationProgram =
129                ShaderManager::GetSingleton()->GetShaderProgram("treeAnimation");
130        static ShaderProgram *sTreeAnimationProgramMrt =
131                ShaderManager::GetSingleton()->GetShaderProgram("treeAnimationMrt");
132
133
[3041]134        ///////////
[3047]135        //-- hack: add tree animation (should be done per material script!)
[3041]136
[3050]137        if (numLODs > 1)
[3041]138        {
[3047]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)
[3041]144                {
145                        sceneGeom->GetLODLevel(i, sstart, send);
146
147                        for (ShapeContainer::iterator it = sstart; it != send; ++ it)
148                        {
149                                Shape *shape = *it;
[3045]150
[3041]151                                Material *mat = shape->GetMaterial();
152
[3045]153                                // add the vertex animation program
[3050]154                                for (int i = 0; i < 3; ++ i)
[3045]155                                {
156                                        Technique *tech = mat->GetTechnique(i);
[3041]157
[3045]158                                        GPUProgramParameters *vtxParams = tech->GetVertexProgramParameters();
[3041]159
[3046]160                                        if (i == 0)
161                                        {
162                                                tech->SetVertexProgram(sTreeAnimationProgram);
163                                                vtxParams->SetLightDirParam(5);
164                                        }
165                                        else
166                                        {
167                                                tech->SetVertexProgram(sTreeAnimationProgramMrt);
[3115]168                                                vtxParams->SetOldTimerParam(5);
[3046]169                                        }
170
171                                        /// use a timer to simulate the moving of the tree in the wind
[3045]172                                        vtxParams->SetTimerParam(0);
[3113]173
[3045]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                                }
[3041]185                        }
186                }
187        }
188
[2755]189        return sceneGeom;
[2747]190}
191
192
[2795]193void ResourceManager::LoadTextures(igzstream &str)
[2757]194{
195        int numTextures;
196        str.read(reinterpret_cast<char *>(&numTextures), sizeof(int));
[2961]197       
[2757]198        for (int i = 0; i < numTextures; ++ i)
199        {
[2961]200                // load texture name
[2757]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
[2784]207                Texture *tex = new Texture(model_path + texname);
[2757]208
[2846]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
[2764]219                mTextureTable[i] = tex;
[2795]220                mTextures.push_back(tex);
[3021]221
222                delete [] texname;
[2757]223        }
[2764]224
[2800]225        cout << "loaded " << (int)mTextureTable.size() << " textures" << endl;
[2757]226}
227
228
[2795]229void ResourceManager::LoadShapes(igzstream &str)
[2764]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;
[2842]240                mMaterialTable[i] = mat;               
[2795]241
242                mGeometry.push_back(geom);
243                mMaterials.push_back(mat);
[2764]244        }
245
[2800]246        cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl;
[2764]247}
248
249
[2795]250Material *ResourceManager::LoadMaterial(igzstream &str)
[2747]251{
[2756]252        // default material
[2755]253        Material *mat = new Material();
[2756]254       
[3042]255        // set default fragment + vertex programs
256        Technique *tech = mat->GetDefaultTechnique();
257
[2747]258        // texture
[2757]259        int texId;
260        str.read(reinterpret_cast<char *>(&texId), sizeof(int));
[2747]261
[2982]262        if (texId >= 0)
[3042]263                tech->SetTexture(mTextureTable[texId]);
[2764]264       
[3042]265        str.read(reinterpret_cast<char *>(&tech->mAlphaTestEnabled), sizeof(bool));
266        str.read(reinterpret_cast<char *>(&tech->mCullFaceEnabled), sizeof(bool));
[2844]267
[3034]268        // gl material
269        bool hasGlMaterial;
270        str.read(reinterpret_cast<char *>(&hasGlMaterial), sizeof(bool));
[3042]271
[3034]272        if (hasGlMaterial)
[2747]273        {
[2769]274                // only write rgb part of the material
[3042]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));
[2747]279        }
280
[3045]281
282        ///////////////
283        //-- add technique for deferred shading
284
[3057]285        static ShaderProgram *sDefaultFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultFragmentMrt");
286        static ShaderProgram *sDefaultFragmentTexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultFragmentTexMrt");
287        static ShaderProgram *sDefaultVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultVertexMrt");
288
[3128]289        //static ShaderProgram *sNormalMappingVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingVertexMrt");
290        //static ShaderProgram *sNormalMappingFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingFragmentMrt");
[3127]291
[3128]292
[3045]293        Technique *deferred = new Technique(*tech);
294
295        if (deferred->GetTexture())
[3057]296                deferred->SetFragmentProgram(sDefaultFragmentTexProgramMrt);
[3034]297        else
[3058]298                deferred->SetFragmentProgram(sDefaultFragmentProgramMrt);
[3034]299
[3126]300        deferred->GetFragmentProgramParameters()->SetViewMatrixParam(0);
301
[3057]302        deferred->SetVertexProgram(sDefaultVertexProgramMrt);
[3113]303
[3114]304        deferred->GetVertexProgramParameters()->SetModelMatrixParam(1);
305        deferred->GetVertexProgramParameters()->SetOldModelMatrixParam(2);
[3113]306
[3050]307        mat->AddTechnique(deferred);
[3041]308       
[3048]309
310        ///////////
311        //-- add depth pass
312
313        Technique *depthPass = new Technique(*tech);
[3114]314
[3048]315        depthPass->SetColorWriteEnabled(false);
316        depthPass->SetLightingEnabled(false);
317        mat->AddTechnique(depthPass);
[3045]318
[2755]319        return mat;
[2747]320}
321
322
[2795]323Geometry *ResourceManager::LoadGeometry(igzstream &str)
[2747]324{
[2756]325        Vector3 *vertices;
326        Vector3 *normals;
[2980]327        Texcoord2 *texcoords;
[2747]328
329
[2756]330        //////////////
331        //-- read in vertices
[2747]332
[2756]333        int vertexCount;
334        str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
[2747]335       
336        // end of file reached
[2961]337        if (str.eof()) return NULL;
[2747]338
[2757]339        //cout << "vertexcount: " << vertexCount << endl;
[2747]340
[2756]341        vertices = new Vector3[vertexCount];
342    str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
[2747]343       
[2756]344        normals = new Vector3[vertexCount];
345        str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
[2747]346
[3127]347        Vector3 *tangents;
348
349        if (mUseNormalMapping)
350        {
351                tangents = new Vector3[vertexCount];
352                str.read(reinterpret_cast<char *>(tangents), sizeof(Vector3) * vertexCount);
353        }
354        else
355        {
356                tangents = NULL;
357        }
358
[2756]359        int texCoordCount;
360        str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
[2747]361
[2756]362        if (texCoordCount)
[2747]363        {
[2980]364                texcoords = new Texcoord2[texCoordCount];
365                str.read(reinterpret_cast<char *>(texcoords), sizeof(Texcoord2) * texCoordCount);
[2747]366        }
[2756]367        else
[2811]368        {
[2756]369                texcoords = NULL;
[2811]370        }
[2747]371
[3127]372        const bool delGeometry = false;
373        return new Geometry(vertices, normals, texcoords, vertexCount, delGeometry, tangents);
[2747]374}
375
376
[3046]377void ResourceManager::LoadSceneEntities(igzstream &str,
378                                                                                SceneEntityContainer &entities)
[2747]379{
[2764]380        int entityCount;
381        str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
382
[2963]383        entities.reserve(entityCount);
[2764]384
385        for (int i = 0; i < entityCount; ++ i)
386        {
387                SceneEntity *ent = LoadSceneEntity(str);
[2842]388
[2963]389                // return loaded entities
390                entities.push_back(ent);
391                // also store internally
[2795]392                mSceneEntities.push_back(ent);
[2764]393        }
394
395        cout << "loaded " << entityCount << " scene entities" << endl;
396}
397
398
[3046]399bool ResourceManager::Load(const std::string &filename,
400                                                   SceneEntityContainer &entities)
[2764]401{
[2795]402        igzstream istr(filename.c_str());
[2747]403       
[2756]404        if (!istr.is_open())
[2747]405                return false;
406
[2764]407        cout << "loading textures" << endl;
408       
[2757]409        // load the texture table
410        LoadTextures(istr);
411
[2764]412        cout << "loading shapes (geometry + materials)" << endl;
[2747]413
[2764]414        // load the shapees
415        LoadShapes(istr);
[2762]416
[2764]417        cout << "loading scene entites" << endl;
418        LoadSceneEntities(istr, entities);
[2763]419       
[2756]420        cout << "bin loading finished" << endl;
[2747]421
[2795]422        // clean up
423        mTextureTable.clear();
424        mGeometryTable.clear();
425        mMaterialTable.clear();
426
[2756]427        return true;
[2747]428}
429
[3070]430
431void ResourceManager::AddSceneEntity(SceneEntity *ent)
432{
433        mSceneEntities.push_back(ent);
[3036]434}
435
[3070]436
437Transform3 *ResourceManager::CreateTransform(const Matrix4x4 &m)
438{
439        Transform3 *t = new Transform3(m);
440
441        mTrafos.push_back(t);
442        return t;
443}
444
445}
Note: See TracBrowser for help on using the repository browser.