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

Revision 3034, 7.1 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 "Matrix4x4.h"
9#include "Vector3.h"
[2840]10#include "Transform3.h"
[2842]11#include "Shape.h"
[2844]12#include "LODInfo.h"
[3034]13#include "RenderState.h"
14#include "ShaderProgram.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
[2755]34
[2795]35ResourceManager::~ResourceManager()
[2781]36{
[2795]37        // delete all resources.
38        CLEAR_CONTAINER(mSceneEntities);
39        CLEAR_CONTAINER(mGeometry);
40        CLEAR_CONTAINER(mMaterials);
41        CLEAR_CONTAINER(mTextures);
42        CLEAR_CONTAINER(mTrafos);
[2842]43        CLEAR_CONTAINER(mShapes);
[3019]44        CLEAR_CONTAINER(mLODs);
[2781]45}
46
[2795]47
48SceneEntity *ResourceManager::LoadSceneEntity(igzstream &str)
[2747]49{       
[2764]50        bool hasTrafo;
51        str.read(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
52       
[2823]53        SceneEntity *sceneGeom;
[2957]54        Transform3 *trafo;
[2823]55
[2764]56        if (!hasTrafo)
57        {
[2961]58                // identity
[2957]59                trafo = new Transform3();
[2764]60        }
61        else
62        {
[2957]63                Matrix4x4 m;
64                str.read(reinterpret_cast<char *>(m.x), sizeof(Matrix4x4));
65               
66                trafo = new Transform3(m);
[2764]67        }
68
[2840]69        mTrafos.push_back(trafo);
[2844]70
[2839]71        sceneGeom = new SceneEntity(trafo);
[2825]72
[2852]73
[2844]74        ///////////////
75        //-- load lod levels
[2842]76
[2853]77        // note: lods must be ordered from smallest distance to largest
[2844]78        int numLODs;
79        str.read(reinterpret_cast<char *>(&numLODs), sizeof(int));
[2842]80
[2844]81        for (int i = 0; i < numLODs; ++ i)
82        {
[2853]83                float dist;
84                str.read(reinterpret_cast<char *>(&dist), sizeof(float));
[2844]85
86                int numShapes;
87                str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
88
[2853]89                LODLevel *lodLevel = new LODLevel(dist);
[2844]90
91                for (int j = 0; j < numShapes; ++ j)
92                {
93                        int shapeId;
94                        str.read(reinterpret_cast<char *>(&shapeId), sizeof(int));
95
96                        Geometry *geom = mGeometryTable[shapeId];
97                        Material *mat = mMaterialTable[shapeId];
98
99                        // create shape
100                        Shape *shape = new Shape(geom, mat, sceneGeom);
101                       
102                        mShapes.push_back(shape);
103
104                        sceneGeom->AddShape(shape);
[2848]105                        lodLevel->AddShape(shape);
[2844]106                }
107
[3019]108                mLODs.push_back(lodLevel);
[2844]109                sceneGeom->AddLODLevel(lodLevel);
110        }
111
[2755]112        return sceneGeom;
[2747]113}
114
115
[2795]116void ResourceManager::LoadTextures(igzstream &str)
[2757]117{
118        int numTextures;
119        str.read(reinterpret_cast<char *>(&numTextures), sizeof(int));
[2961]120       
[2757]121        for (int i = 0; i < numTextures; ++ i)
122        {
[2961]123                // load texture name
[2757]124                int texnameSize;
125                str.read(reinterpret_cast<char *>(&texnameSize), sizeof(int));
126
127                char *texname = new char[texnameSize];
128                str.read(texname, sizeof(char) * texnameSize);
129
[2784]130                Texture *tex = new Texture(model_path + texname);
[2757]131
[2846]132                int boundS, boundT;
133
134                str.read(reinterpret_cast<char *>(&boundS), sizeof(int));
135                str.read(reinterpret_cast<char *>(&boundT), sizeof(int));
136
137                tex->SetBoundaryModeS(boundS);
138                tex->SetBoundaryModeT(boundT);
139
140                tex->Create();
141
[2764]142                mTextureTable[i] = tex;
[2795]143                mTextures.push_back(tex);
[3021]144
145                delete [] texname;
[2757]146        }
[2764]147
[2800]148        cout << "loaded " << (int)mTextureTable.size() << " textures" << endl;
[2757]149}
150
151
[2795]152void ResourceManager::LoadShapes(igzstream &str)
[2764]153{
154        int numShapes;
155        str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
156
157        for (int i = 0; i < numShapes; ++ i)
158        {
159                Geometry *geom = LoadGeometry(str);
160                Material *mat = LoadMaterial(str);
161
162                mGeometryTable[i] = geom;
[2842]163                mMaterialTable[i] = mat;               
[2795]164
165                mGeometry.push_back(geom);
166                mMaterials.push_back(mat);
[2764]167        }
168
[2800]169        cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl;
[2764]170}
171
172
[2795]173Material *ResourceManager::LoadMaterial(igzstream &str)
[2747]174{
[2756]175        // default material
[2755]176        Material *mat = new Material();
[2756]177       
[2747]178        // texture
[2757]179        int texId;
180        str.read(reinterpret_cast<char *>(&texId), sizeof(int));
[2747]181
[2982]182        if (texId >= 0)
[2757]183                mat->SetTexture(mTextureTable[texId]);
[2764]184       
[2769]185        str.read(reinterpret_cast<char *>(&mat->mAlphaTestEnabled), sizeof(bool));
[2747]186
[2845]187        str.read(reinterpret_cast<char *>(&mat->mCullFaceEnabled), sizeof(bool));
[2844]188
[3034]189        // gl material
190        bool hasGlMaterial;
191        str.read(reinterpret_cast<char *>(&hasGlMaterial), sizeof(bool));
[2747]192       
[3034]193        if (hasGlMaterial)
[2747]194        {
[2769]195                // only write rgb part of the material
196                str.read(reinterpret_cast<char *>(&mat->mAmbientColor), sizeof(Vector3));
197                str.read(reinterpret_cast<char *>(&mat->mDiffuseColor), sizeof(Vector3));
[2795]198                str.read(reinterpret_cast<char *>(&mat->mEmmisiveColor), sizeof(Vector3));
[2769]199                str.read(reinterpret_cast<char *>(&mat->mSpecularColor), sizeof(Vector3));
[2747]200        }
201
[3034]202        mat->mVertexProgram = RenderState::sCgMrtVertexProgram;
203
204        if (mat->GetTexture())
205        {
206                mat->mFragmentProgram = RenderState::sCgMrtFragmentTexProgram;
207
208                mat->mGPUFragmentParameters = new GPUProgramParameters(mat->mFragmentProgram);
209                mat->mGPUFragmentParameters->SetTexture(0, mat->GetTexture()->GetId());
210        }       
211        else
212        {
213                mat->mFragmentProgram = RenderState::sCgMrtFragmentProgram;
214                mat->mGPUFragmentParameters = new GPUProgramParameters(mat->mFragmentProgram);
215        }
216
217        mat->mGPUVertexParameters = new GPUProgramParameters(RenderState::sCgMrtVertexProgram);
218
[2755]219        return mat;
[2747]220}
221
222
[2795]223Geometry *ResourceManager::LoadGeometry(igzstream &str)
[2747]224{
[2756]225        Vector3 *vertices;
226        Vector3 *normals;
[2980]227        Texcoord2 *texcoords;
[2747]228
229
[2756]230        //////////////
231        //-- read in vertices
[2747]232
[2756]233        int vertexCount;
234        str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
[2747]235       
236        // end of file reached
[2961]237        if (str.eof()) return NULL;
[2747]238
[2757]239        //cout << "vertexcount: " << vertexCount << endl;
[2747]240
[2756]241        vertices = new Vector3[vertexCount];
242    str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
[2747]243       
[2756]244        normals = new Vector3[vertexCount];
245        str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
[2747]246
[2756]247        int texCoordCount;
248        str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
[2747]249
[2756]250        if (texCoordCount)
[2747]251        {
[2980]252                texcoords = new Texcoord2[texCoordCount];
253                str.read(reinterpret_cast<char *>(texcoords), sizeof(Texcoord2) * texCoordCount);
[2747]254        }
[2756]255        else
[2811]256        {
[2756]257                texcoords = NULL;
[2811]258        }
[2747]259
[2982]260        return new Geometry(vertices, normals, texcoords, vertexCount, true);
261        //return new Geometry(vertices, normals, texcoords, vertexCount, false);
[2747]262}
263
264
[2795]265void ResourceManager::LoadSceneEntities(igzstream &str, SceneEntityContainer &entities)
[2747]266{
[2764]267        int entityCount;
268        str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
269
[2963]270        entities.reserve(entityCount);
[2764]271
272        for (int i = 0; i < entityCount; ++ i)
273        {
274                SceneEntity *ent = LoadSceneEntity(str);
[2842]275
[2963]276                // return loaded entities
277                entities.push_back(ent);
278                // also store internally
[2795]279                mSceneEntities.push_back(ent);
[2764]280        }
281
282        cout << "loaded " << entityCount << " scene entities" << endl;
283}
284
285
[2795]286bool ResourceManager::Load(const std::string &filename, SceneEntityContainer &entities)
[2764]287{
[2795]288        igzstream istr(filename.c_str());
[2747]289       
[2756]290        if (!istr.is_open())
[2747]291                return false;
292
[2764]293        cout << "loading textures" << endl;
294       
[2757]295        // load the texture table
296        LoadTextures(istr);
297
[2764]298        cout << "loading shapes (geometry + materials)" << endl;
[2747]299
[2764]300        // load the shapees
301        LoadShapes(istr);
[2762]302
[2764]303        cout << "loading scene entites" << endl;
304        LoadSceneEntities(istr, entities);
[2763]305       
[2756]306        cout << "bin loading finished" << endl;
[2747]307
[2795]308        // clean up
309        mTextureTable.clear();
310        mGeometryTable.clear();
311        mMaterialTable.clear();
312
[2756]313        return true;
[2747]314}
315
316}
Note: See TracBrowser for help on using the repository browser.