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