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