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 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
195 |
|
---|
196 | float importance = 0;
|
---|
197 |
|
---|
198 | if (maxPvs > 0)
|
---|
199 | importance = (float)(*it)->GetPvs().GetSize() / (float)maxPvs;
|
---|
200 |
|
---|
201 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
202 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
203 |
|
---|
204 | if ((*it)->GetMesh())
|
---|
205 | ExportViewCell(*it);
|
---|
206 | else
|
---|
207 | {
|
---|
208 | PolygonContainer cell;
|
---|
209 | tree.ConstructGeometry(dynamic_cast<BspViewCell *>(*it), cell);
|
---|
210 |
|
---|
211 | ExportPolygons(cell);
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | void
|
---|
217 | X3dExporter::ExportBspLeaves(const BspTree &tree)
|
---|
218 | {
|
---|
219 | vector<BspLeaf *> leaves;
|
---|
220 | tree.CollectLeaves(leaves);
|
---|
221 |
|
---|
222 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
223 |
|
---|
224 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
225 | {
|
---|
226 | PolygonContainer cell;
|
---|
227 | tree.ConstructGeometry(*it, cell);
|
---|
228 | ExportPolygons(cell);
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | void
|
---|
233 | X3dExporter::ExportViewCell(ViewCell *viewCell)
|
---|
234 | {
|
---|
235 | if (viewCell->GetMesh())
|
---|
236 | ExportMesh(viewCell->GetMesh());
|
---|
237 | }
|
---|
238 |
|
---|
239 | void
|
---|
240 | X3dExporter::ExportMesh(Mesh *mesh)
|
---|
241 | {
|
---|
242 |
|
---|
243 | stream<<"<Shape>"<<endl;
|
---|
244 | stream<<"<Appearance>"<<endl;
|
---|
245 |
|
---|
246 | // $$ tmp -> random material
|
---|
247 |
|
---|
248 | float r, g, b;
|
---|
249 |
|
---|
250 | if (mUseForcedMaterial) {
|
---|
251 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
252 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
253 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
254 |
|
---|
255 | } else
|
---|
256 | if (mesh->mMaterial) {
|
---|
257 | r = mesh->mMaterial->mDiffuseColor.r;
|
---|
258 | g = mesh->mMaterial->mDiffuseColor.g;
|
---|
259 | b = mesh->mMaterial->mDiffuseColor.b;
|
---|
260 | } else {
|
---|
261 | r = RandomValue(0.5, 1.0);
|
---|
262 | g = RandomValue(0.5, 1.0);
|
---|
263 | b = RandomValue(0.5, 1.0);
|
---|
264 | }
|
---|
265 | stream<<"<Material diffuseColor=\""<<r<<" "<<g<<" "<<b<<
|
---|
266 | "\" specularColor=\"0.0 0.0 0.0\"/>"<<endl;
|
---|
267 | stream<<"</Appearance>"<<endl;
|
---|
268 |
|
---|
269 |
|
---|
270 | if (mWireframe)
|
---|
271 | stream<<"<IndexedLineSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
272 | else
|
---|
273 | stream<<"<IndexedFaceSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
274 |
|
---|
275 | FaceContainer::const_iterator fi = mesh->mFaces.begin();
|
---|
276 |
|
---|
277 | int index = 0;
|
---|
278 |
|
---|
279 | for (; fi != mesh->mFaces.end(); fi++) {
|
---|
280 | Face *face = *fi;
|
---|
281 | VertexIndexContainer::const_iterator vi = face->mVertexIndices.begin();
|
---|
282 | for (; vi != face->mVertexIndices.end(); vi++)
|
---|
283 | stream<<*vi<<" ";
|
---|
284 | if (mWireframe) // final line to finish polygon
|
---|
285 | stream << (*face->mVertexIndices.begin()) << " ";
|
---|
286 |
|
---|
287 | stream<<"-1"<<endl;
|
---|
288 | }
|
---|
289 | stream<<"\" >"<<endl;
|
---|
290 |
|
---|
291 | stream<<"<Coordinate point=\""<<endl;
|
---|
292 |
|
---|
293 | VertexContainer::const_iterator vi = mesh->mVertices.begin();
|
---|
294 | for (; vi != mesh->mVertices.end(); vi++) {
|
---|
295 | stream<<(*vi).x<<" "<<(*vi).y<<" "<<(*vi).z;
|
---|
296 | stream<<","<<endl;
|
---|
297 | }
|
---|
298 |
|
---|
299 | stream<<"\" >"<<endl;
|
---|
300 | stream<<"</Coordinate>"<<endl;
|
---|
301 |
|
---|
302 | if (mWireframe)
|
---|
303 | stream<<"</IndexedLineSet>"<<endl;
|
---|
304 | else
|
---|
305 | stream<<"</IndexedFaceSet>"<<endl;
|
---|
306 |
|
---|
307 | stream<<"</Shape>"<<endl;
|
---|
308 |
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | void X3dExporter::ExportPolygon(Polygon3 *poly)
|
---|
313 | {
|
---|
314 | stream << "<Shape>" << endl;
|
---|
315 | stream << "<Appearance>" << endl;
|
---|
316 |
|
---|
317 | // $$ tmp -> random material
|
---|
318 |
|
---|
319 | float r, g, b;
|
---|
320 |
|
---|
321 | if (mUseForcedMaterial)
|
---|
322 | {
|
---|
323 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
324 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
325 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
326 | }
|
---|
327 | else if (poly->mMaterial)
|
---|
328 | {
|
---|
329 | r = poly->mMaterial->mDiffuseColor.r;
|
---|
330 | g = poly->mMaterial->mDiffuseColor.g;
|
---|
331 | b = poly->mMaterial->mDiffuseColor.b;
|
---|
332 | } else
|
---|
333 | {
|
---|
334 | r = RandomValue(0.5, 1.0);
|
---|
335 | g = RandomValue(0.5, 1.0);
|
---|
336 | b = RandomValue(0.5, 1.0);
|
---|
337 | }
|
---|
338 |
|
---|
339 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
340 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
341 |
|
---|
342 | stream << "</Appearance>" << endl;
|
---|
343 |
|
---|
344 |
|
---|
345 | //-- create and write indices
|
---|
346 | if (mWireframe)
|
---|
347 | stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
348 | else
|
---|
349 | stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
350 |
|
---|
351 | int index = 0;
|
---|
352 |
|
---|
353 | VertexContainer::const_iterator vi;
|
---|
354 |
|
---|
355 | for (index = 0; index < (int)poly->mVertices.size(); ++ index)
|
---|
356 | stream << index << " ";
|
---|
357 |
|
---|
358 | if (mWireframe) // final line to finish polygon
|
---|
359 | stream << "0 ";
|
---|
360 |
|
---|
361 | stream << "-1" << endl;
|
---|
362 | stream << "\" >" << endl;
|
---|
363 |
|
---|
364 | stream << "<Coordinate point=\"" << endl;
|
---|
365 |
|
---|
366 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
367 | {
|
---|
368 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
369 | stream << "," << endl;
|
---|
370 | }
|
---|
371 |
|
---|
372 | stream << "\" >" << endl;
|
---|
373 | stream << "</Coordinate>" << endl;
|
---|
374 |
|
---|
375 | if (mWireframe)
|
---|
376 | stream << "</IndexedLineSet>" << endl;
|
---|
377 | else
|
---|
378 | stream << "</IndexedFaceSet>" << endl;
|
---|
379 |
|
---|
380 | stream << "</Shape>" << endl;
|
---|
381 | }
|
---|
382 |
|
---|
383 | void X3dExporter::ExportPolygons(const PolygonContainer &polys)
|
---|
384 | {
|
---|
385 | stream << "<Shape>" << endl;
|
---|
386 | stream << "<Appearance>" << endl;
|
---|
387 |
|
---|
388 | // $$ tmp -> random material
|
---|
389 |
|
---|
390 | float r, g, b;
|
---|
391 |
|
---|
392 | if (mUseForcedMaterial)
|
---|
393 | {
|
---|
394 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
395 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
396 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
397 | }
|
---|
398 | else
|
---|
399 | {
|
---|
400 | r = RandomValue(0.5, 1.0);
|
---|
401 | g = RandomValue(0.5, 1.0);
|
---|
402 | b = RandomValue(0.5, 1.0);
|
---|
403 | }
|
---|
404 |
|
---|
405 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
406 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
407 |
|
---|
408 | stream << "</Appearance>" << endl;
|
---|
409 |
|
---|
410 |
|
---|
411 | //-- create and write indices
|
---|
412 | if (mWireframe)
|
---|
413 | stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
414 | else
|
---|
415 | stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
416 |
|
---|
417 | int index = 0;
|
---|
418 |
|
---|
419 | PolygonContainer::const_iterator pit;
|
---|
420 |
|
---|
421 | VertexContainer::const_iterator vi;
|
---|
422 |
|
---|
423 | for (pit = polys.begin(); pit != polys.end(); ++pit)
|
---|
424 | {
|
---|
425 | Polygon3 *poly = *pit;
|
---|
426 | int startIdx = index;
|
---|
427 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
428 | {
|
---|
429 | stream << index ++ << " ";
|
---|
430 | }
|
---|
431 |
|
---|
432 | stream << startIdx << " ";// finish line
|
---|
433 | stream << "-1" << endl;
|
---|
434 | }
|
---|
435 |
|
---|
436 | stream << "\" >" << endl;
|
---|
437 |
|
---|
438 | stream << "<Coordinate point=\"" << endl;
|
---|
439 | for (pit = polys.begin(); pit != polys.end(); ++ pit)
|
---|
440 | {
|
---|
441 | Polygon3 *poly = *pit;
|
---|
442 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
443 | {
|
---|
444 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
445 | stream << "," << endl;
|
---|
446 | }
|
---|
447 | }
|
---|
448 | stream << "\" >" << endl;
|
---|
449 | stream << "</Coordinate>" << endl;
|
---|
450 |
|
---|
451 | if (mWireframe)
|
---|
452 | stream << "</IndexedLineSet>" << endl;
|
---|
453 | else
|
---|
454 | stream << "</IndexedFaceSet>" << endl;
|
---|
455 |
|
---|
456 | stream << "</Shape>" << endl;
|
---|
457 | }
|
---|
458 |
|
---|
459 | bool
|
---|
460 | X3dExporter::ExportBox(const AxisAlignedBox3 &box)
|
---|
461 | {
|
---|
462 | Mesh *mesh = new Mesh;
|
---|
463 | // add 6 vertices of the box
|
---|
464 | int index = (int)mesh->mVertices.size();
|
---|
465 | for (int i=0; i < 8; i++) {
|
---|
466 | Vector3 v;
|
---|
467 | box.GetVertex(i, v);
|
---|
468 | mesh->mVertices.push_back(v);
|
---|
469 | }
|
---|
470 |
|
---|
471 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
472 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
473 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
474 |
|
---|
475 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
476 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
477 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
478 |
|
---|
479 | ExportMesh(mesh);
|
---|
480 | delete mesh;
|
---|
481 | return true;
|
---|
482 | }
|
---|
483 |
|
---|
484 | bool
|
---|
485 | X3dExporter::ExportBspTree(const BspTree &tree)
|
---|
486 | {
|
---|
487 | if (mExportRayDensity)
|
---|
488 | {
|
---|
489 | return ExportBspTreeRayDensity(tree);
|
---|
490 | }
|
---|
491 |
|
---|
492 | stack<BspNode *> tStack;
|
---|
493 |
|
---|
494 | tStack.push(tree.GetRoot());
|
---|
495 |
|
---|
496 | Mesh *mesh = new Mesh;
|
---|
497 |
|
---|
498 | AxisAlignedBox3 box = tree.GetBoundingBox();
|
---|
499 | bool savedWireframe = mWireframe;
|
---|
500 |
|
---|
501 | SetWireframe();
|
---|
502 | ExportBox(box);
|
---|
503 |
|
---|
504 | if (!savedWireframe)
|
---|
505 | SetFilled();
|
---|
506 |
|
---|
507 | //ViewCellContainer foundViewCells;
|
---|
508 |
|
---|
509 | if (BspTree::sStoreSplitPolys)
|
---|
510 | {
|
---|
511 | while (!tStack.empty())
|
---|
512 | {
|
---|
513 | BspNode *node = tStack.top();
|
---|
514 |
|
---|
515 | tStack.pop();
|
---|
516 |
|
---|
517 | PolygonContainer::const_iterator it;
|
---|
518 | PolygonContainer::const_iterator it_end = node->GetPolygons()->end();
|
---|
519 |
|
---|
520 | for (it = node->GetPolygons()->begin(); it != it_end; ++ it)
|
---|
521 | ExportPolygon(*it);
|
---|
522 |
|
---|
523 | if (!node->IsLeaf())
|
---|
524 | {
|
---|
525 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
526 |
|
---|
527 | tStack.push(interior->GetFront());
|
---|
528 | tStack.push(interior->GetBack());
|
---|
529 |
|
---|
530 | }
|
---|
531 | }
|
---|
532 | }
|
---|
533 | // export view cells
|
---|
534 | ExportBspViewCellPartition(tree);
|
---|
535 |
|
---|
536 | return true;
|
---|
537 | }
|
---|
538 |
|
---|
539 | bool X3dExporter::ExportKdTree(const KdTree &tree)
|
---|
540 | {
|
---|
541 | if (mExportRayDensity) {
|
---|
542 | return ExportKdTreeRayDensity(tree);
|
---|
543 | }
|
---|
544 |
|
---|
545 | stack<KdNode *> tStack;
|
---|
546 |
|
---|
547 | tStack.push(tree.GetRoot());
|
---|
548 |
|
---|
549 | Mesh *mesh = new Mesh;
|
---|
550 |
|
---|
551 | while (!tStack.empty()) {
|
---|
552 | KdNode *node = tStack.top();
|
---|
553 | tStack.pop();
|
---|
554 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
555 | // add 6 vertices of the box
|
---|
556 | int index = (int)mesh->mVertices.size();
|
---|
557 | for (int i=0; i < 8; i++) {
|
---|
558 | Vector3 v;
|
---|
559 | box.GetVertex(i, v);
|
---|
560 | mesh->mVertices.push_back(v);
|
---|
561 | }
|
---|
562 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
563 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
564 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
565 |
|
---|
566 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
567 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
568 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
569 |
|
---|
570 | if (!node->IsLeaf()) {
|
---|
571 | KdInterior *interior = (KdInterior *)node;
|
---|
572 | tStack.push(interior->mFront);
|
---|
573 | tStack.push(interior->mBack);
|
---|
574 | }
|
---|
575 | }
|
---|
576 |
|
---|
577 | ExportMesh(mesh);
|
---|
578 | delete mesh;
|
---|
579 | return true;
|
---|
580 | // TODO
|
---|
581 | return true;
|
---|
582 | }
|
---|
583 |
|
---|
584 |
|
---|
585 | bool
|
---|
586 | X3dExporter::ExportBspTreeRayDensity(const BspTree &tree)
|
---|
587 | {
|
---|
588 | stack<BspNode *> tStack;
|
---|
589 |
|
---|
590 | tStack.push(tree.GetRoot());
|
---|
591 |
|
---|
592 | bool fm = mUseForcedMaterial;
|
---|
593 |
|
---|
594 | mUseForcedMaterial = true;
|
---|
595 |
|
---|
596 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
597 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
598 |
|
---|
599 | while (!tStack.empty())
|
---|
600 | {
|
---|
601 | BspNode *node = tStack.top();
|
---|
602 | tStack.pop();
|
---|
603 |
|
---|
604 | if (node->IsLeaf())
|
---|
605 | {
|
---|
606 | ViewCell *vc = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
607 |
|
---|
608 | // set the mesh material according to the ray density
|
---|
609 | if (vc->mPassingRays.mRays)
|
---|
610 | {
|
---|
611 | float importance =
|
---|
612 | vc->mPassingRays.mContributions / (float)vc->mPassingRays.mRays;
|
---|
613 |
|
---|
614 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
615 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
616 | ExportViewCell(vc);
|
---|
617 | }
|
---|
618 | } else
|
---|
619 | {
|
---|
620 | BspInterior *interior = (BspInterior *)node;
|
---|
621 | tStack.push(interior->GetFront());
|
---|
622 | tStack.push(interior->GetBack());
|
---|
623 | }
|
---|
624 | }
|
---|
625 |
|
---|
626 | // restore the state of forced material
|
---|
627 | mUseForcedMaterial = fm;
|
---|
628 |
|
---|
629 | return true;
|
---|
630 | }
|
---|
631 |
|
---|
632 | bool
|
---|
633 | X3dExporter::ExportKdTreeRayDensity(const KdTree &tree)
|
---|
634 | {
|
---|
635 | stack<KdNode *> tStack;
|
---|
636 |
|
---|
637 | tStack.push(tree.GetRoot());
|
---|
638 |
|
---|
639 | bool fm = mUseForcedMaterial;
|
---|
640 | mUseForcedMaterial = true;
|
---|
641 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
642 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
643 | while (!tStack.empty()) {
|
---|
644 | KdNode *node = tStack.top();
|
---|
645 | tStack.pop();
|
---|
646 | if (node->IsLeaf()) {
|
---|
647 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
648 | Mesh *mesh = new Mesh;
|
---|
649 |
|
---|
650 | // add 6 vertices of the box
|
---|
651 | int index = (int)mesh->mVertices.size();
|
---|
652 | for (int i=0; i < 8; i++) {
|
---|
653 | Vector3 v;
|
---|
654 | box.GetVertex(i, v);
|
---|
655 | mesh->mVertices.push_back(v);
|
---|
656 | }
|
---|
657 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
658 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
659 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
660 |
|
---|
661 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
662 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
663 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
664 |
|
---|
665 |
|
---|
666 | // set the mesh material according to the ray density
|
---|
667 | KdLeaf *leaf = (KdLeaf *) node;
|
---|
668 | if (leaf->mPassingRays.mRays) {
|
---|
669 | float importance = leaf->mPassingRays.mContributions/(float)leaf->mPassingRays.mRays;
|
---|
670 | // float importance = leaf->mPassingRays.mContributions/1000.0f;
|
---|
671 | // float importance = leaf->mPassingRays.mRays/1000.0f;
|
---|
672 | ///(float)leaf->mPassingRays.mRays;
|
---|
673 | // mForcedMaterial.mDiffuseColor.r = log10(leaf->mPassingRays.mRays)/3.0f;
|
---|
674 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
675 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
676 | ExportMesh(mesh);
|
---|
677 | }
|
---|
678 | delete mesh;
|
---|
679 | } else {
|
---|
680 | KdInterior *interior = (KdInterior *)node;
|
---|
681 | tStack.push(interior->mFront);
|
---|
682 | tStack.push(interior->mBack);
|
---|
683 | }
|
---|
684 | }
|
---|
685 | // restore the state of forced material
|
---|
686 | mUseForcedMaterial = fm;
|
---|
687 | return true;
|
---|
688 | }
|
---|
689 |
|
---|
690 |
|
---|
691 | struct BspSplitData
|
---|
692 | {
|
---|
693 | /// the current node
|
---|
694 | BspNode *mNode;
|
---|
695 |
|
---|
696 | vector<Plane3 *> mPlanes;
|
---|
697 | vector<bool> mSides;
|
---|
698 | bool mIsFront;
|
---|
699 |
|
---|
700 | BspSplitData(BspNode *node):
|
---|
701 | mNode(node), mIsFront(false)
|
---|
702 | {};
|
---|
703 | BspSplitData(BspNode *node,
|
---|
704 | vector<Plane3 *> planes,
|
---|
705 | vector<bool> sides,
|
---|
706 | bool isFront):
|
---|
707 | mNode(node), mPlanes(planes),
|
---|
708 | mSides(sides), mIsFront(isFront)
|
---|
709 | {};
|
---|
710 | };
|
---|
711 |
|
---|
712 | void X3dExporter::ExportLeavesGeometry(const BspTree &tree, const vector<BspLeaf *> &leaves)
|
---|
713 | {
|
---|
714 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
715 |
|
---|
716 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
717 | {
|
---|
718 | PolygonContainer cell;
|
---|
719 | tree.ConstructGeometry(*it, cell);
|
---|
720 |
|
---|
721 | ExportPolygons(cell);
|
---|
722 |
|
---|
723 | CLEAR_CONTAINER(cell);
|
---|
724 | }
|
---|
725 | }
|
---|
726 |
|
---|
727 | void X3dExporter::ExportBspSplits(const BspTree &tree)
|
---|
728 | {
|
---|
729 | std::stack<BspSplitData> tStack;
|
---|
730 |
|
---|
731 | BspSplitData tData(tree.GetRoot());
|
---|
732 | tStack.push(tData);
|
---|
733 |
|
---|
734 | PolygonContainer polys;
|
---|
735 |
|
---|
736 | while (!tStack.empty())
|
---|
737 | {
|
---|
738 | // filter polygons donw the tree
|
---|
739 | BspSplitData tData = tStack.top();
|
---|
740 | tStack.pop();
|
---|
741 |
|
---|
742 | if (!tData.mNode->IsLeaf())
|
---|
743 | {
|
---|
744 | BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode);
|
---|
745 |
|
---|
746 | if (tData.mNode != tree.GetRoot())
|
---|
747 | tData.mSides.push_back(tData.mIsFront); // add current side of split plane
|
---|
748 |
|
---|
749 | // bounded plane is added to the polygons
|
---|
750 | Polygon3 *planePoly = tree.GetBoundingBox().CrossSection(*interior->GetPlane());
|
---|
751 |
|
---|
752 | // do all the splits with the previous planes
|
---|
753 | for (int i = 0; i < (int)tData.mPlanes.size(); ++ i)
|
---|
754 | {
|
---|
755 | VertexContainer splitPts;
|
---|
756 |
|
---|
757 | if (planePoly->ClassifyPlane(*tData.mPlanes[i]) == Polygon3::SPLIT)
|
---|
758 | {
|
---|
759 | Polygon3 *frontPoly = new Polygon3();
|
---|
760 | Polygon3 *backPoly = new Polygon3();
|
---|
761 |
|
---|
762 | planePoly->Split(*tData.mPlanes[i], *frontPoly, *backPoly, splitPts);
|
---|
763 | DEL_PTR(planePoly);
|
---|
764 |
|
---|
765 | if(tData.mSides[i] == true)
|
---|
766 | {
|
---|
767 | planePoly = frontPoly;
|
---|
768 | DEL_PTR(backPoly);
|
---|
769 | }
|
---|
770 | else
|
---|
771 | {
|
---|
772 | planePoly = backPoly;
|
---|
773 | DEL_PTR(frontPoly);
|
---|
774 | }
|
---|
775 | }
|
---|
776 | }
|
---|
777 |
|
---|
778 | tData.mPlanes.push_back(interior->GetPlane()); // add plane to split planes
|
---|
779 |
|
---|
780 | if (planePoly->Valid())
|
---|
781 | polys.push_back(planePoly);
|
---|
782 | else
|
---|
783 | DEL_PTR(planePoly);
|
---|
784 |
|
---|
785 | // push the children on the stack
|
---|
786 | tStack.push(BspSplitData(interior->GetFront(), tData.mPlanes, tData.mSides, true));
|
---|
787 | tStack.push(BspSplitData(interior->GetBack(), tData.mPlanes, tData.mSides, false));
|
---|
788 | }
|
---|
789 | }
|
---|
790 | ExportPolygons(polys);
|
---|
791 | CLEAR_CONTAINER(polys);
|
---|
792 | }
|
---|
793 |
|
---|
794 | void X3dExporter::ExportBspSplitPlanes(const BspTree &tree)
|
---|
795 | {
|
---|
796 | std::stack<BspNode *> tStack;
|
---|
797 |
|
---|
798 | tStack.push(tree.GetRoot());
|
---|
799 |
|
---|
800 | PolygonContainer polys;
|
---|
801 |
|
---|
802 | while (!tStack.empty())
|
---|
803 | {
|
---|
804 | // filter polygons donw the tree
|
---|
805 | BspNode *node = tStack.top();
|
---|
806 | tStack.pop();
|
---|
807 |
|
---|
808 | if (!node->IsLeaf())
|
---|
809 | {
|
---|
810 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
811 |
|
---|
812 | // bounded plane is added to the polygons
|
---|
813 | polys.push_back(tree.GetBoundingBox().CrossSection(*interior->GetPlane()));
|
---|
814 |
|
---|
815 | // push the children on the stack
|
---|
816 | tStack.push(interior->GetBack());
|
---|
817 | tStack.push(interior->GetFront());
|
---|
818 | }
|
---|
819 | }
|
---|
820 |
|
---|
821 | ExportPolygons(polys);
|
---|
822 | CLEAR_CONTAINER(polys);
|
---|
823 | }
|
---|