source: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp @ 386

Revision 386, 21.8 KB checked in by bittner, 19 years ago (diff)

VssPreprocessor? updates - directional rays, x3dexporter updates - removed duplicated code for ray exports

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