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

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

detected memory leaks mainly in shadowmapping!!
strange problems with deferred rendering, seems to be uninitialized sometimes (solved?)

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