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

Revision 2889, 6.4 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        // note: lods must be ordered from smallest distance to largest
63        int numLODs;
64        str.read(reinterpret_cast<char *>(&numLODs), sizeof(int));
65
66        for (int i = 0; i < numLODs; ++ i)
67        {
68                float dist;
69                str.read(reinterpret_cast<char *>(&dist), sizeof(float));
70
71                //if (i>=3) dist=2e20;
72                int numShapes;
73                str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
74
75                LODLevel *lodLevel = new LODLevel(dist);
76
77                for (int j = 0; j < numShapes; ++ j)
78                {
79                        int shapeId;
80                        str.read(reinterpret_cast<char *>(&shapeId), sizeof(int));
81
82                        Geometry *geom = mGeometryTable[shapeId];
83                        Material *mat = mMaterialTable[shapeId];
84
85                        // create shape
86                        Shape *shape = new Shape(geom, mat, sceneGeom);
87                       
88                        mShapes.push_back(shape);
89
90                        sceneGeom->AddShape(shape);
91                        lodLevel->AddShape(shape);
92                }
93
94                sceneGeom->AddLODLevel(lodLevel);
95        }
96
97        return sceneGeom;
98}
99
100
101void ResourceManager::LoadTextures(igzstream &str)
102{
103        int numTextures;
104        str.read(reinterpret_cast<char *>(&numTextures), sizeof(int));
105
106        for (int i = 0; i < numTextures; ++ i)
107        {
108                // texture
109                int texnameSize;
110
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())
205                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 float[texCoordCount * 2];
221                str.read(reinterpret_cast<char *>(texcoords), sizeof(float) * vertexCount * 2);
222        }
223        else
224        {
225                texcoords = NULL;
226        }
227       
228#if 0
229        // construct normals
230        for (int i = 0; i < vertexCount; i += 3)
231        {
232                Triangle3 tri(vertices[i], vertices[i + 1], vertices[i + 2]);
233
234                normals[i] = tri.GetNormal();
235                normals[i+1] = tri.GetNormal();
236                normals[i+2] = tri.GetNormal();
237        }
238#endif
239
240        return new Geometry(vertices, normals, texcoords, vertexCount, true);
241}
242
243
244void ResourceManager::LoadSceneEntities(igzstream &str, SceneEntityContainer &entities)
245{
246        int entityCount;
247        str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
248
249        entities.resize(entityCount);
250
251        for (int i = 0; i < entityCount; ++ i)
252        {
253                SceneEntity *ent = LoadSceneEntity(str);
254                entities[i] = ent;
255
256                mSceneEntities.push_back(ent);
257        }
258
259        cout << "loaded " << entityCount << " scene entities" << endl;
260}
261
262
263bool ResourceManager::Load(const std::string &filename, SceneEntityContainer &entities)
264{
265        igzstream istr(filename.c_str());
266       
267        if (!istr.is_open())
268                return false;
269
270        cout << "loading textures" << endl;
271       
272        // load the texture table
273        LoadTextures(istr);
274
275        cout << "loading shapes (geometry + materials)" << endl;
276
277        // load the shapees
278        LoadShapes(istr);
279
280        cout << "loading scene entites" << endl;
281        LoadSceneEntities(istr, entities);
282       
283        cout << "bin loading finished" << endl;
284
285        // clean up
286        mTextureTable.clear();
287        mGeometryTable.clear();
288        mMaterialTable.clear();
289        //mShapesTable.clear();
290
291        return true;
292}
293
294}
Note: See TracBrowser for help on using the repository browser.