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 | Geometry *geom = LoadGeometry(str);
|
---|
21 | Material *mat = LoadMaterial(str);
|
---|
22 | Matrix4x4 *trafo = NULL;//IdentityMatrix();
|
---|
23 |
|
---|
24 | SceneEntity *sceneGeom = new SceneEntity(geom, mat, trafo);
|
---|
25 |
|
---|
26 | return sceneGeom;
|
---|
27 | }
|
---|
28 |
|
---|
29 |
|
---|
30 | void BinaryLoader::LoadTextures(ifstream &str)
|
---|
31 | {
|
---|
32 | int numTextures;
|
---|
33 | str.read(reinterpret_cast<char *>(&numTextures), sizeof(int));
|
---|
34 |
|
---|
35 | for (int i = 0; i < numTextures; ++ i)
|
---|
36 | {
|
---|
37 | // texture
|
---|
38 | int texnameSize;
|
---|
39 | int id;
|
---|
40 |
|
---|
41 | str.read(reinterpret_cast<char *>(&id), sizeof(int));
|
---|
42 | str.read(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
43 |
|
---|
44 | char *texname = new char[texnameSize];
|
---|
45 | str.read(texname, sizeof(char) * texnameSize);
|
---|
46 |
|
---|
47 | //cout << "loading texture " << texname << " with len " << texnameSize << " id: " << id << endl;
|
---|
48 | Texture *tex = new Texture(texname);
|
---|
49 |
|
---|
50 | mTextureTable[id] = tex;
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | //Appearance *BinaryLoader::LoadMaterial(igzstream &str)
|
---|
56 | Material *BinaryLoader::LoadMaterial(ifstream &str)
|
---|
57 | {
|
---|
58 | // default material
|
---|
59 | Material *mat = new Material();
|
---|
60 |
|
---|
61 | // texture
|
---|
62 | int texId;
|
---|
63 |
|
---|
64 | str.read(reinterpret_cast<char *>(&texId), sizeof(int));
|
---|
65 | //cout << "texid: " << texId << endl;
|
---|
66 |
|
---|
67 | if (texId >= 0)
|
---|
68 | {
|
---|
69 | mat->SetTexture(mTextureTable[texId]);
|
---|
70 | }
|
---|
71 |
|
---|
72 | // material
|
---|
73 | /*char hasMaterial;
|
---|
74 | str.read(&hasMaterial, sizeof(char));
|
---|
75 |
|
---|
76 | if (hasMaterial)
|
---|
77 | {
|
---|
78 | Color3f amb(0.0f, 0.0f, 0.0f);
|
---|
79 | Color3f dif(0.5f, 0.5f, 0.5f);
|
---|
80 | Color3f spec(0.0f, 0.0f, 0.0f);
|
---|
81 | Color3f emi(0.0f, 0.0f, 0.0f);
|
---|
82 |
|
---|
83 | float shine = 0;
|
---|
84 |
|
---|
85 | //str.read(reinterpret_cast<char *>(&amb), sizeof(Color3f));
|
---|
86 | str.read(reinterpret_cast<char *>(&dif), sizeof(Color3f));
|
---|
87 | //str.read(reinterpret_cast<char *>(&emi), sizeof(Color3f));
|
---|
88 | //str.read(reinterpret_cast<char *>(&spec), sizeof(Color3f));
|
---|
89 | //str.read(reinterpret_cast<char *>(&shine), sizeof(float));
|
---|
90 |
|
---|
91 | mat->setAmbientColor(amb);
|
---|
92 | mat->setDiffuseColor(dif);
|
---|
93 | mat->setEmissiveColor(emi);
|
---|
94 | mat->setSpecularColor(spec);
|
---|
95 | mat->setShininess(shine);
|
---|
96 |
|
---|
97 | app->setMaterial(mat);
|
---|
98 | }
|
---|
99 |
|
---|
100 | return app;*/
|
---|
101 |
|
---|
102 | return mat;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | //Geometry *BinaryLoader::LoadGeometry(igzstream &str)
|
---|
107 | Geometry *BinaryLoader::LoadGeometry(ifstream &str)
|
---|
108 | {
|
---|
109 | Vector3 *vertices;
|
---|
110 | Vector3 *normals;
|
---|
111 | float *texcoords;
|
---|
112 |
|
---|
113 |
|
---|
114 | //////////////
|
---|
115 | //-- read in vertices
|
---|
116 |
|
---|
117 | int vertexCount;
|
---|
118 | str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
119 |
|
---|
120 | // end of file reached
|
---|
121 | if (str.eof())
|
---|
122 | return NULL;
|
---|
123 |
|
---|
124 | //cout << "vertexcount: " << vertexCount << endl;
|
---|
125 |
|
---|
126 | vertices = new Vector3[vertexCount];
|
---|
127 | str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
|
---|
128 |
|
---|
129 | normals = new Vector3[vertexCount];
|
---|
130 | str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
|
---|
131 |
|
---|
132 | int texCoordCount;
|
---|
133 | str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
134 |
|
---|
135 | if (texCoordCount)
|
---|
136 | {
|
---|
137 | //cout << "loading texcoords of size " << texCoordCount << endl;
|
---|
138 | texcoords = new float[texCoordCount * 2];
|
---|
139 | str.read(reinterpret_cast<char *>(texcoords), sizeof(float) * vertexCount * 2);
|
---|
140 | }
|
---|
141 | else
|
---|
142 | texcoords = NULL;
|
---|
143 |
|
---|
144 | for (int i = 0; i < vertexCount; i += 3)
|
---|
145 | {
|
---|
146 | Triangle3 tri(vertices[i], vertices[i + 1], vertices[i + 2]);
|
---|
147 | Vector3 n = tri.GetNormal();
|
---|
148 |
|
---|
149 | normals[i + 0] = n;
|
---|
150 | normals[i + 1] = n;
|
---|
151 | normals[i + 2] = n;
|
---|
152 | }
|
---|
153 |
|
---|
154 | return new Geometry(vertices, normals, texcoords, vertexCount);
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | bool BinaryLoader::Load(const std::string &filename, SceneEntityContainer &geometry)
|
---|
159 | {
|
---|
160 | //clear_textures(); // clear the texture table
|
---|
161 |
|
---|
162 | //igzstream str(filename.c_str());//, ios::binary);
|
---|
163 | ifstream istr(filename.c_str(), ios::binary);
|
---|
164 |
|
---|
165 | if (!istr.is_open())
|
---|
166 | return false;
|
---|
167 |
|
---|
168 | // load the texture table
|
---|
169 | LoadTextures(istr);
|
---|
170 |
|
---|
171 | // #shapes
|
---|
172 | int shapeCount;
|
---|
173 | istr.read(reinterpret_cast<char *>(&shapeCount), sizeof(int));
|
---|
174 |
|
---|
175 | geometry.resize(shapeCount);
|
---|
176 |
|
---|
177 | for (size_t i = 0; i < geometry.size(); ++ i)
|
---|
178 | geometry[i] = NULL;
|
---|
179 |
|
---|
180 |
|
---|
181 | int progress = 0;
|
---|
182 |
|
---|
183 | for (int i = 0; i < shapeCount; ++ i)
|
---|
184 | {
|
---|
185 | //int id;istr.read(reinterpret_cast<char *>(&id), sizeof(int));
|
---|
186 | SceneEntity *ent = LoadSceneEntity(istr);
|
---|
187 | ent->id = i;
|
---|
188 |
|
---|
189 | geometry[i] = ent;
|
---|
190 |
|
---|
191 | int p = (i + 1) * 100 / shapeCount;
|
---|
192 |
|
---|
193 | if (p >= progress)
|
---|
194 | {
|
---|
195 | cout << "% loaded: " << p << endl;
|
---|
196 | progress += 10;
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | for (size_t i = 0; i < geometry.size(); ++ i)
|
---|
201 | {
|
---|
202 | if (!geometry[i])
|
---|
203 | Debug << "error: index " << i << " missing" << endl;
|
---|
204 | }
|
---|
205 |
|
---|
206 | cout << "bin loading finished" << endl;
|
---|
207 |
|
---|
208 | return true;
|
---|
209 | }
|
---|
210 |
|
---|
211 | }
|
---|