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