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