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

Revision 3215, 10.8 KB checked in by mattausch, 16 years ago (diff)

worked on lense flare, cleaned up code a little bit

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);
[2844]116                        mShapes.push_back(shape);
117
118                        sceneGeom->AddShape(shape);
[3070]119                        lodLevel.AddShape(shape);
[2844]120                }
121
[3070]122                //mLODs.push_back(lodLevel);
[2844]123                sceneGeom->AddLODLevel(lodLevel);
124        }
125
[3057]126
[3113]127        static ShaderProgram *sTreeAnimationProgram =
128                ShaderManager::GetSingleton()->GetShaderProgram("treeAnimation");
129        static ShaderProgram *sTreeAnimationProgramMrt =
130                ShaderManager::GetSingleton()->GetShaderProgram("treeAnimationMrt");
131
132
[3041]133        ///////////
[3047]134        //-- hack: add tree animation (should be done per material script!)
[3041]135
[3050]136        if (numLODs > 1)
[3041]137        {
[3047]138                ShapeContainer::iterator sstart, send;
139
140                // only use shader for the first two lod levels (the non-billboards)
141                for (int i = 0; i < min(numLODs, 2); ++ i)
142                //for (int i = 0; i < numLODs; ++ i)
[3041]143                {
144                        sceneGeom->GetLODLevel(i, sstart, send);
145
146                        for (ShapeContainer::iterator it = sstart; it != send; ++ it)
147                        {
148                                Shape *shape = *it;
[3045]149
[3041]150                                Material *mat = shape->GetMaterial();
151
[3045]152                                // add the vertex animation program
[3050]153                                for (int i = 0; i < 3; ++ i)
[3045]154                                {
155                                        Technique *tech = mat->GetTechnique(i);
[3041]156
[3045]157                                        GPUProgramParameters *vtxParams = tech->GetVertexProgramParameters();
[3041]158
[3046]159                                        if (i == 0)
160                                        {
161                                                tech->SetVertexProgram(sTreeAnimationProgram);
162                                                vtxParams->SetLightDirParam(5);
163                                        }
164                                        else
165                                        {
166                                                tech->SetVertexProgram(sTreeAnimationProgramMrt);
[3115]167                                                vtxParams->SetOldTimerParam(5);
[3046]168                                        }
169
170                                        /// use a timer to simulate the moving of the tree in the wind
[3045]171                                        vtxParams->SetTimerParam(0);
[3113]172
[3045]173                                        // wind direction
174                                        static Vector3 windDir = Normalize(Vector3(0.8f, 0.2f, 0.0f));
175                                        vtxParams->SetValue3f(1, windDir.x, windDir.y, windDir.z);
176                                        // amplitude
177                                        vtxParams->SetValue1f(2, 0.3f);
178
179                                        AxisAlignedBox3 box = sceneGeom->GetBoundingBox();
180                                        vtxParams->SetValue2f(3, box.Min().z, box.Max().z);
181                                        // frequency
182                                        vtxParams->SetValue1f(4, 0.1f);
183                                }
[3041]184                        }
185                }
186        }
187
[2755]188        return sceneGeom;
[2747]189}
190
191
[2795]192void ResourceManager::LoadTextures(igzstream &str)
[2757]193{
194        int numTextures;
195        str.read(reinterpret_cast<char *>(&numTextures), sizeof(int));
[2961]196       
[2757]197        for (int i = 0; i < numTextures; ++ i)
198        {
[2961]199                // load texture name
[2757]200                int texnameSize;
201                str.read(reinterpret_cast<char *>(&texnameSize), sizeof(int));
202
203                char *texname = new char[texnameSize];
204                str.read(texname, sizeof(char) * texnameSize);
205
[2784]206                Texture *tex = new Texture(model_path + texname);
[2757]207
[2846]208                int boundS, boundT;
209
210                str.read(reinterpret_cast<char *>(&boundS), sizeof(int));
211                str.read(reinterpret_cast<char *>(&boundT), sizeof(int));
212
213                tex->SetBoundaryModeS(boundS);
214                tex->SetBoundaryModeT(boundT);
215
216                tex->Create();
217
[2764]218                mTextureTable[i] = tex;
[2795]219                mTextures.push_back(tex);
[3021]220
221                delete [] texname;
[2757]222        }
[2764]223
[2800]224        cout << "loaded " << (int)mTextureTable.size() << " textures" << endl;
[2757]225}
226
[3146]227// mtt todo_ split that up
[2795]228void ResourceManager::LoadShapes(igzstream &str)
[2764]229{
230        int numShapes;
231        str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
232
233        for (int i = 0; i < numShapes; ++ i)
234        {
235                Geometry *geom = LoadGeometry(str);
236                Material *mat = LoadMaterial(str);
237
238                mGeometryTable[i] = geom;
[2842]239                mMaterialTable[i] = mat;               
[2795]240
241                mGeometry.push_back(geom);
242                mMaterials.push_back(mat);
[2764]243        }
244
[2800]245        cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl;
[2764]246}
247
248
[2795]249Material *ResourceManager::LoadMaterial(igzstream &str)
[2747]250{
[2756]251        // default material
[2755]252        Material *mat = new Material();
[2756]253       
[3042]254        // set default fragment + vertex programs
255        Technique *tech = mat->GetDefaultTechnique();
256
[2747]257        // texture
[2757]258        int texId;
259        str.read(reinterpret_cast<char *>(&texId), sizeof(int));
[2747]260
[2982]261        if (texId >= 0)
[3042]262                tech->SetTexture(mTextureTable[texId]);
[2764]263       
[3042]264        str.read(reinterpret_cast<char *>(&tech->mAlphaTestEnabled), sizeof(bool));
265        str.read(reinterpret_cast<char *>(&tech->mCullFaceEnabled), sizeof(bool));
[2844]266
[3034]267        // gl material
268        bool hasGlMaterial;
269        str.read(reinterpret_cast<char *>(&hasGlMaterial), sizeof(bool));
[3042]270
[3034]271        if (hasGlMaterial)
[2747]272        {
[2769]273                // only write rgb part of the material
[3042]274                str.read(reinterpret_cast<char *>(&tech->mAmbientColor), sizeof(Vector3));
275                str.read(reinterpret_cast<char *>(&tech->mDiffuseColor), sizeof(Vector3));
276                str.read(reinterpret_cast<char *>(&tech->mEmmisiveColor), sizeof(Vector3));
277                str.read(reinterpret_cast<char *>(&tech->mSpecularColor), sizeof(Vector3));
[2747]278        }
279
[3045]280
281        ///////////////
282        //-- add technique for deferred shading
283
[3057]284        static ShaderProgram *sDefaultFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultFragmentMrt");
285        static ShaderProgram *sDefaultFragmentTexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultFragmentTexMrt");
286        static ShaderProgram *sDefaultVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultVertexMrt");
287
[3144]288        static ShaderProgram *sNormalMappingVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingVertexMrt");
289        static ShaderProgram *sNormalMappingFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingFragmentMrt");
[3127]290
[3128]291
[3045]292        Technique *deferred = new Technique(*tech);
293
[3145]294        if (mUseNormalMapping)
295        {
[3146]296                deferred->SetFragmentProgram(sNormalMappingFragmentProgramMrt);
[3145]297                deferred->SetVertexProgram(sNormalMappingVertexProgramMrt);
298        }
[3034]299        else
[3145]300        {
301                if (deferred->GetTexture())
302                        deferred->SetFragmentProgram(sDefaultFragmentTexProgramMrt);
303                else
304                        deferred->SetFragmentProgram(sDefaultFragmentProgramMrt);
[3034]305
[3145]306                deferred->SetVertexProgram(sDefaultVertexProgramMrt);
307        }
308
[3126]309        deferred->GetFragmentProgramParameters()->SetViewMatrixParam(0);
[3145]310       
[3114]311        deferred->GetVertexProgramParameters()->SetModelMatrixParam(1);
312        deferred->GetVertexProgramParameters()->SetOldModelMatrixParam(2);
[3113]313
[3050]314        mat->AddTechnique(deferred);
[3041]315       
[3048]316
317        ///////////
318        //-- add depth pass
319
320        Technique *depthPass = new Technique(*tech);
[3114]321
[3048]322        depthPass->SetColorWriteEnabled(false);
323        depthPass->SetLightingEnabled(false);
324        mat->AddTechnique(depthPass);
[3045]325
[2755]326        return mat;
[2747]327}
328
329
[2795]330Geometry *ResourceManager::LoadGeometry(igzstream &str)
[2747]331{
[2756]332        Vector3 *vertices;
333        Vector3 *normals;
[2980]334        Texcoord2 *texcoords;
[2747]335
336
[2756]337        //////////////
338        //-- read in vertices
[2747]339
[2756]340        int vertexCount;
341        str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
[2747]342       
343        // end of file reached
[2961]344        if (str.eof()) return NULL;
[2747]345
[2756]346        vertices = new Vector3[vertexCount];
347    str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
[2747]348       
[2756]349        normals = new Vector3[vertexCount];
350        str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
[2747]351
[3127]352        Vector3 *tangents;
353
354        if (mUseNormalMapping)
355        {
356                tangents = new Vector3[vertexCount];
357                str.read(reinterpret_cast<char *>(tangents), sizeof(Vector3) * vertexCount);
358        }
359        else
360        {
361                tangents = NULL;
362        }
363
[2756]364        int texCoordCount;
365        str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
[2747]366
[3153]367
[2756]368        if (texCoordCount)
[2747]369        {
[2980]370                texcoords = new Texcoord2[texCoordCount];
371                str.read(reinterpret_cast<char *>(texcoords), sizeof(Texcoord2) * texCoordCount);
[2747]372        }
[2756]373        else
[2811]374        {
[2756]375                texcoords = NULL;
[2811]376        }
[2747]377
[3127]378        const bool delGeometry = false;
[3154]379        return new Geometry(vertices, normals, texcoords, vertexCount, delGeometry, NULL);//tangents);
[2747]380}
381
382
[3046]383void ResourceManager::LoadSceneEntities(igzstream &str,
384                                                                                SceneEntityContainer &entities)
[2747]385{
[2764]386        int entityCount;
387        str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
388
[2963]389        entities.reserve(entityCount);
[2764]390
391        for (int i = 0; i < entityCount; ++ i)
392        {
393                SceneEntity *ent = LoadSceneEntity(str);
[2842]394
[2963]395                // return loaded entities
396                entities.push_back(ent);
397                // also store internally
[2795]398                mSceneEntities.push_back(ent);
[2764]399        }
400
401        cout << "loaded " << entityCount << " scene entities" << endl;
402}
403
404
[3046]405bool ResourceManager::Load(const std::string &filename,
406                                                   SceneEntityContainer &entities)
[2764]407{
[2795]408        igzstream istr(filename.c_str());
[2747]409       
[2756]410        if (!istr.is_open())
[2747]411                return false;
412
[2764]413        cout << "loading textures" << endl;
414       
[2757]415        // load the texture table
416        LoadTextures(istr);
417
[2764]418        cout << "loading shapes (geometry + materials)" << endl;
[2747]419
[2764]420        // load the shapees
421        LoadShapes(istr);
[2762]422
[2764]423        cout << "loading scene entites" << endl;
424        LoadSceneEntities(istr, entities);
[2763]425       
[2756]426        cout << "bin loading finished" << endl;
[2747]427
[2795]428        // clean up
429        mTextureTable.clear();
430        mGeometryTable.clear();
431        mMaterialTable.clear();
432
[2756]433        return true;
[2747]434}
435
[3070]436
437void ResourceManager::AddSceneEntity(SceneEntity *ent)
438{
439        mSceneEntities.push_back(ent);
[3036]440}
441
[3070]442
443Transform3 *ResourceManager::CreateTransform(const Matrix4x4 &m)
444{
445        Transform3 *t = new Transform3(m);
446
447        mTrafos.push_back(t);
448        return t;
449}
450
[3215]451
452
453Material *ResourceManager::CreateMaterial()
454{
455        Material *mat = new Material();
456        mMaterials.push_back(mat);
457
458        return mat;
459}
460
[3070]461}
Note: See TracBrowser for help on using the repository browser.