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

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                // hack
71                distance = i * 20;
72
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);
94                        lodLevel->AddShape(shape);
95                }
96
97                //if (numLODs > 1) cout << endl;
98
99                sceneGeom->AddLODLevel(lodLevel);
100        }
101
102        return sceneGeom;
103}
104
105
106void ResourceManager::LoadTextures(igzstream &str)
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
116                //str.read(reinterpret_cast<char *>(&id), sizeof(int));
117                str.read(reinterpret_cast<char *>(&texnameSize), sizeof(int));
118
119                char *texname = new char[texnameSize];
120                str.read(texname, sizeof(char) * texnameSize);
121
122                Texture *tex = new Texture(model_path + texname);
123
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
134                mTextureTable[i] = tex;
135                mTextures.push_back(tex);
136        }
137
138        cout << "loaded " << (int)mTextureTable.size() << " textures" << endl;
139}
140
141
142void ResourceManager::LoadShapes(igzstream &str)
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;
153                mMaterialTable[i] = mat;               
154
155                mGeometry.push_back(geom);
156                mMaterials.push_back(mat);
157        }
158
159        cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl;
160}
161
162
163Material *ResourceManager::LoadMaterial(igzstream &str)
164{
165        // default material
166        Material *mat = new Material();
167       
168        // texture
169        int texId;
170        str.read(reinterpret_cast<char *>(&texId), sizeof(int));
171
172        if (texId >= 0)
173                mat->SetTexture(mTextureTable[texId]);
174       
175        str.read(reinterpret_cast<char *>(&mat->mAlphaTestEnabled), sizeof(bool));
176
177        str.read(reinterpret_cast<char *>(&mat->mCullFaceEnabled), sizeof(bool));
178
179        // material
180        bool hasMaterial;
181        str.read(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
182       
183        if (hasMaterial)
184        {
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));
188                str.read(reinterpret_cast<char *>(&mat->mEmmisiveColor), sizeof(Vector3));
189                str.read(reinterpret_cast<char *>(&mat->mSpecularColor), sizeof(Vector3));
190        }
191
192        return mat;
193}
194
195
196Geometry *ResourceManager::LoadGeometry(igzstream &str)
197{
198        Vector3 *vertices;
199        Vector3 *normals;
200        float *texcoords;
201
202
203        //////////////
204        //-- read in vertices
205
206        int vertexCount;
207        str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
208       
209        // end of file reached
210        if (str.eof())
211                return NULL;
212
213        //cout << "vertexcount: " << vertexCount << endl;
214
215        vertices = new Vector3[vertexCount];
216    str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
217       
218        normals = new Vector3[vertexCount];
219        str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
220
221        int texCoordCount;
222        str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
223
224        if (texCoordCount)
225        {
226                texcoords = new float[texCoordCount * 2];
227                str.read(reinterpret_cast<char *>(texcoords), sizeof(float) * vertexCount * 2);
228        }
229        else
230        {
231                texcoords = NULL;
232        }
233       
234
235        return new Geometry(vertices, normals, texcoords, vertexCount, true);
236}
237
238
239void ResourceManager::LoadSceneEntities(igzstream &str, SceneEntityContainer &entities)
240{
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;
250
251                mSceneEntities.push_back(ent);
252        }
253
254        cout << "loaded " << entityCount << " scene entities" << endl;
255}
256
257
258bool ResourceManager::Load(const std::string &filename, SceneEntityContainer &entities)
259{
260        igzstream istr(filename.c_str());
261       
262        if (!istr.is_open())
263                return false;
264
265        cout << "loading textures" << endl;
266       
267        // load the texture table
268        LoadTextures(istr);
269
270        cout << "loading shapes (geometry + materials)" << endl;
271
272        // load the shapees
273        LoadShapes(istr);
274
275        cout << "loading scene entites" << endl;
276        LoadSceneEntities(istr, entities);
277       
278        cout << "bin loading finished" << endl;
279
280        // clean up
281        mTextureTable.clear();
282        mGeometryTable.clear();
283        mMaterialTable.clear();
284        //mShapesTable.clear();
285
286        return true;
287}
288
289}
Note: See TracBrowser for help on using the repository browser.