source: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp @ 600

Revision 600, 20.4 KB checked in by mattausch, 18 years ago (diff)
Line 
1#include "SceneGraph.h"
2#include "KdTree.h"
3#include "VssPreprocessor.h"
4#include "X3dExporter.h"
5#include "Environment.h"
6#include "MutualVisibility.h"
7#include "Polygon3.h"
8#include "ViewCell.h"
9#include "VssRay.h"
10#include "VssTree.h"
11#include "ViewCellsManager.h"
12#include "RenderSimulator.h"
13#include "Beam.h"
14#include "GlRenderer.h"
15
16bool use2dSampling = false;
17bool useViewspacePlane = false;
18
19VssPreprocessor::VssPreprocessor():
20  mPass(0),
21  mVssRays()
22{
23  // this should increase coherence of the samples
24  environment->GetIntValue("VssPreprocessor.samplesPerPass", mSamplesPerPass);
25  environment->GetIntValue("VssPreprocessor.initialSamples", mInitialSamples);
26  environment->GetIntValue("VssPreprocessor.vssSamples", mVssSamples);
27  environment->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass);
28  environment->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling);
29 
30  environment->GetBoolValue("VssPreprocessor.loadInitialSamples", mLoadInitialSamples);
31  environment->GetBoolValue("VssPreprocessor.storeInitialSamples", mStoreInitialSamples);
32  environment->GetBoolValue("VssPreprocessor.useViewSpaceBox", mUseViewSpaceBox);
33  environment->GetBoolValue("VssPreprocessor.testBeamSampling", mTestBeamSampling);
34
35  useViewspacePlane = mUseViewSpaceBox; //hack
36
37  mStats.open("stats.log");
38}
39
40VssPreprocessor::~VssPreprocessor()
41{
42  CLEAR_CONTAINER(mVssRays);
43  DEL_PTR(mViewSpaceBox);
44}
45
46void
47VssPreprocessor::SetupRay(Ray &ray,
48                                                  const Vector3 &point,
49                                                  const Vector3 &direction
50                                                  )
51{
52  ray.Clear();
53  // do not store anything else then intersections at the ray
54  ray.Init(point, direction, Ray::LOCAL_RAY);
55}
56
57
58void
59VssPreprocessor::CastRays(
60                                                  SimpleRayContainer &rays,
61                                                  VssRayContainer &vssRays
62                                                  )
63{
64  for (int i=0; i < rays.size(); i++)
65        CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
66}
67
68
69int
70VssPreprocessor::CastRay(
71                                                 Vector3 &viewPoint,
72                                                 Vector3 &direction,
73                                                 VssRayContainer &vssRays
74                                                 )
75{
76
77    int hits = 0;
78  static Ray ray;
79  AxisAlignedBox3 box =  mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
80
81  AxisAlignedBox3 sbox = box;
82  sbox.Enlarge(Vector3(-Limits::Small));
83  if (!sbox.IsInside(viewPoint))
84        return 0;
85       
86  SetupRay(ray, viewPoint, direction);
87  // cast ray to KD tree to find intersection with other objects
88  Intersectable *objectA, *objectB;
89  Vector3 pointA, pointB;
90  float bsize = Magnitude(box.Size());
91
92
93        if (!mDetectEmptyViewSpace)
94          ray.mFlags &= ~Ray::CULL_BACKFACES;
95        else
96          ray.mFlags |= Ray::CULL_BACKFACES;
97
98  if (mKdTree->CastRay(ray)) {
99        objectA = ray.intersections[0].mObject;
100        pointA = ray.Extrap(ray.intersections[0].mT);
101  } else {
102        objectA = NULL;
103        // compute intersection with the scene bounding box
104        float tmin, tmax;
105        if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
106          pointA = ray.Extrap(tmax);
107        else
108          return 0;
109  }
110
111       
112  if (mDetectEmptyViewSpace) {
113        SetupRay(ray, pointA, -direction);
114  } else
115        SetupRay(ray, viewPoint, -direction);
116 
117  if (!mDetectEmptyViewSpace)
118        ray.mFlags &= ~Ray::CULL_BACKFACES;
119  else
120         ray.mFlags |= Ray::CULL_BACKFACES;
121
122  if (mKdTree->CastRay(ray)) {
123        objectB = ray.intersections[0].mObject;
124        pointB = ray.Extrap(ray.intersections[0].mT);
125  } else {
126        objectB = NULL;
127        float tmin, tmax;
128        if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
129          pointB = ray.Extrap(tmax);
130        else
131          return 0;
132  }
133 
134  //  if (objectA == NULL && objectB != NULL) {
135  if (mDetectEmptyViewSpace) {
136        // cast again to ensure that there is no objectA
137        SetupRay(ray, pointB, direction);
138          ray.mFlags |= Ray::CULL_BACKFACES;
139        if (mKdTree->CastRay(ray)) {
140          objectA = ray.intersections[0].mObject;
141          pointA = ray.Extrap(ray.intersections[0].mT);
142        }
143  }
144
145 
146  VssRay *vssRay  = NULL;
147
148  bool validSample = (objectA != objectB);
149  if (0 && mDetectEmptyViewSpace) {   // consider all samples valid
150        // check if the viewpoint lies on the line segment AB
151        if (Distance(pointA, pointB) <
152                Distance(viewPoint, pointA) + Distance(viewPoint, pointB) - Limits::Small) {
153          validSample = false;
154        }
155  }
156       
157  if (validSample) {
158        if (objectA) {
159          vssRay = new VssRay(pointB,
160                                                  pointA,
161                                                  objectB,
162                                                  objectA,
163                                                  mPass
164                                                  );
165          vssRays.push_back(vssRay);
166          hits ++;
167        }
168       
169        if (objectB) {
170          vssRay = new VssRay(pointA,
171                                                  pointB,
172                                                  objectA,
173                                                  objectB,
174                                                  mPass
175                                                  );
176          vssRays.push_back(vssRay);
177          hits ++;
178        }
179  }
180       
181  return hits;
182}
183
184
185Vector3
186VssPreprocessor::GetViewpoint(AxisAlignedBox3 *viewSpaceBox)
187{
188  AxisAlignedBox3 box;
189
190  if (viewSpaceBox)
191        box =*viewSpaceBox;
192  else
193        box = mKdTree->GetBox();
194
195  // shrink the box in the y direction
196  return box.GetRandomPoint();
197}
198
199Vector3
200VssPreprocessor::GetDirection(const Vector3 &viewpoint,
201                                                          AxisAlignedBox3 *viewSpaceBox
202                                                          )
203{
204  Vector3 point;
205  if (!use2dSampling)
206  {
207          if (0)
208          {
209                  Vector3 normal;
210                  int i = Random((int)mObjects.size());
211                  Intersectable *object = mObjects[i];
212                  object->GetRandomSurfacePoint(point, normal);
213          }
214          else
215                  point = mKdTree->GetBox().GetRandomPoint();
216        //        point = viewpoint + UniformRandomVector();
217  }
218  else
219  {
220          AxisAlignedBox3 box;
221
222          if (viewSpaceBox)
223                  box =*viewSpaceBox;
224          else
225                  box = mKdTree->GetBox();
226
227          point = box.GetRandomPoint();
228          point.y = viewpoint.y;
229  }
230
231  return point - viewpoint;
232}
233
234int
235VssPreprocessor::GenerateImportanceRays(VssTree *vssTree,
236                                                                                const int desiredSamples,
237                                                                                SimpleRayContainer &rays
238                                                                                )
239{
240  int num;
241  if (0) {
242        float minRayContribution;
243        float maxRayContribution;
244        float avgRayContribution;
245
246        vssTree->GetRayContributionStatistics(minRayContribution,
247                                                                                  maxRayContribution,
248                                                                                  avgRayContribution);
249
250        cout<<
251          "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<<
252          "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<<
253          "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl;
254
255        float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves());
256        num = vssTree->GenerateRays(p, rays);
257  } else {
258        int leaves = vssTree->stat.Leaves();
259        num = vssTree->GenerateRays(desiredSamples, leaves, rays);
260  }
261
262  cout<<"Generated "<<num<<" rays."<<endl;
263
264  return num;
265}
266
267
268bool
269VssPreprocessor::ExportRays(const char *filename,
270                                                        const VssRayContainer &vssRays,
271                                                        const int number
272                                                        )
273{
274  cout<<"Exporting vss rays..."<<endl<<flush;
275
276  Exporter *exporter = NULL;
277  exporter = Exporter::GetExporter(filename);
278  //    exporter->SetWireframe();
279  //    exporter->ExportKdTree(*mKdTree);
280  exporter->SetFilled();
281  exporter->ExportScene(mSceneGraph->mRoot);
282  exporter->SetWireframe();
283
284  if (mViewSpaceBox) {
285        exporter->SetForcedMaterial(RgbColor(1,0,1));
286        exporter->ExportBox(*mViewSpaceBox);
287        exporter->ResetForcedMaterial();
288  }
289
290  VssRayContainer rays;
291  vssRays.SelectRays(number, rays);
292 
293  exporter->ExportRays(rays, RgbColor(1, 0, 0));
294
295  delete exporter;
296
297  cout<<"done."<<endl<<flush;
298
299  return true;
300}
301
302
303bool
304VssPreprocessor::ExportVssTree(char *filename,
305                                                           VssTree *tree,
306                                                           const Vector3 &dir
307                                                           )
308{
309  Exporter *exporter = Exporter::GetExporter(filename);
310  exporter->SetFilled();
311  exporter->ExportScene(mSceneGraph->mRoot);
312  //  exporter->SetWireframe();
313  bool result = exporter->ExportVssTree2( *tree, dir );
314  delete exporter;
315  return result;
316}
317
318bool
319VssPreprocessor::ExportVssTreeLeaf(char *filename,
320                                                                   VssTree *tree,
321                                                                   VssTreeLeaf *leaf)
322{
323  Exporter *exporter = NULL;
324  exporter = Exporter::GetExporter(filename);
325  exporter->SetWireframe();
326  exporter->ExportKdTree(*mKdTree);
327
328  if (mViewSpaceBox) {
329        exporter->SetForcedMaterial(RgbColor(1,0,0));
330        exporter->ExportBox(*mViewSpaceBox);
331        exporter->ResetForcedMaterial();
332  }
333
334  exporter->SetForcedMaterial(RgbColor(0,0,1));
335  exporter->ExportBox(tree->GetBBox(leaf));
336  exporter->ResetForcedMaterial();
337
338  VssRayContainer rays[4];
339  for (int i=0; i < leaf->rays.size(); i++) {
340        int k = leaf->rays[i].GetRayClass();
341        rays[k].push_back(leaf->rays[i].mRay);
342  }
343
344  // SOURCE RAY
345  exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
346  // TERMINATION RAY
347  exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
348  // PASSING_RAY
349  exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
350  // CONTAINED_RAY
351  exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
352
353  delete exporter;
354  return true;
355}
356
357void
358VssPreprocessor::ExportVssTreeLeaves(VssTree *tree, const int number)
359{
360  vector<VssTreeLeaf *> leaves;
361  tree->CollectLeaves(leaves);
362
363  int num = 0;
364  int i;
365  float p = number / (float)leaves.size();
366  for (i=0; i < leaves.size(); i++) {
367        if (RandomValue(0,1) < p) {
368          char filename[64];
369          sprintf(filename, "vss-leaf-%04d.x3d", num);
370          ExportVssTreeLeaf(filename, tree, leaves[i]);
371          num++;
372        }
373        if (num >= number)
374          break;
375  }
376}
377#include "ViewCellBsp.h"
378
379void VssPreprocessor::TestBeamCasting(VssTree *tree,
380                                                                          ViewCellsManager *vm,
381                                                                          const ObjectContainer &objects)
382{
383        //debuggerWidget = new GlDebuggerWidget(renderer);
384        //  renderer->resize(640, 480);
385        //debuggerWidget->resize(640, 480);
386
387        vector<VssTreeLeaf *> leaves;
388        tree->CollectLeaves(leaves);
389
390        Exporter *exporter = Exporter::GetExporter("shafts.x3d");
391
392        exporter->SetWireframe();
393        exporter->ExportGeometry(objects);
394        exporter->SetFilled();
395        //Randomize();
396        debuggerWidget = new GlDebuggerWidget(renderer);
397
398        /*debuggerWidget->mBeam = beam;
399        debuggerWidget->mSourceObject = sourceObj;
400        debuggerWidget->mSamples = 10000;
401       
402        Debug << "showing window" << endl;
403        debuggerWidget->show();*/
404       
405        renderer->makeCurrent();
406
407        for (int i = 0; i < 10; ++ i)
408        {
409                Beam beam;
410                Intersectable *sourceObj = mObjects[5];
411
412                const int index = (int)RandomValue(0, (Real)((int)leaves.size() - 1));
413                VssTreeLeaf *leaf = leaves[index];
414
415                AxisAlignedBox3 dirBox = tree->GetDirBBox(leaf);
416                AxisAlignedBox3 box = tree->GetBBox(leaf);
417               
418                beam.Construct(box, dirBox);
419
420                // collect kd leaves and view cells
421                mKdTree->CastBeam(beam);
422                vm->CastBeam(beam);
423
424                Debug << "found " << (int)beam.mViewCells.size() << " view cells and "
425                          << (int)beam.mKdNodes.size() << " kd nodes" << endl;
426
427                BeamSampleStatistics stats;
428
429                renderer->SampleBeamContributions(sourceObj,
430                                                                                  beam,
431                                                                                  200000,
432                                                                                  stats);
433
434                char s[64]; sprintf(s, "shaft%04d.png", i);
435
436                QImage image = renderer->toImage();
437                image.save(s, "PNG");
438                Debug << "beam statistics: " << stats << endl << endl;
439
440                if (1)
441                {
442                        AxisAlignedBox3 sbox = mSceneGraph->GetBox();
443                        Vector3 bmin = sbox.Min() - 150.0f;
444                        Vector3 bmax = sbox.Max() + 150.0f;
445                        AxisAlignedBox3 vbox(bmin, bmax);
446               
447                        exporter->ExportBeam(beam, vbox);
448                }
449
450                bool exportViewCells = false;
451               
452                if (exportViewCells)
453                {
454                        ViewCellContainer::const_iterator it, it_end = beam.mViewCells.end();
455                       
456                        for (it = beam.mViewCells.begin(); it != beam.mViewCells.end(); ++ it)
457                        {
458                                BspNodeGeometry geom;
459                                AxisAlignedBox3 vbox;
460                                vbox.Initialize();
461                                vbox.Include((*it)->GetMesh());
462                       
463                                exporter->SetWireframe();
464                                exporter->ExportBox(vbox);
465                                exporter->SetFilled();
466                                exporter->ExportViewCell(*it);
467                        }
468
469                        /*vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
470                       
471                        for (it = beam.mKdNodes.begin(); it != beam.mKdNodes.end(); ++ it)
472                        {
473                                exporter->ExportBox(mKdTree->GetBox((*it)));
474                        }*/
475                }
476        }
477        /*while (1)
478        { debuggerWidget->repaint();
479        };*/
480        delete exporter;
481}
482
483
484float
485VssPreprocessor::GetAvgPvsSize(VssTree *tree,
486                                                           const vector<AxisAlignedBox3> &viewcells
487                                                           )
488{
489  vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
490
491  int sum = 0;
492  for (it = viewcells.begin(); it != it_end; ++ it)
493        sum += tree->GetPvsSize(*it);
494
495  return sum/(float)viewcells.size();
496}
497
498bool
499VssPreprocessor::ComputeVisibility()
500{
501        Debug << "type: vss" << endl;
502
503  long startTime = GetTime();
504
505  int totalSamples = 0;
506
507
508  AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox());
509 
510  if (!useViewspacePlane) {
511        float size = 0.05f;
512        float s = 0.5f - size;
513        float olds = Magnitude(box->Size());
514        box->Enlarge(box->Size()*Vector3(-s));
515        Vector3 translation = Vector3(-olds*0.1f, 0, 0);
516        box->SetMin(box->Min() + translation);
517        box->SetMax(box->Max() + translation);
518  } else {
519
520        // sample city like heights
521        box->SetMin(1, box->Min(1) + box->Size(1)*0.2f);
522        box->SetMax(1, box->Min(1) + box->Size(1)*0.3f);
523  }
524
525  if (use2dSampling)
526        box->SetMax(1, box->Min(1));
527
528  cout<<"mUseViewSpaceBox="<<mUseViewSpaceBox<<endl;
529  Debug << "use view space box=" << mUseViewSpaceBox << endl;
530
531  if (mUseViewSpaceBox)
532  {
533          if (0)
534                  mViewSpaceBox = box;
535          else
536          {
537                // HACK: enlarge in y directon
538                mViewSpaceBox = new AxisAlignedBox3(mKdTree->GetBox());
539                //Vector3 pmin = mViewSpaceBox->Min();
540                Vector3 size = mViewSpaceBox->Size();
541                //size[1] *= 1.25;
542                Vector3 enlarge(size[0] * 0.25f, size[1] * 0.0f, size[2] * 0.25f);
543
544                mViewSpaceBox->Enlarge(enlarge);
545          }
546
547          //Debug << "view space box: " << *mViewSpaceBox << endl;
548  }
549  else
550  {
551          mViewSpaceBox = NULL;
552  }
553 
554  AxisAlignedBox3 vbox = mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
555
556  mSceneGraph->CollectObjects(&mObjects);
557
558  //-- load view cells from file if requested
559  if (!mLoadViewCells)
560  {
561          mViewCellsManager->SetViewSpaceBox(vbox);
562          // construct view cells using it's own set of samples
563          mViewCellsManager->Construct(this);
564
565          //-- several visualizations and statistics
566          Debug << "view cells construction finished: " << endl;
567          mViewCellsManager->PrintStatistics(Debug);
568  }
569  else
570  {
571         
572          VssRayContainer dummies;
573          mViewCellsManager->Visualize(mObjects, dummies);
574          mViewCellsManager->ExportViewCells("test.xml");
575  }
576  VssTree *vssTree = NULL;
577
578 
579
580  long initialTime = GetTime();
581
582  if (mLoadInitialSamples)
583  {
584          cout << "Loading samples from file ... ";
585          LoadSamples(mVssRays, mObjects);
586          cout << "finished\n" << endl;
587          totalSamples = (int)mVssRays.size();
588  }
589  else
590  {
591       
592        while (totalSamples < mInitialSamples) {
593                int passContributingSamples = 0;
594                int passSampleContributions = 0;
595                int passSamples = 0;
596
597                int index = 0;
598
599                int sampleContributions;
600
601                int s = Min(mSamplesPerPass, mInitialSamples);
602                for (int k=0; k < s; k++) {
603                        // changed by matt
604                        Vector3 viewpoint;
605                        //                      viewpoint = GetViewpoint(mViewSpaceBox);
606                        mViewCellsManager->GetViewPoint(viewpoint);
607                        Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
608
609                        sampleContributions = CastRay(viewpoint, direction, mVssRays);
610
611                        if (sampleContributions) {
612                                passContributingSamples ++;
613                                passSampleContributions += sampleContributions;
614                        }
615                        passSamples++;
616                        totalSamples++;
617                }
618
619                mPass++;
620                int pvsSize = 0;
621                float avgRayContrib = (passContributingSamples > 0) ?
622                        passSampleContributions/(float)passContributingSamples : 0;
623
624                cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
625                cout << "#TotalSamples=" << totalSamples/1000
626                        << "k   #SampleContributions=" << passSampleContributions << " ("
627                        << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
628                        << pvsSize/(float)mObjects.size() << endl
629                        << "avg ray contrib=" << avgRayContrib << endl;
630
631                mStats <<
632                        "#Pass\n" <<mPass<<endl<<
633                        "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
634                        "#TotalSamples\n" << totalSamples<< endl<<
635                        "#SampleContributions\n" << passSampleContributions << endl <<
636                        "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
637                        "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
638                        "#AvgRayContrib\n" << avgRayContrib << endl;
639          }
640 
641          cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
642
643
644         
645  }
646 
647
648  cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl << flush;
649  Debug << (int)mVssRays.size() << " rays generated in "
650            << TimeDiff(initialTime, GetTime()) * 1e-3 << " seconds" << endl;
651
652  if (mStoreInitialSamples)
653  {
654          cout << "Writing " << (int)mVssRays.size() << " samples to file ... ";
655          ExportSamples(mVssRays);
656          cout << "finished\n" << endl;
657
658          /*VssRayContainer dummyRays;
659          LoadSamples(dummyRays, mObjects);
660          Debug << "rays " << (int)mVssRays.size() << " " << dummyRays.size() << endl;
661
662          for (int i = 0; i < (int)mVssRays.size(); ++ i)
663          {
664                  Debug << mVssRays[i]->GetOrigin() << " " << mVssRays[i]->GetTermination() << " " << mVssRays[i]->mOriginObject << " " << mVssRays[i]->mTerminationObject << endl;
665                  Debug << dummyRays[i]->GetOrigin() << " " << dummyRays[i]->GetTermination() << " " << dummyRays[i]->mOriginObject << " " << dummyRays[i]->mTerminationObject << endl << endl;
666          }*/
667  }
668
669 
670  //int numExportRays = 5000;
671  int numExportRays = 0;
672
673  if (numExportRays) {
674        char filename[64];
675        sprintf(filename, "vss-rays-initial.x3d");
676        ExportRays(filename, mVssRays, numExportRays);
677  }
678
679  vssTree = new VssTree;
680  // viewcells = Construct(mVssRays);
681
682  vssTree->Construct(mVssRays, mViewSpaceBox);
683  cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
684
685  if (0)
686  {
687          ExportVssTree("vss-tree-100.x3d", vssTree, Vector3(1,0,0));
688          ExportVssTree("vss-tree-001.x3d", vssTree, Vector3(0,0,1));
689          ExportVssTree("vss-tree-101.x3d", vssTree, Vector3(1,0,1));
690          ExportVssTree("vss-tree-101m.x3d", vssTree, Vector3(-1,0,-1));
691          ExportVssTreeLeaves(vssTree, 10);
692  }
693
694  // viewcells->UpdatePVS(newVssRays);
695  // get viewcells as kd tree boxes
696  vector<AxisAlignedBox3> kdViewcells;
697  if (0) {
698        vector<KdLeaf *> leaves;
699        mKdTree->CollectLeaves(leaves);
700        vector<KdLeaf *>::const_iterator it;
701        int targetLeaves = 50;
702        float prob = targetLeaves/(float)leaves.size();
703        for (it = leaves.begin(); it != leaves.end(); ++it)
704          if (RandomValue(0.0f,1.0f) < prob)
705                kdViewcells.push_back(mKdTree->GetBox(*it));
706
707        float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
708        cout<<"Initial average PVS size = "<<avgPvs<<endl;
709  }
710
711
712  int samples = 0;
713  int pass = 0;
714
715 
716  // cast view cell samples
717  while (samples < mVssSamples)
718  {
719       
720        int num = mVssSamplesPerPass;
721        SimpleRayContainer rays;
722        VssRayContainer vssRays;
723
724        if (!mUseImportanceSampling) {
725          for (int j=0; j < num; j++) {
726            // changed by matt
727                //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
728                Vector3 viewpoint;
729                mViewCellsManager->GetViewPoint(viewpoint);
730                Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
731                rays.push_back(SimpleRay(viewpoint, direction));
732          }
733        } else {
734          num = GenerateImportanceRays(vssTree, num, rays);
735        }
736
737        for (int i=0; i < rays.size(); i++)
738          CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
739
740        vssTree->AddRays(vssRays);
741
742        if (0) {
743          int subdivided = vssTree->UpdateSubdivision();
744          cout<<"subdivided leafs = "<<subdivided<<endl;
745        }
746
747        float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
748        cout<<"Average PVS size = "<<avgPvs<<endl;
749
750        /// compute view cell contribution of rays
751        mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
752       
753        if (numExportRays) {
754          char filename[64];
755          if (mUseImportanceSampling)
756                sprintf(filename, "vss-rays-i%04d.x3d", pass);
757          else
758                sprintf(filename, "vss-rays-%04d.x3d", pass);
759
760          ExportRays(filename, vssRays, numExportRays);
761        }
762
763        samples+=num;
764        float pvs = vssTree->GetAvgPvsSize();
765        cout<<"*****************************\n";
766        cout<<samples<<" avgPVS ="<<pvs<<endl;
767        cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
768        cout<<"*****************************\n";
769//      if (samples >= mVssSamples) break;
770        pass ++;
771  }
772
773  if (mTestBeamSampling && mUseGlRenderer)
774  {     
775          TestBeamCasting(vssTree, mViewCellsManager, mObjects);
776  }
777
778  if (0)
779          Debug << vssTree->stat << endl;
780
781  if (0)
782  {
783        VssRayContainer viewCellRays;
784 
785        // compute rays used for view cells construction
786        const int numRays = mViewCellsManager->GetVisualizationSamples();
787
788        vssTree->CollectRays(viewCellRays, numRays);
789  }
790
791  //-- render simulation after merge
792  cout << "\nevaluating bsp view cells render time after sampling ... ";
793  mRenderSimulator->RenderScene();
794  SimulationStatistics ss;
795  mRenderSimulator->GetStatistics(ss);
796 
797  cout << " finished" << endl;
798  cout << ss << endl;
799  Debug << ss << endl;
800
801  delete vssTree;
802 
803  return true;
804}
Note: See TracBrowser for help on using the repository browser.