source: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp @ 350

Revision 350, 19.3 KB checked in by mattausch, 19 years ago (diff)

ray merge started

Line 
1#include "SceneGraph.h"
2#include "KdTree.h"
3#include "SamplingPreprocessor.h"
4#include "X3dExporter.h"
5#include "Environment.h"
6#include "MutualVisibility.h"
7#include "Polygon3.h"
8#include "ViewCell.h"
9
10SamplingPreprocessor::SamplingPreprocessor(): mPass(0), mSampleRays(NULL)
11{
12  // this should increase coherence of the samples
13  environment->GetIntValue("Sampling.samplesPerPass", mSamplesPerPass);
14  environment->GetIntValue("Sampling.totalSamples", mTotalSamples);
15  environment->GetIntValue("BspTree.Construction.samples", mBspConstructionSamples);
16
17  mKdPvsDepth = 100;
18  mStats.open("stats.log");
19
20}
21
22SamplingPreprocessor::~SamplingPreprocessor()
23{
24        CLEAR_CONTAINER(mSampleRays);
25}
26
27void
28SamplingPreprocessor::SetupRay(Ray &ray,
29                                                                                                                         const Vector3 &point,
30                                                                                                                         const Vector3 &direction,
31                                                                                                                         const int type)
32{
33  ray.intersections.clear();
34  ray.leaves.clear();
35  ray.meshes.clear();
36  ray.viewCells.clear();
37
38  //  cout<<point<<" "<<direction<<endl;
39  ray.Init(point, direction, type);
40}
41
42KdNode *
43SamplingPreprocessor::GetNodeForPvs(KdLeaf *leaf)
44{
45  KdNode *node = leaf;
46  while (node->mParent && node->mDepth > mKdPvsDepth)
47    node = node->mParent;
48  return node;
49}
50
51bool
52SamplingPreprocessor::BuildBspTree()
53{
54        // delete old tree
55        DEL_PTR(mBspTree);
56        mBspTree = new BspTree(&mUnbounded);
57        ObjectContainer objects;
58       
59        switch (BspTree::sConstructionMethod)
60        {
61        case BspTree::FROM_INPUT_VIEW_CELLS:
62                mBspTree->SetGenerateViewCells(false);
63                mBspTree->Construct(mViewCells);
64                break;
65        case BspTree::FROM_SCENE_GEOMETRY:
66                DeleteViewCells(); // we generate new view cells
67                mBspTree->SetGenerateViewCells(true);
68                mSceneGraph->CollectObjects(&objects);
69                mBspTree->Construct(objects);
70               
71                mBspTree->CollectViewCells(mViewCells);
72                break;
73        case BspTree::FROM_RAYS:
74                DeleteViewCells(); // we generate new view cells
75                mBspTree->SetGenerateViewCells(true);
76                mBspTree->Construct(mSampleRays);
77               
78                mBspTree->CollectViewCells(mViewCells);
79                break;
80        default:
81                Debug << "Error: Method not available\n";
82                break;
83        }
84       
85       
86        return true;
87}
88
89int
90SamplingPreprocessor::AddNodeSamples(Intersectable *object,
91                                                                                                                                                 const Ray &ray
92                                                                                                                                                 )
93{
94  int contributingSamples = 0;
95  int j;
96  for (j=0; j < ray.leaves.size(); j++) {
97    KdNode *node = GetNodeForPvs( ray.leaves[j] );
98    contributingSamples += object->mKdPvs.AddSample(node);
99  }
100   
101  if (mPass > 10)
102    for (j=1; j < ((int)ray.leaves.size() - 1); j++) {
103      ray.leaves[j]->AddPassingRay(ray, contributingSamples ? 1 : 0);
104  }
105 
106  return contributingSamples;
107}
108
109
110int SamplingPreprocessor::AddObjectSamples(Intersectable *obj, const Ray &ray)
111{
112        int contributingSamples = 0;
113        int j;
114 
115        // object can be seen from the view cell => add to view cell pvs
116        for (j=0; j < ray.viewCells.size(); ++ j)
117        {       // if ray not in unbounded space
118                if (ray.viewCells[j] != &mUnbounded)
119                        contributingSamples += ray.viewCells[j]->GetPvs().AddSample(obj);
120        }
121 
122        // rays passing through this viewcell
123        if (mPass > 1)
124                for (j=1; j < ((int)ray.viewCells.size() - 1); ++ j)
125                {
126                        if (ray.viewCells[j] != &mUnbounded)
127                                ray.viewCells[j]->AddPassingRay(ray, contributingSamples ? 1 : 0);
128                }
129 
130        return contributingSamples;
131}
132
133
134void
135SamplingPreprocessor::HoleSamplingPass()
136{
137  vector<KdLeaf *> leaves;
138  mKdTree->CollectLeaves(leaves);
139 
140  // go through all the leaves and evaluate their passing contribution
141  for (int i=0 ; i < leaves.size(); i++) {
142    KdLeaf *leaf = leaves[i];
143    cout<<leaf->mPassingRays<<endl;
144  }
145}
146
147
148int
149SamplingPreprocessor::CastRay(Intersectable *object,
150                                                                                                                        Ray &ray)
151{
152        int sampleContributions = 0;
153
154        long t1 = GetRealTime();
155        // cast ray to KD tree to find intersection with other objects
156        mKdTree->CastRay(ray);
157        long t2 = GetRealTime();
158
159        if (0 && object->GetId() > 2197) {
160                object->Describe(cout)<<endl;
161                cout<<ray<<endl;
162        }
163
164        if (mViewCellsType == BSP_VIEW_CELLS)
165        {
166                // cast ray to BSP tree to get intersection with view cells
167                if (mBspTree)
168                {
169                        mBspTree->CastRay(ray);
170                                       
171                        sampleContributions += AddObjectSamples(object, ray);
172                               
173                        if (!ray.intersections.empty()) // second intersection found
174                        {
175                                sampleContributions +=
176                                        AddObjectSamples(ray.intersections[0].mObject, ray);
177                        }
178                }
179        }
180        else
181                {
182                        if (ray.leaves.size()) {
183                                sampleContributions += AddNodeSamples(object, ray);
184                               
185                                if (ray.intersections.size()) {
186                                        sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray);
187                                }
188                        }
189                }       
190       
191        return sampleContributions;
192}
193
194//  void
195//  SamplingPreprocessor::AvsGenerateRandomRay(Ray &ray)
196//  {
197//    int objId = RandomValue(0, mObjects.size());
198//    Intersectable *object = objects[objId];
199//    object->GetRandomSurfacePoint(point, normal);
200//    direction = UniformRandomVector(normal);
201//    SetupRay(ray, point, direction);
202//  }
203
204//  void
205//  SamplingPreprocessor::AvsHandleRay(Ray &ray)
206//  {
207//    int sampleContributions = 0;
208
209//    mKdTree->CastRay(ray);
210 
211//    if (ray.leaves.size()) {
212//      sampleContributions += AddNodeSamples(object, ray, pass);
213   
214//      if (ray.intersections.size()) {
215//        sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray, pass);
216//      }
217//    }
218//  }
219
220//  void
221//  SamplingPreprocessor::AvsBorderSampling(Ray &ray)
222//  {
223 
224
225//  }
226
227//  void
228//  SamplingPreprocessor::AvsPass()
229//  {
230//    Ray ray;
231//    while (1) {
232//      AvsGenerateRay(ray);
233//      HandleRay(ray);
234//      while ( !mRayQueue.empty() ) {
235//        Ray ray = mRayQueue.pop();
236//        mRayQueue.pop();
237//        AdaptiveBorderSampling(ray);
238//      }
239//    }
240 
241 
242
243//  }
244
245
246int
247SamplingPreprocessor::CastEdgeSamples(
248                                                                                                                                                        Intersectable *object,
249                                                                                                                                                        const Vector3 &point,
250                                                                                                                                                        MeshInstance *mi,
251                                                                                                                                                        const int samples
252                                                                                                                                                        )
253{
254        Ray ray;
255        int maxTries = samples*10;
256        int i;
257        int rays = 0;
258        int edgeSamplesContributions = 0;
259        for (i=0; i < maxTries && rays < samples; i++) {
260                // pickup a random face of each mesh
261                Mesh *mesh = mi->GetMesh();
262                int face = RandomValue(0, mesh->mFaces.size()-1);
263               
264                Polygon3 poly(mesh->mFaces[face], mesh);
265                poly.Scale(1.001);
266                // now extend a random edge of the face
267                int edge = RandomValue(0, poly.mVertices.size()-1);
268                float t = RandomValue(0.0f,1.0f);
269                Vector3 target = t*poly.mVertices[edge] + (1.0f-t)*poly.mVertices[(edge + 1)%
270                                                                                                                                                                                                                                                                                 poly.mVertices.size()];
271                SetupRay(ray, point, target - point, Ray::LOCAL_RAY);
272                if (!mesh->CastRay(ray, mi)) {
273                        // the rays which intersect the mesh have been discarded since they are not tangent
274                        // to the mesh
275                        rays++;
276                        edgeSamplesContributions += CastRay(object, ray);
277                }
278        }
279        return edgeSamplesContributions;
280}
281
282bool
283SamplingPreprocessor::ComputeVisibility()
284{
285 
286  // pickup an object
287  ObjectContainer objects;
288 
289  mSceneGraph->CollectObjects(&objects);
290
291  Vector3 point, normal, direction;
292  Ray ray;
293
294  long startTime = GetTime();
295 
296  int i;
297  int totalSamples = 0;
298
299  int pvsOut = Min((int)objects.size(), 10);
300
301  vector<Ray> rays[10];
302
303  vector<Ray> vcRays[5];
304  ViewCellContainer pvsViewCells;
305  vector<Ray> viewCellRays; // used for BSP tree construction
306
307  while (totalSamples < mTotalSamples) {
308                int passContributingSamples = 0;
309                int passSampleContributions = 0;
310                int passSamples = 0;
311                int index = 0;
312                Real maxTime = 0;
313                int maxTimeIdx = 0;
314
315                bool collectSamplesForBsp =
316                        (mViewCellsType == BSP_VIEW_CELLS) &&
317                        (BspTree::sConstructionMethod == BspTree::FROM_RAYS) &&
318                        (totalSamples < mBspConstructionSamples);
319                       
320                cout << "totalSamples: "  << totalSamples << endl;
321
322                for (i = 0; i < objects.size(); i++) {
323                                               
324                        KdNode *nodeToSample = NULL;
325                        Intersectable *object = objects[i];
326               
327                        int pvsSize = 0;
328                        if (mViewCellsType == KD_VIEW_CELLS)
329                                pvsSize = object->mKdPvs.GetSize();
330                                               
331                        if (0 && pvsSize) {
332                                // mail all nodes from the pvs
333                                Intersectable::NewMail();
334                                KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
335                         
336                                for (; i != object->mKdPvs.mEntries.end(); i++) {
337                                        KdNode *node = (*i).first;
338                                        node->Mail();
339                                }
340                               
341                                int maxTries = 2*pvsSize;
342                                Debug << "Finding random neighbour" << endl;   
343                                for (int tries = 0; tries < 10; tries++) {
344                                        index = RandomValue(0, pvsSize - 1);
345                                        KdPvsData data;
346                                        KdNode *node;
347                                        object->mKdPvs.GetData(index, node, data);
348                                        nodeToSample = mKdTree->FindRandomNeighbor(node, true);
349                                        if (nodeToSample)
350                                                break;
351                                }
352                        }
353                       
354                        if (0 && pvsSize && mPass == 1000 ) {
355                                // mail all nodes from the pvs
356                                Intersectable::NewMail();
357                                KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
358                                for (; i != object->mKdPvs.mEntries.end(); i++) {
359                                        KdNode *node = (*i).first;
360                                        node->Mail();
361                                }
362                                Debug << "Get all neighbours from PVS" << endl;
363                                vector<KdNode *> invisibleNeighbors;
364                                // get all neighbors of all PVS nodes
365                                i = object->mKdPvs.mEntries.begin();
366                                for (; i != object->mKdPvs.mEntries.end(); i++) {
367                                        KdNode *node = (*i).first;
368                                        mKdTree->FindNeighbors(node, invisibleNeighbors, true);
369                                        AxisAlignedBox3 box = object->GetBox();
370                                        for (int j=0; j < invisibleNeighbors.size(); j++) {
371                                                int visibility = ComputeBoxVisibility(mSceneGraph,
372                                                                                                                                                                                                        mKdTree,
373                                                                                                                                                                                                        box,
374                                                                                                                                                                                                        mKdTree->GetBox(invisibleNeighbors[j]),
375                                                                                                                                                                                                        1e-6f);
376                                                //            exit(0);
377                                        }
378                                        // now rank all the neighbors according to probability that a new
379                                        // sample creates some contribution
380                                }
381                        }
382                       
383                        int faceIndex = object->GetRandomSurfacePoint(point, normal);
384                       
385                        long samplesPerObjStart = GetTime();
386
387                        bool viewcellSample = true;
388                        int sampleContributions;
389                        bool debug = false; //(object->GetId() >= 2199);
390                        if (viewcellSample) {
391                                nodeToSample = mKdTree->GetRandomLeaf(Plane3(normal, point));
392                                       
393                                for (int k=0; k < mSamplesPerPass; k++) {
394                                        if (nodeToSample) {
395                                                int maxTries = 5;
396                                               
397                                                for (int tries = 0; tries < maxTries; tries++) {
398                                                        direction = mKdTree->GetBox(nodeToSample).GetRandomPoint() - point;
399                                                       
400                                                        if (DotProd(direction, normal) > Limits::Small)
401                                                                break;                                                 
402                                                }
403                                                if (tries == maxTries)
404                                                        direction = UniformRandomVector(normal);
405                                               
406                                                if (debug) {
407                                                        cout<<
408                                                                "normal "<<normal<<endl<<
409                                                                "tries "<<tries<<endl<<
410                                                                "dir="<<direction<<endl;
411                                                }
412
413                                        }
414                                        else {
415                                                direction = UniformRandomVector(normal);
416                                        }
417                                       
418                                        // construct a ray
419                                        SetupRay(ray, point, direction, Ray::LOCAL_RAY);
420                       
421                                        sampleContributions = CastRay(object, ray);
422
423                                        //-- CORR matt: put block inside loop
424                                        if (sampleContributions) {
425                                                passContributingSamples ++;
426                                                passSampleContributions += sampleContributions;
427                                        }
428
429                                        if ( i < pvsOut )
430                                                rays[i].push_back(ray);
431               
432                                        if (!ray.intersections.empty()) {
433                                                // check whether we can add this to the rays
434                                                for (int j = 0; j < pvsOut; j++) {
435                                                        if (objects[j] == ray.intersections[0].mObject) {
436                                                                rays[j].push_back(ray);
437                                                        }
438                                                }
439                                        }
440                                        //-------------------
441
442                                        if (mViewCellsType == BSP_VIEW_CELLS)
443                                        {
444                                                // save rays for bsp tree construction
445                                                if (collectSamplesForBsp)
446                                                {
447                                                        // also add origin to sample in order to extract it as input polygons
448                                                        MeshInstance *mi = dynamic_cast<MeshInstance *>(object);
449                                                        ray.sourceObject = Ray::Intersection(0.0, mi, faceIndex);
450                                                       
451                            mSampleRays.push_back(new Ray(ray));
452                                                }
453                                                else
454                                                {
455                                                        // construct BSP tree using the samples
456                                                        if (!mBspTree)
457                                                        {
458                                                                BuildBspTree();
459
460                                                                cout << "generated " << (int)mViewCells.size() << " view cells" << endl;
461
462                                                                passContributingSamples += mBspTree->GetStat().contributingSamples;
463                                                                passSampleContributions += mBspTree->GetStat().sampleContributions;
464
465                                                                BspTreeStatistics(Debug);       
466                                                                Export("vc_bsptree.x3d", false, false, true);
467                                                        }
468                                                               
469                                                        // some random view cells for output
470                                                        if (pvsViewCells.empty())
471                                                        {
472                                                                int vcPvsOut = Min((int)mViewCells.size(), 5);
473                                                       
474                                                                for (int j = 0; j < vcPvsOut; ++ j)
475                                                                {
476                                                                        int idx = Random((int)mViewCells.size());
477                                                                        Debug << "output view cell=" << idx << endl;
478                                                                        pvsViewCells.push_back(mViewCells[Random((int)mViewCells.size())]);
479                                                                }
480                                                        }
481                                                        else
482                                                        {       
483                                                                // check whether we can add the current ray to the rays
484                                                                for (int k = 0; k < (int)ray.viewCells.size(); ++ k)
485                                                                        for (int j = 0; j < (int)pvsViewCells.size(); ++ j)
486                                                                                if (pvsViewCells[j] == ray.viewCells[k])
487                                                                                        vcRays[j].push_back(ray);                                                       
488                                                        }
489                                                }
490                                        }
491               
492                                }
493                        } else {
494                                // edge samples
495                                // get random visible mesh
496                                //                              object->GetRandomVisibleMesh(Plane3(normal, point));
497                        }
498                               
499                        // measure maximal time for samples per object
500                        Real t = TimeDiff(samplesPerObjStart, GetTime());
501
502                        if (t > maxTime)
503                        {
504                                maxTime = t;
505                                maxTimeIdx = i;
506                        }
507       
508                        // CORR matt: must add all samples
509                        passSamples += mSamplesPerPass;
510                }
511       
512                totalSamples += passSamples;
513               
514                //    if (pass>10)
515                //      HoleSamplingPass();
516   
517                mPass++;
518
519                int pvsSize = 0;
520       
521                if (mViewCellsType == BSP_VIEW_CELLS) {
522                        for (i=0; i < mViewCells.size(); i++) {
523                                ViewCell *vc = mViewCells[i];
524                                pvsSize += vc->GetPvs().GetSize();
525                        }
526                } else  {
527                        for (i=0; i < objects.size(); i++) {
528                                Intersectable *object = objects[i];
529                                pvsSize += object->mKdPvs.GetSize();
530                        }
531                }
532
533                Debug << "maximal time needed for pass: " << maxTime << " (object " << maxTimeIdx << ")" << endl;
534
535                float avgRayContrib = (passContributingSamples > 0) ?
536                        passSampleContributions/(float)passContributingSamples : 0;
537
538                cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
539                cout << "#TotalSamples=" << totalSamples/1000
540                                 << "k   #SampleContributions=" << passSampleContributions << " ("
541                                 << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
542                                 << pvsSize/(float)objects.size() << endl
543                                 << "avg ray contrib=" << avgRayContrib << endl;
544               
545                mStats <<
546                        "#Pass\n" <<mPass<<endl<<
547                        "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
548                        "#TotalSamples\n" << totalSamples<< endl<<
549                        "#SampleContributions\n" << passSampleContributions << endl <<
550                        "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
551                        "#AvgPVS\n"<< pvsSize/(float)objects.size() << endl <<
552                        "#AvgRayContrib\n" << avgRayContrib << endl;
553        }
554       
555        if (mViewCellsType == KD_VIEW_CELLS)   
556                cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
557 
558  //  HoleSamplingPass();
559  if (0) {
560    Exporter *exporter = Exporter::GetExporter("ray-density.x3d");
561    exporter->SetExportRayDensity(true);
562    exporter->ExportKdTree(*mKdTree);
563
564        if (mBspTree && (mViewCellsType == BSP_VIEW_CELLS))
565                exporter->ExportBspTree(*mBspTree);
566
567    delete exporter;
568  }
569 
570  bool exportRays = false;
571  if (exportRays) {
572    Exporter *exporter = NULL;
573    exporter = Exporter::GetExporter("sample-rays.x3d");
574    exporter->SetWireframe();
575    exporter->ExportKdTree(*mKdTree);
576        exporter->ExportBspTree(*mBspTree);
577
578    for (i=0; i < pvsOut; i++)
579      exporter->ExportRays(rays[i], 1000, RgbColor(1, 0, 0));
580    exporter->SetFilled();
581         
582    delete exporter;
583  }
584
585  //-- several visualizations and statistics
586  if (1) {
587  if (mBspTree && (mViewCellsType == BSP_VIEW_CELLS))
588  {
589          bool exportSplits = false;
590          environment->GetBoolValue("BspTree.exportSplits", exportSplits);
591
592          // export the bsp splits
593          if (exportSplits)
594                  ExportSplits(objects);
595                 
596          for (int j = 0; j < pvsViewCells.size(); ++ j)
597          {
598                  ViewCell *vc = pvsViewCells[j];
599
600                  Intersectable::NewMail();
601                  char s[64]; sprintf(s, "bsp-pvs%04d.x3d", j);
602
603                  Exporter *exporter = Exporter::GetExporter(s);
604                  exporter->SetFilled();
605
606                  ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin();
607
608                  Material m;//= RandomMaterial();
609                  m.mDiffuseColor = RgbColor(0, 1, 0);
610                  exporter->SetForcedMaterial(m);
611
612                  exporter->ExportViewCell(vc);
613
614                  Debug << j << ": pvs size=" << (int)vc->GetPvs().GetSize()
615                            << ", piercing rays=" << (int)vcRays[j].size() << endl;
616
617                  exporter->SetWireframe();
618
619                  // export view cells
620                  m.mDiffuseColor = RgbColor(1, 0, 1);
621                  exporter->SetForcedMaterial(m);
622                  exporter->ExportViewCells(mViewCells);
623                       
624                  // export rays piercing this view cell
625                  exporter->ExportRays(vcRays[j], 1000, RgbColor(0, 1, 0));
626
627                  m.mDiffuseColor = RgbColor(1, 0, 0);
628                  exporter->SetForcedMaterial(m);
629
630                  // output PVS of view cell
631                  for (; it != vc->GetPvs().mEntries.end(); ++ it)
632                  {
633                          Intersectable *intersect = (*it).first;
634                          if (!intersect->Mailed())
635                          {
636                                  exporter->ExportIntersectable(intersect);
637                                  intersect->Mail();
638                          }                     
639                  }
640               
641                  // output rest of the objects
642                  if (0)
643                  {
644                          Material m;//= RandomMaterial();
645                          m.mDiffuseColor = RgbColor(0, 0, 1);
646                          exporter->SetForcedMaterial(m);
647
648                          for (int j = 0; j < objects.size(); ++ j)
649                                  if (!objects[j]->Mailed())
650                                  {
651                                          //if (j == 2198)m.mDiffuseColor = RgbColor(1, 0, 1);
652                                          //else m.mDiffuseColor = RgbColor(1, 1, 0);
653                                          exporter->SetForcedMaterial(m);
654                                          exporter->ExportIntersectable(objects[j]);
655                                          objects[j]->Mail();
656                                  }
657                  }
658                  DEL_PTR(exporter);
659                }
660  } 
661
662   for (int k=0; k < pvsOut; k++) {
663      Intersectable *object = objects[k];
664      char s[64];
665      sprintf(s, "sample-pvs%04d.x3d", k);
666      Exporter *exporter = Exporter::GetExporter(s);
667      exporter->SetWireframe();
668
669       
670          KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
671          Intersectable::NewMail();
672                 
673          // avoid adding the object to the list
674          object->Mail();
675          ObjectContainer visibleObjects;
676
677          for (; i != object->mKdPvs.mEntries.end(); i++)
678          {
679                  KdNode *node = (*i).first;
680                  exporter->ExportBox(mKdTree->GetBox(node));
681                  mKdTree->CollectObjects(node, visibleObjects);
682          }
683
684          exporter->ExportRays(rays[k], 1000, RgbColor(0, 1, 0));
685          exporter->SetFilled();
686
687          for (int j = 0; j < visibleObjects.size(); j++)
688                  exporter->ExportIntersectable(visibleObjects[j]);
689       
690
691          Material m;
692          m.mDiffuseColor = RgbColor(1, 0, 0);
693          exporter->SetForcedMaterial(m);
694          exporter->ExportIntersectable(object);
695
696          delete exporter;
697    }
698  }
699 
700  return true;
701}
702
703
704void SamplingPreprocessor::ExportSplits(const ObjectContainer &objects)
705{
706        Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
707
708        if (exporter)
709        {       
710                cout << "exporting splits ... ";
711
712                Material m;
713                m.mDiffuseColor = RgbColor(1, 0, 0);
714                exporter->SetForcedMaterial(m);
715                exporter->SetWireframe();
716                exporter->ExportBspSplits(*mBspTree);
717
718                // take forced material, else big scenes cannot be viewed
719                m.mDiffuseColor = RgbColor(0, 1, 0);
720                exporter->SetForcedMaterial(m);
721                exporter->SetFilled();
722
723                exporter->ResetForcedMaterial();
724               
725                // export rays
726                if (0)
727                {
728                        RayContainer outRays;
729               
730                        for (int i = 0; i < mSampleRays.size(); ++ i)
731                        {
732                                // only rays piercing geometry
733                                if (!mSampleRays[i]->intersections.empty())
734                                        outRays.push_back(mSampleRays[i]);
735                        }
736                        if (BspTree::sConstructionMethod == BspTree::FROM_RAYS)
737                        {
738                                // export rays
739                                exporter->ExportRays(outRays, 1000, RgbColor(1, 1, 0));
740                        }
741                }
742
743                // export scene geometry
744                if (1)
745                {
746                        Material m;//= RandomMaterial();
747                        m.mDiffuseColor = RgbColor(0, 1, 0);
748                        exporter->SetForcedMaterial(m);
749            exporter->SetWireframe();
750
751                        for (int j = 0; j < objects.size(); ++ j)
752                                exporter->ExportIntersectable(objects[j]);
753                }
754
755                delete exporter;
756
757                cout << "finished" << endl;
758        }
759}
Note: See TracBrowser for help on using the repository browser.