1 | #include "VisibilitySolutionConverter.h"
|
---|
2 | #include "SimpleTri.h"
|
---|
3 | #include "SimpleVec.h"
|
---|
4 | #include "gzstream.h"
|
---|
5 | #include <iostream>
|
---|
6 |
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | // MAGIC of all bin exports
|
---|
11 | #ifndef MAGIC
|
---|
12 | #define MAGIC 0x827923
|
---|
13 | #endif
|
---|
14 |
|
---|
15 | #define BVH_VERSION 2.1
|
---|
16 |
|
---|
17 |
|
---|
18 | static void LoadIndices(char *str,
|
---|
19 | const VertexArray &vertices,
|
---|
20 | const VertexArray &normals,
|
---|
21 | const vector<pair<float, float> > &texCoords,
|
---|
22 | VertexArray &faceVertices,
|
---|
23 | VertexArray &faceNormals,
|
---|
24 | vector<TexCoord> &faceTexCoords
|
---|
25 | )
|
---|
26 | {
|
---|
27 | vector<string> triples;
|
---|
28 |
|
---|
29 | char *next_token;
|
---|
30 |
|
---|
31 | // extract the triples of the form v/t/n v/t/n ...
|
---|
32 | char *pch = strtok_s(str + 1, " ", &next_token);
|
---|
33 |
|
---|
34 | while (pch)
|
---|
35 | {
|
---|
36 | string s(pch);
|
---|
37 | //s += "\n",
|
---|
38 | triples.push_back(s);
|
---|
39 |
|
---|
40 | pch = strtok_s(NULL, " ", &next_token);
|
---|
41 | }
|
---|
42 |
|
---|
43 | vector<int> indices;
|
---|
44 | vector<int> nIndices;
|
---|
45 | vector<int> tIndices;
|
---|
46 |
|
---|
47 | char seps[] = " /\t\n";
|
---|
48 |
|
---|
49 | for (size_t i = 0; i < triples.size(); ++ i)
|
---|
50 | {
|
---|
51 | static int dummy = 0;
|
---|
52 |
|
---|
53 | size_t found;
|
---|
54 | found = triples[i].find_first_of(seps);
|
---|
55 | size_t prevfound = 0;
|
---|
56 | // vertex, normal, texture indices
|
---|
57 | string str = triples[i].substr(prevfound, found);
|
---|
58 |
|
---|
59 | int index = (int)strtol(str.c_str(), NULL, 10) - 1;
|
---|
60 |
|
---|
61 | int tIndex = index;
|
---|
62 | int nIndex = index;
|
---|
63 |
|
---|
64 | prevfound = found;
|
---|
65 | found = triples[i].find_first_of(seps, found + 1);
|
---|
66 |
|
---|
67 | if (found != string::npos)
|
---|
68 | {
|
---|
69 | str = triples[i].substr(prevfound, found);
|
---|
70 |
|
---|
71 | int idx = (int)strtol(str.c_str(), NULL, 10) - 1;
|
---|
72 | if (idx > 0) tIndex = idx;
|
---|
73 | }
|
---|
74 |
|
---|
75 | if ((found + 1) < triples[i].size())
|
---|
76 | {
|
---|
77 | str = triples[i].substr(found + 1);
|
---|
78 |
|
---|
79 | int idx = (int)strtol(str.c_str(), NULL, 10) - 1;
|
---|
80 | if (idx > 0) nIndex = idx;
|
---|
81 | }
|
---|
82 |
|
---|
83 | // store indices
|
---|
84 | if (index >= 0)
|
---|
85 | {
|
---|
86 | indices.push_back(index);
|
---|
87 | nIndices.push_back(nIndex);
|
---|
88 | tIndices.push_back(tIndex);
|
---|
89 | }
|
---|
90 |
|
---|
91 | // new triangle found
|
---|
92 | if (indices.size() > 2)
|
---|
93 | {
|
---|
94 | // change orientation of faces?
|
---|
95 | #if 1
|
---|
96 | int idx1 = 0;
|
---|
97 | int idx2 = (int)indices.size() - 2;
|
---|
98 | int idx3 = (int)indices.size() - 1;
|
---|
99 | #else
|
---|
100 | int idx3 = 0;
|
---|
101 | int idx2 = (int)indices.size() - 2;
|
---|
102 | int idx1 = (int)indices.size() - 1;
|
---|
103 | #endif
|
---|
104 | faceVertices.push_back(vertices[indices[idx1]]);
|
---|
105 | faceVertices.push_back(vertices[indices[idx2]]);
|
---|
106 | faceVertices.push_back(vertices[indices[idx3]]);
|
---|
107 |
|
---|
108 |
|
---|
109 | if (!normals.empty())
|
---|
110 | {
|
---|
111 | faceNormals.push_back(normals[nIndices[idx1]]);
|
---|
112 | faceNormals.push_back(normals[nIndices[idx2]]);
|
---|
113 | faceNormals.push_back(normals[nIndices[idx3]]);
|
---|
114 | }
|
---|
115 | else
|
---|
116 | {
|
---|
117 | // no face normals? => create normals
|
---|
118 | const SimpleTri tri(vertices[indices[idx1]],
|
---|
119 | vertices[indices[idx2]],
|
---|
120 | vertices[indices[idx3]]);
|
---|
121 |
|
---|
122 | const SimpleVec n = tri.GetNormal();
|
---|
123 |
|
---|
124 | faceNormals.push_back(n);
|
---|
125 | faceNormals.push_back(n);
|
---|
126 | faceNormals.push_back(n);
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (!texCoords.empty())
|
---|
130 | {
|
---|
131 | faceTexCoords.push_back(texCoords[tIndices[idx1]]);
|
---|
132 | faceTexCoords.push_back(texCoords[tIndices[idx2]]);
|
---|
133 | faceTexCoords.push_back(texCoords[tIndices[idx3]]);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | VisibilitySolutionConverter::VisibilitySolutionConverter()
|
---|
141 | {}
|
---|
142 |
|
---|
143 |
|
---|
144 | VisibilitySolutionConverter::~VisibilitySolutionConverter()
|
---|
145 | {
|
---|
146 | for (size_t i = 0; i < mGeometry.size(); ++ i)
|
---|
147 | {
|
---|
148 | delete [] mGeometry[i]->mVertices;
|
---|
149 | delete [] mGeometry[i]->mNormals;
|
---|
150 | delete [] mGeometry[i]->mTexCoords;
|
---|
151 |
|
---|
152 | delete mGeometry[i];
|
---|
153 | }
|
---|
154 |
|
---|
155 | mGeometry.clear();
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | void VisibilitySolutionConverter::LoadShape(const VertexArray &vertices,
|
---|
160 | const VertexArray &normals,
|
---|
161 | const vector<TexCoord> &texCoords)
|
---|
162 | {
|
---|
163 | int numElements = (int)vertices.size();
|
---|
164 | Geometry *geom = new Geometry();
|
---|
165 |
|
---|
166 | // convert the triangles to geometry
|
---|
167 | geom->mVertices = new SimpleVec[numElements];
|
---|
168 | geom->mNormals = new SimpleVec[numElements];
|
---|
169 | geom->mTexCoords = new TexCoord[numElements];
|
---|
170 |
|
---|
171 | geom->mVertexCount = numElements;
|
---|
172 | geom->mTexcoordCount = (int)texCoords.size();
|
---|
173 |
|
---|
174 | cout << "number of vertices=" << numElements << endl;
|
---|
175 |
|
---|
176 | for (int i = 0; i < numElements; ++ i)
|
---|
177 | {
|
---|
178 | #if 1
|
---|
179 | // convert to our camera system: change y and z
|
---|
180 | geom->mVertices[i].x = vertices[i].x;
|
---|
181 | geom->mVertices[i].y = -vertices[i].z;
|
---|
182 | geom->mVertices[i].z = vertices[i].y;
|
---|
183 |
|
---|
184 | geom->mNormals[i].x = normals[i].x;
|
---|
185 | geom->mNormals[i].y = -normals[i].z;
|
---|
186 | geom->mNormals[i].z = normals[i].y;
|
---|
187 |
|
---|
188 | #else
|
---|
189 | geom->mVertices[i].x = vertices[i].x;
|
---|
190 | geom->mVertices[i].y = vertices[i].y;
|
---|
191 | geom->mVertices[i].z = vertices[i].z;
|
---|
192 |
|
---|
193 | geom->mNormals[i].x = normals[i].x;
|
---|
194 | geom->mNormals[i].y = normals[i].y;
|
---|
195 | geom->mNormals[i].z = normals[i].z;
|
---|
196 |
|
---|
197 | #endif
|
---|
198 |
|
---|
199 | if (i < geom->mTexcoordCount)
|
---|
200 | {
|
---|
201 | geom->mTexCoords[i].first = texCoords[i].first;
|
---|
202 | geom->mTexCoords[i].second = texCoords[i].second;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | mGeometry.push_back(geom);
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | bool VisibilitySolutionConverter::Convert(const std::string &sceneInputFilename,
|
---|
211 | const std::string &sceneOutputFilename,
|
---|
212 | const std::string &bvhInputFilename,
|
---|
213 | const std::string &bvhOutputFilename)
|
---|
214 | {
|
---|
215 | mNumShapes = 0;
|
---|
216 |
|
---|
217 | // delete previous geometry
|
---|
218 | for (size_t i = 0; i < mGeometry.size(); ++ i)
|
---|
219 | {
|
---|
220 | delete [] mGeometry[i]->mVertices;
|
---|
221 | delete [] mGeometry[i]->mNormals;
|
---|
222 | delete [] mGeometry[i]->mTexCoords;
|
---|
223 |
|
---|
224 | delete mGeometry[i];
|
---|
225 | }
|
---|
226 |
|
---|
227 | mGeometry.clear();
|
---|
228 |
|
---|
229 | if (!LoadSolution(bvhInputFilename))
|
---|
230 | {
|
---|
231 | cerr << "could not read file" << endl;
|
---|
232 | return false;
|
---|
233 | }
|
---|
234 |
|
---|
235 | if (!ReadScene(sceneInputFilename))
|
---|
236 | {
|
---|
237 | cerr << "could not read file" << endl;
|
---|
238 | return false;
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (!WriteScene(sceneOutputFilename))
|
---|
242 | {
|
---|
243 | cerr << "could not write file" << endl;
|
---|
244 | return false;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /*if (!WriteBvh(bvhOutputFilename))
|
---|
248 | {
|
---|
249 | cerr << "could not write file" << endl;
|
---|
250 | return false;
|
---|
251 | }*/
|
---|
252 |
|
---|
253 | return true;
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | bool VisibilitySolutionConverter::ReadScene(const string &filename)
|
---|
258 | {
|
---|
259 | FILE *file;
|
---|
260 |
|
---|
261 | if ((file = fopen(filename.c_str(), "r")) == NULL)
|
---|
262 | {
|
---|
263 | return false;
|
---|
264 | }
|
---|
265 |
|
---|
266 | VertexArray faceVertices;
|
---|
267 | VertexArray faceNormals;
|
---|
268 | vector<TexCoord> faceTexcoords;
|
---|
269 |
|
---|
270 | VertexArray vertices;
|
---|
271 | VertexArray normals;
|
---|
272 | vector<TexCoord> texcoords;
|
---|
273 |
|
---|
274 | vector<int> indices;
|
---|
275 |
|
---|
276 | int line = 0;
|
---|
277 | const int len = 10000;
|
---|
278 | char str[len];
|
---|
279 |
|
---|
280 | while (fgets(str, len, file) != NULL)
|
---|
281 | {
|
---|
282 | if (line % 1000000 == 0)
|
---|
283 | cout << line << " " << str << endl;
|
---|
284 |
|
---|
285 | ++ line;
|
---|
286 |
|
---|
287 | switch (str[0])
|
---|
288 | {
|
---|
289 | case 'v': // vertex or normal
|
---|
290 | {
|
---|
291 | float x, y, z;
|
---|
292 |
|
---|
293 | if (vertices.size() >= 1000000 * 3) continue;
|
---|
294 |
|
---|
295 | switch (str[1])
|
---|
296 | {
|
---|
297 | case 'n' :
|
---|
298 | sscanf(str + 2, "%f %f %f", &x, &y, &z);
|
---|
299 | normals.push_back(SimpleVec(x, y, z));
|
---|
300 | break;
|
---|
301 | case 't':
|
---|
302 | sscanf(str + 2, "%f %f", &x, &y);
|
---|
303 | texcoords.push_back(pair<float, float>(x, y));
|
---|
304 | break;
|
---|
305 | default:
|
---|
306 | sscanf(str + 1, "%f %f %f", &x, &y, &z);
|
---|
307 | //const float scale = 5e-3f;
|
---|
308 | const float scale = 0.1f;
|
---|
309 | vertices.push_back(SimpleVec(x * scale, y * scale, z * scale));
|
---|
310 | //cout <<"v " << x << " " << y << " "<< z << " ";
|
---|
311 | }
|
---|
312 | break;
|
---|
313 | }
|
---|
314 | case 'f':
|
---|
315 | {
|
---|
316 | //////////
|
---|
317 | //-- indices in the current line
|
---|
318 |
|
---|
319 | if (faceVertices.size() >= 1000000 * 3) continue;
|
---|
320 |
|
---|
321 | LoadIndices(str,
|
---|
322 | vertices, normals, texcoords,
|
---|
323 | faceVertices, faceNormals, faceTexcoords);
|
---|
324 |
|
---|
325 | break;
|
---|
326 | }
|
---|
327 | break;
|
---|
328 | default:
|
---|
329 | // throw away line
|
---|
330 | break;
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | if (!faceVertices.empty())
|
---|
335 | {
|
---|
336 | ++ mNumShapes;
|
---|
337 |
|
---|
338 | ConstructBvhObjects(faceVertices, faceNormals, faceTexcoords);
|
---|
339 |
|
---|
340 | faceVertices.clear();
|
---|
341 | faceNormals.clear();
|
---|
342 | faceTexcoords.clear();
|
---|
343 | }
|
---|
344 |
|
---|
345 | fclose(file);
|
---|
346 |
|
---|
347 | return true;
|
---|
348 | }
|
---|
349 |
|
---|
350 |
|
---|
351 | static inline float RandomColor(float x)
|
---|
352 | {
|
---|
353 | return x + (1.0f - x) * (float)rand() / RAND_MAX;
|
---|
354 | }
|
---|
355 |
|
---|
356 |
|
---|
357 | void VisibilitySolutionConverter::WriteGeometry(ogzstream &str, Geometry *geom)
|
---|
358 | {
|
---|
359 | int vertexCount = geom->mVertexCount;
|
---|
360 | str.write(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
361 |
|
---|
362 | str.write(reinterpret_cast<char *>(geom->mVertices), sizeof(SimpleVec) * vertexCount);
|
---|
363 | str.write(reinterpret_cast<char *>(geom->mNormals), sizeof(SimpleVec) * vertexCount);
|
---|
364 |
|
---|
365 | int texCoordCount = geom->mTexcoordCount;
|
---|
366 | str.write(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
367 |
|
---|
368 | if (texCoordCount)
|
---|
369 | {
|
---|
370 | str.write(reinterpret_cast<char *>(geom->mTexCoords), sizeof(float) * texCoordCount * 2);
|
---|
371 | }
|
---|
372 |
|
---|
373 | ///////
|
---|
374 | //-- texture
|
---|
375 |
|
---|
376 | #ifdef USE_TEXTURE
|
---|
377 | int texId = 0;
|
---|
378 | #else
|
---|
379 | int texId = -1;
|
---|
380 | #endif
|
---|
381 |
|
---|
382 | str.write(reinterpret_cast<char *>(&texId), sizeof(int));
|
---|
383 |
|
---|
384 | bool alphaTestEnabled = false;
|
---|
385 | //bool cullFaceEnabled = false;
|
---|
386 | bool cullFaceEnabled = true;
|
---|
387 |
|
---|
388 | str.write(reinterpret_cast<char *>(&alphaTestEnabled), sizeof(bool));
|
---|
389 | str.write(reinterpret_cast<char *>(&cullFaceEnabled), sizeof(bool));
|
---|
390 |
|
---|
391 | // material
|
---|
392 | bool hasMaterial = true;
|
---|
393 | //bool hasMaterial = false;
|
---|
394 | str.write(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
|
---|
395 |
|
---|
396 | if (hasMaterial)
|
---|
397 | {
|
---|
398 | SimpleVec ambient, diffuse, spec, emm;
|
---|
399 |
|
---|
400 | ambient.x = ambient.y = ambient.z = 0.2f;
|
---|
401 | //diffuse.x = diffuse.y = diffuse.z = 1.0f;
|
---|
402 |
|
---|
403 | diffuse.x = RandomColor(0.5f);
|
---|
404 | diffuse.y = RandomColor(0.5f);
|
---|
405 | diffuse.z = RandomColor(0.5f);
|
---|
406 |
|
---|
407 | spec.x = spec.y = spec.z = .0f;
|
---|
408 | emm = spec;
|
---|
409 |
|
---|
410 | // only write rgb part of the material
|
---|
411 | str.write(reinterpret_cast<char *>(&ambient), sizeof(SimpleVec));
|
---|
412 | str.write(reinterpret_cast<char *>(&diffuse), sizeof(SimpleVec));
|
---|
413 | str.write(reinterpret_cast<char *>(&spec), sizeof(SimpleVec));
|
---|
414 | str.write(reinterpret_cast<char *>(&emm), sizeof(SimpleVec));
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 |
|
---|
419 | bool VisibilitySolutionConverter::WriteScene(const string &filename)
|
---|
420 | {
|
---|
421 | ogzstream ofile(filename.c_str());
|
---|
422 |
|
---|
423 | if (!ofile.is_open())
|
---|
424 | return false;
|
---|
425 |
|
---|
426 |
|
---|
427 | /////////
|
---|
428 | //-- write textures
|
---|
429 |
|
---|
430 | #ifdef USE_TEXTURE
|
---|
431 | int textureCount = 1;
|
---|
432 | #else
|
---|
433 | int textureCount = 0;
|
---|
434 | #endif
|
---|
435 | ofile.write(reinterpret_cast<char *>(&textureCount), sizeof(int));
|
---|
436 |
|
---|
437 | if (textureCount > 0)
|
---|
438 | {
|
---|
439 | // hack
|
---|
440 | const string texName("wood.jpg");
|
---|
441 |
|
---|
442 | int texnameSize = (int)texName.length() + 1;
|
---|
443 | ofile.write(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
444 |
|
---|
445 | ofile.write(texName.c_str(), sizeof(char) * texnameSize);
|
---|
446 |
|
---|
447 | int boundS = 1, boundT = 1;
|
---|
448 |
|
---|
449 | ofile.write(reinterpret_cast<char *>(&boundS), sizeof(int));
|
---|
450 | ofile.write(reinterpret_cast<char *>(&boundT), sizeof(int));
|
---|
451 | }
|
---|
452 |
|
---|
453 |
|
---|
454 | ///////////
|
---|
455 | //-- write shapes
|
---|
456 |
|
---|
457 | ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
458 |
|
---|
459 | vector<Geometry *>::const_iterator it, it_end = mGeometry.end();
|
---|
460 |
|
---|
461 | for (it = mGeometry.begin(); it != it_end; ++ it)
|
---|
462 | {
|
---|
463 | WriteGeometry(ofile, *it);
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 | int entityCount = 1;
|
---|
468 | ofile.write(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
469 |
|
---|
470 |
|
---|
471 | //////////
|
---|
472 | //-- write single scene entity
|
---|
473 |
|
---|
474 | // no transformation
|
---|
475 | bool hasTrafo = false;
|
---|
476 | ofile.write(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
|
---|
477 |
|
---|
478 | // a dummy lod
|
---|
479 | int numLODs = 1;
|
---|
480 | ofile.write(reinterpret_cast<char *>(&numLODs), sizeof(int));
|
---|
481 |
|
---|
482 | float dist = 0;
|
---|
483 | ofile.write(reinterpret_cast<char *>(&dist), sizeof(float));
|
---|
484 |
|
---|
485 | ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
486 |
|
---|
487 | // all shapes belong to this scene entity
|
---|
488 | for (int i = 0; i < mNumShapes; ++ i)
|
---|
489 | {
|
---|
490 | int shapeId = i;
|
---|
491 | ofile.write(reinterpret_cast<char *>(&shapeId), sizeof(int));
|
---|
492 | }
|
---|
493 |
|
---|
494 | return true;
|
---|
495 | }
|
---|
496 |
|
---|
497 |
|
---|
498 | void VisibilitySolutionConverter::ConstructBvhObjects(const VertexArray &vertices,
|
---|
499 | const VertexArray &normals,
|
---|
500 | const vector<TexCoord> &texCoords)
|
---|
501 | {
|
---|
502 | cout << "vtx: " << vertices.size() << endl;
|
---|
503 | /* BvhNode n;
|
---|
504 |
|
---|
505 | mGlobalTriangleIds.clear();
|
---|
506 | for (size_t i = 0; i < vertices.size() / 3; ++ i)
|
---|
507 | {
|
---|
508 | mGlobalTriangleIds.push_back((int)i);
|
---|
509 | }
|
---|
510 |
|
---|
511 | for (size_t i = 0; i < vertices.size() / 3; ++ i)
|
---|
512 | n.mTriangleIds.push_back((int)i);
|
---|
513 |
|
---|
514 | mBvhNodes.push_back(n);
|
---|
515 | */
|
---|
516 | for (size_t i = 0; i < mBvhNodes.size(); ++ i)
|
---|
517 | {
|
---|
518 | VertexArray _vertices;
|
---|
519 | VertexArray _normals;
|
---|
520 | vector<TexCoord> _texCoords;
|
---|
521 |
|
---|
522 | BvhNode *node = mBvhNodes[i];
|
---|
523 |
|
---|
524 | //for (size_t j = 0; j < node->mTriangleIds.size(); ++ j)
|
---|
525 | for (int j = node->first; j < node->last; ++ j)
|
---|
526 | {
|
---|
527 | const int idx = 3 * mGlobalTriangleIds[j];
|
---|
528 |
|
---|
529 | //cout << "idx: " << idx << " ";
|
---|
530 |
|
---|
531 | if (idx < 1000000 * 3)
|
---|
532 | {
|
---|
533 | for (int k = 0; k < 3; ++ k)
|
---|
534 | {
|
---|
535 | _vertices.push_back(vertices[idx + k]);
|
---|
536 | _normals.push_back(normals[idx + k]);
|
---|
537 | //_texCoords.push_back(texCoords[idx + k]);
|
---|
538 | }
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | LoadShape(_vertices, _normals, _texCoords);
|
---|
543 | }
|
---|
544 | }
|
---|
545 |
|
---|
546 |
|
---|
547 | bool VisibilitySolutionConverter::WriteBvh(const string &filename)
|
---|
548 | {
|
---|
549 | ogzstream ofile(filename.c_str());
|
---|
550 |
|
---|
551 | if (!ofile.is_open())
|
---|
552 | return false;
|
---|
553 |
|
---|
554 |
|
---|
555 | /////////
|
---|
556 | //-- write textures
|
---|
557 |
|
---|
558 | #ifdef USE_TEXTURE
|
---|
559 | int textureCount = 1;
|
---|
560 | #else
|
---|
561 | int textureCount = 0;
|
---|
562 | #endif
|
---|
563 | ofile.write(reinterpret_cast<char *>(&textureCount), sizeof(int));
|
---|
564 |
|
---|
565 | if (textureCount > 0)
|
---|
566 | {
|
---|
567 | // hack
|
---|
568 | const string texName("wood.jpg");
|
---|
569 |
|
---|
570 | int texnameSize = (int)texName.length() + 1;
|
---|
571 | ofile.write(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
572 |
|
---|
573 | ofile.write(texName.c_str(), sizeof(char) * texnameSize);
|
---|
574 |
|
---|
575 | int boundS = 1, boundT = 1;
|
---|
576 |
|
---|
577 | ofile.write(reinterpret_cast<char *>(&boundS), sizeof(int));
|
---|
578 | ofile.write(reinterpret_cast<char *>(&boundT), sizeof(int));
|
---|
579 | }
|
---|
580 |
|
---|
581 |
|
---|
582 | ///////////
|
---|
583 | //-- write shapes
|
---|
584 |
|
---|
585 | ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
586 |
|
---|
587 | vector<Geometry *>::const_iterator it, it_end = mGeometry.end();
|
---|
588 |
|
---|
589 | for (it = mGeometry.begin(); it != it_end; ++ it)
|
---|
590 | {
|
---|
591 | WriteGeometry(ofile, *it);
|
---|
592 | }
|
---|
593 |
|
---|
594 |
|
---|
595 | int entityCount = 1;
|
---|
596 | ofile.write(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
597 |
|
---|
598 |
|
---|
599 | //////////
|
---|
600 | //-- write single scene entity
|
---|
601 |
|
---|
602 | // no transformation
|
---|
603 | bool hasTrafo = false;
|
---|
604 | ofile.write(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
|
---|
605 |
|
---|
606 | // a dummy lod
|
---|
607 | int numLODs = 1;
|
---|
608 | ofile.write(reinterpret_cast<char *>(&numLODs), sizeof(int));
|
---|
609 |
|
---|
610 | float dist = 0;
|
---|
611 | ofile.write(reinterpret_cast<char *>(&dist), sizeof(float));
|
---|
612 |
|
---|
613 | ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
614 |
|
---|
615 | // all shapes belong to this scene entity
|
---|
616 | for (int i = 0; i < mNumShapes; ++ i)
|
---|
617 | {
|
---|
618 | int shapeId = i;
|
---|
619 | ofile.write(reinterpret_cast<char *>(&shapeId), sizeof(int));
|
---|
620 | }
|
---|
621 |
|
---|
622 | return true;
|
---|
623 | }
|
---|
624 |
|
---|
625 |
|
---|
626 | bool VisibilitySolutionConverter::ReadBvh(FILE *fr)
|
---|
627 | {
|
---|
628 | int buffer[6];
|
---|
629 | fread(buffer, sizeof(int), 6, fr);
|
---|
630 |
|
---|
631 | if (buffer[0] != MAGIC)
|
---|
632 | {
|
---|
633 | cerr << "Error: Wrong file type" << endl;
|
---|
634 | return false;
|
---|
635 | }
|
---|
636 |
|
---|
637 | if (buffer[1] != (int)(1000 * BVH_VERSION))
|
---|
638 | {
|
---|
639 | cerr << "Error: Wrong BVH version" << endl;
|
---|
640 | return false;
|
---|
641 | }
|
---|
642 |
|
---|
643 | // load triangle ids
|
---|
644 | size_t numTriangles = buffer[2];
|
---|
645 |
|
---|
646 | for (size_t i = 0; i < numTriangles; ++i)
|
---|
647 | {
|
---|
648 | int id;
|
---|
649 | fread(&id, sizeof(int), 1, fr);
|
---|
650 | mGlobalTriangleIds[i] = id;
|
---|
651 | //triangles[i] = scene->triangles[id];
|
---|
652 | }
|
---|
653 |
|
---|
654 | //if (mRoot) delete mRoot;
|
---|
655 |
|
---|
656 | mNumNodes = 0; // this was set to 1 in constructor!
|
---|
657 |
|
---|
658 | mRoot = LoadNode(fr, 0);
|
---|
659 |
|
---|
660 | if (mNumNodes != buffer[5])
|
---|
661 | {
|
---|
662 | cerr << "Warning: Loaded " << mNumNodes <<
|
---|
663 | " bvh nodes instead of " << buffer[5] << endl;
|
---|
664 | }
|
---|
665 |
|
---|
666 | fclose(fr);
|
---|
667 |
|
---|
668 | return true;
|
---|
669 | }
|
---|
670 |
|
---|
671 |
|
---|
672 | BvhNode *VisibilitySolutionConverter::LoadNode(FILE *fr, int depth)
|
---|
673 | {
|
---|
674 | ++ mNumNodes;
|
---|
675 |
|
---|
676 | int buffer[4];
|
---|
677 | fread(buffer, sizeof(int), 4, fr);
|
---|
678 |
|
---|
679 | if (buffer[2] != -1)
|
---|
680 | {
|
---|
681 | BvhInterior *interior = new BvhInterior();
|
---|
682 |
|
---|
683 | interior->first = buffer[0];
|
---|
684 | interior->last = buffer[1];
|
---|
685 | interior->axis = buffer[2];
|
---|
686 | interior->id = buffer[3];
|
---|
687 | interior->depth = depth;
|
---|
688 |
|
---|
689 | BvhNode *front, *back;
|
---|
690 |
|
---|
691 | front = LoadNode(fr, depth + 1);
|
---|
692 | back = LoadNode(fr, depth + 1);
|
---|
693 |
|
---|
694 | // front->parent = (BvhNode *)interior;
|
---|
695 | // back->parent = (BvhNode *)interior;
|
---|
696 | interior->front = front;
|
---|
697 | interior->back = back;
|
---|
698 |
|
---|
699 | //interior->box = Union(front->box, back->box);
|
---|
700 |
|
---|
701 | return (BvhNode *)interior;
|
---|
702 | }
|
---|
703 | else
|
---|
704 | {
|
---|
705 | // leaf
|
---|
706 | //Debug << "Info: Loading leaf (id: " << numberOfNodes << ", depth: " << depth << ")" << endl;
|
---|
707 | BvhLeaf *leaf = new BvhLeaf();
|
---|
708 | leaf->first = buffer[0];
|
---|
709 | leaf->last = buffer[1];
|
---|
710 | leaf->axis = buffer[2];
|
---|
711 | leaf->id = buffer[3];
|
---|
712 |
|
---|
713 | leaf->depth = depth;
|
---|
714 | //UpdateLeafBox(leaf);
|
---|
715 |
|
---|
716 | mBvhNodes.push_back(leaf);
|
---|
717 | // leaf->id = numberOfNodes;
|
---|
718 | return (BvhNode *)leaf;
|
---|
719 | }
|
---|
720 | }
|
---|
721 |
|
---|
722 |
|
---|
723 | bool VisibilitySolutionConverter::ReadDummyTree(FILE *fr)
|
---|
724 | {
|
---|
725 | int buffer[256];
|
---|
726 | fread(buffer, sizeof(int), 3, fr);
|
---|
727 |
|
---|
728 | // read dummy bounding box
|
---|
729 | float dummy[6];
|
---|
730 | fread(dummy, sizeof(float) * 6, 1, fr);
|
---|
731 |
|
---|
732 | int stack = 1;
|
---|
733 |
|
---|
734 | // read dummy tree
|
---|
735 | while (stack)
|
---|
736 | {
|
---|
737 | int axis;
|
---|
738 | fread(&axis, sizeof(int), 1, fr);
|
---|
739 | -- stack;
|
---|
740 |
|
---|
741 | if (axis != - 1)
|
---|
742 | {
|
---|
743 | float dummy;
|
---|
744 | fread(&dummy, sizeof(float), 1, fr);
|
---|
745 |
|
---|
746 | stack += 2;
|
---|
747 | }
|
---|
748 | }
|
---|
749 |
|
---|
750 | return true;
|
---|
751 | }
|
---|
752 |
|
---|
753 |
|
---|
754 |
|
---|
755 | bool VisibilitySolutionConverter::LoadSolution(const string &filename)
|
---|
756 | {
|
---|
757 | FILE *fr = fopen(filename.c_str(), "rb");
|
---|
758 |
|
---|
759 | cerr << "Info: Loading visibility solution from file '" + filename + "'" << endl;
|
---|
760 |
|
---|
761 | if (fr == NULL)
|
---|
762 | {
|
---|
763 | cerr << "Error: Cannot open file for reading" << endl;
|
---|
764 | return false;
|
---|
765 | }
|
---|
766 |
|
---|
767 | float totalSamples, totalTime;
|
---|
768 |
|
---|
769 | fread(&totalSamples, sizeof(float), 1, fr);
|
---|
770 | fread(&totalTime, sizeof(float), 1, fr);
|
---|
771 |
|
---|
772 | //viewCellsTree = new ViewCellsTree;
|
---|
773 | //bool ok = viewCellsTree->_LoadFromFile(fr);
|
---|
774 |
|
---|
775 | bool ok = ReadDummyTree(fr);
|
---|
776 |
|
---|
777 | if (ok)
|
---|
778 | {
|
---|
779 | //AllocateLeafViewCells();
|
---|
780 |
|
---|
781 | //objectsTree = new Bvh(*scene);
|
---|
782 | //ok = objectsTree->_LoadFromFile(fr);
|
---|
783 | ok = ReadBvh(fr);
|
---|
784 |
|
---|
785 | /*if (ok)
|
---|
786 | {
|
---|
787 | AllocatePvsObjects();
|
---|
788 | ok = LoadPvs(fr);
|
---|
789 | }
|
---|
790 | */
|
---|
791 | }
|
---|
792 |
|
---|
793 | fclose(fr);
|
---|
794 |
|
---|
795 | if (ok)
|
---|
796 | cout << "Info: visibility solution loaded" << endl;
|
---|
797 | else
|
---|
798 | cout << "Info: loading visibility solution failed" << endl;
|
---|
799 |
|
---|
800 | return true;
|
---|
801 | }
|
---|
802 |
|
---|
803 |
|
---|
804 | #if 0
|
---|
805 | bool VisibilitySolution::LoadPvs(FILE *fw)
|
---|
806 | {
|
---|
807 | int number, entries;
|
---|
808 |
|
---|
809 | fread(&number, sizeof(int), 1, fw);
|
---|
810 |
|
---|
811 | if (number == 0)
|
---|
812 | {
|
---|
813 | Message("Info: Warning empty PVSs in visibility solution");
|
---|
814 | return true;
|
---|
815 | }
|
---|
816 |
|
---|
817 | if (number != viewCells.size())
|
---|
818 | {
|
---|
819 | Message("Info: Number of view cells does not match when loading PVSs!");
|
---|
820 | return false;
|
---|
821 | }
|
---|
822 |
|
---|
823 | for (int i=0; i < number; i++)
|
---|
824 | {
|
---|
825 | fread(&entries, sizeof(int), 1, fw);
|
---|
826 |
|
---|
827 | for (int j=0; j < entries; j++)
|
---|
828 | {
|
---|
829 | int objectId;
|
---|
830 | float time;
|
---|
831 | fread(&objectId, sizeof(int), 1, fw);
|
---|
832 | fread(&time, sizeof(float), 1, fw);
|
---|
833 | viewCells[i]->pvs.Insert(objectId, time);
|
---|
834 | }
|
---|
835 | }
|
---|
836 | return true;
|
---|
837 | }
|
---|
838 |
|
---|
839 |
|
---|
840 | bool ViewCellsTree::_LoadFromFile(FILE *fr)
|
---|
841 | {
|
---|
842 | int buffer[256];
|
---|
843 | fread(buffer, sizeof(int), 3, fr);
|
---|
844 |
|
---|
845 | if (buffer[0] != MAGIC)
|
---|
846 | {
|
---|
847 | Message( "Error: Wrong file type");
|
---|
848 | return false;
|
---|
849 | }
|
---|
850 |
|
---|
851 | if (buffer[1] != (int)(1000*VIEWCELLS_VERSION))
|
---|
852 | {
|
---|
853 | Message( "Error: Wrong viewcells version" );
|
---|
854 |
|
---|
855 | return false;
|
---|
856 | }
|
---|
857 |
|
---|
858 | // get the bounding box
|
---|
859 | fread(&box, sizeof(AxisAlignedBox3), 1, fr);
|
---|
860 |
|
---|
861 | stack<ViewCellsTreeNode **> nodeStack;
|
---|
862 | nodeStack.push(&root);
|
---|
863 |
|
---|
864 | while(!nodeStack.empty())
|
---|
865 | {
|
---|
866 | ViewCellsTreeNode *&node = *nodeStack.top();
|
---|
867 |
|
---|
868 | nodeStack.pop();
|
---|
869 | node = new ViewCellsTreeNode;
|
---|
870 |
|
---|
871 | fread(&node->axis, sizeof(int), 1, fr);
|
---|
872 |
|
---|
873 | if (!node->IsLeaf())
|
---|
874 | {
|
---|
875 | fread(&node->position, sizeof(float), 1, fr);
|
---|
876 |
|
---|
877 | nodeStack.push(&node->front);
|
---|
878 | nodeStack.push(&node->back);
|
---|
879 | }
|
---|
880 | }
|
---|
881 |
|
---|
882 | return true;
|
---|
883 | }
|
---|
884 | #endif |
---|