[162] | 1 | #include <stack>
|
---|
| 2 | #include "common.h"
|
---|
| 3 | #include "SceneGraph.h"
|
---|
| 4 | #include "X3dExporter.h"
|
---|
| 5 | #include "Mesh.h"
|
---|
| 6 | #include "KdTree.h"
|
---|
[242] | 7 | #include "ViewCellBsp.h"
|
---|
| 8 | #include "ViewCell.h"
|
---|
[260] | 9 | #include "Polygon3.h"
|
---|
[162] | 10 |
|
---|
| 11 |
|
---|
| 12 | X3dExporter::X3dExporter(const string filename):Exporter(filename)
|
---|
| 13 | {
|
---|
| 14 | stream.open(mFilename.c_str());
|
---|
| 15 | stream<<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
|
---|
| 16 | stream<<"<X3D>"<<endl;
|
---|
| 17 | stream<<"<Scene>"<<endl;
|
---|
| 18 |
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | X3dExporter::~X3dExporter()
|
---|
| 22 | {
|
---|
| 23 | stream<<"</Scene>"<<endl;
|
---|
| 24 | stream<<"</X3D>"<<endl;
|
---|
| 25 | stream.close();
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[176] | 28 |
|
---|
[162] | 29 | bool
|
---|
[176] | 30 | X3dExporter::ExportRays(const vector<Ray> &rays,
|
---|
| 31 | const float length,
|
---|
| 32 | const RgbColor &color)
|
---|
[162] | 33 | {
|
---|
| 34 | vector<Ray>::const_iterator ri = rays.begin();
|
---|
| 35 | stream<<"<Shape>"<<endl;
|
---|
[176] | 36 | stream<<"<Appearance>"<<endl;
|
---|
| 37 | stream<<"<Material ambientColor=\""<<color.r<<" "<<color.g<<" "<<color.b<<
|
---|
| 38 | "\" />"<<endl;
|
---|
| 39 | stream<<"</Appearance>"<<endl;
|
---|
| 40 |
|
---|
[209] | 41 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
[162] | 42 |
|
---|
| 43 | int index = 0;
|
---|
| 44 | for (; ri != rays.end(); ri++) {
|
---|
| 45 | stream<<index<<" "<<index+1<<" -1\n";
|
---|
| 46 | index+=2;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | stream<<"\" >"<<endl;
|
---|
| 50 |
|
---|
| 51 | stream<<"<Coordinate point=\""<<endl;
|
---|
| 52 |
|
---|
| 53 | ri = rays.begin();
|
---|
| 54 | for (; ri != rays.end(); ri++) {
|
---|
| 55 | Vector3 a = (*ri).GetLoc();
|
---|
| 56 |
|
---|
[176] | 57 | Vector3 b;
|
---|
| 58 | if (length < 0)
|
---|
| 59 | b = (*ri).GetLoc() - length*(*ri).GetDir();
|
---|
| 60 | else
|
---|
| 61 | if ((*ri).intersections.size()==0)
|
---|
| 62 | b = (*ri).GetLoc() + length*(*ri).GetDir();
|
---|
| 63 | else
|
---|
| 64 | b = (*ri).Extrap((*ri).intersections[0].mT);
|
---|
| 65 |
|
---|
[162] | 66 | stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
|
---|
| 67 | stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | stream<<"\" >"<<endl;
|
---|
| 71 | stream<<"</Coordinate>"<<endl;
|
---|
| 72 | stream<<"</IndexedLineSet>"<<endl;
|
---|
| 73 | stream<<"</Shape>"<<endl;
|
---|
| 74 | return true;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void
|
---|
| 78 | X3dExporter::ExportSceneNode(SceneGraphNode *node)
|
---|
| 79 | {
|
---|
| 80 | stream<<"<Group>"<<endl;
|
---|
| 81 |
|
---|
| 82 | SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
|
---|
| 83 | for (; ni != node->mChildren.end(); ni++)
|
---|
| 84 | ExportSceneNode(*ni);
|
---|
| 85 |
|
---|
| 86 |
|
---|
[176] | 87 | ObjectContainer::const_iterator mi = node->mGeometry.begin();
|
---|
[162] | 88 | for (; mi != node->mGeometry.end(); mi++) {
|
---|
| 89 | // export the transform...
|
---|
[176] | 90 | ExportIntersectable(*mi);
|
---|
[162] | 91 | }
|
---|
| 92 |
|
---|
| 93 | stream<<"</Group>"<<endl;
|
---|
| 94 |
|
---|
| 95 | }
|
---|
[176] | 96 | void
|
---|
| 97 | X3dExporter::ExportIntersectable(Intersectable *object)
|
---|
| 98 | {
|
---|
| 99 | switch (object->Type()) {
|
---|
| 100 | case Intersectable::MESH_INSTANCE:
|
---|
| 101 | case Intersectable::TRANSFORMED_MESH_INSTANCE:
|
---|
| 102 | ExportMeshInstance((MeshInstance *)object);
|
---|
[242] | 103 | break;
|
---|
| 104 | case Intersectable::VIEWCELL:
|
---|
| 105 | ExportViewCell((ViewCell *)object);
|
---|
[176] | 106 | break;
|
---|
| 107 | default:
|
---|
| 108 | cerr<<"Sorry the export for object not yet defined"<<endl;
|
---|
| 109 | break;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
[162] | 112 |
|
---|
| 113 | void
|
---|
[176] | 114 | X3dExporter::ExportMeshInstance(MeshInstance *object)
|
---|
| 115 | {
|
---|
| 116 | // $$JB$$
|
---|
| 117 | // in the future check whether the mesh was not already exportet
|
---|
| 118 | // and use a reference to the that mesh instead
|
---|
| 119 | ExportMesh(object->GetMesh());
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[261] | 122 | void X3dExporter::ExportViewCells(ViewCellContainer *viewCells)
|
---|
| 123 | {
|
---|
| 124 | ViewCellContainer::iterator it, it_end = viewCells->end();
|
---|
| 125 |
|
---|
| 126 | for (it = viewCells->begin(); it != it_end; ++it)
|
---|
| 127 | ExportViewCell(*it);
|
---|
| 128 | }
|
---|
[176] | 129 | void
|
---|
[261] | 130 | X3dExporter::ExportViewCell(ViewCell *viewCell)
|
---|
[242] | 131 | {
|
---|
[261] | 132 | if (viewCell->GetMesh())
|
---|
| 133 | ExportMesh(viewCell->GetMesh());
|
---|
[242] | 134 | }
|
---|
| 135 |
|
---|
| 136 | void
|
---|
[162] | 137 | X3dExporter::ExportMesh(Mesh *mesh)
|
---|
| 138 | {
|
---|
| 139 |
|
---|
| 140 | stream<<"<Shape>"<<endl;
|
---|
| 141 | stream<<"<Appearance>"<<endl;
|
---|
[176] | 142 |
|
---|
[162] | 143 | // $$ tmp -> random material
|
---|
| 144 |
|
---|
| 145 | float r, g, b;
|
---|
| 146 |
|
---|
[176] | 147 | if (mUseForcedMaterial) {
|
---|
| 148 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
| 149 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
| 150 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
| 151 |
|
---|
| 152 | } else
|
---|
| 153 | if (mesh->mMaterial) {
|
---|
| 154 | r = mesh->mMaterial->mDiffuseColor.r;
|
---|
| 155 | g = mesh->mMaterial->mDiffuseColor.g;
|
---|
| 156 | b = mesh->mMaterial->mDiffuseColor.b;
|
---|
| 157 | } else {
|
---|
| 158 | r = RandomValue(0.5, 1.0);
|
---|
| 159 | g = RandomValue(0.5, 1.0);
|
---|
| 160 | b = RandomValue(0.5, 1.0);
|
---|
| 161 | }
|
---|
[162] | 162 | stream<<"<Material diffuseColor=\""<<r<<" "<<g<<" "<<b<<
|
---|
| 163 | "\" specularColor=\"0.0 0.0 0.0\"/>"<<endl;
|
---|
| 164 | stream<<"</Appearance>"<<endl;
|
---|
| 165 |
|
---|
| 166 |
|
---|
[191] | 167 | if (mWireframe)
|
---|
[162] | 168 | stream<<"<IndexedLineSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
| 169 | else
|
---|
| 170 | stream<<"<IndexedFaceSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
| 171 |
|
---|
| 172 | FaceContainer::const_iterator fi = mesh->mFaces.begin();
|
---|
| 173 |
|
---|
| 174 | int index = 0;
|
---|
| 175 |
|
---|
| 176 | for (; fi != mesh->mFaces.end(); fi++) {
|
---|
| 177 | Face *face = *fi;
|
---|
| 178 | VertexIndexContainer::const_iterator vi = face->mVertexIndices.begin();
|
---|
| 179 | for (; vi != face->mVertexIndices.end(); vi++)
|
---|
| 180 | stream<<*vi<<" ";
|
---|
| 181 | stream<<"-1"<<endl;
|
---|
| 182 | }
|
---|
| 183 | stream<<"\" >"<<endl;
|
---|
| 184 |
|
---|
| 185 | stream<<"<Coordinate point=\""<<endl;
|
---|
| 186 |
|
---|
| 187 | VertexContainer::const_iterator vi = mesh->mVertices.begin();
|
---|
| 188 | for (; vi != mesh->mVertices.end(); vi++) {
|
---|
| 189 | stream<<(*vi).x<<" "<<(*vi).y<<" "<<(*vi).z;
|
---|
| 190 | stream<<","<<endl;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | stream<<"\" >"<<endl;
|
---|
| 194 | stream<<"</Coordinate>"<<endl;
|
---|
| 195 |
|
---|
[191] | 196 | if (mWireframe)
|
---|
[162] | 197 | stream<<"</IndexedLineSet>"<<endl;
|
---|
| 198 | else
|
---|
| 199 | stream<<"</IndexedFaceSet>"<<endl;
|
---|
| 200 |
|
---|
| 201 | stream<<"</Shape>"<<endl;
|
---|
| 202 |
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[242] | 205 |
|
---|
| 206 | void X3dExporter::ExportPolygon(Polygon3 *poly)
|
---|
| 207 | {
|
---|
| 208 | stream << "<Shape>" << endl;
|
---|
| 209 | stream << "<Appearance>" << endl;
|
---|
| 210 |
|
---|
| 211 | // $$ tmp -> random material
|
---|
| 212 |
|
---|
| 213 | float r, g, b;
|
---|
| 214 |
|
---|
| 215 | if (mUseForcedMaterial)
|
---|
| 216 | {
|
---|
| 217 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
| 218 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
| 219 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
| 220 | }
|
---|
| 221 | else if (poly->mMaterial)
|
---|
| 222 | {
|
---|
| 223 | r = poly->mMaterial->mDiffuseColor.r;
|
---|
| 224 | g = poly->mMaterial->mDiffuseColor.g;
|
---|
| 225 | b = poly->mMaterial->mDiffuseColor.b;
|
---|
| 226 | } else
|
---|
| 227 | {
|
---|
| 228 | r = RandomValue(0.5, 1.0);
|
---|
| 229 | g = RandomValue(0.5, 1.0);
|
---|
| 230 | b = RandomValue(0.5, 1.0);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
| 234 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
| 235 |
|
---|
| 236 | stream << "</Appearance>" << endl;
|
---|
| 237 |
|
---|
| 238 |
|
---|
[262] | 239 | //-- create and write indices
|
---|
[242] | 240 | if (mWireframe)
|
---|
| 241 | stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
| 242 | else
|
---|
| 243 | stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
| 244 |
|
---|
[262] | 245 | int index = 0;
|
---|
| 246 |
|
---|
| 247 | VertexContainer::const_iterator vi;
|
---|
| 248 |
|
---|
| 249 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
| 250 | {
|
---|
| 251 | stream << index ++ << " ";
|
---|
| 252 | }
|
---|
| 253 | stream << "-1" << endl;
|
---|
[242] | 254 |
|
---|
[262] | 255 | stream << "\" >" << endl;
|
---|
| 256 |
|
---|
[242] | 257 | stream << "<Coordinate point=\"" << endl;
|
---|
| 258 |
|
---|
[262] | 259 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
[242] | 260 | {
|
---|
| 261 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
| 262 | stream << "," << endl;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[262] | 265 | stream << "\" >" << endl;
|
---|
| 266 | stream << "</Coordinate>" << endl;
|
---|
[242] | 267 |
|
---|
| 268 | if (mWireframe)
|
---|
| 269 | stream << "</IndexedLineSet>" << endl;
|
---|
| 270 | else
|
---|
| 271 | stream << "</IndexedFaceSet>" << endl;
|
---|
| 272 |
|
---|
| 273 | stream << "</Shape>" << endl;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[263] | 276 | void X3dExporter::ExportPolygons(PolygonContainer *polys)
|
---|
| 277 | {
|
---|
| 278 | stream << "<Shape>" << endl;
|
---|
| 279 | stream << "<Appearance>" << endl;
|
---|
| 280 |
|
---|
| 281 | // $$ tmp -> random material
|
---|
| 282 |
|
---|
| 283 | float r, g, b;
|
---|
[242] | 284 |
|
---|
[263] | 285 | if (mUseForcedMaterial)
|
---|
| 286 | {
|
---|
| 287 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
| 288 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
| 289 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
| 290 | }
|
---|
| 291 | else
|
---|
| 292 | {
|
---|
| 293 | r = RandomValue(0.5, 1.0);
|
---|
| 294 | g = RandomValue(0.5, 1.0);
|
---|
| 295 | b = RandomValue(0.5, 1.0);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
| 299 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
| 300 |
|
---|
| 301 | stream << "</Appearance>" << endl;
|
---|
| 302 |
|
---|
| 303 |
|
---|
| 304 | //-- create and write indices
|
---|
| 305 | if (mWireframe)
|
---|
| 306 | stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
| 307 | else
|
---|
| 308 | stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
| 309 |
|
---|
| 310 | int index = 0;
|
---|
| 311 |
|
---|
| 312 | PolygonContainer::const_iterator pit;
|
---|
| 313 |
|
---|
| 314 | VertexContainer::const_iterator vi;
|
---|
| 315 |
|
---|
| 316 | for (pit = polys->begin(); pit != polys->end(); ++pit)
|
---|
| 317 | {
|
---|
| 318 | Polygon3 *poly = *pit;
|
---|
| 319 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
| 320 | {
|
---|
| 321 | stream << index ++ << " ";
|
---|
| 322 | }
|
---|
| 323 | stream << "-1" << endl;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | stream << "\" >" << endl;
|
---|
| 327 |
|
---|
| 328 | stream << "<Coordinate point=\"" << endl;
|
---|
| 329 | for (pit = polys->begin(); pit != polys->end(); ++pit)
|
---|
| 330 | {
|
---|
| 331 | Polygon3 *poly = *pit;
|
---|
| 332 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
| 333 | {
|
---|
| 334 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
| 335 | stream << "," << endl;
|
---|
| 336 | }
|
---|
| 337 | }
|
---|
| 338 | stream << "\" >" << endl;
|
---|
| 339 | stream << "</Coordinate>" << endl;
|
---|
| 340 |
|
---|
| 341 | if (mWireframe)
|
---|
| 342 | stream << "</IndexedLineSet>" << endl;
|
---|
| 343 | else
|
---|
| 344 | stream << "</IndexedFaceSet>" << endl;
|
---|
| 345 |
|
---|
| 346 | stream << "</Shape>" << endl;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
[162] | 349 | bool
|
---|
| 350 | X3dExporter::ExportBox(const AxisAlignedBox3 &box)
|
---|
| 351 | {
|
---|
| 352 | Mesh *mesh = new Mesh;
|
---|
| 353 | // add 6 vertices of the box
|
---|
| 354 | int index = mesh->mVertices.size();
|
---|
| 355 | for (int i=0; i < 8; i++) {
|
---|
| 356 | Vector3 v;
|
---|
| 357 | box.GetVertex(i, v);
|
---|
| 358 | mesh->mVertices.push_back(v);
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
| 362 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
| 363 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
| 364 |
|
---|
| 365 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
| 366 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
| 367 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
| 368 |
|
---|
| 369 | ExportMesh(mesh);
|
---|
| 370 | delete mesh;
|
---|
| 371 | return true;
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | bool
|
---|
[242] | 375 | X3dExporter::ExportBspTree(const BspTree &tree)
|
---|
[162] | 376 | {
|
---|
[242] | 377 | if (mExportRayDensity)
|
---|
| 378 | {
|
---|
[263] | 379 | return ExportBspTreeRayDensity(tree);
|
---|
[242] | 380 | }
|
---|
| 381 |
|
---|
| 382 | stack<BspNode *> tStack;
|
---|
| 383 |
|
---|
| 384 | tStack.push(tree.GetRoot());
|
---|
| 385 |
|
---|
| 386 | Mesh *mesh = new Mesh;
|
---|
| 387 |
|
---|
[262] | 388 | AxisAlignedBox3 box = tree.GetBoundingBox();
|
---|
[264] | 389 | bool savedWireframe = mWireframe;
|
---|
| 390 |
|
---|
| 391 | SetWireframe();
|
---|
[262] | 392 | ExportBox(box);
|
---|
[264] | 393 |
|
---|
| 394 | if (!savedWireframe)
|
---|
| 395 | SetFilled();
|
---|
| 396 |
|
---|
[260] | 397 | while (!tStack.empty())
|
---|
[242] | 398 | {
|
---|
| 399 | BspNode *node = tStack.top();
|
---|
| 400 |
|
---|
| 401 | tStack.pop();
|
---|
| 402 |
|
---|
[260] | 403 | if (tree.StorePolys()) // extract the polygons
|
---|
| 404 | {
|
---|
| 405 | PolygonContainer::const_iterator it;
|
---|
| 406 | PolygonContainer::const_iterator it_end = node->GetPolygons()->end();
|
---|
| 407 |
|
---|
| 408 | for (it = node->GetPolygons()->begin(); it != it_end; ++ it)
|
---|
| 409 | ExportPolygon(*it);
|
---|
| 410 | }
|
---|
| 411 |
|
---|
[242] | 412 | if (!node->IsLeaf())
|
---|
| 413 | {
|
---|
| 414 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 415 |
|
---|
| 416 | tStack.push(interior->GetFront());
|
---|
| 417 | tStack.push(interior->GetBack());
|
---|
[260] | 418 |
|
---|
[242] | 419 | }
|
---|
[263] | 420 | else if (!tree.StorePolys())
|
---|
[242] | 421 | {
|
---|
[263] | 422 | ViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
[262] | 423 |
|
---|
[263] | 424 | if (viewCell) // export view cell geometry
|
---|
| 425 | ExportViewCell(viewCell);
|
---|
[242] | 426 | }
|
---|
[263] | 427 | }
|
---|
| 428 |
|
---|
| 429 | return true;
|
---|
[242] | 430 | }
|
---|
| 431 |
|
---|
| 432 | bool X3dExporter::ExportKdTree(const KdTree &tree)
|
---|
| 433 | {
|
---|
| 434 | if (mExportRayDensity) {
|
---|
[191] | 435 | return ExportKdTreeRayDensity(tree);
|
---|
| 436 | }
|
---|
| 437 |
|
---|
[162] | 438 | stack<KdNode *> tStack;
|
---|
| 439 |
|
---|
| 440 | tStack.push(tree.GetRoot());
|
---|
| 441 |
|
---|
| 442 | Mesh *mesh = new Mesh;
|
---|
| 443 |
|
---|
| 444 | while (!tStack.empty()) {
|
---|
| 445 | KdNode *node = tStack.top();
|
---|
| 446 | tStack.pop();
|
---|
| 447 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
| 448 | // add 6 vertices of the box
|
---|
| 449 | int index = mesh->mVertices.size();
|
---|
| 450 | for (int i=0; i < 8; i++) {
|
---|
| 451 | Vector3 v;
|
---|
| 452 | box.GetVertex(i, v);
|
---|
| 453 | mesh->mVertices.push_back(v);
|
---|
| 454 | }
|
---|
| 455 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
| 456 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
| 457 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
| 458 |
|
---|
| 459 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
| 460 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
| 461 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
| 462 |
|
---|
| 463 | if (!node->IsLeaf()) {
|
---|
| 464 | KdInterior *interior = (KdInterior *)node;
|
---|
| 465 | tStack.push(interior->mFront);
|
---|
| 466 | tStack.push(interior->mBack);
|
---|
| 467 | }
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | ExportMesh(mesh);
|
---|
| 471 | delete mesh;
|
---|
| 472 | return true;
|
---|
[242] | 473 | // TODO
|
---|
| 474 | return true;
|
---|
[162] | 475 | }
|
---|
| 476 |
|
---|
| 477 |
|
---|
[191] | 478 | bool
|
---|
[263] | 479 | X3dExporter::ExportBspTreeRayDensity(const BspTree &tree)
|
---|
| 480 | {
|
---|
| 481 | // TODO
|
---|
| 482 | return true;
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | bool
|
---|
[191] | 486 | X3dExporter::ExportKdTreeRayDensity(const KdTree &tree)
|
---|
| 487 | {
|
---|
| 488 | stack<KdNode *> tStack;
|
---|
| 489 |
|
---|
| 490 | tStack.push(tree.GetRoot());
|
---|
| 491 |
|
---|
| 492 | bool fm = mUseForcedMaterial;
|
---|
| 493 | mUseForcedMaterial = true;
|
---|
| 494 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
| 495 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
| 496 | while (!tStack.empty()) {
|
---|
| 497 | KdNode *node = tStack.top();
|
---|
| 498 | tStack.pop();
|
---|
| 499 | if (node->IsLeaf()) {
|
---|
| 500 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
| 501 | Mesh *mesh = new Mesh;
|
---|
| 502 |
|
---|
| 503 | // add 6 vertices of the box
|
---|
| 504 | int index = mesh->mVertices.size();
|
---|
| 505 | for (int i=0; i < 8; i++) {
|
---|
| 506 | Vector3 v;
|
---|
| 507 | box.GetVertex(i, v);
|
---|
| 508 | mesh->mVertices.push_back(v);
|
---|
| 509 | }
|
---|
| 510 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
| 511 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
| 512 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
| 513 |
|
---|
| 514 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
| 515 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
| 516 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
| 517 |
|
---|
| 518 |
|
---|
| 519 | // set the mesh material according to the ray density
|
---|
| 520 | KdLeaf *leaf = (KdLeaf *) node;
|
---|
| 521 | if (leaf->mPassingRays.mRays) {
|
---|
| 522 | float importance = leaf->mPassingRays.mContributions/(float)leaf->mPassingRays.mRays;
|
---|
| 523 | // float importance = leaf->mPassingRays.mContributions/1000.0f;
|
---|
| 524 | // float importance = leaf->mPassingRays.mRays/1000.0f;
|
---|
| 525 | ///(float)leaf->mPassingRays.mRays;
|
---|
| 526 | // mForcedMaterial.mDiffuseColor.r = log10(leaf->mPassingRays.mRays)/3.0f;
|
---|
| 527 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
| 528 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
| 529 | ExportMesh(mesh);
|
---|
| 530 | }
|
---|
| 531 | delete mesh;
|
---|
| 532 | } else {
|
---|
| 533 | KdInterior *interior = (KdInterior *)node;
|
---|
| 534 | tStack.push(interior->mFront);
|
---|
| 535 | tStack.push(interior->mBack);
|
---|
| 536 | }
|
---|
| 537 | }
|
---|
| 538 | // restore the state of forced material
|
---|
| 539 | mUseForcedMaterial = fm;
|
---|
| 540 | return true;
|
---|
| 541 | }
|
---|