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