1 | #include "SceneGraph.h" |
---|
2 | #include "KdTree.h" |
---|
3 | #include "SamplingPreprocessor.h" |
---|
4 | #include "X3dExporter.h" |
---|
5 | |
---|
6 | SamplingPreprocessor::SamplingPreprocessor() |
---|
7 | { |
---|
8 | // this should increase coherence of the samples |
---|
9 | mSamplesPerPass = 1; |
---|
10 | mTotalSamples = 1e8; |
---|
11 | mKdPvsDepth = 10; |
---|
12 | |
---|
13 | } |
---|
14 | |
---|
15 | void |
---|
16 | SamplingPreprocessor::SetupRay(Ray &ray, const Vector3 &point, const Vector3 &direction) |
---|
17 | { |
---|
18 | ray.intersections.clear(); |
---|
19 | ray.leaves.clear(); |
---|
20 | ray.meshes.clear(); |
---|
21 | // cout<<point<<" "<<direction<<endl; |
---|
22 | ray.Init(point, direction, Ray::LOCAL_RAY); |
---|
23 | } |
---|
24 | |
---|
25 | KdNode * |
---|
26 | SamplingPreprocessor::GetNodeForPvs(KdLeaf *leaf) |
---|
27 | { |
---|
28 | KdNode *node = leaf; |
---|
29 | while (node->mParent && node->mDepth > mKdPvsDepth) |
---|
30 | node = node->mParent; |
---|
31 | return node; |
---|
32 | } |
---|
33 | |
---|
34 | int |
---|
35 | SamplingPreprocessor::AddNodeSamples(Intersectable *object, const Ray &ray) |
---|
36 | { |
---|
37 | int contributingSamples = 0; |
---|
38 | |
---|
39 | for (int j=0; j < ray.leaves.size(); j++) { |
---|
40 | KdNode *node = GetNodeForPvs( ray.leaves[j] ); |
---|
41 | contributingSamples += object->mKdPvs.AddNodeSample(node); |
---|
42 | } |
---|
43 | |
---|
44 | return contributingSamples; |
---|
45 | } |
---|
46 | |
---|
47 | bool |
---|
48 | SamplingPreprocessor::ComputeVisibility() |
---|
49 | { |
---|
50 | |
---|
51 | // pickup an object |
---|
52 | ObjectContainer objects; |
---|
53 | |
---|
54 | mSceneGraph->CollectObjects(&objects); |
---|
55 | |
---|
56 | Vector3 point, normal, direction; |
---|
57 | Ray ray; |
---|
58 | |
---|
59 | long startTime = GetTime(); |
---|
60 | |
---|
61 | int i; |
---|
62 | int pass = 0; |
---|
63 | int totalSamples = 0; |
---|
64 | |
---|
65 | |
---|
66 | int pvsOut = Min((int)objects.size(), 10); |
---|
67 | |
---|
68 | vector<Ray> rays[10]; |
---|
69 | |
---|
70 | while (totalSamples < mTotalSamples) { |
---|
71 | |
---|
72 | int passContributingSamples = 0; |
---|
73 | int passSampleContributions = 0; |
---|
74 | int passSamples = 0; |
---|
75 | |
---|
76 | for (i =0; i < objects.size(); i++) { |
---|
77 | for (int k=0; k < mSamplesPerPass; k++) { |
---|
78 | Intersectable *object = objects[i]; |
---|
79 | object->GetRandomSurfacePoint(point, normal); |
---|
80 | direction = UniformRandomVector(normal); |
---|
81 | // construct a ray |
---|
82 | SetupRay(ray, point, direction); |
---|
83 | mKdTree->CastRay(ray); |
---|
84 | |
---|
85 | if (i < pvsOut) |
---|
86 | rays[i].push_back(ray); |
---|
87 | |
---|
88 | int sampleContributions = 0; |
---|
89 | |
---|
90 | if (ray.leaves.size()) { |
---|
91 | sampleContributions += AddNodeSamples(object, ray); |
---|
92 | |
---|
93 | if (ray.intersections.size()) { |
---|
94 | sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray); |
---|
95 | // check whether we can add this to the rays |
---|
96 | for (int j = 0; j < pvsOut; j++) { |
---|
97 | if (objects[j] == ray.intersections[0].mObject) { |
---|
98 | rays[j].push_back(ray); |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | passSamples++; |
---|
103 | if (sampleContributions) { |
---|
104 | passContributingSamples++; |
---|
105 | passSampleContributions += sampleContributions; |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | totalSamples += passSamples; |
---|
111 | pass++; |
---|
112 | |
---|
113 | int pvsSize = 0; |
---|
114 | for (i=0; i < objects.size(); i++) { |
---|
115 | Intersectable *object = objects[i]; |
---|
116 | pvsSize += object->mKdPvs.mEntries.size(); |
---|
117 | } |
---|
118 | |
---|
119 | cout<<"pass "<<pass<<" : t = "<<TimeDiff(startTime, GetTime())*1e-3<<"s"<<endl; |
---|
120 | cout<<"#totalSamples="<<totalSamples/1000<< |
---|
121 | "k #sampleContributions="<<passSampleContributions<< |
---|
122 | " ("<<100*passContributingSamples/passSamples<<"%)"<< |
---|
123 | " avgPVS="<<pvsSize/(float)objects.size()<<endl; |
---|
124 | } |
---|
125 | bool exportRays = false; |
---|
126 | if (exportRays) { |
---|
127 | Exporter *exporter = NULL; |
---|
128 | exporter = Exporter::GetExporter("sample-rays.x3d"); |
---|
129 | exporter->SetWireframe(); |
---|
130 | exporter->ExportKdTree(*mKdTree); |
---|
131 | for (i=0; i < pvsOut; i++) |
---|
132 | exporter->ExportRays(rays[i], 1000, RgbColor(1, 0, 0)); |
---|
133 | exporter->SetFilled(); |
---|
134 | delete exporter; |
---|
135 | } |
---|
136 | |
---|
137 | if (1) { |
---|
138 | for (int k=0; k < pvsOut; k++) { |
---|
139 | Intersectable *object = objects[k]; |
---|
140 | char s[64]; |
---|
141 | sprintf(s, "sample-pvs%04d.x3d", k); |
---|
142 | Exporter *exporter = Exporter::GetExporter(s); |
---|
143 | exporter->SetWireframe(); |
---|
144 | KdPvsMap::iterator i = object->mKdPvs.mEntries.begin(); |
---|
145 | Intersectable::NewMail(); |
---|
146 | // avoid adding the object to the list |
---|
147 | object->Mail(); |
---|
148 | ObjectContainer visibleObjects; |
---|
149 | for (; i != object->mKdPvs.mEntries.end(); i++) { |
---|
150 | KdNode *node = (*i).first; |
---|
151 | exporter->ExportBox(mKdTree->GetBox(node)); |
---|
152 | mKdTree->CollectObjects(node, visibleObjects); |
---|
153 | } |
---|
154 | |
---|
155 | exporter->ExportRays(rays[k], 1000, RgbColor(0, 1, 0)); |
---|
156 | exporter->SetFilled(); |
---|
157 | |
---|
158 | for (int j = 0; j < visibleObjects.size(); j++) |
---|
159 | exporter->ExportIntersectable(visibleObjects[j]); |
---|
160 | |
---|
161 | Material m; |
---|
162 | m.mDiffuseColor = RgbColor(1, 0, 0); |
---|
163 | exporter->SetForcedMaterial(m); |
---|
164 | exporter->ExportIntersectable(object); |
---|
165 | |
---|
166 | delete exporter; |
---|
167 | } |
---|
168 | |
---|
169 | } |
---|
170 | |
---|
171 | return true; |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | |
---|
176 | |
---|