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 | str.read(reinterpret_cast<char *>(&mat->mAlphaTestEnabled), sizeof(bool));
|
---|
110 |
|
---|
111 | // material
|
---|
112 | bool hasMaterial;
|
---|
113 | str.read(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
|
---|
114 |
|
---|
115 | if (hasMaterial)
|
---|
116 | {
|
---|
117 | // only write rgb part of the material
|
---|
118 | str.read(reinterpret_cast<char *>(&mat->mAmbientColor), sizeof(Vector3));
|
---|
119 | str.read(reinterpret_cast<char *>(&mat->mDiffuseColor), sizeof(Vector3));
|
---|
120 | str.read(reinterpret_cast<char *>(&mat->mSpecularColor), sizeof(Vector3));
|
---|
121 |
|
---|
122 | /*RgbColor dum;
|
---|
123 | str.read(reinterpret_cast<char *>(&dum), sizeof(RgbColor));
|
---|
124 | str.read(reinterpret_cast<char *>(&dum), sizeof(RgbColor));
|
---|
125 | str.read(reinterpret_cast<char *>(&dum), sizeof(RgbColor));
|
---|
126 | */
|
---|
127 | }
|
---|
128 |
|
---|
129 | return mat;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | //Geometry *BinaryLoader::LoadGeometry(igzstream &str)
|
---|
134 | Geometry *BinaryLoader::LoadGeometry(ifstream &str)
|
---|
135 | {
|
---|
136 | Vector3 *vertices;
|
---|
137 | Vector3 *normals;
|
---|
138 | float *texcoords;
|
---|
139 |
|
---|
140 |
|
---|
141 | //////////////
|
---|
142 | //-- read in vertices
|
---|
143 |
|
---|
144 | int vertexCount;
|
---|
145 | str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
146 |
|
---|
147 | // end of file reached
|
---|
148 | if (str.eof())
|
---|
149 | return NULL;
|
---|
150 |
|
---|
151 | //cout << "vertexcount: " << vertexCount << endl;
|
---|
152 |
|
---|
153 | vertices = new Vector3[vertexCount];
|
---|
154 | str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
|
---|
155 |
|
---|
156 | normals = new Vector3[vertexCount];
|
---|
157 | str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
|
---|
158 |
|
---|
159 | int texCoordCount;
|
---|
160 | str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
161 |
|
---|
162 | if (texCoordCount)
|
---|
163 | {
|
---|
164 | //cout << "loading texcoords of size " << texCoordCount << endl;
|
---|
165 | texcoords = new float[texCoordCount * 2];
|
---|
166 | str.read(reinterpret_cast<char *>(texcoords), sizeof(float) * vertexCount * 2);
|
---|
167 | }
|
---|
168 | else
|
---|
169 | texcoords = NULL;
|
---|
170 |
|
---|
171 | for (int i = 0; i < vertexCount; i += 3)
|
---|
172 | {
|
---|
173 | Triangle3 tri(vertices[i], vertices[i + 1], vertices[i + 2]);
|
---|
174 | Vector3 n = tri.GetNormal();
|
---|
175 |
|
---|
176 | normals[i + 0] = n;
|
---|
177 | normals[i + 1] = n;
|
---|
178 | normals[i + 2] = n;
|
---|
179 | }
|
---|
180 |
|
---|
181 | return new Geometry(vertices, normals, texcoords, vertexCount);
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | void BinaryLoader::LoadSceneEntities(ifstream &str, SceneEntityContainer &entities)
|
---|
186 | {
|
---|
187 | int entityCount;
|
---|
188 | str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
189 |
|
---|
190 | entities.resize(entityCount);
|
---|
191 |
|
---|
192 | for (int i = 0; i < entityCount; ++ i)
|
---|
193 | {
|
---|
194 | SceneEntity *ent = LoadSceneEntity(str);
|
---|
195 | ent->id = i;
|
---|
196 | entities[i] = ent;
|
---|
197 | }
|
---|
198 |
|
---|
199 | cout << "loaded " << entityCount << " scene entities" << endl;
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | bool BinaryLoader::Load(const std::string &filename, SceneEntityContainer &entities)
|
---|
204 | {
|
---|
205 | //clear_textures(); // clear the texture table
|
---|
206 |
|
---|
207 | //igzstream str(filename.c_str());//, ios::binary);
|
---|
208 | ifstream istr(filename.c_str(), ios::binary);
|
---|
209 |
|
---|
210 | if (!istr.is_open())
|
---|
211 | return false;
|
---|
212 |
|
---|
213 | cout << "loading textures" << endl;
|
---|
214 |
|
---|
215 | // load the texture table
|
---|
216 | LoadTextures(istr);
|
---|
217 |
|
---|
218 | cout << "loading shapes (geometry + materials)" << endl;
|
---|
219 |
|
---|
220 | // load the shapees
|
---|
221 | LoadShapes(istr);
|
---|
222 |
|
---|
223 | cout << "loading scene entites" << endl;
|
---|
224 | LoadSceneEntities(istr, entities);
|
---|
225 |
|
---|
226 | cout << "bin loading finished" << endl;
|
---|
227 |
|
---|
228 | return true;
|
---|
229 | }
|
---|
230 |
|
---|
231 | }
|
---|