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

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