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

Revision 2963, 6.1 KB checked in by mattausch, 16 years ago (diff)

debug version trying to find out how to speed up app

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