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 CHCDemo
|
---|
14 | {
|
---|
15 |
|
---|
16 |
|
---|
17 | //SceneEntity *BinaryLoader::LoadSceneEntity(igzstream &str)
|
---|
18 | SceneEntity *BinaryLoader::LoadSceneEntity(ifstream &str)
|
---|
19 | {
|
---|
20 | // shape
|
---|
21 | int shapeId;
|
---|
22 | str.read(reinterpret_cast<char *>(&shapeId), sizeof(int));
|
---|
23 |
|
---|
24 | Geometry *geom = mGeometryTable[shapeId];
|
---|
25 | Material *mat = mMaterialTable[shapeId];
|
---|
26 |
|
---|
27 |
|
---|
28 | bool hasTrafo;
|
---|
29 | str.read(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
|
---|
30 |
|
---|
31 | Matrix4x4 *trafo;
|
---|
32 |
|
---|
33 | if (!hasTrafo)
|
---|
34 | {
|
---|
35 | trafo = NULL;
|
---|
36 | }
|
---|
37 | else
|
---|
38 | {
|
---|
39 | trafo = new Matrix4x4();
|
---|
40 | //*trafo = IdentityMatrix();
|
---|
41 | str.read(reinterpret_cast<char *>(trafo->x), sizeof(Matrix4x4));
|
---|
42 | //str.read(reinterpret_cast<char *>(trafo), sizeof(Matrix4x4));
|
---|
43 | //cout << "m:\n" << *trafo<<endl;
|
---|
44 | }
|
---|
45 |
|
---|
46 | SceneEntity *sceneGeom = new SceneEntity(geom, mat, trafo);
|
---|
47 |
|
---|
48 | return sceneGeom;
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | void BinaryLoader::LoadTextures(ifstream &str)
|
---|
53 | {
|
---|
54 | int numTextures;
|
---|
55 | str.read(reinterpret_cast<char *>(&numTextures), sizeof(int));
|
---|
56 |
|
---|
57 | for (int i = 0; i < numTextures; ++ i)
|
---|
58 | {
|
---|
59 | // texture
|
---|
60 | int texnameSize;
|
---|
61 |
|
---|
62 | //str.read(reinterpret_cast<char *>(&id), sizeof(int));
|
---|
63 | str.read(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
64 |
|
---|
65 | char *texname = new char[texnameSize];
|
---|
66 | str.read(texname, sizeof(char) * texnameSize);
|
---|
67 |
|
---|
68 | //cout << "loading texture " << texname << " with len " << texnameSize << " id: " << id << endl;
|
---|
69 | Texture *tex = new Texture(texname);
|
---|
70 |
|
---|
71 | mTextureTable[i] = tex;
|
---|
72 | }
|
---|
73 |
|
---|
74 | cout << "loaded " << mTextureTable.size() << " textures" << endl;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | void BinaryLoader::LoadShapes(ifstream &str)
|
---|
79 | {
|
---|
80 | int numShapes;
|
---|
81 | str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
|
---|
82 |
|
---|
83 | for (int i = 0; i < numShapes; ++ i)
|
---|
84 | {
|
---|
85 | Geometry *geom = LoadGeometry(str);
|
---|
86 | Material *mat = LoadMaterial(str);
|
---|
87 |
|
---|
88 | mGeometryTable[i] = geom;
|
---|
89 | mMaterialTable[i] = mat;
|
---|
90 | }
|
---|
91 |
|
---|
92 | cout << "loaded " << mGeometryTable.size() << " shapes" << endl;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | //Appearance *BinaryLoader::LoadMaterial(igzstream &str)
|
---|
97 | Material *BinaryLoader::LoadMaterial(ifstream &str)
|
---|
98 | {
|
---|
99 | // default material
|
---|
100 | Material *mat = new Material();
|
---|
101 |
|
---|
102 | // texture
|
---|
103 | int texId;
|
---|
104 | str.read(reinterpret_cast<char *>(&texId), sizeof(int));
|
---|
105 |
|
---|
106 | if (texId >= 0)
|
---|
107 | mat->SetTexture(mTextureTable[texId]);
|
---|
108 |
|
---|
109 |
|
---|
110 | // material
|
---|
111 | /*char hasMaterial;
|
---|
112 | str.read(&hasMaterial, sizeof(char));
|
---|
113 |
|
---|
114 | if (hasMaterial)
|
---|
115 | {
|
---|
116 | Color3f amb(0.0f, 0.0f, 0.0f);
|
---|
117 | Color3f dif(0.5f, 0.5f, 0.5f);
|
---|
118 | Color3f spec(0.0f, 0.0f, 0.0f);
|
---|
119 | Color3f emi(0.0f, 0.0f, 0.0f);
|
---|
120 |
|
---|
121 | float shine = 0;
|
---|
122 |
|
---|
123 | //str.read(reinterpret_cast<char *>(&amb), sizeof(Color3f));
|
---|
124 | str.read(reinterpret_cast<char *>(&dif), sizeof(Color3f));
|
---|
125 | //str.read(reinterpret_cast<char *>(&emi), sizeof(Color3f));
|
---|
126 | //str.read(reinterpret_cast<char *>(&spec), sizeof(Color3f));
|
---|
127 | //str.read(reinterpret_cast<char *>(&shine), sizeof(float));
|
---|
128 |
|
---|
129 | mat->setAmbientColor(amb);
|
---|
130 | mat->setDiffuseColor(dif);
|
---|
131 | mat->setEmissiveColor(emi);
|
---|
132 | mat->setSpecularColor(spec);
|
---|
133 | mat->setShininess(shine);
|
---|
134 |
|
---|
135 | app->setMaterial(mat);
|
---|
136 | }
|
---|
137 |
|
---|
138 | return app;*/
|
---|
139 | return mat;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | //Geometry *BinaryLoader::LoadGeometry(igzstream &str)
|
---|
144 | Geometry *BinaryLoader::LoadGeometry(ifstream &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 | //cout << "loading texcoords of size " << texCoordCount << endl;
|
---|
175 | texcoords = new float[texCoordCount * 2];
|
---|
176 | str.read(reinterpret_cast<char *>(texcoords), sizeof(float) * vertexCount * 2);
|
---|
177 | }
|
---|
178 | else
|
---|
179 | texcoords = NULL;
|
---|
180 |
|
---|
181 | for (int i = 0; i < vertexCount; i += 3)
|
---|
182 | {
|
---|
183 | Triangle3 tri(vertices[i], vertices[i + 1], vertices[i + 2]);
|
---|
184 | Vector3 n = tri.GetNormal();
|
---|
185 |
|
---|
186 | normals[i + 0] = n;
|
---|
187 | normals[i + 1] = n;
|
---|
188 | normals[i + 2] = n;
|
---|
189 | }
|
---|
190 |
|
---|
191 | return new Geometry(vertices, normals, texcoords, vertexCount);
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | void BinaryLoader::LoadSceneEntities(ifstream &str, SceneEntityContainer &entities)
|
---|
196 | {
|
---|
197 | int entityCount;
|
---|
198 | str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
199 |
|
---|
200 | entities.resize(entityCount);
|
---|
201 |
|
---|
202 | for (int i = 0; i < entityCount; ++ i)
|
---|
203 | {
|
---|
204 | SceneEntity *ent = LoadSceneEntity(str);
|
---|
205 | ent->id = i;
|
---|
206 | entities[i] = ent;
|
---|
207 | }
|
---|
208 |
|
---|
209 | cout << "loaded " << entityCount << " scene entities" << endl;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | bool BinaryLoader::Load(const std::string &filename, SceneEntityContainer &entities)
|
---|
214 | {
|
---|
215 | //clear_textures(); // clear the texture table
|
---|
216 |
|
---|
217 | //igzstream str(filename.c_str());//, ios::binary);
|
---|
218 | ifstream istr(filename.c_str(), ios::binary);
|
---|
219 |
|
---|
220 | if (!istr.is_open())
|
---|
221 | return false;
|
---|
222 |
|
---|
223 | cout << "loading textures" << endl;
|
---|
224 |
|
---|
225 | // load the texture table
|
---|
226 | LoadTextures(istr);
|
---|
227 |
|
---|
228 | cout << "loading shapes (geometry + materials)" << endl;
|
---|
229 |
|
---|
230 | // load the shapees
|
---|
231 | LoadShapes(istr);
|
---|
232 |
|
---|
233 | cout << "loading scene entites" << endl;
|
---|
234 | LoadSceneEntities(istr, entities);
|
---|
235 |
|
---|
236 | cout << "bin loading finished" << endl;
|
---|
237 |
|
---|
238 | return true;
|
---|
239 | }
|
---|
240 |
|
---|
241 | }
|
---|