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

Revision 598, 20.3 KB checked in by mattausch, 18 years ago (diff)

added cutting plane and enlarging view space

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 = 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        if (0) {
207          Vector3 normal;
208          int i = Random((int)mObjects.size());
209          Intersectable *object = mObjects[i];
210          object->GetRandomSurfacePoint(point, normal);
211        } else
212          point = mKdTree->GetBox().GetRandomPoint();
213        //        point = viewpoint + UniformRandomVector();
214
215  } else {
216        AxisAlignedBox3 box;
217
218        if (viewSpaceBox)
219          box =*viewSpaceBox;
220        else
221          box = mKdTree->GetBox();
222
223        point = box.GetRandomPoint();
224        point.y = viewpoint.y;
225  }
226
227  return point - viewpoint;
228}
229
230int
231VssPreprocessor::GenerateImportanceRays(VssTree *vssTree,
232                                                                                const int desiredSamples,
233                                                                                SimpleRayContainer &rays
234                                                                                )
235{
236  int num;
237  if (0) {
238        float minRayContribution;
239        float maxRayContribution;
240        float avgRayContribution;
241
242        vssTree->GetRayContributionStatistics(minRayContribution,
243                                                                                  maxRayContribution,
244                                                                                  avgRayContribution);
245
246        cout<<
247          "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<<
248          "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<<
249          "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl;
250
251        float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves());
252        num = vssTree->GenerateRays(p, rays);
253  } else {
254        int leaves = vssTree->stat.Leaves();
255        num = vssTree->GenerateRays(desiredSamples, leaves, rays);
256  }
257
258  cout<<"Generated "<<num<<" rays."<<endl;
259
260  return num;
261}
262
263
264bool
265VssPreprocessor::ExportRays(const char *filename,
266                                                        const VssRayContainer &vssRays,
267                                                        const int number
268                                                        )
269{
270  cout<<"Exporting vss rays..."<<endl<<flush;
271
272  Exporter *exporter = NULL;
273  exporter = Exporter::GetExporter(filename);
274  //    exporter->SetWireframe();
275  //    exporter->ExportKdTree(*mKdTree);
276  exporter->SetFilled();
277  exporter->ExportScene(mSceneGraph->mRoot);
278  exporter->SetWireframe();
279
280  if (mViewSpaceBox) {
281        exporter->SetForcedMaterial(RgbColor(1,0,1));
282        exporter->ExportBox(*mViewSpaceBox);
283        exporter->ResetForcedMaterial();
284  }
285
286  VssRayContainer rays;
287  vssRays.SelectRays(number, rays);
288 
289  exporter->ExportRays(rays, RgbColor(1, 0, 0));
290
291  delete exporter;
292
293  cout<<"done."<<endl<<flush;
294
295  return true;
296}
297
298
299bool
300VssPreprocessor::ExportVssTree(char *filename,
301                                                           VssTree *tree,
302                                                           const Vector3 &dir
303                                                           )
304{
305  Exporter *exporter = Exporter::GetExporter(filename);
306  exporter->SetFilled();
307  exporter->ExportScene(mSceneGraph->mRoot);
308  //  exporter->SetWireframe();
309  bool result = exporter->ExportVssTree2( *tree, dir );
310  delete exporter;
311  return result;
312}
313
314bool
315VssPreprocessor::ExportVssTreeLeaf(char *filename,
316                                                                   VssTree *tree,
317                                                                   VssTreeLeaf *leaf)
318{
319  Exporter *exporter = NULL;
320  exporter = Exporter::GetExporter(filename);
321  exporter->SetWireframe();
322  exporter->ExportKdTree(*mKdTree);
323
324  if (mViewSpaceBox) {
325        exporter->SetForcedMaterial(RgbColor(1,0,0));
326        exporter->ExportBox(*mViewSpaceBox);
327        exporter->ResetForcedMaterial();
328  }
329
330  exporter->SetForcedMaterial(RgbColor(0,0,1));
331  exporter->ExportBox(tree->GetBBox(leaf));
332  exporter->ResetForcedMaterial();
333
334  VssRayContainer rays[4];
335  for (int i=0; i < leaf->rays.size(); i++) {
336        int k = leaf->rays[i].GetRayClass();
337        rays[k].push_back(leaf->rays[i].mRay);
338  }
339
340  // SOURCE RAY
341  exporter->ExportRays(rays[0], RgbColor(1, 0, 0));
342  // TERMINATION RAY
343  exporter->ExportRays(rays[1], RgbColor(1, 1, 1));
344  // PASSING_RAY
345  exporter->ExportRays(rays[2], RgbColor(1, 1, 0));
346  // CONTAINED_RAY
347  exporter->ExportRays(rays[3], RgbColor(0, 0, 1));
348
349  delete exporter;
350  return true;
351}
352
353void
354VssPreprocessor::ExportVssTreeLeaves(VssTree *tree, const int number)
355{
356  vector<VssTreeLeaf *> leaves;
357  tree->CollectLeaves(leaves);
358
359  int num = 0;
360  int i;
361  float p = number / (float)leaves.size();
362  for (i=0; i < leaves.size(); i++) {
363        if (RandomValue(0,1) < p) {
364          char filename[64];
365          sprintf(filename, "vss-leaf-%04d.x3d", num);
366          ExportVssTreeLeaf(filename, tree, leaves[i]);
367          num++;
368        }
369        if (num >= number)
370          break;
371  }
372}
373#include "ViewCellBsp.h"
374
375void VssPreprocessor::TestBeamCasting(VssTree *tree,
376                                                                          ViewCellsManager *vm,
377                                                                          const ObjectContainer &objects)
378{
379        //debuggerWidget = new GlDebuggerWidget(renderer);
380        //  renderer->resize(640, 480);
381        //debuggerWidget->resize(640, 480);
382
383        vector<VssTreeLeaf *> leaves;
384        tree->CollectLeaves(leaves);
385
386        Exporter *exporter = Exporter::GetExporter("shafts.x3d");
387
388        exporter->SetWireframe();
389        exporter->ExportGeometry(objects);
390        exporter->SetFilled();
391        //Randomize();
392        debuggerWidget = new GlDebuggerWidget(renderer);
393
394        /*debuggerWidget->mBeam = beam;
395        debuggerWidget->mSourceObject = sourceObj;
396        debuggerWidget->mSamples = 10000;
397       
398        Debug << "showing window" << endl;
399        debuggerWidget->show();*/
400       
401        renderer->makeCurrent();
402
403        for (int i = 0; i < 10; ++ i)
404        {
405                Beam beam;
406                Intersectable *sourceObj = mObjects[5];
407
408                const int index = (int)RandomValue(0, (Real)((int)leaves.size() - 1));
409                VssTreeLeaf *leaf = leaves[index];
410
411                AxisAlignedBox3 dirBox = tree->GetDirBBox(leaf);
412                AxisAlignedBox3 box = tree->GetBBox(leaf);
413               
414                beam.Construct(box, dirBox);
415
416                // collect kd leaves and view cells
417                mKdTree->CastBeam(beam);
418                vm->CastBeam(beam);
419
420                Debug << "found " << (int)beam.mViewCells.size() << " view cells and "
421                          << (int)beam.mKdNodes.size() << " kd nodes" << endl;
422
423                BeamSampleStatistics stats;
424
425                renderer->SampleBeamContributions(sourceObj,
426                                                                                  beam,
427                                                                                  200000,
428                                                                                  stats);
429
430                char s[64]; sprintf(s, "shaft%04d.png", i);
431
432                QImage image = renderer->toImage();
433                image.save(s, "PNG");
434                Debug << "beam statistics: " << stats << endl << endl;
435
436                if (1)
437                {
438                        AxisAlignedBox3 sbox = mSceneGraph->GetBox();
439                        Vector3 bmin = sbox.Min() - 150.0f;
440                        Vector3 bmax = sbox.Max() + 150.0f;
441                        AxisAlignedBox3 vbox(bmin, bmax);
442               
443                        exporter->ExportBeam(beam, vbox);
444                }
445
446                bool exportViewCells = false;
447               
448                if (exportViewCells)
449                {
450                        ViewCellContainer::const_iterator it, it_end = beam.mViewCells.end();
451                       
452                        for (it = beam.mViewCells.begin(); it != beam.mViewCells.end(); ++ it)
453                        {
454                                BspNodeGeometry geom;
455                                AxisAlignedBox3 vbox;
456                                vbox.Initialize();
457                                vbox.Include((*it)->GetMesh());
458                       
459                                exporter->SetWireframe();
460                                exporter->ExportBox(vbox);
461                                exporter->SetFilled();
462                                exporter->ExportViewCell(*it);
463                        }
464
465                        /*vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
466                       
467                        for (it = beam.mKdNodes.begin(); it != beam.mKdNodes.end(); ++ it)
468                        {
469                                exporter->ExportBox(mKdTree->GetBox((*it)));
470                        }*/
471                }
472        }
473        /*while (1)
474        { debuggerWidget->repaint();
475        };*/
476        delete exporter;
477}
478
479
480float
481VssPreprocessor::GetAvgPvsSize(VssTree *tree,
482                                                           const vector<AxisAlignedBox3> &viewcells
483                                                           )
484{
485  vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end();
486
487  int sum = 0;
488  for (it = viewcells.begin(); it != it_end; ++ it)
489        sum += tree->GetPvsSize(*it);
490
491  return sum/(float)viewcells.size();
492}
493
494bool
495VssPreprocessor::ComputeVisibility()
496{
497        Debug << "type: vss" << endl;
498
499  long startTime = GetTime();
500
501  int totalSamples = 0;
502
503
504  AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox());
505 
506  if (!useViewspacePlane) {
507        float size = 0.05f;
508        float s = 0.5f - size;
509        float olds = Magnitude(box->Size());
510        box->Enlarge(box->Size()*Vector3(-s));
511        Vector3 translation = Vector3(-olds*0.1f, 0, 0);
512        box->SetMin(box->Min() + translation);
513        box->SetMax(box->Max() + translation);
514  } else {
515
516        // sample city like heights
517        box->SetMin(1, box->Min(1) + box->Size(1)*0.2f);
518        box->SetMax(1, box->Min(1) + box->Size(1)*0.3f);
519  }
520
521  if (use2dSampling)
522        box->SetMax(1, box->Min(1));
523
524  cout<<"mUseViewSpaceBox="<<mUseViewSpaceBox<<endl;
525  Debug << "use view space box=" << mUseViewSpaceBox << endl;
526
527  if (mUseViewSpaceBox)
528  {
529          if (0)
530                mViewSpaceBox = box;
531          else
532          {
533                // HACK: enlarge in y directon
534                mViewSpaceBox = new AxisAlignedBox3(mKdTree->GetBox());
535                //Vector3 pmin = mViewSpaceBox->Min();
536                Vector3 size = mViewSpaceBox->Size();
537                //size[1] *= 1.25;
538                Vector3 enlarge(size[0] * 0.1f, size[1] * 0.0f, size[2] * 0.1);
539
540                mViewSpaceBox->Enlarge(enlarge);
541          }
542
543          //Debug << "view space box: " << *mViewSpaceBox << endl;
544  }
545  else
546  {
547          mViewSpaceBox = NULL;
548  }
549 
550  AxisAlignedBox3 vbox = mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
551
552  mSceneGraph->CollectObjects(&mObjects);
553
554  //-- load view cells from file if requested
555  if (!mLoadViewCells)
556  {
557          mViewCellsManager->SetViewSpaceBox(vbox);
558          // construct view cells using it's own set of samples
559          mViewCellsManager->Construct(this);
560
561          //-- several visualizations and statistics
562          Debug << "view cells construction finished: " << endl;
563          mViewCellsManager->PrintStatistics(Debug);
564  }
565  else
566  {
567         
568          VssRayContainer dummies;
569          mViewCellsManager->Visualize(mObjects, dummies);
570          mViewCellsManager->ExportViewCells("test.xml");
571  }
572  VssTree *vssTree = NULL;
573
574 
575
576  long initialTime = GetTime();
577
578  if (mLoadInitialSamples)
579  {
580          cout << "Loading samples from file ... ";
581          LoadSamples(mVssRays, mObjects);
582          cout << "finished\n" << endl;
583          totalSamples = (int)mVssRays.size();
584  }
585  else
586  {
587       
588        while (totalSamples < mInitialSamples) {
589                int passContributingSamples = 0;
590                int passSampleContributions = 0;
591                int passSamples = 0;
592
593                int index = 0;
594
595                int sampleContributions;
596
597                int s = Min(mSamplesPerPass, mInitialSamples);
598                for (int k=0; k < s; k++) {
599                        // changed by matt
600                        Vector3 viewpoint;
601                        //                      viewpoint = GetViewpoint(mViewSpaceBox);
602                        mViewCellsManager->GetViewPoint(viewpoint);
603                        Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
604
605                        sampleContributions = CastRay(viewpoint, direction, mVssRays);
606
607                        if (sampleContributions) {
608                                passContributingSamples ++;
609                                passSampleContributions += sampleContributions;
610                        }
611                        passSamples++;
612                        totalSamples++;
613                }
614
615                mPass++;
616                int pvsSize = 0;
617                float avgRayContrib = (passContributingSamples > 0) ?
618                        passSampleContributions/(float)passContributingSamples : 0;
619
620                cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
621                cout << "#TotalSamples=" << totalSamples/1000
622                        << "k   #SampleContributions=" << passSampleContributions << " ("
623                        << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
624                        << pvsSize/(float)mObjects.size() << endl
625                        << "avg ray contrib=" << avgRayContrib << endl;
626
627                mStats <<
628                        "#Pass\n" <<mPass<<endl<<
629                        "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<<
630                        "#TotalSamples\n" << totalSamples<< endl<<
631                        "#SampleContributions\n" << passSampleContributions << endl <<
632                        "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl <<
633                        "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
634                        "#AvgRayContrib\n" << avgRayContrib << endl;
635          }
636 
637          cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
638
639
640         
641  }
642 
643
644  cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl << flush;
645  Debug << (int)mVssRays.size() << " rays generated in "
646            << TimeDiff(initialTime, GetTime()) * 1e-3 << " seconds" << endl;
647
648  if (mStoreInitialSamples)
649  {
650          cout << "Writing " << (int)mVssRays.size() << " samples to file ... ";
651          ExportSamples(mVssRays);
652          cout << "finished\n" << endl;
653
654          /*VssRayContainer dummyRays;
655          LoadSamples(dummyRays, mObjects);
656          Debug << "rays " << (int)mVssRays.size() << " " << dummyRays.size() << endl;
657
658          for (int i = 0; i < (int)mVssRays.size(); ++ i)
659          {
660                  Debug << mVssRays[i]->GetOrigin() << " " << mVssRays[i]->GetTermination() << " " << mVssRays[i]->mOriginObject << " " << mVssRays[i]->mTerminationObject << endl;
661                  Debug << dummyRays[i]->GetOrigin() << " " << dummyRays[i]->GetTermination() << " " << dummyRays[i]->mOriginObject << " " << dummyRays[i]->mTerminationObject << endl << endl;
662          }*/
663  }
664
665 
666  //int numExportRays = 5000;
667  int numExportRays = 0;
668
669  if (numExportRays) {
670        char filename[64];
671        sprintf(filename, "vss-rays-initial.x3d");
672        ExportRays(filename, mVssRays, numExportRays);
673  }
674
675  vssTree = new VssTree;
676  // viewcells = Construct(mVssRays);
677
678  vssTree->Construct(mVssRays, mViewSpaceBox);
679  cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
680
681  if (0)
682  {
683          ExportVssTree("vss-tree-100.x3d", vssTree, Vector3(1,0,0));
684          ExportVssTree("vss-tree-001.x3d", vssTree, Vector3(0,0,1));
685          ExportVssTree("vss-tree-101.x3d", vssTree, Vector3(1,0,1));
686          ExportVssTree("vss-tree-101m.x3d", vssTree, Vector3(-1,0,-1));
687          ExportVssTreeLeaves(vssTree, 10);
688  }
689
690  // viewcells->UpdatePVS(newVssRays);
691  // get viewcells as kd tree boxes
692  vector<AxisAlignedBox3> kdViewcells;
693  if (0) {
694        vector<KdLeaf *> leaves;
695        mKdTree->CollectLeaves(leaves);
696        vector<KdLeaf *>::const_iterator it;
697        int targetLeaves = 50;
698        float prob = targetLeaves/(float)leaves.size();
699        for (it = leaves.begin(); it != leaves.end(); ++it)
700          if (RandomValue(0.0f,1.0f) < prob)
701                kdViewcells.push_back(mKdTree->GetBox(*it));
702
703        float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
704        cout<<"Initial average PVS size = "<<avgPvs<<endl;
705  }
706
707
708  int samples = 0;
709  int pass = 0;
710
711 
712  // cast view cell samples
713  while (samples < mVssSamples)
714  {
715       
716        int num = mVssSamplesPerPass;
717        SimpleRayContainer rays;
718        VssRayContainer vssRays;
719
720        if (!mUseImportanceSampling) {
721          for (int j=0; j < num; j++) {
722            // changed by matt
723                //Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
724                Vector3 viewpoint;
725                mViewCellsManager->GetViewPoint(viewpoint);
726                Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
727                rays.push_back(SimpleRay(viewpoint, direction));
728          }
729        } else {
730          num = GenerateImportanceRays(vssTree, num, rays);
731        }
732
733        for (int i=0; i < rays.size(); i++)
734          CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
735
736        vssTree->AddRays(vssRays);
737
738        if (0) {
739          int subdivided = vssTree->UpdateSubdivision();
740          cout<<"subdivided leafs = "<<subdivided<<endl;
741        }
742
743        float avgPvs = GetAvgPvsSize(vssTree, kdViewcells);
744        cout<<"Average PVS size = "<<avgPvs<<endl;
745
746        /// compute view cell contribution of rays
747        mViewCellsManager->ComputeSampleContributions(vssRays, true, false);
748       
749        if (numExportRays) {
750          char filename[64];
751          if (mUseImportanceSampling)
752                sprintf(filename, "vss-rays-i%04d.x3d", pass);
753          else
754                sprintf(filename, "vss-rays-%04d.x3d", pass);
755
756          ExportRays(filename, vssRays, numExportRays);
757        }
758
759        samples+=num;
760        float pvs = vssTree->GetAvgPvsSize();
761        cout<<"*****************************\n";
762        cout<<samples<<" avgPVS ="<<pvs<<endl;
763        cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
764        cout<<"*****************************\n";
765//      if (samples >= mVssSamples) break;
766        pass ++;
767  }
768
769  if (mTestBeamSampling && mUseGlRenderer)
770  {     
771          TestBeamCasting(vssTree, mViewCellsManager, mObjects);
772  }
773
774  if (0)
775          Debug << vssTree->stat << endl;
776
777  if (0)
778  {
779        VssRayContainer viewCellRays;
780 
781        // compute rays used for view cells construction
782        const int numRays = mViewCellsManager->GetVisualizationSamples();
783
784        vssTree->CollectRays(viewCellRays, numRays);
785  }
786
787  //-- render simulation after merge
788  cout << "\nevaluating bsp view cells render time after sampling ... ";
789  mRenderSimulator->RenderScene();
790  SimulationStatistics ss;
791  mRenderSimulator->GetStatistics(ss);
792 
793  cout << " finished" << endl;
794  cout << ss << endl;
795  Debug << ss << endl;
796
797  delete vssTree;
798 
799  return true;
800}
Note: See TracBrowser for help on using the repository browser.