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