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

Revision 3146, 10.7 KB checked in by mattausch, 16 years ago (diff)

normal mapping hack not working yet. found problems with ssao if the geometry is not tesselated enough (especially with smoothed
normals: one can see the underlying tesselation!

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
[2757]346        //cout << "vertexcount: " << vertexCount << endl;
[2747]347
[2756]348        vertices = new Vector3[vertexCount];
349    str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
[2747]350       
[2756]351        normals = new Vector3[vertexCount];
352        str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
[2747]353
[3127]354        Vector3 *tangents;
355
356        if (mUseNormalMapping)
357        {
358                tangents = new Vector3[vertexCount];
359                str.read(reinterpret_cast<char *>(tangents), sizeof(Vector3) * vertexCount);
360        }
361        else
362        {
363                tangents = NULL;
364        }
365
[2756]366        int texCoordCount;
367        str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
[2747]368
[2756]369        if (texCoordCount)
[2747]370        {
[2980]371                texcoords = new Texcoord2[texCoordCount];
372                str.read(reinterpret_cast<char *>(texcoords), sizeof(Texcoord2) * texCoordCount);
[2747]373        }
[2756]374        else
[2811]375        {
[2756]376                texcoords = NULL;
[2811]377        }
[2747]378
[3127]379        const bool delGeometry = false;
380        return new Geometry(vertices, normals, texcoords, vertexCount, delGeometry, tangents);
[2747]381}
382
383
[3046]384void ResourceManager::LoadSceneEntities(igzstream &str,
385                                                                                SceneEntityContainer &entities)
[2747]386{
[2764]387        int entityCount;
388        str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
389
[2963]390        entities.reserve(entityCount);
[2764]391
392        for (int i = 0; i < entityCount; ++ i)
393        {
394                SceneEntity *ent = LoadSceneEntity(str);
[2842]395
[2963]396                // return loaded entities
397                entities.push_back(ent);
398                // also store internally
[2795]399                mSceneEntities.push_back(ent);
[2764]400        }
401
402        cout << "loaded " << entityCount << " scene entities" << endl;
403}
404
405
[3046]406bool ResourceManager::Load(const std::string &filename,
407                                                   SceneEntityContainer &entities)
[2764]408{
[2795]409        igzstream istr(filename.c_str());
[2747]410       
[2756]411        if (!istr.is_open())
[2747]412                return false;
413
[2764]414        cout << "loading textures" << endl;
415       
[2757]416        // load the texture table
417        LoadTextures(istr);
418
[2764]419        cout << "loading shapes (geometry + materials)" << endl;
[2747]420
[2764]421        // load the shapees
422        LoadShapes(istr);
[2762]423
[2764]424        cout << "loading scene entites" << endl;
425        LoadSceneEntities(istr, entities);
[2763]426       
[2756]427        cout << "bin loading finished" << endl;
[2747]428
[2795]429        // clean up
430        mTextureTable.clear();
431        mGeometryTable.clear();
432        mMaterialTable.clear();
433
[2756]434        return true;
[2747]435}
436
[3070]437
438void ResourceManager::AddSceneEntity(SceneEntity *ent)
439{
440        mSceneEntities.push_back(ent);
[3036]441}
442
[3070]443
444Transform3 *ResourceManager::CreateTransform(const Matrix4x4 &m)
445{
446        Transform3 *t = new Transform3(m);
447
448        mTrafos.push_back(t);
449        return t;
450}
451
452}
Note: See TracBrowser for help on using the repository browser.