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