1 | #include "BinaryLoader.h"
|
---|
2 | #include "yare.h"
|
---|
3 | #include "yamath.h"
|
---|
4 | #include "gzstream.h"
|
---|
5 | #include "ImageComponent.h"
|
---|
6 | #include "RenderState.h"
|
---|
7 | #include "GeometryProcessor.h"
|
---|
8 |
|
---|
9 |
|
---|
10 | using namespace std;
|
---|
11 |
|
---|
12 | static STL_MAP<String,Texture *> stlm_textures;
|
---|
13 |
|
---|
14 |
|
---|
15 | // clear the texture table (called on new file load)
|
---|
16 | static void clear_textures()
|
---|
17 | {
|
---|
18 | STL_MAP<String,Texture *>::iterator it, it_end = stlm_textures.end();
|
---|
19 |
|
---|
20 | for (it = stlm_textures.begin(); it != it_end; ++ it)
|
---|
21 | {
|
---|
22 | it->second->unref();
|
---|
23 | delete [] it->first;
|
---|
24 |
|
---|
25 | it ++;
|
---|
26 | }
|
---|
27 |
|
---|
28 | stlm_textures.clear();
|
---|
29 | }
|
---|
30 |
|
---|
31 |
|
---|
32 | static Texture *get_texture(const char *filename, TextureAttributes *&myTextureAttributes)
|
---|
33 | {
|
---|
34 | Texture * myTexture = NULL;
|
---|
35 |
|
---|
36 | myTextureAttributes = new TextureAttributes;
|
---|
37 | myTextureAttributes->setTextureMode(TextureAttributes::DECAL);
|
---|
38 |
|
---|
39 | // Michi: avoid multiple loading of same texture!
|
---|
40 | if (stlm_textures.find(filename) != stlm_textures.end())
|
---|
41 | {
|
---|
42 | myTexture = stlm_textures[filename];
|
---|
43 | }
|
---|
44 | else
|
---|
45 | {
|
---|
46 | ImageComponent *image = new ImageComponent(filename);
|
---|
47 | //Texture *myTexture = new Texture(Texture::BASE_LEVEL, Texture::RGB, image->getWidth(), image->getHeight());
|
---|
48 | myTexture = new Texture(Texture::MULTI_LEVEL_MIPMAP, ImageComponent::FORMAT_RGBP, image->getWidth(), image->getHeight());
|
---|
49 | myTexture->setImage(0, image);
|
---|
50 |
|
---|
51 | myTexture->setMagFilter(Texture::BASE_LEVEL_LINEAR);
|
---|
52 | myTexture->setMinFilter(Texture::MULTI_LEVEL_LINEAR_POINT);
|
---|
53 |
|
---|
54 | myTexture->setBoundaryModeS(Texture::WRAP);
|
---|
55 | myTexture->setBoundaryModeT(Texture::WRAP);
|
---|
56 |
|
---|
57 | stlm_textures[filename] = myTexture;
|
---|
58 | /// HACK!!! (texture not deleted on reload!)
|
---|
59 | myTexture->ref();
|
---|
60 | }
|
---|
61 |
|
---|
62 | return myTexture;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | Shape3D *BinaryLoader::LoadShape(igzstream &str)
|
---|
67 | {
|
---|
68 | Geometry *geom = LoadGeometry(str);
|
---|
69 | Appearance *app = LoadAppearance(str);
|
---|
70 |
|
---|
71 | Shape3D *shape = new Shape3D(geom, app);
|
---|
72 |
|
---|
73 | return shape;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | Appearance *BinaryLoader::LoadAppearance(igzstream &str)
|
---|
78 | {
|
---|
79 | Appearance *app = new Appearance();
|
---|
80 |
|
---|
81 | // texture
|
---|
82 | int texnameSize;
|
---|
83 |
|
---|
84 | str.read(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
85 |
|
---|
86 | if (texnameSize)
|
---|
87 | {
|
---|
88 | TextureAttributes *texAttr;
|
---|
89 | char *texname = new char[texnameSize];
|
---|
90 | str.read(texname, sizeof(char) * texnameSize);
|
---|
91 |
|
---|
92 | Texture *tex = get_texture(texname, texAttr);
|
---|
93 | app->setTexture(tex);
|
---|
94 | app->setTextureAttributes(texAttr);
|
---|
95 | }
|
---|
96 |
|
---|
97 | // material
|
---|
98 | char hasMaterial;
|
---|
99 | str.read(&hasMaterial, sizeof(char));
|
---|
100 |
|
---|
101 | if (hasMaterial)
|
---|
102 | {
|
---|
103 | Color3f amb(0.0f, 0.0f, 0.0f);
|
---|
104 | Color3f dif(0.5f, 0.5f, 0.5f);
|
---|
105 | Color3f spec(0.0f, 0.0f, 0.0f);
|
---|
106 | Color3f emi(0.0f, 0.0f, 0.0f);
|
---|
107 |
|
---|
108 | float shine = 0;
|
---|
109 |
|
---|
110 | //str.read(reinterpret_cast<char *>(&amb), sizeof(Color3f));
|
---|
111 | str.read(reinterpret_cast<char *>(&dif), sizeof(Color3f));
|
---|
112 | //str.read(reinterpret_cast<char *>(&emi), sizeof(Color3f));
|
---|
113 | //str.read(reinterpret_cast<char *>(&spec), sizeof(Color3f));
|
---|
114 | //str.read(reinterpret_cast<char *>(&shine), sizeof(float));
|
---|
115 | #if 1
|
---|
116 | Material *mat = new Material();
|
---|
117 |
|
---|
118 | mat->setAmbientColor(amb);
|
---|
119 | mat->setDiffuseColor(dif);
|
---|
120 | mat->setEmissiveColor(emi);
|
---|
121 | mat->setSpecularColor(spec);
|
---|
122 | mat->setShininess(shine);
|
---|
123 |
|
---|
124 | app->setMaterial(mat);
|
---|
125 | #endif
|
---|
126 | }
|
---|
127 |
|
---|
128 | return app;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | Geometry *BinaryLoader::LoadGeometry(igzstream &str)
|
---|
133 | {
|
---|
134 | int vertexCount;
|
---|
135 | int stripCount;
|
---|
136 |
|
---|
137 | Point3f *vertices;
|
---|
138 | Vector3f *normals = NULL;
|
---|
139 | Point2f *texCoords = NULL;
|
---|
140 |
|
---|
141 | int *stripIndexCounts;
|
---|
142 | int *indices;
|
---|
143 | int *normalIndices;
|
---|
144 |
|
---|
145 | // todo
|
---|
146 | int colorCount = 0;
|
---|
147 | int normalCount = 0;
|
---|
148 | int texCount = 0;
|
---|
149 |
|
---|
150 | int format;
|
---|
151 |
|
---|
152 | // read in format
|
---|
153 | str.read(reinterpret_cast<char *>(&format), sizeof(int));
|
---|
154 |
|
---|
155 | format = IndexedTriangleStripArray::COORDINATES
|
---|
156 | | IndexedTriangleStripArray::NORMALS
|
---|
157 | ;
|
---|
158 |
|
---|
159 | //OUT1("new format: " << format);
|
---|
160 |
|
---|
161 | // end of file reached
|
---|
162 | if (str.eof())
|
---|
163 | return NULL;
|
---|
164 |
|
---|
165 |
|
---|
166 | //////////////
|
---|
167 | // read in vertices
|
---|
168 |
|
---|
169 | str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
170 |
|
---|
171 | vertices = new Point3f[vertexCount];
|
---|
172 | str.read(reinterpret_cast<char *>(vertices), sizeof(Point3f) * vertexCount);
|
---|
173 |
|
---|
174 |
|
---|
175 | /////////////////
|
---|
176 | // read in strip counters
|
---|
177 |
|
---|
178 | str.read(reinterpret_cast<char *>(&stripCount), sizeof(int));
|
---|
179 |
|
---|
180 | stripIndexCounts = new int[stripCount];
|
---|
181 | str.read(reinterpret_cast<char *>(stripIndexCounts), sizeof(int) * stripCount);
|
---|
182 |
|
---|
183 |
|
---|
184 | //////////
|
---|
185 | // read in indices
|
---|
186 |
|
---|
187 | int indexCount;
|
---|
188 | str.read(reinterpret_cast<char *>(&indexCount), sizeof(int));
|
---|
189 | indices = new int[indexCount];
|
---|
190 | str.read(reinterpret_cast<char *>(indices), sizeof(int) * indexCount);
|
---|
191 |
|
---|
192 |
|
---|
193 | ///////////////
|
---|
194 | // read in normals
|
---|
195 |
|
---|
196 | str.read(reinterpret_cast<char *>(&normalCount), sizeof(int));
|
---|
197 |
|
---|
198 | if (normalCount)
|
---|
199 | {
|
---|
200 | normals = new Vector3f[normalCount];
|
---|
201 | str.read(reinterpret_cast<char *>(normals), sizeof(Vector3f) * normalCount);
|
---|
202 | normalIndices = new int[indexCount];
|
---|
203 | str.read(reinterpret_cast<char *>(normalIndices), sizeof(int) * indexCount);
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | //////////////
|
---|
208 | // read in texCoords
|
---|
209 |
|
---|
210 | str.read(reinterpret_cast<char *>(&texCount), sizeof(int));
|
---|
211 | if (texCount)
|
---|
212 | {
|
---|
213 | texCoords = new Point2f[texCount];
|
---|
214 | str.read(reinterpret_cast<char *>(texCoords), sizeof(Point2f) * texCount);
|
---|
215 | }
|
---|
216 |
|
---|
217 | IndexedTriangleStripArray *geom =
|
---|
218 | new IndexedTriangleStripArray(vertexCount,
|
---|
219 | normalCount,
|
---|
220 | texCount,
|
---|
221 | colorCount,
|
---|
222 | format,
|
---|
223 | indexCount,
|
---|
224 | stripIndexCounts,
|
---|
225 | stripCount);
|
---|
226 |
|
---|
227 | geom->setCoordinates(0, vertices, vertexCount);
|
---|
228 | geom->setCoordinateIndices(0, indices, indexCount);
|
---|
229 |
|
---|
230 |
|
---|
231 | if (texCount)
|
---|
232 | {
|
---|
233 | geom->setTextureCoordinateIndices(0, indices, indexCount);
|
---|
234 | geom->setTextureCoordinates(0, texCoords, texCount);
|
---|
235 | }
|
---|
236 |
|
---|
237 | if (normalCount)
|
---|
238 | {
|
---|
239 | geom->setNormals(0, normals, normalCount);
|
---|
240 | geom->setNormalIndices(0, normalIndices, indexCount);
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | ///////
|
---|
245 | //-- cleanup
|
---|
246 |
|
---|
247 | DELAPTR(stripIndexCounts);
|
---|
248 | DELAPTR(vertices);
|
---|
249 | DELAPTR(normals);
|
---|
250 | DELAPTR(texCoords);
|
---|
251 | DELAPTR(indices);
|
---|
252 | DELAPTR(normalIndices);
|
---|
253 |
|
---|
254 |
|
---|
255 | return geom;
|
---|
256 | }
|
---|
257 |
|
---|
258 |
|
---|
259 | BranchGroup *BinaryLoader::Load(const string &filename)
|
---|
260 | {
|
---|
261 | clear_textures(); // clear the texture table
|
---|
262 |
|
---|
263 | BranchGroup *root = new BranchGroup();
|
---|
264 | //root->setName("start");
|
---|
265 | igzstream str(filename.c_str());//, ios::binary);
|
---|
266 |
|
---|
267 | if (!str.is_open())
|
---|
268 | return false;
|
---|
269 |
|
---|
270 | // #shapes
|
---|
271 | int shapeCount;
|
---|
272 | str.read(reinterpret_cast<char *>(&shapeCount), sizeof(int));
|
---|
273 |
|
---|
274 | int progress = 0;
|
---|
275 |
|
---|
276 | GeometryProcessor geomConv(500000, NULL);
|
---|
277 |
|
---|
278 | for (int i = 0; i < shapeCount; ++ i)
|
---|
279 | {
|
---|
280 | Shape3D *shape = LoadShape(str);
|
---|
281 |
|
---|
282 | vector<Shape3D *> newShapes;
|
---|
283 | ShapePair shapePair = ShapePair(shape, -1);
|
---|
284 |
|
---|
285 | // check, if this node actually contains any geometry
|
---|
286 | CalcNumTrianglesVisitor triangleVisitor;
|
---|
287 | triangleVisitor.Apply(shape);
|
---|
288 | const int numTriangles = triangleVisitor.GetNumTriangles();
|
---|
289 |
|
---|
290 | // the shape should be subdivided => replace old shape
|
---|
291 | if (geomConv.SubdivideShape(newShapes, shapePair, numTriangles))
|
---|
292 | {
|
---|
293 | OUT1("triangleCount: " << numTriangles << " => subdividing to " << newShapes.size() << " shapes");
|
---|
294 | delete shape;//shape->unref();
|
---|
295 | }
|
---|
296 | else
|
---|
297 | {
|
---|
298 | newShapes.push_back(shape);
|
---|
299 | }
|
---|
300 |
|
---|
301 | VisibilityGroup * visGrp = new VisibilityGroup();
|
---|
302 | root->addChild(visGrp);
|
---|
303 |
|
---|
304 | for (size_t j = 0; j < newShapes.size(); ++ j)
|
---|
305 | {
|
---|
306 | Shape3D *sh = newShapes[j];
|
---|
307 | visGrp->addChild(sh);
|
---|
308 | }
|
---|
309 |
|
---|
310 | int p = i * 100 / shapeCount;
|
---|
311 |
|
---|
312 | if (p >= progress)
|
---|
313 | {
|
---|
314 | OUT1("% loaded: " << p);
|
---|
315 | progress += 10;
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | OUT1("bin loading finished");
|
---|
320 |
|
---|
321 | return root;
|
---|
322 | }
|
---|
323 |
|
---|
324 |
|
---|
325 | #if 0
|
---|
326 |
|
---|
327 | collectvertices
|
---|
328 | {
|
---|
329 | int newIndex = 0;
|
---|
330 |
|
---|
331 | for (int i = 0; i < numTriangles; ++ i)
|
---|
332 | {
|
---|
333 | Triangle tri = mTriangles[i];
|
---|
334 |
|
---|
335 | for (int j = 0; j < 3; ++ j)
|
---|
336 | {
|
---|
337 | const int index = tri.mVertexIndices[j];
|
---|
338 | if (!mHashtable[index])
|
---|
339 | {
|
---|
340 | mHashTable[index] = newIndex;
|
---|
341 | mNewVertices.push_back(mVertices[index];
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | // now loop again, exchange indices;
|
---|
346 | for (int i = 0; i < numTriangles; ++ i
|
---|
347 | {
|
---|
348 | Triangle tri = mTriangles[i];
|
---|
349 |
|
---|
350 | for (int j = 0; j < 3; ++ j)
|
---|
351 | {
|
---|
352 | const int index = tri.mVertexIndices[j];
|
---|
353 | tri.mVertexIndices[j] = mHashTable[index];
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | #endif |
---|