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