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

Revision 2861, 6.1 KB checked in by mattausch, 16 years ago (diff)

cleaned up code

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