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

Revision 2800, 5.0 KB checked in by mattausch, 16 years ago (diff)

friendly culling debug version with timers, no materials

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
9
10using namespace std;
11
12
13namespace CHCDemoEngine
14{
15
16
17ResourceManager::~ResourceManager()
18{
19        // delete all resources.
20        CLEAR_CONTAINER(mSceneEntities);
21        CLEAR_CONTAINER(mGeometry);
22        CLEAR_CONTAINER(mMaterials);
23        CLEAR_CONTAINER(mTextures);
24        CLEAR_CONTAINER(mTrafos);
25}
26
27
28SceneEntity *ResourceManager::LoadSceneEntity(igzstream &str)
29{       
30        // shape
31        int shapeId;
32        str.read(reinterpret_cast<char *>(&shapeId), sizeof(int));
33
34        Geometry *geom = mGeometryTable[shapeId];
35        Material *mat = mMaterialTable[shapeId];
36
37
38        bool hasTrafo;
39        str.read(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
40
41        Matrix4x4 *trafo;
42       
43        if (!hasTrafo)
44        {
45                trafo = NULL;
46        }
47        else
48        {
49                trafo = new Matrix4x4();
50                str.read(reinterpret_cast<char *>(trafo->x), sizeof(Matrix4x4));
51                mTrafos.push_back(trafo);
52        }
53
54        SceneEntity *sceneGeom = new SceneEntity(geom, mat, trafo);
55
56        return sceneGeom;
57}
58
59
60void ResourceManager::LoadTextures(igzstream &str)
61{
62        int numTextures;
63        str.read(reinterpret_cast<char *>(&numTextures), sizeof(int));
64
65        for (int i = 0; i < numTextures; ++ i)
66        {
67                // texture
68                int texnameSize;
69
70                //str.read(reinterpret_cast<char *>(&id), sizeof(int));
71                str.read(reinterpret_cast<char *>(&texnameSize), sizeof(int));
72
73                char *texname = new char[texnameSize];
74                str.read(texname, sizeof(char) * texnameSize);
75
76                //cout << "loading texture " << texname << " with len " << texnameSize << " id: " << id << endl;
77                Texture *tex = new Texture(model_path + texname);
78
79                mTextureTable[i] = tex;
80                mTextures.push_back(tex);
81        }
82
83        cout << "loaded " << (int)mTextureTable.size() << " textures" << endl;
84}
85
86
87void ResourceManager::LoadShapes(igzstream &str)
88{
89        int numShapes;
90        str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
91
92        for (int i = 0; i < numShapes; ++ i)
93        {
94                Geometry *geom = LoadGeometry(str);
95                Material *mat = LoadMaterial(str);
96
97                mGeometryTable[i] = geom;
98                mMaterialTable[i] = mat;
99
100                mGeometry.push_back(geom);
101                mMaterials.push_back(mat);
102        }
103
104        cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl;
105}
106
107
108Material *ResourceManager::LoadMaterial(igzstream &str)
109{
110        // default material
111        Material *mat = new Material();
112       
113        // texture
114        int texId;
115        str.read(reinterpret_cast<char *>(&texId), sizeof(int));
116
117        if (texId >= 0)
118                mat->SetTexture(mTextureTable[texId]);
119       
120        str.read(reinterpret_cast<char *>(&mat->mAlphaTestEnabled), sizeof(bool));
121
122        // material
123        bool hasMaterial;
124        str.read(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
125       
126        if (hasMaterial)
127        {
128                // only write rgb part of the material
129                str.read(reinterpret_cast<char *>(&mat->mAmbientColor), sizeof(Vector3));
130                str.read(reinterpret_cast<char *>(&mat->mDiffuseColor), sizeof(Vector3));
131                str.read(reinterpret_cast<char *>(&mat->mEmmisiveColor), sizeof(Vector3));
132                str.read(reinterpret_cast<char *>(&mat->mSpecularColor), sizeof(Vector3));
133        }
134
135        return mat;
136}
137
138
139Geometry *ResourceManager::LoadGeometry(igzstream &str)
140{
141        Vector3 *vertices;
142        Vector3 *normals;
143        float *texcoords;
144
145
146        //////////////
147        //-- read in vertices
148
149        int vertexCount;
150        str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
151       
152        // end of file reached
153        if (str.eof())
154                return NULL;
155
156        //cout << "vertexcount: " << vertexCount << endl;
157
158        vertices = new Vector3[vertexCount];
159    str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
160       
161        normals = new Vector3[vertexCount];
162        str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
163
164        int texCoordCount;
165        str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
166
167        if (texCoordCount)
168        {
169                texcoords = new float[texCoordCount * 2];
170                str.read(reinterpret_cast<char *>(texcoords), sizeof(float) * vertexCount * 2);
171        }
172        else
173                texcoords = NULL;
174
175        return new Geometry(vertices, normals, texcoords, vertexCount, true);
176}
177
178
179void ResourceManager::LoadSceneEntities(igzstream &str, SceneEntityContainer &entities)
180{
181        int entityCount;
182        str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
183
184        entities.resize(entityCount);
185
186        for (int i = 0; i < entityCount; ++ i)
187        {
188                SceneEntity *ent = LoadSceneEntity(str);
189                entities[i] = ent;
190                mSceneEntities.push_back(ent);
191        }
192
193        cout << "loaded " << entityCount << " scene entities" << endl;
194}
195
196
197bool ResourceManager::Load(const std::string &filename, SceneEntityContainer &entities)
198{
199        igzstream istr(filename.c_str());
200       
201        if (!istr.is_open())
202                return false;
203
204        cout << "loading textures" << endl;
205       
206        // load the texture table
207        LoadTextures(istr);
208
209        cout << "loading shapes (geometry + materials)" << endl;
210
211        // load the shapees
212        LoadShapes(istr);
213
214        cout << "loading scene entites" << endl;
215        LoadSceneEntities(istr, entities);
216       
217        cout << "bin loading finished" << endl;
218
219        // clean up
220        mTextureTable.clear();
221        mGeometryTable.clear();
222        mMaterialTable.clear();
223
224        return true;
225}
226
227}
Note: See TracBrowser for help on using the repository browser.