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

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