1 | #include <stack>
|
---|
2 | #include "common.h"
|
---|
3 | #include "SceneGraph.h"
|
---|
4 | #include "VrmlExporter.h"
|
---|
5 | #include "Mesh.h"
|
---|
6 | #include "KdTree.h"
|
---|
7 | #include "ViewCellBsp.h"
|
---|
8 | #include "ViewCell.h"
|
---|
9 | #include "Polygon3.h"
|
---|
10 | #include "VssRay.h"
|
---|
11 | #include "VssTree.h"
|
---|
12 | #include "VspBspTree.h"
|
---|
13 | #include "RssTree.h"
|
---|
14 | #include "Beam.h"
|
---|
15 |
|
---|
16 | namespace GtpVisibilityPreprocessor {
|
---|
17 |
|
---|
18 |
|
---|
19 | VrmlExporter::VrmlExporter(const string filename):Exporter(filename)
|
---|
20 | {
|
---|
21 | stream.open(mFilename.c_str());
|
---|
22 | stream<<"#VRML V2.0 utf8"<<endl;
|
---|
23 | }
|
---|
24 |
|
---|
25 |
|
---|
26 | VrmlExporter::~VrmlExporter()
|
---|
27 | {
|
---|
28 | stream.close();
|
---|
29 | }
|
---|
30 |
|
---|
31 |
|
---|
32 | bool VrmlExporter::ExportRays(const RayContainer &rays,
|
---|
33 | const float length,
|
---|
34 | const RgbColor &color)
|
---|
35 | {
|
---|
36 | if (rays.empty())
|
---|
37 | return false;
|
---|
38 | RayContainer::const_iterator ri = rays.begin();
|
---|
39 |
|
---|
40 | stream<<"Shape {"<<endl;
|
---|
41 | stream<<"appearance Appearance {"<<endl;
|
---|
42 | stream << "material Material { diffuseColor=\"" << color.r << " " << color.g << " "
|
---|
43 | << color.b << "}" << endl;
|
---|
44 | stream<<"}"<<endl;
|
---|
45 |
|
---|
46 | stream<<"geometry IndexedLineSet { coordIndex [" << endl;
|
---|
47 |
|
---|
48 | int index = 0;
|
---|
49 | for (; ri != rays.end(); ri++)
|
---|
50 | {
|
---|
51 | stream << index << " " << index + 1 << " -1\n";
|
---|
52 | index += 2;
|
---|
53 | }
|
---|
54 |
|
---|
55 | stream << "]"<<endl;
|
---|
56 |
|
---|
57 | stream << "coord Coordinate { point [" << endl;
|
---|
58 |
|
---|
59 | ri = rays.begin();
|
---|
60 | for (; ri != rays.end(); ri++)
|
---|
61 | {
|
---|
62 | Vector3 a = (*ri)->GetLoc();
|
---|
63 |
|
---|
64 | Vector3 b;
|
---|
65 |
|
---|
66 | if (length < 0)
|
---|
67 | {
|
---|
68 | b = (*ri)->GetLoc() - length*(*ri)->GetDir();
|
---|
69 | }
|
---|
70 | else
|
---|
71 | {
|
---|
72 | if ((*ri)->intersections.empty())
|
---|
73 | b = (*ri)->GetLoc() + length*(*ri)->GetDir();
|
---|
74 | else
|
---|
75 | b = (*ri)->Extrap((*ri)->intersections[0].mT);
|
---|
76 | }
|
---|
77 |
|
---|
78 | stream << a.x << " " << a.y << " " << a.z << " ,";
|
---|
79 | stream << b.x << " " << b.y << " " << b.z << " ,\n";
|
---|
80 | }
|
---|
81 | stream << "]" << endl;
|
---|
82 | stream << "}" << endl;
|
---|
83 | stream << "}" << endl;
|
---|
84 | stream << "}" << endl;
|
---|
85 |
|
---|
86 | return true;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | bool VrmlExporter::ExportRays(const VssRayContainer &rays,
|
---|
91 | const RgbColor &color)
|
---|
92 | {
|
---|
93 |
|
---|
94 | if (rays.empty())
|
---|
95 | return false;
|
---|
96 |
|
---|
97 | stream << "Shape {" << endl;
|
---|
98 | stream << "appearance Appearance {" << endl;
|
---|
99 | stream << "material Material {" << endl;
|
---|
100 | stream << "diffuseColor " << color.r << " " << color.g << " " << color.b << endl;
|
---|
101 | stream << "}" << endl; // end material
|
---|
102 | stream << "}" << endl; // end appearance
|
---|
103 |
|
---|
104 | stream << "geometry IndexedLineSet { coordIndex [" << endl;
|
---|
105 |
|
---|
106 | int index = 0;
|
---|
107 | VssRayContainer::const_iterator ri = rays.begin();
|
---|
108 |
|
---|
109 | for (; ri != rays.end(); ++ ri)
|
---|
110 | {
|
---|
111 | stream << index << " " << index + 1 << " -1\n";
|
---|
112 | index += 2 ;
|
---|
113 | }
|
---|
114 |
|
---|
115 | stream << "]" << endl;
|
---|
116 |
|
---|
117 | stream << "coord Coordinate { point [" << endl;
|
---|
118 |
|
---|
119 | for (ri = rays.begin(); ri != rays.end(); ++ ri)
|
---|
120 | {
|
---|
121 | const Vector3 a = (*ri)->GetOrigin();
|
---|
122 | const Vector3 b = (*ri)->mTerminationObject ? (*ri)->GetTermination() : a + 1000 * Normalize((*ri)->GetDir());
|
---|
123 | //const Vector3 b = (*ri)->GetTermination(); // matt: change back!!
|
---|
124 |
|
---|
125 | stream << a.x << " " << a.y << " " << a.z << " ,";
|
---|
126 | stream << b.x << " " << b.y << " " << b.z << " ,\n";
|
---|
127 | }
|
---|
128 |
|
---|
129 | stream << "]" << endl;
|
---|
130 | stream << "}" << endl;
|
---|
131 | stream << "}" << endl;
|
---|
132 | stream << "}" << endl;
|
---|
133 | //stream << "}" << endl;
|
---|
134 |
|
---|
135 | return true;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | void VrmlExporter::ExportSceneNode(SceneGraphNode *node)
|
---|
140 | {
|
---|
141 | stream << "Group {" << endl;
|
---|
142 |
|
---|
143 | SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
|
---|
144 |
|
---|
145 | for (; ni != node->mChildren.end(); ni++)
|
---|
146 | {
|
---|
147 | ExportSceneNode(*ni);
|
---|
148 | }
|
---|
149 | ObjectContainer::const_iterator mi = node->mGeometry.begin();
|
---|
150 |
|
---|
151 | for (; mi != node->mGeometry.end(); mi++)
|
---|
152 | {
|
---|
153 | // export the transform...
|
---|
154 | ExportIntersectable(*mi);
|
---|
155 | }
|
---|
156 |
|
---|
157 | stream<<"</Group>"<<endl;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | void VrmlExporter::ExportIntersectable(Intersectable *object)
|
---|
162 | {
|
---|
163 | switch (object->Type())
|
---|
164 | {
|
---|
165 | case Intersectable::MESH_INSTANCE:
|
---|
166 | ExportMeshInstance((MeshInstance *)object);
|
---|
167 | break;
|
---|
168 | case Intersectable::TRANSFORMED_MESH_INSTANCE:
|
---|
169 | ExportTransformedMeshInstance(dynamic_cast<TransformedMeshInstance *>(object));
|
---|
170 | break;
|
---|
171 | case Intersectable::VIEW_CELL:
|
---|
172 | ExportViewCell((ViewCell *)object);
|
---|
173 | break;
|
---|
174 | default:
|
---|
175 | cerr << "Sorry the export for object not yet defined" << endl;
|
---|
176 | break;
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | void VrmlExporter::ExportMeshInstance(MeshInstance *object)
|
---|
182 | {
|
---|
183 | // $$JB$$
|
---|
184 | // in the future check whether the mesh was not already exported
|
---|
185 | // and use a reference to the that mesh instead
|
---|
186 | ExportMesh(object->GetMesh());
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | void VrmlExporter::ExportTransformedMeshInstance(TransformedMeshInstance *mi)
|
---|
191 | {
|
---|
192 | Mesh mesh(*mi->GetMesh());
|
---|
193 |
|
---|
194 | Matrix4x4 m;
|
---|
195 | mi->GetWorldTransform(m);
|
---|
196 | mesh.ApplyTransformation(m);
|
---|
197 |
|
---|
198 | ExportMesh(&mesh);
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | void VrmlExporter::ExportViewCells(const ViewCellContainer &viewCells)
|
---|
203 | {
|
---|
204 | ViewCellContainer::const_iterator it, it_end = viewCells.end();
|
---|
205 |
|
---|
206 | for (it = viewCells.begin(); it != it_end; ++ it)
|
---|
207 | {
|
---|
208 | ExportViewCell(*it);
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | void VrmlExporter::ExportBspLeaves(const BspTree &tree, const int maxPvs)
|
---|
214 | {
|
---|
215 | stack<pair<BspNode *, BspNodeGeometry *> > tStack;
|
---|
216 | ViewCell::NewMail();
|
---|
217 |
|
---|
218 | BspNodeGeometry *geom = new BspNodeGeometry();
|
---|
219 | tree.ConstructGeometry(tree.GetRoot(), *geom);
|
---|
220 |
|
---|
221 | tStack.push(pair<BspNode *, BspNodeGeometry *>(tree.GetRoot(), geom));
|
---|
222 |
|
---|
223 | if (maxPvs)
|
---|
224 | mUseForcedMaterial = true;
|
---|
225 |
|
---|
226 | while (!tStack.empty())
|
---|
227 | {
|
---|
228 | BspNode *node = tStack.top().first;
|
---|
229 | BspNodeGeometry *cell = tStack.top().second;
|
---|
230 | tStack.pop();
|
---|
231 |
|
---|
232 | if (!node->IsLeaf())
|
---|
233 | {
|
---|
234 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
235 |
|
---|
236 | BspNodeGeometry *front = new BspNodeGeometry();
|
---|
237 | BspNodeGeometry *back = new BspNodeGeometry();
|
---|
238 |
|
---|
239 | cell->SplitGeometry(*front,
|
---|
240 | *back,
|
---|
241 | interior->GetPlane(),
|
---|
242 | tree.GetBoundingBox(),
|
---|
243 | tree.GetEpsilon());
|
---|
244 |
|
---|
245 | tStack.push(pair<BspNode *, BspNodeGeometry *>(interior->GetFront(), front));
|
---|
246 | tStack.push(pair<BspNode *, BspNodeGeometry *>(interior->GetBack(), back));
|
---|
247 | }
|
---|
248 | else
|
---|
249 | {
|
---|
250 | if (maxPvs)
|
---|
251 | {
|
---|
252 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
253 |
|
---|
254 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
255 | const float importance = (float)leaf->GetViewCell()->GetPvs().GetSize() / (float)maxPvs;
|
---|
256 |
|
---|
257 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
258 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
259 | }
|
---|
260 |
|
---|
261 | ExportPolygons(cell->GetPolys());
|
---|
262 | }
|
---|
263 |
|
---|
264 | DEL_PTR(cell);
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 | void VrmlExporter::ExportViewCell(ViewCell *viewCell)
|
---|
270 | {
|
---|
271 | if (viewCell->GetMesh())
|
---|
272 | {
|
---|
273 | ExportMesh(viewCell->GetMesh());
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | void VrmlExporter::ExportMesh(Mesh *mesh)
|
---|
279 | {
|
---|
280 | stream << "Shape {" << endl;
|
---|
281 | stream << "appearance Appearance {" << endl;
|
---|
282 |
|
---|
283 | // $$ tmp -> random material
|
---|
284 |
|
---|
285 | float r, g, b;
|
---|
286 |
|
---|
287 | if (mUseForcedMaterial)
|
---|
288 | {
|
---|
289 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
290 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
291 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
292 |
|
---|
293 | }
|
---|
294 | else if (mesh->mMaterial)
|
---|
295 | {
|
---|
296 | r = mesh->mMaterial->mDiffuseColor.r;
|
---|
297 | g = mesh->mMaterial->mDiffuseColor.g;
|
---|
298 | b = mesh->mMaterial->mDiffuseColor.b;
|
---|
299 | }
|
---|
300 | else
|
---|
301 | {
|
---|
302 | r = RandomValue(0.5, 1.0);
|
---|
303 | g = RandomValue(0.5, 1.0);
|
---|
304 | b = RandomValue(0.5, 1.0);
|
---|
305 | }
|
---|
306 |
|
---|
307 | stream << "material Material {" << endl;
|
---|
308 | stream << "diffuseColor " << r << " " << g << " " << b << endl;
|
---|
309 | stream << "specularColor 0.0 0.0 0.0" << endl;
|
---|
310 |
|
---|
311 | stream << "}" << endl; // end material
|
---|
312 | stream << "}" << endl; // end apperance
|
---|
313 |
|
---|
314 |
|
---|
315 | // wireframe modes => set indexed lines
|
---|
316 | if (mWireframe)
|
---|
317 | {
|
---|
318 | stream << "geometry IndexedLineSet {" << endl;
|
---|
319 | }
|
---|
320 | else
|
---|
321 | {
|
---|
322 | //stream<<"<IndexedFaceSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
323 | stream << "geometry IndexedFaceSet {" << endl;
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | stream << "coordIndex [" << endl;
|
---|
328 |
|
---|
329 | FaceContainer::const_iterator fi = mesh->mFaces.begin();
|
---|
330 |
|
---|
331 | int index = 0;
|
---|
332 |
|
---|
333 | for (; fi != mesh->mFaces.end(); fi++)
|
---|
334 | {
|
---|
335 | Face *face = *fi;
|
---|
336 |
|
---|
337 | VertexIndexContainer::const_iterator vi = face->mVertexIndices.begin();
|
---|
338 |
|
---|
339 | for (; vi != face->mVertexIndices.end(); ++ vi)
|
---|
340 | {
|
---|
341 | stream << *vi << " ";
|
---|
342 | }
|
---|
343 |
|
---|
344 | if (mWireframe) // final line to finish polygon
|
---|
345 | {
|
---|
346 | stream << (*face->mVertexIndices.begin()) << " ";
|
---|
347 | }
|
---|
348 |
|
---|
349 | stream << "-1" << endl;
|
---|
350 | }
|
---|
351 |
|
---|
352 | stream << "]" << endl; // end coordIndex
|
---|
353 |
|
---|
354 |
|
---|
355 | stream << "coord Coordinate {" << endl;
|
---|
356 | stream << "point [" << endl;
|
---|
357 |
|
---|
358 | VertexContainer::const_iterator vi = mesh->mVertices.begin();
|
---|
359 |
|
---|
360 | for (; vi != mesh->mVertices.end(); vi++)
|
---|
361 | {
|
---|
362 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
363 | stream << "," << endl;
|
---|
364 | }
|
---|
365 |
|
---|
366 | stream << "]" << endl; // end point
|
---|
367 | stream << "}" << endl; // end coord
|
---|
368 | stream << "}" << endl; // end geometry
|
---|
369 |
|
---|
370 | stream << "}" << endl; // end shape
|
---|
371 | }
|
---|
372 |
|
---|
373 |
|
---|
374 | void VrmlExporter::ExportPolygon(Polygon3 *poly)
|
---|
375 | {
|
---|
376 | stream << "Shape {" << endl;
|
---|
377 | stream << "appearance Appearance {" << endl;
|
---|
378 |
|
---|
379 | // $$ tmp -> random material
|
---|
380 | float r, g, b;
|
---|
381 |
|
---|
382 | if (mUseForcedMaterial)
|
---|
383 | {
|
---|
384 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
385 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
386 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
387 | }
|
---|
388 | else if (poly->mMaterial)
|
---|
389 | {
|
---|
390 | r = poly->mMaterial->mDiffuseColor.r;
|
---|
391 | g = poly->mMaterial->mDiffuseColor.g;
|
---|
392 | b = poly->mMaterial->mDiffuseColor.b;
|
---|
393 | }
|
---|
394 | else
|
---|
395 | {
|
---|
396 | r = RandomValue(0.5, 1.0);
|
---|
397 | g = RandomValue(0.5, 1.0);
|
---|
398 | b = RandomValue(0.5, 1.0);
|
---|
399 | }
|
---|
400 |
|
---|
401 | stream << "material Material {" << endl;
|
---|
402 | stream << "diffuseColor " << r << " " << g << " " << b << endl;
|
---|
403 | stream << "specularColor 0.0 0.0 0.0" << endl;
|
---|
404 |
|
---|
405 | stream << "}" << endl; // end material
|
---|
406 | stream << "}" << endl; // end apperance
|
---|
407 |
|
---|
408 |
|
---|
409 | // wireframe modes => set indexed lines
|
---|
410 | if (mWireframe)
|
---|
411 | {
|
---|
412 | stream << "geometry IndexedLineSet {" << endl;
|
---|
413 | }
|
---|
414 | else
|
---|
415 | {
|
---|
416 | //stream<<"<IndexedFaceSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
417 | stream << "geometry IndexedFaceSet {" << endl;
|
---|
418 | }
|
---|
419 |
|
---|
420 |
|
---|
421 | stream << "coordIndex [" << endl;
|
---|
422 |
|
---|
423 |
|
---|
424 | int index = 0;
|
---|
425 |
|
---|
426 | VertexContainer::const_iterator vi;
|
---|
427 |
|
---|
428 | for (index = 0; index < (int)poly->mVertices.size(); ++ index)
|
---|
429 | {
|
---|
430 | stream << index << " ";
|
---|
431 | }
|
---|
432 |
|
---|
433 | if (mWireframe) // final line to finish polygon
|
---|
434 | {
|
---|
435 | stream << "0 ";
|
---|
436 | }
|
---|
437 |
|
---|
438 | stream << "-1" << endl;
|
---|
439 |
|
---|
440 | stream << "]" << endl; // end coordIndex
|
---|
441 |
|
---|
442 |
|
---|
443 | stream << "coord Coordinate {" << endl;
|
---|
444 | stream << "point [" << endl;
|
---|
445 |
|
---|
446 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
447 | {
|
---|
448 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
449 | stream << "," << endl;
|
---|
450 | }
|
---|
451 |
|
---|
452 | stream << "]" << endl; // end point
|
---|
453 | stream << "}" << endl; // end coord
|
---|
454 | stream << "}" << endl; // end geometry
|
---|
455 |
|
---|
456 | stream << "}" << endl; // end shape
|
---|
457 | }
|
---|
458 |
|
---|
459 |
|
---|
460 | void VrmlExporter::ExportPolygons(const PolygonContainer &polys)
|
---|
461 | {
|
---|
462 | stream << "Shape {" << endl;
|
---|
463 | stream << "appearance Appearance {" << endl;
|
---|
464 |
|
---|
465 | // $$ tmp -> random material
|
---|
466 |
|
---|
467 | float r, g, b;
|
---|
468 |
|
---|
469 | if (mUseForcedMaterial)
|
---|
470 | {
|
---|
471 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
472 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
473 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
474 | }
|
---|
475 | else
|
---|
476 | {
|
---|
477 | r = RandomValue(0.5, 1.0);
|
---|
478 | g = RandomValue(0.5, 1.0);
|
---|
479 | b = RandomValue(0.5, 1.0);
|
---|
480 | }
|
---|
481 |
|
---|
482 | stream << "material Material {" << endl;
|
---|
483 | stream << "diffuseColor " << r << " " << g << " " << b << endl;
|
---|
484 | stream << "specularColor 0.0 0.0 0.0" << endl;
|
---|
485 |
|
---|
486 | stream << "}" << endl; // end material
|
---|
487 | stream << "}" << endl; // end apperance
|
---|
488 |
|
---|
489 | // wireframe modes => set indexed lines
|
---|
490 | if (mWireframe)
|
---|
491 | {
|
---|
492 | stream << "geometry IndexedLineSet {" << endl;
|
---|
493 | }
|
---|
494 | else
|
---|
495 | {
|
---|
496 | //stream<<"<IndexedFaceSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
497 | stream << "geometry IndexedFaceSet {" << endl;
|
---|
498 | }
|
---|
499 |
|
---|
500 | stream << "coordIndex [" << endl;
|
---|
501 |
|
---|
502 | int index = 0;
|
---|
503 |
|
---|
504 | PolygonContainer::const_iterator pit;
|
---|
505 |
|
---|
506 | VertexContainer::const_iterator vi;
|
---|
507 |
|
---|
508 | for (pit = polys.begin(); pit != polys.end(); ++ pit)
|
---|
509 | {
|
---|
510 | Polygon3 *poly = *pit;
|
---|
511 | int startIdx = index;
|
---|
512 |
|
---|
513 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
514 | {
|
---|
515 | stream << index ++ << " ";
|
---|
516 | }
|
---|
517 |
|
---|
518 | stream << startIdx << " ";// finish line
|
---|
519 | stream << "-1" << endl;
|
---|
520 | }
|
---|
521 |
|
---|
522 | stream << "]" << endl; // end coordIndex
|
---|
523 |
|
---|
524 | stream << "coord Coordinate {" << endl;
|
---|
525 | stream << "point [" << endl;
|
---|
526 |
|
---|
527 |
|
---|
528 | for (pit = polys.begin(); pit != polys.end(); ++ pit)
|
---|
529 | {
|
---|
530 | Polygon3 *poly = *pit;
|
---|
531 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
532 | {
|
---|
533 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
534 | stream << "," << endl;
|
---|
535 | }
|
---|
536 | }
|
---|
537 |
|
---|
538 | stream << "]" << endl; // end point
|
---|
539 | stream << "}" << endl; // end coord
|
---|
540 | stream << "}" << endl; // end geometry
|
---|
541 |
|
---|
542 | stream << "}" << endl; // end shape
|
---|
543 | }
|
---|
544 |
|
---|
545 |
|
---|
546 | bool VrmlExporter::ExportBox(const AxisAlignedBox3 &box)
|
---|
547 | {
|
---|
548 | Mesh *mesh = new Mesh;
|
---|
549 |
|
---|
550 | // add 8 vertices of the box
|
---|
551 | int index = (int)mesh->mVertices.size();
|
---|
552 |
|
---|
553 | for (int i=0; i < 8; i++)
|
---|
554 | {
|
---|
555 | Vector3 v;
|
---|
556 | box.GetVertex(i, v);
|
---|
557 | mesh->mVertices.push_back(v);
|
---|
558 | }
|
---|
559 |
|
---|
560 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
561 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
562 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
563 |
|
---|
564 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
565 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
566 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
567 |
|
---|
568 | ExportMesh(mesh);
|
---|
569 |
|
---|
570 | delete mesh;
|
---|
571 |
|
---|
572 | return true;
|
---|
573 | }
|
---|
574 |
|
---|
575 | bool VrmlExporter::ExportBspTree(const BspTree &tree)
|
---|
576 | {
|
---|
577 | if (mExportRayDensity)
|
---|
578 | {
|
---|
579 | return ExportBspTreeRayDensity(tree);
|
---|
580 | }
|
---|
581 |
|
---|
582 | bool savedWireframe = mWireframe;
|
---|
583 |
|
---|
584 | SetWireframe();
|
---|
585 |
|
---|
586 | ExportBox(tree.GetBoundingBox());
|
---|
587 |
|
---|
588 | if (!savedWireframe)
|
---|
589 | SetFilled();
|
---|
590 |
|
---|
591 | // export view cells
|
---|
592 | ExportBspLeaves(tree);
|
---|
593 |
|
---|
594 | return true;
|
---|
595 | }
|
---|
596 |
|
---|
597 | #if 0
|
---|
598 | bool VrmlExporter::ExportVspOspTree(const VspOspTree &tree,
|
---|
599 | const int maxPvs)
|
---|
600 | {
|
---|
601 | stack<VspKdNode *> tStack;
|
---|
602 |
|
---|
603 | tStack.push(tree.GetRoot());
|
---|
604 |
|
---|
605 | //Mesh *mesh = new Mesh;
|
---|
606 |
|
---|
607 | if (maxPvs)
|
---|
608 | mUseForcedMaterial = true;
|
---|
609 |
|
---|
610 | while (!tStack.empty())
|
---|
611 | {
|
---|
612 | VspKdNode *node = tStack.top();
|
---|
613 |
|
---|
614 | tStack.pop();
|
---|
615 |
|
---|
616 | if (node->IsLeaf())
|
---|
617 | {
|
---|
618 | AxisAlignedBox3 box = tree.GetBBox(node);
|
---|
619 |
|
---|
620 | Mesh *mesh = new Mesh;
|
---|
621 |
|
---|
622 | // add 6 vertices of the box
|
---|
623 | int index = (int)mesh->mVertices.size();
|
---|
624 |
|
---|
625 | for (int i=0; i < 8; ++ i)
|
---|
626 | {
|
---|
627 | Vector3 v;
|
---|
628 | box.GetVertex(i, v);
|
---|
629 | mesh->mVertices.push_back(v);
|
---|
630 | }
|
---|
631 |
|
---|
632 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
633 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
634 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
635 |
|
---|
636 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
637 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
638 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
639 |
|
---|
640 | if (maxPvs)
|
---|
641 | {
|
---|
642 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node);
|
---|
643 |
|
---|
644 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
645 |
|
---|
646 | leaf->UpdatePvsSize();
|
---|
647 |
|
---|
648 | const float importance = (float)leaf->GetPvsSize() / (float)maxPvs;
|
---|
649 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
650 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
651 | }
|
---|
652 |
|
---|
653 | ExportMesh(mesh);
|
---|
654 | DEL_PTR(mesh);
|
---|
655 | }
|
---|
656 | else
|
---|
657 | {
|
---|
658 | VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node);
|
---|
659 | tStack.push(interior->GetFront());
|
---|
660 | tStack.push(interior->GetBack());
|
---|
661 | }
|
---|
662 | }
|
---|
663 |
|
---|
664 | //ExportMesh(mesh);
|
---|
665 | //DEL_PTR(mesh);
|
---|
666 |
|
---|
667 | return true;
|
---|
668 | }
|
---|
669 | #endif
|
---|
670 |
|
---|
671 | bool VrmlExporter::ExportKdTree(const KdTree &tree)
|
---|
672 | {
|
---|
673 | if (mExportRayDensity)
|
---|
674 | {
|
---|
675 | return ExportKdTreeRayDensity(tree);
|
---|
676 | }
|
---|
677 |
|
---|
678 | stack<KdNode *> tStack;
|
---|
679 |
|
---|
680 | tStack.push(tree.GetRoot());
|
---|
681 |
|
---|
682 | Mesh *mesh = new Mesh;
|
---|
683 |
|
---|
684 | while (!tStack.empty())
|
---|
685 | {
|
---|
686 | KdNode *node = tStack.top();
|
---|
687 |
|
---|
688 | tStack.pop();
|
---|
689 |
|
---|
690 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
691 | // add 6 vertices of the box
|
---|
692 | int index = (int)mesh->mVertices.size();
|
---|
693 |
|
---|
694 | for (int i=0; i < 8; i++)
|
---|
695 | {
|
---|
696 | Vector3 v;
|
---|
697 | box.GetVertex(i, v);
|
---|
698 | mesh->mVertices.push_back(v);
|
---|
699 | }
|
---|
700 |
|
---|
701 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
702 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
703 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
704 |
|
---|
705 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
706 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
707 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
708 |
|
---|
709 | if (!node->IsLeaf())
|
---|
710 | {
|
---|
711 | KdInterior *interior = (KdInterior *)node;
|
---|
712 | tStack.push(interior->mFront);
|
---|
713 | tStack.push(interior->mBack);
|
---|
714 | }
|
---|
715 | }
|
---|
716 |
|
---|
717 | ExportMesh(mesh);
|
---|
718 |
|
---|
719 | delete mesh;
|
---|
720 | return true;
|
---|
721 | }
|
---|
722 |
|
---|
723 | bool VrmlExporter::ExportVssTree(const VssTree &tree)
|
---|
724 | {
|
---|
725 | stack<VssTreeNode *> tStack;
|
---|
726 |
|
---|
727 | tStack.push(tree.GetRoot());
|
---|
728 |
|
---|
729 | Mesh *mesh = new Mesh;
|
---|
730 | VssRayContainer rays;
|
---|
731 |
|
---|
732 | while (!tStack.empty())
|
---|
733 | {
|
---|
734 | VssTreeNode *node = tStack.top();
|
---|
735 |
|
---|
736 | tStack.pop();
|
---|
737 |
|
---|
738 | if (!node->IsLeaf())
|
---|
739 | {
|
---|
740 | VssTreeInterior *interior = (VssTreeInterior *)node;
|
---|
741 |
|
---|
742 | tStack.push(interior->front);
|
---|
743 | tStack.push(interior->back);
|
---|
744 | }
|
---|
745 | else
|
---|
746 | {
|
---|
747 | VssTreeLeaf *leaf = (VssTreeLeaf *)node;
|
---|
748 | AxisAlignedBox3 box;
|
---|
749 | box = tree.GetBBox(leaf);
|
---|
750 | IncludeBoxInMesh(box, *mesh);
|
---|
751 |
|
---|
752 | if (tree.ValidLeaf(leaf))
|
---|
753 | {
|
---|
754 | Vector3 origin = box.Center();
|
---|
755 | box = tree.GetDirBBox(leaf);
|
---|
756 | VssRay *ray;
|
---|
757 |
|
---|
758 | const int indices[][2] = {{0,0}, {0,1}, {1,1}, {1,0}};
|
---|
759 | MeshInstance dummy(mesh);
|
---|
760 | for (int i=0; i < 4; i++)
|
---|
761 | {
|
---|
762 | // Vector3 v = box.GetVertex(indices[i][0], indices[i][1], 0);
|
---|
763 | Vector3 v = box.Center();
|
---|
764 |
|
---|
765 | Vector3 direction = VssRay::GetDirection(v.x, v.y);
|
---|
766 | if (Magnitude(direction) > Limits::Small)
|
---|
767 | {
|
---|
768 | direction.Normalize();
|
---|
769 | }
|
---|
770 | else
|
---|
771 | {
|
---|
772 | direction = Vector3(0, 1, 0);
|
---|
773 | }
|
---|
774 |
|
---|
775 | const float k = 100.0f * leaf->GetImportance();
|
---|
776 |
|
---|
777 |
|
---|
778 | // get 4 corners of the ray directions
|
---|
779 | ray = new VssRay(origin, origin + (direction * k), NULL, &dummy);
|
---|
780 | rays.push_back(ray);
|
---|
781 | }
|
---|
782 | }
|
---|
783 | }
|
---|
784 | }
|
---|
785 |
|
---|
786 | ExportMesh(mesh);
|
---|
787 | ExportRays(rays);
|
---|
788 | CLEAR_CONTAINER(rays);
|
---|
789 |
|
---|
790 | delete mesh;
|
---|
791 | return true;
|
---|
792 | }
|
---|
793 |
|
---|
794 |
|
---|
795 | bool VrmlExporter::ExportVssTree2(const VssTree &tree,
|
---|
796 | const Vector3 direction)
|
---|
797 | {
|
---|
798 | stack<VssTreeNode *> tStack;
|
---|
799 |
|
---|
800 | mUseForcedMaterial = true;
|
---|
801 |
|
---|
802 | Vector3 dirParam;
|
---|
803 |
|
---|
804 | dirParam.x = VssRay::GetDirParam(0, Normalize(direction));
|
---|
805 | dirParam.y = VssRay::GetDirParam(1, Normalize(direction));
|
---|
806 |
|
---|
807 | float maxImportance = 0.0f;
|
---|
808 |
|
---|
809 | tStack.push(tree.GetRoot());
|
---|
810 |
|
---|
811 | while (!tStack.empty())
|
---|
812 | {
|
---|
813 | VssTreeNode *node = tStack.top();
|
---|
814 | tStack.pop();
|
---|
815 |
|
---|
816 | if (!node->IsLeaf())
|
---|
817 | {
|
---|
818 | VssTreeInterior *interior = (VssTreeInterior *)node;
|
---|
819 |
|
---|
820 | if (interior->axis < 3)
|
---|
821 | {
|
---|
822 | tStack.push(interior->front);
|
---|
823 | tStack.push(interior->back);
|
---|
824 | }
|
---|
825 | else
|
---|
826 | {
|
---|
827 | if (dirParam[interior->axis-3] < interior->position)
|
---|
828 | tStack.push(interior->back);
|
---|
829 | else
|
---|
830 | tStack.push(interior->front);
|
---|
831 | }
|
---|
832 | }
|
---|
833 | else
|
---|
834 | {
|
---|
835 | VssTreeLeaf *leaf = (VssTreeLeaf *)node;
|
---|
836 |
|
---|
837 | if (tree.ValidLeaf(leaf))
|
---|
838 | {
|
---|
839 | float i = leaf->GetImportance();
|
---|
840 |
|
---|
841 | if (i > maxImportance)
|
---|
842 | maxImportance = i;
|
---|
843 | }
|
---|
844 | }
|
---|
845 | }
|
---|
846 |
|
---|
847 | tStack.push(tree.GetRoot());
|
---|
848 |
|
---|
849 | while (!tStack.empty())
|
---|
850 | {
|
---|
851 | VssTreeNode *node = tStack.top();
|
---|
852 | tStack.pop();
|
---|
853 |
|
---|
854 | if (!node->IsLeaf())
|
---|
855 | {
|
---|
856 | VssTreeInterior *interior = (VssTreeInterior *)node;
|
---|
857 |
|
---|
858 | if (interior->axis < 3)
|
---|
859 | {
|
---|
860 | tStack.push(interior->front);
|
---|
861 | tStack.push(interior->back);
|
---|
862 | }
|
---|
863 | else
|
---|
864 | {
|
---|
865 | if (dirParam[interior->axis-3] < interior->position)
|
---|
866 | tStack.push(interior->back);
|
---|
867 | else
|
---|
868 | tStack.push(interior->front);
|
---|
869 | }
|
---|
870 | }
|
---|
871 | else
|
---|
872 | {
|
---|
873 | VssTreeLeaf *leaf = (VssTreeLeaf *)node;
|
---|
874 |
|
---|
875 | if (tree.ValidLeaf(leaf))
|
---|
876 | {
|
---|
877 | AxisAlignedBox3 box;
|
---|
878 | box = tree.GetBBox(leaf);
|
---|
879 | Mesh *mesh = new Mesh;
|
---|
880 | IncludeBoxInMesh(box, *mesh);
|
---|
881 |
|
---|
882 | // get 4 corners of the ray directions
|
---|
883 |
|
---|
884 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
885 | mForcedMaterial.mDiffuseColor.r = leaf->GetImportance()/maxImportance;
|
---|
886 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
887 |
|
---|
888 | ExportMesh(mesh);
|
---|
889 | delete mesh;
|
---|
890 | }
|
---|
891 | }
|
---|
892 | }
|
---|
893 |
|
---|
894 | mUseForcedMaterial = false;
|
---|
895 |
|
---|
896 | return true;
|
---|
897 | }
|
---|
898 |
|
---|
899 |
|
---|
900 | bool VrmlExporter::ExportBspTreeRayDensity(const BspTree &tree)
|
---|
901 | {
|
---|
902 | stack<BspNode *> tStack;
|
---|
903 |
|
---|
904 | tStack.push(tree.GetRoot());
|
---|
905 |
|
---|
906 | bool fm = mUseForcedMaterial;
|
---|
907 |
|
---|
908 | mUseForcedMaterial = true;
|
---|
909 |
|
---|
910 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
911 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
912 |
|
---|
913 | while (!tStack.empty())
|
---|
914 | {
|
---|
915 | BspNode *node = tStack.top();
|
---|
916 | tStack.pop();
|
---|
917 |
|
---|
918 | if (node->IsLeaf())
|
---|
919 | {
|
---|
920 | ViewCell *vc = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
921 | #if 0
|
---|
922 | // set the mesh material according to the ray density
|
---|
923 | if (vc->mPassingRays.mRays)
|
---|
924 | {
|
---|
925 | float importance =
|
---|
926 | vc->mPassingRays.mContributions / (float)vc->mPassingRays.mRays;
|
---|
927 |
|
---|
928 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
929 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
930 | ExportViewCell(vc);
|
---|
931 |
|
---|
932 | }
|
---|
933 | #endif
|
---|
934 | }
|
---|
935 | else
|
---|
936 | {
|
---|
937 | BspInterior *interior = (BspInterior *)node;
|
---|
938 | tStack.push(interior->GetFront());
|
---|
939 | tStack.push(interior->GetBack());
|
---|
940 | }
|
---|
941 | }
|
---|
942 |
|
---|
943 | // restore the state of forced material
|
---|
944 | mUseForcedMaterial = fm;
|
---|
945 |
|
---|
946 | return true;
|
---|
947 | }
|
---|
948 |
|
---|
949 |
|
---|
950 | bool VrmlExporter::ExportKdTreeRayDensity(const KdTree &tree)
|
---|
951 | {
|
---|
952 | stack<KdNode *> tStack;
|
---|
953 |
|
---|
954 | tStack.push(tree.GetRoot());
|
---|
955 |
|
---|
956 | bool fm = mUseForcedMaterial;
|
---|
957 |
|
---|
958 | mUseForcedMaterial = true;
|
---|
959 |
|
---|
960 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
961 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
962 |
|
---|
963 |
|
---|
964 | while (!tStack.empty())
|
---|
965 | {
|
---|
966 | KdNode *node = tStack.top();
|
---|
967 | tStack.pop();
|
---|
968 |
|
---|
969 | if (node->IsLeaf())
|
---|
970 | {
|
---|
971 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
972 | Mesh *mesh = new Mesh;
|
---|
973 |
|
---|
974 | // add 6 vertices of the box
|
---|
975 | int index = (int)mesh->mVertices.size();
|
---|
976 | for (int i=0; i < 8; i++)
|
---|
977 | {
|
---|
978 | Vector3 v;
|
---|
979 | box.GetVertex(i, v);
|
---|
980 | mesh->mVertices.push_back(v);
|
---|
981 | }
|
---|
982 |
|
---|
983 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
984 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
985 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
986 |
|
---|
987 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
988 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
989 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
990 |
|
---|
991 | // set the mesh material according to the ray density
|
---|
992 | KdLeaf *leaf = (KdLeaf *) node;
|
---|
993 |
|
---|
994 | if (leaf->mPassingRays.mRays)
|
---|
995 | {
|
---|
996 | const float importance =
|
---|
997 | leaf->mPassingRays.mContributions/(float)leaf->mPassingRays.mRays;
|
---|
998 | // float importance = leaf->mPassingRays.mContributions/1000.0f;
|
---|
999 | // float importance = leaf->mPassingRays.mRays/1000.0f;
|
---|
1000 | ///(float)leaf->mPassingRays.mRays;
|
---|
1001 | // mForcedMaterial.mDiffuseColor.r = log10(leaf->mPassingRays.mRays)/3.0f;
|
---|
1002 |
|
---|
1003 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
1004 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
1005 |
|
---|
1006 | ExportMesh(mesh);
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | delete mesh;
|
---|
1010 | }
|
---|
1011 | else
|
---|
1012 | {
|
---|
1013 | KdInterior *interior = (KdInterior *)node;
|
---|
1014 | tStack.push(interior->mFront);
|
---|
1015 | tStack.push(interior->mBack);
|
---|
1016 | }
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | // restore the state of forced material
|
---|
1020 | mUseForcedMaterial = fm;
|
---|
1021 |
|
---|
1022 | return true;
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 |
|
---|
1026 | struct BspSplitData
|
---|
1027 | {
|
---|
1028 | /// the current node
|
---|
1029 | BspNode *mNode;
|
---|
1030 |
|
---|
1031 | vector<Plane3> mPlanes;
|
---|
1032 | vector<bool> mSides;
|
---|
1033 | bool mIsFront;
|
---|
1034 | int mDepth;
|
---|
1035 |
|
---|
1036 | BspSplitData(BspNode *node):
|
---|
1037 | mNode(node), mIsFront(false), mDepth(0)
|
---|
1038 | {};
|
---|
1039 |
|
---|
1040 | BspSplitData(BspNode *node,
|
---|
1041 | vector<Plane3> planes,
|
---|
1042 | vector<bool> sides,
|
---|
1043 | const bool isFront,
|
---|
1044 | const int depth):
|
---|
1045 | mNode(node), mPlanes(planes), mSides(sides),
|
---|
1046 | mIsFront(isFront), mDepth(depth)
|
---|
1047 | {};
|
---|
1048 | };
|
---|
1049 |
|
---|
1050 |
|
---|
1051 | void VrmlExporter::ExportLeavesGeometry(const BspTree &tree,
|
---|
1052 | const vector<BspLeaf *> &leaves)
|
---|
1053 | {
|
---|
1054 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
1055 |
|
---|
1056 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
1057 | {
|
---|
1058 | BspNodeGeometry geom;
|
---|
1059 | tree.ConstructGeometry(*it, geom);
|
---|
1060 |
|
---|
1061 | ExportPolygons(geom.GetPolys());
|
---|
1062 | }
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 |
|
---|
1066 | void VrmlExporter::ExportBspNodeSplits(BspNode *root,
|
---|
1067 | const AxisAlignedBox3 &box,
|
---|
1068 | const bool exportDepth,
|
---|
1069 | const bool epsilon)
|
---|
1070 | {
|
---|
1071 | std::stack<BspSplitData> tStack;
|
---|
1072 |
|
---|
1073 | BspSplitData tData(root);
|
---|
1074 | tStack.push(tData);
|
---|
1075 |
|
---|
1076 | PolygonContainer polys;
|
---|
1077 | vector <int> depths;
|
---|
1078 |
|
---|
1079 | int maxDepth = 0;
|
---|
1080 |
|
---|
1081 | while (!tStack.empty())
|
---|
1082 | {
|
---|
1083 | // filter polygons donw the tree
|
---|
1084 | BspSplitData tData = tStack.top();
|
---|
1085 | tStack.pop();
|
---|
1086 |
|
---|
1087 | if (tData.mNode->IsLeaf())
|
---|
1088 | {
|
---|
1089 | if (tData.mDepth > maxDepth)
|
---|
1090 | maxDepth = tData.mDepth;
|
---|
1091 | }
|
---|
1092 | else
|
---|
1093 | {
|
---|
1094 | BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode);
|
---|
1095 |
|
---|
1096 | // add current side of split plane
|
---|
1097 | if (tData.mNode != root)
|
---|
1098 | tData.mSides.push_back(tData.mIsFront);
|
---|
1099 |
|
---|
1100 | // bounded plane is added to the polygons
|
---|
1101 | Polygon3 *planePoly =
|
---|
1102 | box.CrossSection(interior->GetPlane());
|
---|
1103 |
|
---|
1104 | // do all the splits with the previous planes
|
---|
1105 | for (int i = 0; i < (int)tData.mPlanes.size(); ++ i)
|
---|
1106 | {
|
---|
1107 | if (planePoly->ClassifyPlane(tData.mPlanes[i], epsilon)
|
---|
1108 | == Polygon3::SPLIT)
|
---|
1109 | {
|
---|
1110 | Polygon3 *frontPoly = new Polygon3();
|
---|
1111 | Polygon3 *backPoly = new Polygon3();
|
---|
1112 |
|
---|
1113 | planePoly->Split(tData.mPlanes[i],
|
---|
1114 | *frontPoly,
|
---|
1115 | *backPoly,
|
---|
1116 | epsilon);
|
---|
1117 |
|
---|
1118 | DEL_PTR(planePoly);
|
---|
1119 |
|
---|
1120 | if(tData.mSides[i] == true)
|
---|
1121 | {
|
---|
1122 | planePoly = frontPoly;
|
---|
1123 | DEL_PTR(backPoly);
|
---|
1124 | }
|
---|
1125 | else
|
---|
1126 | {
|
---|
1127 | planePoly = backPoly;
|
---|
1128 | DEL_PTR(frontPoly);
|
---|
1129 | }
|
---|
1130 | }
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | tData.mPlanes.push_back(interior->GetPlane()); // add plane to split planes
|
---|
1134 |
|
---|
1135 | if (planePoly->Valid(epsilon))
|
---|
1136 | {
|
---|
1137 | polys.push_back(planePoly);
|
---|
1138 | depths.push_back(tData.mDepth);
|
---|
1139 | }
|
---|
1140 | else
|
---|
1141 | DEL_PTR(planePoly);
|
---|
1142 |
|
---|
1143 | // push the children on the stack
|
---|
1144 | tStack.push(BspSplitData(interior->GetFront(), tData.mPlanes,
|
---|
1145 | tData.mSides, true, tData.mDepth + 1));
|
---|
1146 | tStack.push(BspSplitData(interior->GetBack(), tData.mPlanes,
|
---|
1147 | tData.mSides, false, tData.mDepth + 1));
|
---|
1148 | }
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | if (maxDepth > 0)
|
---|
1152 | {
|
---|
1153 | mUseForcedMaterial = true;
|
---|
1154 |
|
---|
1155 | for (int i = 0; i < (int)polys.size(); ++ i)
|
---|
1156 | {
|
---|
1157 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
1158 | float importance = (float)depths[i]/ (float)maxDepth;
|
---|
1159 |
|
---|
1160 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
1161 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
1162 |
|
---|
1163 | ExportPolygon(polys[i]);
|
---|
1164 | }
|
---|
1165 | }
|
---|
1166 | else
|
---|
1167 | {
|
---|
1168 | ExportPolygons(polys);
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 | CLEAR_CONTAINER(polys);
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 |
|
---|
1175 | void VrmlExporter::ExportBspSplits(const BspTree &tree,
|
---|
1176 | const bool exportDepth)
|
---|
1177 | {
|
---|
1178 | ExportBspNodeSplits(tree.GetRoot(),
|
---|
1179 | tree.GetBoundingBox(),
|
---|
1180 | exportDepth,
|
---|
1181 | tree.GetEpsilon());
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 |
|
---|
1185 | void VrmlExporter::ExportBspSplits(const VspBspTree &tree,
|
---|
1186 | const bool exportDepth)
|
---|
1187 | {
|
---|
1188 | ExportBspNodeSplits(tree.GetRoot(),
|
---|
1189 | tree.GetBoundingBox(),
|
---|
1190 | exportDepth,
|
---|
1191 | tree.GetEpsilon());
|
---|
1192 | }
|
---|
1193 |
|
---|
1194 |
|
---|
1195 | void VrmlExporter::ExportBspSplitPlanes(const BspTree &tree)
|
---|
1196 | {
|
---|
1197 | std::stack<BspNode *> tStack;
|
---|
1198 |
|
---|
1199 | tStack.push(tree.GetRoot());
|
---|
1200 |
|
---|
1201 | PolygonContainer polys;
|
---|
1202 |
|
---|
1203 | while (!tStack.empty())
|
---|
1204 | {
|
---|
1205 | // filter polygons donw the tree
|
---|
1206 | BspNode *node = tStack.top();
|
---|
1207 | tStack.pop();
|
---|
1208 |
|
---|
1209 | if (!node->IsLeaf())
|
---|
1210 | {
|
---|
1211 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
1212 |
|
---|
1213 | // bounded plane is added to the polygons
|
---|
1214 | polys.push_back(tree.GetBoundingBox().CrossSection(interior->GetPlane()));
|
---|
1215 |
|
---|
1216 | // push the children on the stack
|
---|
1217 | tStack.push(interior->GetBack());
|
---|
1218 | tStack.push(interior->GetFront());
|
---|
1219 | }
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | ExportPolygons(polys);
|
---|
1223 | CLEAR_CONTAINER(polys);
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 |
|
---|
1227 | void VrmlExporter::ExportGeometry(const ObjectContainer &objects)
|
---|
1228 | {
|
---|
1229 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
1230 |
|
---|
1231 | if (1)
|
---|
1232 | {
|
---|
1233 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
1234 | {
|
---|
1235 | if (1)
|
---|
1236 | {
|
---|
1237 | mUseForcedMaterial = true;
|
---|
1238 | SetForcedMaterial(RandomMaterial());
|
---|
1239 | }
|
---|
1240 | ExportIntersectable(*oit);
|
---|
1241 | }
|
---|
1242 | return;
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | // hack: all object exported as one mesh
|
---|
1246 | PolygonContainer polys;
|
---|
1247 |
|
---|
1248 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
1249 | {
|
---|
1250 | polys.push_back(new Polygon3(dynamic_cast<MeshInstance *>(*oit)->GetMesh()->mVertices));
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | Mesh dummyMesh;
|
---|
1254 | PolygonContainer::const_iterator pit, pit_end = polys.end();
|
---|
1255 |
|
---|
1256 | for (pit = polys.begin(); pit != pit_end; ++ pit)
|
---|
1257 | {
|
---|
1258 | Polygon3 *poly = (*pit);
|
---|
1259 | IncludePolyInMesh(*poly, dummyMesh);
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | ExportMesh(&dummyMesh);
|
---|
1263 |
|
---|
1264 | CLEAR_CONTAINER(polys);
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 |
|
---|
1268 |
|
---|
1269 | bool VrmlExporter::ExportRssTree2(const RssTree &tree,
|
---|
1270 | const Vector3 direction)
|
---|
1271 | {
|
---|
1272 | stack<RssTreeNode *> tStack;
|
---|
1273 |
|
---|
1274 |
|
---|
1275 | mUseForcedMaterial = true;
|
---|
1276 |
|
---|
1277 | Vector3 dirParam;
|
---|
1278 |
|
---|
1279 | dirParam.x = VssRay::GetDirParam(0, Normalize(direction));
|
---|
1280 | dirParam.y = VssRay::GetDirParam(1, Normalize(direction));
|
---|
1281 |
|
---|
1282 | float maxImportance = 0.0f;
|
---|
1283 | tStack.push(tree.GetRoot());
|
---|
1284 | while (!tStack.empty()) {
|
---|
1285 |
|
---|
1286 | RssTreeNode *node = tStack.top();
|
---|
1287 | tStack.pop();
|
---|
1288 |
|
---|
1289 | if (!node->IsLeaf()) {
|
---|
1290 | RssTreeInterior *interior = (RssTreeInterior *)node;
|
---|
1291 | if (interior->axis < 3) {
|
---|
1292 | tStack.push(interior->front);
|
---|
1293 | tStack.push(interior->back);
|
---|
1294 | } else {
|
---|
1295 | if (dirParam[interior->axis-3] < interior->position)
|
---|
1296 | tStack.push(interior->back);
|
---|
1297 | else
|
---|
1298 | tStack.push(interior->front);
|
---|
1299 | }
|
---|
1300 | } else {
|
---|
1301 | RssTreeLeaf *leaf = (RssTreeLeaf *)node;
|
---|
1302 | if (tree.ValidLeaf(leaf)) {
|
---|
1303 | float i = leaf->GetImportance();
|
---|
1304 | if (i > maxImportance)
|
---|
1305 | maxImportance = i;
|
---|
1306 | }
|
---|
1307 | }
|
---|
1308 | }
|
---|
1309 |
|
---|
1310 | tStack.push(tree.GetRoot());
|
---|
1311 | while (!tStack.empty()) {
|
---|
1312 |
|
---|
1313 | RssTreeNode *node = tStack.top();
|
---|
1314 | tStack.pop();
|
---|
1315 |
|
---|
1316 |
|
---|
1317 | if (!node->IsLeaf()) {
|
---|
1318 | RssTreeInterior *interior = (RssTreeInterior *)node;
|
---|
1319 | if (interior->axis < 3) {
|
---|
1320 | tStack.push(interior->front);
|
---|
1321 | tStack.push(interior->back);
|
---|
1322 | } else {
|
---|
1323 | if (dirParam[interior->axis-3] < interior->position)
|
---|
1324 | tStack.push(interior->back);
|
---|
1325 | else
|
---|
1326 | tStack.push(interior->front);
|
---|
1327 | }
|
---|
1328 | } else {
|
---|
1329 | RssTreeLeaf *leaf = (RssTreeLeaf *)node;
|
---|
1330 | if (tree.ValidLeaf(leaf)) {
|
---|
1331 | AxisAlignedBox3 box;
|
---|
1332 | box = tree.GetShrankedBBox(leaf);
|
---|
1333 | Mesh *mesh = new Mesh;
|
---|
1334 | IncludeBoxInMesh(box, *mesh);
|
---|
1335 |
|
---|
1336 | // get 4 corners of the ray directions
|
---|
1337 |
|
---|
1338 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
1339 | mForcedMaterial.mDiffuseColor.r = leaf->GetImportance()/maxImportance;
|
---|
1340 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
1341 |
|
---|
1342 | ExportMesh(mesh);
|
---|
1343 | delete mesh;
|
---|
1344 | }
|
---|
1345 | }
|
---|
1346 | }
|
---|
1347 |
|
---|
1348 | mUseForcedMaterial = false;
|
---|
1349 |
|
---|
1350 | return true;
|
---|
1351 | }
|
---|
1352 |
|
---|
1353 |
|
---|
1354 | void
|
---|
1355 | VrmlExporter::ExportViewpoint(const Vector3 &point,
|
---|
1356 | const Vector3 &direction)
|
---|
1357 | {
|
---|
1358 | stream << "Viewpoint {" << endl;
|
---|
1359 |
|
---|
1360 | stream << "position " << point.x << " " << point.y << " " << point.z << endl;
|
---|
1361 | // stream<<"orientation "<<direction.x<<direction.y<<direction.z<<endl;
|
---|
1362 |
|
---|
1363 | stream << "}" << endl; // end view point;
|
---|
1364 |
|
---|
1365 | }
|
---|
1366 |
|
---|
1367 |
|
---|
1368 |
|
---|
1369 | void VrmlExporter::ExportBeam(const Beam &beam, const AxisAlignedBox3 &box)
|
---|
1370 | {
|
---|
1371 | if (beam.mMesh)
|
---|
1372 | {
|
---|
1373 | ExportMesh(beam.mMesh);
|
---|
1374 | return;
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | PolygonContainer polys;
|
---|
1378 | //ExportBox(beam.mBox);
|
---|
1379 |
|
---|
1380 | const float zfar = 2.0f * Magnitude(box.Diagonal());
|
---|
1381 |
|
---|
1382 | // box should not never remove part of beam polygons
|
---|
1383 | Vector3 bmin = beam.mBox.Min() - Vector3(zfar * 2.0f);
|
---|
1384 | Vector3 bmax = beam.mBox.Max() + Vector3(zfar * 2.0f);
|
---|
1385 |
|
---|
1386 | AxisAlignedBox3 bbox(bmin, bmax);
|
---|
1387 | Plane3 fplane;
|
---|
1388 | fplane.mNormal = -beam.mPlanes[0].mNormal;
|
---|
1389 |
|
---|
1390 | fplane.mD = beam.mPlanes[0].mD - zfar - 1.0f;
|
---|
1391 |
|
---|
1392 | vector<Plane3> planes = beam.mPlanes;
|
---|
1393 | planes.push_back(fplane);
|
---|
1394 |
|
---|
1395 | for (int i = 0; i < planes.size(); ++ i)
|
---|
1396 | {
|
---|
1397 | Polygon3 *poly = bbox.CrossSection(planes[i]);
|
---|
1398 | if (!poly->Valid(Limits::Small))
|
---|
1399 | DEL_PTR(poly);
|
---|
1400 |
|
---|
1401 | for (int j = 0; (j < planes.size()) && poly; ++ j)
|
---|
1402 | {
|
---|
1403 | if (j != i)
|
---|
1404 | {
|
---|
1405 | Polygon3 *front = new Polygon3();
|
---|
1406 | Polygon3 *back = new Polygon3();
|
---|
1407 |
|
---|
1408 | poly->Split(planes[j], *front, *back, Limits::Small);
|
---|
1409 | DEL_PTR(poly);
|
---|
1410 | DEL_PTR(front);
|
---|
1411 |
|
---|
1412 | if (!back->Valid(Limits::Small))
|
---|
1413 | DEL_PTR(back);
|
---|
1414 | poly = back;
|
---|
1415 | }
|
---|
1416 | }
|
---|
1417 | if (poly)
|
---|
1418 | polys.push_back(poly);
|
---|
1419 | }
|
---|
1420 |
|
---|
1421 | ExportPolygons(polys);
|
---|
1422 | CLEAR_CONTAINER(polys);
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | }
|
---|