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

Revision 2850, 6.3 KB checked in by mattausch, 16 years ago (diff)

debug version before siggraph

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