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