1 | #include <stack>
|
---|
2 | #include "common.h"
|
---|
3 | #include "SceneGraph.h"
|
---|
4 | #include "X3dExporter.h"
|
---|
5 | #include "Mesh.h"
|
---|
6 | #include "KdTree.h"
|
---|
7 | #include "ViewCellBsp.h"
|
---|
8 | #include "ViewCell.h"
|
---|
9 | #include "Polygon3.h"
|
---|
10 |
|
---|
11 | X3dExporter::X3dExporter(const string filename):Exporter(filename)
|
---|
12 | {
|
---|
13 | stream.open(mFilename.c_str());
|
---|
14 | stream<<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
|
---|
15 | stream<<"<X3D>"<<endl;
|
---|
16 | stream<<"<Scene>"<<endl;
|
---|
17 |
|
---|
18 | }
|
---|
19 |
|
---|
20 | X3dExporter::~X3dExporter()
|
---|
21 | {
|
---|
22 | stream<<"</Scene>"<<endl;
|
---|
23 | stream<<"</X3D>"<<endl;
|
---|
24 | stream.close();
|
---|
25 | }
|
---|
26 |
|
---|
27 |
|
---|
28 | bool
|
---|
29 | X3dExporter::ExportRays(const vector<Ray> &rays,
|
---|
30 | const float length,
|
---|
31 | const RgbColor &color)
|
---|
32 | {
|
---|
33 | vector<Ray>::const_iterator ri = rays.begin();
|
---|
34 | stream<<"<Shape>"<<endl;
|
---|
35 | stream<<"<Appearance>"<<endl;
|
---|
36 | stream<<"<Material ambientColor=\""<<color.r<<" "<<color.g<<" "<<color.b<<
|
---|
37 | "\" />"<<endl;
|
---|
38 | stream<<"</Appearance>"<<endl;
|
---|
39 |
|
---|
40 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
41 |
|
---|
42 | int index = 0;
|
---|
43 | for (; ri != rays.end(); ri++) {
|
---|
44 | stream<<index<<" "<<index+1<<" -1\n";
|
---|
45 | index+=2;
|
---|
46 | }
|
---|
47 |
|
---|
48 | stream<<"\" >"<<endl;
|
---|
49 |
|
---|
50 | stream<<"<Coordinate point=\""<<endl;
|
---|
51 |
|
---|
52 | ri = rays.begin();
|
---|
53 | for (; ri != rays.end(); ri++) {
|
---|
54 | Vector3 a = (*ri).GetLoc();
|
---|
55 |
|
---|
56 | Vector3 b;
|
---|
57 | if (length < 0)
|
---|
58 | b = (*ri).GetLoc() - length*(*ri).GetDir();
|
---|
59 | else
|
---|
60 | if ((*ri).intersections.size()==0)
|
---|
61 | b = (*ri).GetLoc() + length*(*ri).GetDir();
|
---|
62 | else
|
---|
63 | b = (*ri).Extrap((*ri).intersections[0].mT);
|
---|
64 |
|
---|
65 | stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
|
---|
66 | stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
|
---|
67 | }
|
---|
68 |
|
---|
69 | stream<<"\" >"<<endl;
|
---|
70 | stream<<"</Coordinate>"<<endl;
|
---|
71 | stream<<"</IndexedLineSet>"<<endl;
|
---|
72 | stream<<"</Shape>"<<endl;
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 |
|
---|
76 | bool
|
---|
77 | X3dExporter::ExportRays(const RayContainer &rays,
|
---|
78 | const float length,
|
---|
79 | const RgbColor &color)
|
---|
80 | {
|
---|
81 | RayContainer::const_iterator ri = rays.begin();
|
---|
82 |
|
---|
83 | stream<<"<Shape>"<<endl;
|
---|
84 | stream<<"<Appearance>"<<endl;
|
---|
85 | stream<<"<Material ambientColor=\""<<color.r<<" "<<color.g<<" "<<color.b<<
|
---|
86 | "\" />"<<endl;
|
---|
87 | stream<<"</Appearance>"<<endl;
|
---|
88 |
|
---|
89 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
90 |
|
---|
91 | int index = 0;
|
---|
92 | for (; ri != rays.end(); ri++) {
|
---|
93 | stream<<index<<" "<<index+1<<" -1\n";
|
---|
94 | index+=2;
|
---|
95 | }
|
---|
96 |
|
---|
97 | stream<<"\" >"<<endl;
|
---|
98 |
|
---|
99 | stream<<"<Coordinate point=\""<<endl;
|
---|
100 |
|
---|
101 | ri = rays.begin();
|
---|
102 | for (; ri != rays.end(); ri++) {
|
---|
103 | Vector3 a = (*ri)->GetLoc();
|
---|
104 |
|
---|
105 | Vector3 b;
|
---|
106 | if (length < 0)
|
---|
107 | b = (*ri)->GetLoc() - length*(*ri)->GetDir();
|
---|
108 | else
|
---|
109 | if ((*ri)->intersections.size()==0)
|
---|
110 | b = (*ri)->GetLoc() + length*(*ri)->GetDir();
|
---|
111 | else
|
---|
112 | b = (*ri)->Extrap((*ri)->intersections[0].mT);
|
---|
113 |
|
---|
114 | stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
|
---|
115 | stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
|
---|
116 | }
|
---|
117 |
|
---|
118 | stream<<"\" >"<<endl;
|
---|
119 | stream<<"</Coordinate>"<<endl;
|
---|
120 | stream<<"</IndexedLineSet>"<<endl;
|
---|
121 | stream<<"</Shape>"<<endl;
|
---|
122 |
|
---|
123 | return true;
|
---|
124 | }
|
---|
125 |
|
---|
126 | void
|
---|
127 | X3dExporter::ExportSceneNode(SceneGraphNode *node)
|
---|
128 | {
|
---|
129 | stream<<"<Group>"<<endl;
|
---|
130 |
|
---|
131 | SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
|
---|
132 | for (; ni != node->mChildren.end(); ni++)
|
---|
133 | ExportSceneNode(*ni);
|
---|
134 |
|
---|
135 |
|
---|
136 | ObjectContainer::const_iterator mi = node->mGeometry.begin();
|
---|
137 | for (; mi != node->mGeometry.end(); mi++) {
|
---|
138 | // export the transform...
|
---|
139 | ExportIntersectable(*mi);
|
---|
140 | }
|
---|
141 |
|
---|
142 | stream<<"</Group>"<<endl;
|
---|
143 |
|
---|
144 | }
|
---|
145 | void
|
---|
146 | X3dExporter::ExportIntersectable(Intersectable *object)
|
---|
147 | {
|
---|
148 | switch (object->Type()) {
|
---|
149 | case Intersectable::MESH_INSTANCE:
|
---|
150 | case Intersectable::TRANSFORMED_MESH_INSTANCE:
|
---|
151 | ExportMeshInstance((MeshInstance *)object);
|
---|
152 | break;
|
---|
153 | case Intersectable::VIEW_CELL:
|
---|
154 | ExportViewCell((ViewCell *)object);
|
---|
155 | break;
|
---|
156 | default:
|
---|
157 | cerr<<"Sorry the export for object not yet defined"<<endl;
|
---|
158 | break;
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | void
|
---|
163 | X3dExporter::ExportMeshInstance(MeshInstance *object)
|
---|
164 | {
|
---|
165 | // $$JB$$
|
---|
166 | // in the future check whether the mesh was not already exported
|
---|
167 | // and use a reference to the that mesh instead
|
---|
168 | ExportMesh(object->GetMesh());
|
---|
169 | }
|
---|
170 |
|
---|
171 | void
|
---|
172 | X3dExporter::ExportViewCells(const ViewCellContainer &viewCells)
|
---|
173 | {
|
---|
174 | ViewCellContainer::const_iterator it, it_end = viewCells.end();
|
---|
175 |
|
---|
176 | for (it = viewCells.begin(); it != it_end; ++ it)
|
---|
177 | ExportViewCell(*it);
|
---|
178 | }
|
---|
179 |
|
---|
180 | void
|
---|
181 | X3dExporter::ExportBspViewCellPartition(const BspTree &tree, const int maxPvs)
|
---|
182 | {
|
---|
183 | ViewCellContainer viewCells;
|
---|
184 | tree.CollectViewCells(viewCells);
|
---|
185 |
|
---|
186 | ViewCellContainer::const_iterator it, it_end = viewCells.end();
|
---|
187 |
|
---|
188 |
|
---|
189 | if (maxPvs > 0)
|
---|
190 | mUseForcedMaterial = true;
|
---|
191 |
|
---|
192 | for (it = viewCells.begin(); it != it_end; ++ it)
|
---|
193 | {
|
---|
194 | if (maxPvs > 0)
|
---|
195 | {
|
---|
196 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
197 | float importance = (float)(*it)->GetPvs().GetSize() / (float)maxPvs;
|
---|
198 |
|
---|
199 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
200 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
201 | }
|
---|
202 |
|
---|
203 | if ((*it)->GetMesh())
|
---|
204 | ExportViewCell(*it);
|
---|
205 | else
|
---|
206 | {
|
---|
207 | PolygonContainer cell;
|
---|
208 | tree.ConstructGeometry(dynamic_cast<BspViewCell *>(*it), cell);
|
---|
209 |
|
---|
210 | ExportPolygons(cell);
|
---|
211 | }
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | void
|
---|
216 | X3dExporter::ExportBspLeaves(const BspTree &tree)
|
---|
217 | {
|
---|
218 | vector<BspLeaf *> leaves;
|
---|
219 | tree.CollectLeaves(leaves);
|
---|
220 |
|
---|
221 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
222 |
|
---|
223 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
224 | {
|
---|
225 | PolygonContainer cell;
|
---|
226 | tree.ConstructGeometry(*it, cell);
|
---|
227 | ExportPolygons(cell);
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | void
|
---|
232 | X3dExporter::ExportViewCell(ViewCell *viewCell)
|
---|
233 | {
|
---|
234 | if (viewCell->GetMesh())
|
---|
235 | ExportMesh(viewCell->GetMesh());
|
---|
236 | }
|
---|
237 |
|
---|
238 | void
|
---|
239 | X3dExporter::ExportMesh(Mesh *mesh)
|
---|
240 | {
|
---|
241 |
|
---|
242 | stream<<"<Shape>"<<endl;
|
---|
243 | stream<<"<Appearance>"<<endl;
|
---|
244 |
|
---|
245 | // $$ tmp -> random material
|
---|
246 |
|
---|
247 | float r, g, b;
|
---|
248 |
|
---|
249 | if (mUseForcedMaterial) {
|
---|
250 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
251 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
252 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
253 |
|
---|
254 | } else
|
---|
255 | if (mesh->mMaterial) {
|
---|
256 | r = mesh->mMaterial->mDiffuseColor.r;
|
---|
257 | g = mesh->mMaterial->mDiffuseColor.g;
|
---|
258 | b = mesh->mMaterial->mDiffuseColor.b;
|
---|
259 | } else {
|
---|
260 | r = RandomValue(0.5, 1.0);
|
---|
261 | g = RandomValue(0.5, 1.0);
|
---|
262 | b = RandomValue(0.5, 1.0);
|
---|
263 | }
|
---|
264 | stream<<"<Material diffuseColor=\""<<r<<" "<<g<<" "<<b<<
|
---|
265 | "\" specularColor=\"0.0 0.0 0.0\"/>"<<endl;
|
---|
266 | stream<<"</Appearance>"<<endl;
|
---|
267 |
|
---|
268 |
|
---|
269 | if (mWireframe)
|
---|
270 | stream<<"<IndexedLineSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
271 | else
|
---|
272 | stream<<"<IndexedFaceSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
273 |
|
---|
274 | FaceContainer::const_iterator fi = mesh->mFaces.begin();
|
---|
275 |
|
---|
276 | int index = 0;
|
---|
277 |
|
---|
278 | for (; fi != mesh->mFaces.end(); fi++) {
|
---|
279 | Face *face = *fi;
|
---|
280 | VertexIndexContainer::const_iterator vi = face->mVertexIndices.begin();
|
---|
281 | for (; vi != face->mVertexIndices.end(); vi++)
|
---|
282 | stream<<*vi<<" ";
|
---|
283 | if (mWireframe) // final line to finish polygon
|
---|
284 | stream << (*face->mVertexIndices.begin()) << " ";
|
---|
285 |
|
---|
286 | stream<<"-1"<<endl;
|
---|
287 | }
|
---|
288 | stream<<"\" >"<<endl;
|
---|
289 |
|
---|
290 | stream<<"<Coordinate point=\""<<endl;
|
---|
291 |
|
---|
292 | VertexContainer::const_iterator vi = mesh->mVertices.begin();
|
---|
293 | for (; vi != mesh->mVertices.end(); vi++) {
|
---|
294 | stream<<(*vi).x<<" "<<(*vi).y<<" "<<(*vi).z;
|
---|
295 | stream<<","<<endl;
|
---|
296 | }
|
---|
297 |
|
---|
298 | stream<<"\" >"<<endl;
|
---|
299 | stream<<"</Coordinate>"<<endl;
|
---|
300 |
|
---|
301 | if (mWireframe)
|
---|
302 | stream<<"</IndexedLineSet>"<<endl;
|
---|
303 | else
|
---|
304 | stream<<"</IndexedFaceSet>"<<endl;
|
---|
305 |
|
---|
306 | stream<<"</Shape>"<<endl;
|
---|
307 |
|
---|
308 | }
|
---|
309 |
|
---|
310 |
|
---|
311 | void X3dExporter::ExportPolygon(Polygon3 *poly)
|
---|
312 | {
|
---|
313 | stream << "<Shape>" << endl;
|
---|
314 | stream << "<Appearance>" << endl;
|
---|
315 |
|
---|
316 | // $$ tmp -> random material
|
---|
317 |
|
---|
318 | float r, g, b;
|
---|
319 |
|
---|
320 | if (mUseForcedMaterial)
|
---|
321 | {
|
---|
322 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
323 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
324 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
325 | }
|
---|
326 | else if (poly->mMaterial)
|
---|
327 | {
|
---|
328 | r = poly->mMaterial->mDiffuseColor.r;
|
---|
329 | g = poly->mMaterial->mDiffuseColor.g;
|
---|
330 | b = poly->mMaterial->mDiffuseColor.b;
|
---|
331 | } else
|
---|
332 | {
|
---|
333 | r = RandomValue(0.5, 1.0);
|
---|
334 | g = RandomValue(0.5, 1.0);
|
---|
335 | b = RandomValue(0.5, 1.0);
|
---|
336 | }
|
---|
337 |
|
---|
338 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
339 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
340 |
|
---|
341 | stream << "</Appearance>" << endl;
|
---|
342 |
|
---|
343 |
|
---|
344 | //-- create and write indices
|
---|
345 | if (mWireframe)
|
---|
346 | stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
347 | else
|
---|
348 | stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
349 |
|
---|
350 | int index = 0;
|
---|
351 |
|
---|
352 | VertexContainer::const_iterator vi;
|
---|
353 |
|
---|
354 | for (index = 0; index < (int)poly->mVertices.size(); ++ index)
|
---|
355 | stream << index << " ";
|
---|
356 |
|
---|
357 | if (mWireframe) // final line to finish polygon
|
---|
358 | stream << "0 ";
|
---|
359 |
|
---|
360 | stream << "-1" << endl;
|
---|
361 | stream << "\" >" << endl;
|
---|
362 |
|
---|
363 | stream << "<Coordinate point=\"" << endl;
|
---|
364 |
|
---|
365 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
366 | {
|
---|
367 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
368 | stream << "," << endl;
|
---|
369 | }
|
---|
370 |
|
---|
371 | stream << "\" >" << endl;
|
---|
372 | stream << "</Coordinate>" << endl;
|
---|
373 |
|
---|
374 | if (mWireframe)
|
---|
375 | stream << "</IndexedLineSet>" << endl;
|
---|
376 | else
|
---|
377 | stream << "</IndexedFaceSet>" << endl;
|
---|
378 |
|
---|
379 | stream << "</Shape>" << endl;
|
---|
380 | }
|
---|
381 |
|
---|
382 | void X3dExporter::ExportPolygons(const PolygonContainer &polys)
|
---|
383 | {
|
---|
384 | stream << "<Shape>" << endl;
|
---|
385 | stream << "<Appearance>" << endl;
|
---|
386 |
|
---|
387 | // $$ tmp -> random material
|
---|
388 |
|
---|
389 | float r, g, b;
|
---|
390 |
|
---|
391 | if (mUseForcedMaterial)
|
---|
392 | {
|
---|
393 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
394 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
395 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
396 | }
|
---|
397 | else
|
---|
398 | {
|
---|
399 | r = RandomValue(0.5, 1.0);
|
---|
400 | g = RandomValue(0.5, 1.0);
|
---|
401 | b = RandomValue(0.5, 1.0);
|
---|
402 | }
|
---|
403 |
|
---|
404 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
405 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
406 |
|
---|
407 | stream << "</Appearance>" << endl;
|
---|
408 |
|
---|
409 |
|
---|
410 | //-- create and write indices
|
---|
411 | if (mWireframe)
|
---|
412 | stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
413 | else
|
---|
414 | stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
415 |
|
---|
416 | int index = 0;
|
---|
417 |
|
---|
418 | PolygonContainer::const_iterator pit;
|
---|
419 |
|
---|
420 | VertexContainer::const_iterator vi;
|
---|
421 |
|
---|
422 | for (pit = polys.begin(); pit != polys.end(); ++pit)
|
---|
423 | {
|
---|
424 | Polygon3 *poly = *pit;
|
---|
425 | int startIdx = index;
|
---|
426 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
427 | {
|
---|
428 | stream << index ++ << " ";
|
---|
429 | }
|
---|
430 |
|
---|
431 | stream << startIdx << " ";// finish line
|
---|
432 | stream << "-1" << endl;
|
---|
433 | }
|
---|
434 |
|
---|
435 | stream << "\" >" << endl;
|
---|
436 |
|
---|
437 | stream << "<Coordinate point=\"" << endl;
|
---|
438 | for (pit = polys.begin(); pit != polys.end(); ++ pit)
|
---|
439 | {
|
---|
440 | Polygon3 *poly = *pit;
|
---|
441 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
442 | {
|
---|
443 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
444 | stream << "," << endl;
|
---|
445 | }
|
---|
446 | }
|
---|
447 | stream << "\" >" << endl;
|
---|
448 | stream << "</Coordinate>" << endl;
|
---|
449 |
|
---|
450 | if (mWireframe)
|
---|
451 | stream << "</IndexedLineSet>" << endl;
|
---|
452 | else
|
---|
453 | stream << "</IndexedFaceSet>" << endl;
|
---|
454 |
|
---|
455 | stream << "</Shape>" << endl;
|
---|
456 | }
|
---|
457 |
|
---|
458 | bool
|
---|
459 | X3dExporter::ExportBox(const AxisAlignedBox3 &box)
|
---|
460 | {
|
---|
461 | Mesh *mesh = new Mesh;
|
---|
462 | // add 6 vertices of the box
|
---|
463 | int index = (int)mesh->mVertices.size();
|
---|
464 | for (int i=0; i < 8; i++) {
|
---|
465 | Vector3 v;
|
---|
466 | box.GetVertex(i, v);
|
---|
467 | mesh->mVertices.push_back(v);
|
---|
468 | }
|
---|
469 |
|
---|
470 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
471 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
472 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
473 |
|
---|
474 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
475 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
476 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
477 |
|
---|
478 | ExportMesh(mesh);
|
---|
479 | delete mesh;
|
---|
480 | return true;
|
---|
481 | }
|
---|
482 |
|
---|
483 | bool
|
---|
484 | X3dExporter::ExportBspTree(const BspTree &tree)
|
---|
485 | {
|
---|
486 | if (mExportRayDensity)
|
---|
487 | {
|
---|
488 | return ExportBspTreeRayDensity(tree);
|
---|
489 | }
|
---|
490 |
|
---|
491 | stack<BspNode *> tStack;
|
---|
492 |
|
---|
493 | tStack.push(tree.GetRoot());
|
---|
494 |
|
---|
495 | Mesh *mesh = new Mesh;
|
---|
496 |
|
---|
497 | AxisAlignedBox3 box = tree.GetBoundingBox();
|
---|
498 | bool savedWireframe = mWireframe;
|
---|
499 |
|
---|
500 | SetWireframe();
|
---|
501 | ExportBox(box);
|
---|
502 |
|
---|
503 | if (!savedWireframe)
|
---|
504 | SetFilled();
|
---|
505 |
|
---|
506 | //ViewCellContainer foundViewCells;
|
---|
507 |
|
---|
508 | if (BspTree::sStoreSplitPolys)
|
---|
509 | {
|
---|
510 | while (!tStack.empty())
|
---|
511 | {
|
---|
512 | BspNode *node = tStack.top();
|
---|
513 |
|
---|
514 | tStack.pop();
|
---|
515 |
|
---|
516 | PolygonContainer::const_iterator it;
|
---|
517 | PolygonContainer::const_iterator it_end = node->GetPolygons()->end();
|
---|
518 |
|
---|
519 | for (it = node->GetPolygons()->begin(); it != it_end; ++ it)
|
---|
520 | ExportPolygon(*it);
|
---|
521 |
|
---|
522 | if (!node->IsLeaf())
|
---|
523 | {
|
---|
524 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
525 |
|
---|
526 | tStack.push(interior->GetFront());
|
---|
527 | tStack.push(interior->GetBack());
|
---|
528 |
|
---|
529 | }
|
---|
530 | }
|
---|
531 | }
|
---|
532 | // export view cells
|
---|
533 | ExportBspViewCellPartition(tree);
|
---|
534 |
|
---|
535 | return true;
|
---|
536 | }
|
---|
537 |
|
---|
538 | bool X3dExporter::ExportKdTree(const KdTree &tree)
|
---|
539 | {
|
---|
540 | if (mExportRayDensity) {
|
---|
541 | return ExportKdTreeRayDensity(tree);
|
---|
542 | }
|
---|
543 |
|
---|
544 | stack<KdNode *> tStack;
|
---|
545 |
|
---|
546 | tStack.push(tree.GetRoot());
|
---|
547 |
|
---|
548 | Mesh *mesh = new Mesh;
|
---|
549 |
|
---|
550 | while (!tStack.empty()) {
|
---|
551 | KdNode *node = tStack.top();
|
---|
552 | tStack.pop();
|
---|
553 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
554 | // add 6 vertices of the box
|
---|
555 | int index = (int)mesh->mVertices.size();
|
---|
556 | for (int i=0; i < 8; i++) {
|
---|
557 | Vector3 v;
|
---|
558 | box.GetVertex(i, v);
|
---|
559 | mesh->mVertices.push_back(v);
|
---|
560 | }
|
---|
561 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
562 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
563 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
564 |
|
---|
565 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
566 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
567 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
568 |
|
---|
569 | if (!node->IsLeaf()) {
|
---|
570 | KdInterior *interior = (KdInterior *)node;
|
---|
571 | tStack.push(interior->mFront);
|
---|
572 | tStack.push(interior->mBack);
|
---|
573 | }
|
---|
574 | }
|
---|
575 |
|
---|
576 | ExportMesh(mesh);
|
---|
577 | delete mesh;
|
---|
578 | return true;
|
---|
579 | // TODO
|
---|
580 | return true;
|
---|
581 | }
|
---|
582 |
|
---|
583 |
|
---|
584 | bool
|
---|
585 | X3dExporter::ExportBspTreeRayDensity(const BspTree &tree)
|
---|
586 | {
|
---|
587 | stack<BspNode *> tStack;
|
---|
588 |
|
---|
589 | tStack.push(tree.GetRoot());
|
---|
590 |
|
---|
591 | bool fm = mUseForcedMaterial;
|
---|
592 |
|
---|
593 | mUseForcedMaterial = true;
|
---|
594 |
|
---|
595 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
596 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
597 |
|
---|
598 | while (!tStack.empty())
|
---|
599 | {
|
---|
600 | BspNode *node = tStack.top();
|
---|
601 | tStack.pop();
|
---|
602 |
|
---|
603 | if (node->IsLeaf())
|
---|
604 | {
|
---|
605 | ViewCell *vc = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
606 |
|
---|
607 | // set the mesh material according to the ray density
|
---|
608 | if (vc->mPassingRays.mRays)
|
---|
609 | {
|
---|
610 | float importance =
|
---|
611 | vc->mPassingRays.mContributions / (float)vc->mPassingRays.mRays;
|
---|
612 |
|
---|
613 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
614 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
615 | ExportViewCell(vc);
|
---|
616 | }
|
---|
617 | } else
|
---|
618 | {
|
---|
619 | BspInterior *interior = (BspInterior *)node;
|
---|
620 | tStack.push(interior->GetFront());
|
---|
621 | tStack.push(interior->GetBack());
|
---|
622 | }
|
---|
623 | }
|
---|
624 |
|
---|
625 | // restore the state of forced material
|
---|
626 | mUseForcedMaterial = fm;
|
---|
627 |
|
---|
628 | return true;
|
---|
629 | }
|
---|
630 |
|
---|
631 | bool
|
---|
632 | X3dExporter::ExportKdTreeRayDensity(const KdTree &tree)
|
---|
633 | {
|
---|
634 | stack<KdNode *> tStack;
|
---|
635 |
|
---|
636 | tStack.push(tree.GetRoot());
|
---|
637 |
|
---|
638 | bool fm = mUseForcedMaterial;
|
---|
639 | mUseForcedMaterial = true;
|
---|
640 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
641 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
642 | while (!tStack.empty()) {
|
---|
643 | KdNode *node = tStack.top();
|
---|
644 | tStack.pop();
|
---|
645 | if (node->IsLeaf()) {
|
---|
646 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
647 | Mesh *mesh = new Mesh;
|
---|
648 |
|
---|
649 | // add 6 vertices of the box
|
---|
650 | int index = (int)mesh->mVertices.size();
|
---|
651 | for (int i=0; i < 8; i++) {
|
---|
652 | Vector3 v;
|
---|
653 | box.GetVertex(i, v);
|
---|
654 | mesh->mVertices.push_back(v);
|
---|
655 | }
|
---|
656 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
657 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
658 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
659 |
|
---|
660 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
661 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
662 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
663 |
|
---|
664 |
|
---|
665 | // set the mesh material according to the ray density
|
---|
666 | KdLeaf *leaf = (KdLeaf *) node;
|
---|
667 | if (leaf->mPassingRays.mRays) {
|
---|
668 | float importance = leaf->mPassingRays.mContributions/(float)leaf->mPassingRays.mRays;
|
---|
669 | // float importance = leaf->mPassingRays.mContributions/1000.0f;
|
---|
670 | // float importance = leaf->mPassingRays.mRays/1000.0f;
|
---|
671 | ///(float)leaf->mPassingRays.mRays;
|
---|
672 | // mForcedMaterial.mDiffuseColor.r = log10(leaf->mPassingRays.mRays)/3.0f;
|
---|
673 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
674 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
675 | ExportMesh(mesh);
|
---|
676 | }
|
---|
677 | delete mesh;
|
---|
678 | } else {
|
---|
679 | KdInterior *interior = (KdInterior *)node;
|
---|
680 | tStack.push(interior->mFront);
|
---|
681 | tStack.push(interior->mBack);
|
---|
682 | }
|
---|
683 | }
|
---|
684 | // restore the state of forced material
|
---|
685 | mUseForcedMaterial = fm;
|
---|
686 | return true;
|
---|
687 | }
|
---|
688 |
|
---|
689 |
|
---|
690 | struct BspSplitData
|
---|
691 | {
|
---|
692 | /// the current node
|
---|
693 | BspNode *mNode;
|
---|
694 |
|
---|
695 | vector<Plane3 *> mPlanes;
|
---|
696 | vector<bool> mSides;
|
---|
697 | bool mIsFront;
|
---|
698 |
|
---|
699 | BspSplitData(BspNode *node):
|
---|
700 | mNode(node), mIsFront(false)
|
---|
701 | {};
|
---|
702 | BspSplitData(BspNode *node,
|
---|
703 | vector<Plane3 *> planes,
|
---|
704 | vector<bool> sides,
|
---|
705 | bool isFront):
|
---|
706 | mNode(node), mPlanes(planes),
|
---|
707 | mSides(sides), mIsFront(isFront)
|
---|
708 | {};
|
---|
709 | };
|
---|
710 |
|
---|
711 | void X3dExporter::ExportLeavesGeometry(const BspTree &tree, const vector<BspLeaf *> &leaves)
|
---|
712 | {
|
---|
713 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
714 |
|
---|
715 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
716 | {
|
---|
717 | PolygonContainer cell;
|
---|
718 | tree.ConstructGeometry(*it, cell);
|
---|
719 |
|
---|
720 | ExportPolygons(cell);
|
---|
721 |
|
---|
722 | CLEAR_CONTAINER(cell);
|
---|
723 | }
|
---|
724 | }
|
---|
725 |
|
---|
726 | void X3dExporter::ExportBspSplits(const BspTree &tree)
|
---|
727 | {
|
---|
728 | std::stack<BspSplitData> tStack;
|
---|
729 |
|
---|
730 | BspSplitData tData(tree.GetRoot());
|
---|
731 | tStack.push(tData);
|
---|
732 |
|
---|
733 | PolygonContainer polys;
|
---|
734 |
|
---|
735 | while (!tStack.empty())
|
---|
736 | {
|
---|
737 | // filter polygons donw the tree
|
---|
738 | BspSplitData tData = tStack.top();
|
---|
739 | tStack.pop();
|
---|
740 |
|
---|
741 | if (!tData.mNode->IsLeaf())
|
---|
742 | {
|
---|
743 | BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode);
|
---|
744 |
|
---|
745 | if (tData.mNode != tree.GetRoot())
|
---|
746 | tData.mSides.push_back(tData.mIsFront); // add current side of split plane
|
---|
747 |
|
---|
748 | // bounded plane is added to the polygons
|
---|
749 | Polygon3 *planePoly = tree.GetBoundingBox().CrossSection(*interior->GetPlane());
|
---|
750 |
|
---|
751 | // do all the splits with the previous planes
|
---|
752 | for (int i = 0; i < (int)tData.mPlanes.size(); ++ i)
|
---|
753 | {
|
---|
754 | VertexContainer splitPts;
|
---|
755 |
|
---|
756 | if (planePoly->ClassifyPlane(*tData.mPlanes[i]) == Polygon3::SPLIT)
|
---|
757 | {
|
---|
758 | Polygon3 *frontPoly = new Polygon3();
|
---|
759 | Polygon3 *backPoly = new Polygon3();
|
---|
760 |
|
---|
761 | planePoly->Split(*tData.mPlanes[i], *frontPoly, *backPoly, splitPts);
|
---|
762 | DEL_PTR(planePoly);
|
---|
763 |
|
---|
764 | if(tData.mSides[i] == true)
|
---|
765 | {
|
---|
766 | planePoly = frontPoly;
|
---|
767 | DEL_PTR(backPoly);
|
---|
768 | }
|
---|
769 | else
|
---|
770 | {
|
---|
771 | planePoly = backPoly;
|
---|
772 | DEL_PTR(frontPoly);
|
---|
773 | }
|
---|
774 | }
|
---|
775 | }
|
---|
776 |
|
---|
777 | tData.mPlanes.push_back(interior->GetPlane()); // add plane to split planes
|
---|
778 |
|
---|
779 | if (planePoly->Valid())
|
---|
780 | polys.push_back(planePoly);
|
---|
781 | else
|
---|
782 | DEL_PTR(planePoly);
|
---|
783 |
|
---|
784 | // push the children on the stack
|
---|
785 | tStack.push(BspSplitData(interior->GetFront(), tData.mPlanes, tData.mSides, true));
|
---|
786 | tStack.push(BspSplitData(interior->GetBack(), tData.mPlanes, tData.mSides, false));
|
---|
787 | }
|
---|
788 | }
|
---|
789 | ExportPolygons(polys);
|
---|
790 | CLEAR_CONTAINER(polys);
|
---|
791 | }
|
---|
792 |
|
---|
793 | void X3dExporter::ExportBspSplitPlanes(const BspTree &tree)
|
---|
794 | {
|
---|
795 | std::stack<BspNode *> tStack;
|
---|
796 |
|
---|
797 | tStack.push(tree.GetRoot());
|
---|
798 |
|
---|
799 | PolygonContainer polys;
|
---|
800 |
|
---|
801 | while (!tStack.empty())
|
---|
802 | {
|
---|
803 | // filter polygons donw the tree
|
---|
804 | BspNode *node = tStack.top();
|
---|
805 | tStack.pop();
|
---|
806 |
|
---|
807 | if (!node->IsLeaf())
|
---|
808 | {
|
---|
809 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
810 |
|
---|
811 | // bounded plane is added to the polygons
|
---|
812 | polys.push_back(tree.GetBoundingBox().CrossSection(*interior->GetPlane()));
|
---|
813 |
|
---|
814 | // push the children on the stack
|
---|
815 | tStack.push(interior->GetBack());
|
---|
816 | tStack.push(interior->GetFront());
|
---|
817 | }
|
---|
818 | }
|
---|
819 |
|
---|
820 | ExportPolygons(polys);
|
---|
821 | CLEAR_CONTAINER(polys);
|
---|
822 | }
|
---|