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

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

using sample_to_coverage instead of alpha

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