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 | /*static STL_MAP<String,Texture *> stlm_textures;
|
---|
17 |
|
---|
18 |
|
---|
19 | // clear the texture table (called on new file load)
|
---|
20 | static void clear_textures()
|
---|
21 | {
|
---|
22 | STL_MAP<String,Texture *>::iterator it, it_end = stlm_textures.end();
|
---|
23 |
|
---|
24 | for (it = stlm_textures.begin(); it != it_end; ++ it)
|
---|
25 | {
|
---|
26 | it->second->unref();
|
---|
27 | delete [] it->first;
|
---|
28 |
|
---|
29 | it ++;
|
---|
30 | }
|
---|
31 |
|
---|
32 | stlm_textures.clear();
|
---|
33 | }
|
---|
34 |
|
---|
35 |
|
---|
36 | static Texture *get_texture(const char *filename, TextureAttributes *&myTextureAttributes)
|
---|
37 | {
|
---|
38 | Texture * myTexture = NULL;
|
---|
39 |
|
---|
40 | myTextureAttributes = new TextureAttributes;
|
---|
41 | myTextureAttributes->setTextureMode(TextureAttributes::DECAL);
|
---|
42 |
|
---|
43 | // Michi: avoid multiple loading of same texture!
|
---|
44 | if (stlm_textures.find(filename) != stlm_textures.end())
|
---|
45 | {
|
---|
46 | myTexture = stlm_textures[filename];
|
---|
47 | }
|
---|
48 | else
|
---|
49 | {
|
---|
50 | ImageComponent *image = new ImageComponent(filename);
|
---|
51 | //Texture *myTexture = new Texture(Texture::BASE_LEVEL, Texture::RGB, image->getWidth(), image->getHeight());
|
---|
52 | myTexture = new Texture(Texture::MULTI_LEVEL_MIPMAP, ImageComponent::FORMAT_RGBP, image->getWidth(), image->getHeight());
|
---|
53 | myTexture->setImage(0, image);
|
---|
54 |
|
---|
55 | myTexture->setMagFilter(Texture::BASE_LEVEL_LINEAR);
|
---|
56 | myTexture->setMinFilter(Texture::MULTI_LEVEL_LINEAR_POINT);
|
---|
57 |
|
---|
58 | myTexture->setBoundaryModeS(Texture::WRAP);
|
---|
59 | myTexture->setBoundaryModeT(Texture::WRAP);
|
---|
60 |
|
---|
61 | stlm_textures[filename] = myTexture;
|
---|
62 | /// HACK!!! (texture not deleted on reload!)
|
---|
63 | myTexture->ref();
|
---|
64 | }
|
---|
65 |
|
---|
66 | return myTexture;
|
---|
67 | }*/
|
---|
68 |
|
---|
69 |
|
---|
70 | //SceneEntity *BinaryLoader::LoadSceneEntity(igzstream &str)
|
---|
71 | SceneEntity *BinaryLoader::LoadSceneEntity(ifstream &str)
|
---|
72 | {
|
---|
73 | Geometry *geom = LoadGeometry(str);
|
---|
74 | Material *mat = LoadMaterial(str);
|
---|
75 | Matrix4x4 *trafo = NULL;//IdentityMatrix();
|
---|
76 |
|
---|
77 | SceneEntity *sceneGeom = new SceneEntity(geom, mat, trafo);
|
---|
78 |
|
---|
79 | return sceneGeom;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | void BinaryLoader::LoadTextures(ifstream &str)
|
---|
84 | {
|
---|
85 | int numTextures;
|
---|
86 | str.read(reinterpret_cast<char *>(&numTextures), sizeof(int));
|
---|
87 |
|
---|
88 | for (int i = 0; i < numTextures; ++ i)
|
---|
89 | {
|
---|
90 | // texture
|
---|
91 | int texnameSize;
|
---|
92 | int id;
|
---|
93 |
|
---|
94 | str.read(reinterpret_cast<char *>(&id), sizeof(int));
|
---|
95 | str.read(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
96 |
|
---|
97 | char *texname = new char[texnameSize];
|
---|
98 | str.read(texname, sizeof(char) * texnameSize);
|
---|
99 |
|
---|
100 | cout << "loading texture " << texname << " with len " << texnameSize << " id: " << id << endl;
|
---|
101 |
|
---|
102 | Texture *tex = new Texture(texname);
|
---|
103 |
|
---|
104 | mTextureTable[id] = tex;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | //Appearance *BinaryLoader::LoadMaterial(igzstream &str)
|
---|
110 | Material *BinaryLoader::LoadMaterial(ifstream &str)
|
---|
111 | {
|
---|
112 | // default material
|
---|
113 | Material *mat = new Material();
|
---|
114 |
|
---|
115 | // texture
|
---|
116 | int texId;
|
---|
117 |
|
---|
118 | str.read(reinterpret_cast<char *>(&texId), sizeof(int));
|
---|
119 | //cout << "texid: " << texId << endl;
|
---|
120 |
|
---|
121 | if (texId >= 0)
|
---|
122 | {
|
---|
123 | mat->SetTexture(mTextureTable[texId]);
|
---|
124 | }
|
---|
125 |
|
---|
126 | // material
|
---|
127 | /*char hasMaterial;
|
---|
128 | str.read(&hasMaterial, sizeof(char));
|
---|
129 |
|
---|
130 | if (hasMaterial)
|
---|
131 | {
|
---|
132 | Color3f amb(0.0f, 0.0f, 0.0f);
|
---|
133 | Color3f dif(0.5f, 0.5f, 0.5f);
|
---|
134 | Color3f spec(0.0f, 0.0f, 0.0f);
|
---|
135 | Color3f emi(0.0f, 0.0f, 0.0f);
|
---|
136 |
|
---|
137 | float shine = 0;
|
---|
138 |
|
---|
139 | //str.read(reinterpret_cast<char *>(&amb), sizeof(Color3f));
|
---|
140 | str.read(reinterpret_cast<char *>(&dif), sizeof(Color3f));
|
---|
141 | //str.read(reinterpret_cast<char *>(&emi), sizeof(Color3f));
|
---|
142 | //str.read(reinterpret_cast<char *>(&spec), sizeof(Color3f));
|
---|
143 | //str.read(reinterpret_cast<char *>(&shine), sizeof(float));
|
---|
144 |
|
---|
145 | mat->setAmbientColor(amb);
|
---|
146 | mat->setDiffuseColor(dif);
|
---|
147 | mat->setEmissiveColor(emi);
|
---|
148 | mat->setSpecularColor(spec);
|
---|
149 | mat->setShininess(shine);
|
---|
150 |
|
---|
151 | app->setMaterial(mat);
|
---|
152 | }
|
---|
153 |
|
---|
154 | return app;*/
|
---|
155 |
|
---|
156 | return mat;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | //Geometry *BinaryLoader::LoadGeometry(igzstream &str)
|
---|
161 | Geometry *BinaryLoader::LoadGeometry(ifstream &str)
|
---|
162 | {
|
---|
163 | Vector3 *vertices;
|
---|
164 | Vector3 *normals;
|
---|
165 | float *texcoords;
|
---|
166 |
|
---|
167 |
|
---|
168 | //////////////
|
---|
169 | //-- read in vertices
|
---|
170 |
|
---|
171 | int vertexCount;
|
---|
172 | str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
173 |
|
---|
174 | // end of file reached
|
---|
175 | if (str.eof())
|
---|
176 | return NULL;
|
---|
177 |
|
---|
178 | //cout << "vertexcount: " << vertexCount << endl;
|
---|
179 |
|
---|
180 | vertices = new Vector3[vertexCount];
|
---|
181 | str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
|
---|
182 |
|
---|
183 | normals = new Vector3[vertexCount];
|
---|
184 | str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
|
---|
185 |
|
---|
186 | int texCoordCount;
|
---|
187 | str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
188 |
|
---|
189 | if (texCoordCount)
|
---|
190 | {
|
---|
191 | //cout << "loading texcoords of size " << texCoordCount << endl;
|
---|
192 | texcoords = new float[texCoordCount * 2];
|
---|
193 | str.read(reinterpret_cast<char *>(texcoords), sizeof(float) * vertexCount * 2);
|
---|
194 | }
|
---|
195 | else
|
---|
196 | texcoords = NULL;
|
---|
197 |
|
---|
198 | for (int i = 0; i < vertexCount; i += 3)
|
---|
199 | {
|
---|
200 | Triangle3 tri(vertices[i], vertices[i + 1], vertices[i + 2]);
|
---|
201 | Vector3 n = tri.GetNormal();
|
---|
202 |
|
---|
203 | normals[i + 0] = n;
|
---|
204 | normals[i + 1] = n;
|
---|
205 | normals[i + 2] = n;
|
---|
206 | }
|
---|
207 |
|
---|
208 | return new Geometry(vertices, normals, texcoords, vertexCount);
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | bool BinaryLoader::Load(const std::string &filename, SceneEntityContainer &geometry)
|
---|
213 | {
|
---|
214 | //clear_textures(); // clear the texture table
|
---|
215 |
|
---|
216 | //igzstream str(filename.c_str());//, ios::binary);
|
---|
217 | ifstream istr(filename.c_str(), ios::binary);
|
---|
218 |
|
---|
219 | if (!istr.is_open())
|
---|
220 | return false;
|
---|
221 |
|
---|
222 | // load the texture table
|
---|
223 | LoadTextures(istr);
|
---|
224 |
|
---|
225 | // #shapes
|
---|
226 | int shapeCount;
|
---|
227 | istr.read(reinterpret_cast<char *>(&shapeCount), sizeof(int));
|
---|
228 |
|
---|
229 | int progress = 0;
|
---|
230 |
|
---|
231 | for (int i = 0; i < shapeCount; ++ i)
|
---|
232 | {
|
---|
233 | SceneEntity *ent = LoadSceneEntity(istr);
|
---|
234 | geometry.push_back(ent);
|
---|
235 |
|
---|
236 | int p = (i + 1) * 100 / shapeCount;
|
---|
237 |
|
---|
238 | if (p >= progress)
|
---|
239 | {
|
---|
240 | cout << "% loaded: " << p << endl;
|
---|
241 | progress += 10;
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | cout << "bin loading finished" << endl;
|
---|
246 |
|
---|
247 | return true;
|
---|
248 | }
|
---|
249 |
|
---|
250 | }
|
---|