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

Revision 3271, 11.2 KB checked in by mattausch, 15 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;
[3226]42        mUseSpecialColors = false;
[3038]43}
44
45
[2795]46ResourceManager::~ResourceManager()
[2781]47{
[2795]48        // delete all resources.
49        CLEAR_CONTAINER(mSceneEntities);
50        CLEAR_CONTAINER(mGeometry);
51        CLEAR_CONTAINER(mMaterials);
52        CLEAR_CONTAINER(mTextures);
53        CLEAR_CONTAINER(mTrafos);
[2842]54        CLEAR_CONTAINER(mShapes);
[2781]55}
56
[2795]57
[3052]58void ResourceManager::DelSingleton()
59{
60        DEL_PTR(sResourceManager);
61}
62
63
[2795]64SceneEntity *ResourceManager::LoadSceneEntity(igzstream &str)
[2747]65{       
[2764]66        bool hasTrafo;
67        str.read(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
68       
[2823]69        SceneEntity *sceneGeom;
[2957]70        Transform3 *trafo;
[2823]71
[2764]72        if (!hasTrafo)
[3128]73        {       
74                trafo = new Transform3(); // identity
[2764]75        }
76        else
77        {
[2957]78                Matrix4x4 m;
79                str.read(reinterpret_cast<char *>(m.x), sizeof(Matrix4x4));
80                trafo = new Transform3(m);
[2764]81        }
82
[2840]83        mTrafos.push_back(trafo);
[2844]84
[2839]85        sceneGeom = new SceneEntity(trafo);
[2825]86
[2852]87
[2844]88        ///////////////
[3048]89        //-- load LOD levels
[2842]90
[2853]91        // note: lods must be ordered from smallest distance to largest
[2844]92        int numLODs;
93        str.read(reinterpret_cast<char *>(&numLODs), sizeof(int));
[2842]94
[2844]95        for (int i = 0; i < numLODs; ++ i)
96        {
[3048]97                // the distance until the next LOD level is used
[2853]98                float dist;
99                str.read(reinterpret_cast<char *>(&dist), sizeof(float));
[2844]100
101                int numShapes;
102                str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
103
[3070]104                //LODLevel *lodLevel = new LODLevel(dist);
105                LODLevel lodLevel(dist);
[2844]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
[3071]115                        Shape *shape = new Shape(geom, mat);
[3236]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
[3237]193int 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
[3237]225        return numTextures;
[2757]226}
227
[3237]228// todo: split up geometry and materials!!
229int 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
[3237]246        return numShapes;
[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
[3226]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        }
[3045]286
[3226]287
[3045]288        ///////////////
289        //-- add technique for deferred shading
290
[3057]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
[3144]295        static ShaderProgram *sNormalMappingVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingVertexMrt");
296        static ShaderProgram *sNormalMappingFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingFragmentMrt");
[3127]297
[3128]298
[3045]299        Technique *deferred = new Technique(*tech);
300
[3145]301        if (mUseNormalMapping)
302        {
[3146]303                deferred->SetFragmentProgram(sNormalMappingFragmentProgramMrt);
[3145]304                deferred->SetVertexProgram(sNormalMappingVertexProgramMrt);
305        }
[3034]306        else
[3145]307        {
308                if (deferred->GetTexture())
309                        deferred->SetFragmentProgram(sDefaultFragmentTexProgramMrt);
310                else
311                        deferred->SetFragmentProgram(sDefaultFragmentProgramMrt);
[3034]312
[3145]313                deferred->SetVertexProgram(sDefaultVertexProgramMrt);
314        }
315
[3126]316        deferred->GetFragmentProgramParameters()->SetViewMatrixParam(0);
[3114]317        deferred->GetVertexProgramParameters()->SetModelMatrixParam(1);
318        deferred->GetVertexProgramParameters()->SetOldModelMatrixParam(2);
[3113]319
[3050]320        mat->AddTechnique(deferred);
[3041]321       
[3048]322
323        ///////////
324        //-- add depth pass
325
326        Technique *depthPass = new Technique(*tech);
[3114]327
[3048]328        depthPass->SetColorWriteEnabled(false);
329        depthPass->SetLightingEnabled(false);
330        mat->AddTechnique(depthPass);
[3045]331
[2755]332        return mat;
[2747]333}
334
335
[2795]336Geometry *ResourceManager::LoadGeometry(igzstream &str)
[2747]337{
[2756]338        Vector3 *vertices;
339        Vector3 *normals;
[2980]340        Texcoord2 *texcoords;
[2747]341
342
[2756]343        //////////////
344        //-- read in vertices
[2747]345
[2756]346        int vertexCount;
347        str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
[2747]348       
349        // end of file reached
[2961]350        if (str.eof()) return NULL;
[2747]351
[2756]352        vertices = new Vector3[vertexCount];
353    str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
[2747]354       
[2756]355        normals = new Vector3[vertexCount];
356        str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
[2747]357
[3127]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
[2756]370        int texCoordCount;
371        str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
[2747]372
[3153]373
[2756]374        if (texCoordCount)
[2747]375        {
[2980]376                texcoords = new Texcoord2[texCoordCount];
377                str.read(reinterpret_cast<char *>(texcoords), sizeof(Texcoord2) * texCoordCount);
[2747]378        }
[2756]379        else
[2811]380        {
[2756]381                texcoords = NULL;
[2811]382        }
[2747]383
[3261]384        const bool delGeometry = true;
385        //const bool delGeometry = false;
[3154]386        return new Geometry(vertices, normals, texcoords, vertexCount, delGeometry, NULL);//tangents);
[2747]387}
388
389
[3237]390int ResourceManager::LoadSceneEntities(igzstream &str,
391                                                                           SceneEntityContainer &entities)
[2747]392{
[2764]393        int entityCount;
394        str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
395
[2963]396        entities.reserve(entityCount);
[2764]397
398        for (int i = 0; i < entityCount; ++ i)
399        {
400                SceneEntity *ent = LoadSceneEntity(str);
[2842]401
[2963]402                // return loaded entities
403                entities.push_back(ent);
404                // also store internally
[2795]405                mSceneEntities.push_back(ent);
[2764]406        }
407
[3237]408        return entityCount;
[2764]409}
410
411
[3237]412int ResourceManager::Load(const std::string &filename,
413                                                  SceneEntityContainer &entities)
[2764]414{
[2795]415        igzstream istr(filename.c_str());
[2747]416       
[2756]417        if (!istr.is_open())
[3237]418                return 0;
[2747]419
[2764]420        cout << "loading textures" << endl;
421       
[2757]422        // load the texture table
[3237]423        int numTextures = LoadTextures(istr);
[2757]424
[3237]425        cout << "loaded " << numTextures << " textures" << endl;
[2764]426        cout << "loading shapes (geometry + materials)" << endl;
[2747]427
[2764]428        // load the shapees
[3237]429        int numShapes = LoadShapes(istr);
[2762]430
[3237]431        cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl;
[2764]432        cout << "loading scene entites" << endl;
[2763]433       
[3237]434        int numEntities = LoadSceneEntities(istr, entities);
435       
436        cout << "loaded " << numEntities << " scene entities" << endl;
[2756]437        cout << "bin loading finished" << endl;
[2747]438
[3237]439        // clean up temporary tables
[2795]440        mTextureTable.clear();
441        mGeometryTable.clear();
442        mMaterialTable.clear();
443
[3237]444        return numEntities;
[2747]445}
446
[3070]447
448void ResourceManager::AddSceneEntity(SceneEntity *ent)
449{
450        mSceneEntities.push_back(ent);
[3036]451}
452
[3070]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
[3215]462
463
464Material *ResourceManager::CreateMaterial()
465{
466        Material *mat = new Material();
467        mMaterials.push_back(mat);
468
469        return mat;
470}
471
[3070]472}
Note: See TracBrowser for help on using the repository browser.