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

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