[2582] | 1 |
|
---|
| 2 | #include "Mesh.h"
|
---|
| 3 | #include "FilterBasedDistribution.h"
|
---|
| 4 | #include "Vector3.h"
|
---|
| 5 | #include "Preprocessor.h"
|
---|
| 6 | #include "ViewCellsManager.h"
|
---|
| 7 |
|
---|
| 8 | namespace GtpVisibilityPreprocessor {
|
---|
| 9 |
|
---|
| 10 | bool
|
---|
| 11 | FilterBasedDistribution::GenerateSample(SimpleRay &ray)
|
---|
| 12 | {
|
---|
| 13 | int tries = 0;
|
---|
| 14 | const int maxTries = 10;
|
---|
| 15 |
|
---|
| 16 | while (mViewCell == NULL || samplesGenerated >= samplesToGenerate
|
---|
| 17 | ) {
|
---|
| 18 | // find a new viewcell to cast the sample from
|
---|
| 19 | if (tries++ > maxTries)
|
---|
| 20 | return false;
|
---|
| 21 |
|
---|
| 22 | Vector3 point;
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | float r[3];
|
---|
| 26 | r[0] = RandomValue(0.0f,1.0f);
|
---|
| 27 | r[1] = RandomValue(0.0f,1.0f);
|
---|
| 28 | r[2] = RandomValue(0.0f,1.0f);
|
---|
| 29 | // static HaltonSequence sHalton;
|
---|
| 30 | // sHalton.GetNext(3, r);
|
---|
| 31 |
|
---|
| 32 | mPreprocessor.mViewCellsManager->GetViewPoint(point, Vector3(r[0],
|
---|
| 33 | r[1],
|
---|
| 34 | r[2]));
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | mViewCell = mPreprocessor.mViewCellsManager->GetViewCell(point);
|
---|
| 38 |
|
---|
| 39 | if (mViewCell && mViewCell->GetPvs().GetSize()) {
|
---|
| 40 |
|
---|
| 41 | // cout<<mViewCell<<endl;
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | // apply spatial filter
|
---|
| 46 | mFilteredPvs.Clear();
|
---|
| 47 | PvsFilterStatistics fstat = mPreprocessor.mViewCellsManager->
|
---|
| 48 | ApplyFilter2(mViewCell,
|
---|
| 49 | false,
|
---|
| 50 | mPreprocessor.mViewCellsManager->GetFilterWidth(),
|
---|
| 51 | mFilteredPvs,
|
---|
| 52 | NULL,
|
---|
| 53 | true);
|
---|
| 54 |
|
---|
| 55 | // cout<<mViewCell->GetPvs().GetSize()<<" "<<mFilteredPvs.GetSize()<<endl;
|
---|
| 56 |
|
---|
| 57 | if (mFilteredPvs.GetSize()) {
|
---|
| 58 | samplesGenerated = 0;
|
---|
| 59 | samplesToGenerate = Max(100, Min(1000, mFilteredPvs.GetSize()));
|
---|
| 60 | } else
|
---|
| 61 | mViewCell = NULL; // try another viewcell
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | Vector3 origin, normal;
|
---|
| 66 |
|
---|
| 67 | mViewCell->GetRandomSurfacePoint(origin, normal);
|
---|
| 68 |
|
---|
| 69 | // pickup random object from the filtered pvs
|
---|
[2598] | 70 | int id = (int)RandomValue(0, (Real)((float)mFilteredPvs.GetSize() - 0.5f));
|
---|
[2582] | 71 |
|
---|
| 72 | ObjectPvsIterator pit = mFilteredPvs.GetIterator();
|
---|
| 73 |
|
---|
| 74 | Intersectable *object;
|
---|
| 75 |
|
---|
| 76 | for (; id>=0; --id)
|
---|
| 77 | object = pit.Next();
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | Vector3 point;
|
---|
| 81 | object->GetRandomSurfacePoint(point, normal);
|
---|
| 82 | Vector3 direction = point - origin;
|
---|
| 83 |
|
---|
| 84 | const float c = Magnitude(direction);
|
---|
| 85 |
|
---|
| 86 | if (c <= Limits::Small)
|
---|
| 87 | return false;
|
---|
| 88 |
|
---|
| 89 | direction *= 1.0f / c;
|
---|
| 90 |
|
---|
| 91 | // $$ jb the pdf is yet not correct for all sampling methods!
|
---|
| 92 | const float pdf = 1.0f;
|
---|
| 93 |
|
---|
| 94 | ray = SimpleRay(origin, direction, FILTER_BASED_DISTRIBUTION, pdf);
|
---|
| 95 |
|
---|
| 96 | //cout << "ray: " << ray.mOrigin << " " << ray.mDirection << endl;
|
---|
| 97 |
|
---|
| 98 | samplesGenerated++;
|
---|
| 99 |
|
---|
| 100 | return true;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | }
|
---|