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