1 | #include "ViewCellsManager.h"
|
---|
2 | #include "RenderSimulator.h"
|
---|
3 | #include "Mesh.h"
|
---|
4 | #include "Triangle3.h"
|
---|
5 | #include "ViewCell.h"
|
---|
6 | #include "Environment.h"
|
---|
7 | #include "X3dParser.h"
|
---|
8 | #include "ViewCellBsp.h"
|
---|
9 | #include "KdTree.h"
|
---|
10 | #include "VspKdTree.h"
|
---|
11 | #include "Exporter.h"
|
---|
12 | #include "VspBspTree.h"
|
---|
13 |
|
---|
14 |
|
---|
15 | ViewCellsManager::ViewCellsManager():
|
---|
16 | mRenderSimulator(NULL),
|
---|
17 | mConstructionSamples(0),
|
---|
18 | mPostProcessSamples(0),
|
---|
19 | mVisualizationSamples(0)
|
---|
20 | {
|
---|
21 | // post processing stuff |
---|
22 | environment->GetIntValue("ViewCells.PostProcessing.minPvsDif", mMinPvsDif); |
---|
23 | environment->GetIntValue("ViewCells.PostProcessing.minPvs", mMinPvs); |
---|
24 | environment->GetIntValue("ViewCells.PostProcessing.maxPvs", mMaxPvs);
|
---|
25 | }
|
---|
26 |
|
---|
27 | ViewCellsManager::ViewCellsManager(int constructionSamples):
|
---|
28 | mConstructionSamples(constructionSamples),
|
---|
29 | mRenderSimulator(NULL),
|
---|
30 | mPostProcessSamples(0),
|
---|
31 | mVisualizationSamples(0)
|
---|
32 | {
|
---|
33 | // post processing stuff |
---|
34 | environment->GetIntValue("ViewCells.PostProcessing.minPvsDif", mMinPvsDif); |
---|
35 | environment->GetIntValue("ViewCells.PostProcessing.minPvs", mMinPvs); |
---|
36 | environment->GetIntValue("ViewCells.PostProcessing.maxPvs", mMaxPvs);
|
---|
37 | }
|
---|
38 |
|
---|
39 | ViewCellsManager::~ViewCellsManager()
|
---|
40 | {
|
---|
41 | DEL_PTR(mRenderSimulator);
|
---|
42 | }
|
---|
43 |
|
---|
44 | void ViewCellsManager::InitRenderSimulator()
|
---|
45 | {
|
---|
46 | float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
|
---|
47 |
|
---|
48 | environment->GetFloatValue("Simulation.objRenderCost",objRenderCost);
|
---|
49 | environment->GetFloatValue("Simulation.vcOverhead", vcOverhead);
|
---|
50 | environment->GetFloatValue("Simulation.moveSpeed", moveSpeed);
|
---|
51 |
|
---|
52 | mRenderSimulator->SetObjectRenderCost(objRenderCost);
|
---|
53 | mRenderSimulator->SetVcOverhead(vcOverhead);
|
---|
54 | mRenderSimulator->SetMoveSpeed(moveSpeed);
|
---|
55 | }
|
---|
56 |
|
---|
57 | bool ViewCellsManager::LoadViewCells(const string filename)
|
---|
58 | {
|
---|
59 | X3dParser parser;
|
---|
60 |
|
---|
61 | environment->GetFloatValue("ViewCells.height", parser.mViewCellHeight);
|
---|
62 |
|
---|
63 | bool success = parser.ParseFile(filename, *this);
|
---|
64 | Debug << (int)mViewCells.size() << " view cells loaded" << endl;
|
---|
65 |
|
---|
66 | return success;
|
---|
67 | }
|
---|
68 |
|
---|
69 | void ViewCellsManager::ComputeSampleContributions(const RayContainer &rays,
|
---|
70 | const bool castRays,
|
---|
71 | int &sampleContributions,
|
---|
72 | int &contributingSamples)
|
---|
73 | {
|
---|
74 | // view cells not yet constructed
|
---|
75 | if (!ViewCellsConstructed())
|
---|
76 | return;
|
---|
77 |
|
---|
78 | RayContainer::const_iterator it, it_end = rays.end();
|
---|
79 |
|
---|
80 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
81 | {
|
---|
82 | sampleContributions +=ComputeSampleContributions(*(*it), castRays);
|
---|
83 | contributingSamples += sampleContributions > 0;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | void ViewCellsManager::AddViewCell(ViewCell *viewCell)
|
---|
88 | {
|
---|
89 | mViewCells.push_back(viewCell);
|
---|
90 | }
|
---|
91 |
|
---|
92 | void ViewCellsManager::DeriveViewCells(const ObjectContainer &objects,
|
---|
93 | ViewCellContainer &viewCells,
|
---|
94 | const int maxViewCells) const
|
---|
95 | {
|
---|
96 | // maximal max viewcells
|
---|
97 | int limit = maxViewCells > 0 ?
|
---|
98 | Min((int)objects.size(), maxViewCells) : (int)objects.size();
|
---|
99 |
|
---|
100 | for (int i = 0; i < limit; ++ i)
|
---|
101 | {
|
---|
102 | Intersectable *object = objects[i];
|
---|
103 |
|
---|
104 | // extract the mesh instances
|
---|
105 | if (object->Type() == Intersectable::MESH_INSTANCE)
|
---|
106 | {
|
---|
107 | MeshInstance *inst = dynamic_cast<MeshInstance *>(object);
|
---|
108 |
|
---|
109 | ViewCell *viewCell = GenerateViewCell(inst->GetMesh());
|
---|
110 | viewCells.push_back(viewCell);
|
---|
111 | }
|
---|
112 | //TODO: transformed meshes
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | ViewCell *ViewCellsManager::ExtrudeViewCell(const Triangle3 &baseTri,
|
---|
117 | const float height) const
|
---|
118 | {
|
---|
119 | // one mesh per view cell
|
---|
120 | Mesh *mesh = new Mesh();
|
---|
121 |
|
---|
122 | //-- construct prism
|
---|
123 |
|
---|
124 | // bottom
|
---|
125 | mesh->mFaces.push_back(new Face(2,1,0));
|
---|
126 | // top
|
---|
127 | mesh->mFaces.push_back(new Face(3,4,5));
|
---|
128 | // sides
|
---|
129 | mesh->mFaces.push_back(new Face(1, 4, 3, 0));
|
---|
130 | mesh->mFaces.push_back(new Face(2, 5, 4, 1));
|
---|
131 | mesh->mFaces.push_back(new Face(3, 5, 2, 0));
|
---|
132 |
|
---|
133 | //--- extrude new vertices for top of prism
|
---|
134 | Vector3 triNorm = baseTri.GetNormal();
|
---|
135 |
|
---|
136 | Triangle3 topTri;
|
---|
137 |
|
---|
138 | // add base vertices and calculate top vertices
|
---|
139 | for (int i = 0; i < 3; ++ i)
|
---|
140 | mesh->mVertices.push_back(baseTri.mVertices[i]);
|
---|
141 |
|
---|
142 | // add top vertices
|
---|
143 | for (int i = 0; i < 3; ++ i)
|
---|
144 | mesh->mVertices.push_back(baseTri.mVertices[i] + height * triNorm);
|
---|
145 |
|
---|
146 | mesh->Preprocess();
|
---|
147 |
|
---|
148 | return GenerateViewCell(mesh);
|
---|
149 | }
|
---|
150 |
|
---|
151 | ViewCell *ViewCellsManager::MergeViewCells(ViewCell &front, ViewCell &back) const
|
---|
152 | {
|
---|
153 | ViewCell *vc = GenerateViewCell();
|
---|
154 | // merge pvs
|
---|
155 | vc->GetPvs().Merge(front.GetPvs(), back.GetPvs());
|
---|
156 |
|
---|
157 | // merge ray sets
|
---|
158 | stable_sort(front.mPiercingRays.begin(), front.mPiercingRays.end());
|
---|
159 | stable_sort(back.mPiercingRays.begin(), back.mPiercingRays.end());
|
---|
160 |
|
---|
161 | std::merge(front.mPiercingRays.begin(), front.mPiercingRays.end(),
|
---|
162 | back.mPiercingRays.begin(), back.mPiercingRays.end(),
|
---|
163 | vc->mPiercingRays.begin());
|
---|
164 |
|
---|
165 | return vc;
|
---|
166 | }
|
---|
167 |
|
---|
168 | SimulationStatistics ViewCellsManager::SimulateRendering() const
|
---|
169 | {
|
---|
170 | if (!ViewCellsConstructed())
|
---|
171 | return SimulationStatistics();
|
---|
172 |
|
---|
173 | return mRenderSimulator->SimulateRendering();
|
---|
174 | }
|
---|
175 |
|
---|
176 | ViewCell *ViewCellsManager::GenerateViewCell(Mesh *mesh) const
|
---|
177 | {
|
---|
178 | return new ViewCell(mesh);
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | void ViewCellsManager::SetVisualizationSamples(const int visSamples)
|
---|
183 | {
|
---|
184 | int mVisualizationSamples = visSamples;
|
---|
185 | }
|
---|
186 |
|
---|
187 | void ViewCellsManager::SetConstructionSamples(const int constructionSamples)
|
---|
188 | {
|
---|
189 | mConstructionSamples = constructionSamples;
|
---|
190 | }
|
---|
191 |
|
---|
192 | void ViewCellsManager::SetPostProcessSamples(const int postProcessSamples)
|
---|
193 | {
|
---|
194 | mPostProcessSamples = postProcessSamples;
|
---|
195 | }
|
---|
196 |
|
---|
197 | int ViewCellsManager::GetVisualizationSamples() const
|
---|
198 | {
|
---|
199 | return mVisualizationSamples;
|
---|
200 | }
|
---|
201 |
|
---|
202 | int ViewCellsManager::GetConstructionSamples() const
|
---|
203 | {
|
---|
204 | return mConstructionSamples;
|
---|
205 | }
|
---|
206 |
|
---|
207 | int ViewCellsManager::GetPostProcessSamples() const
|
---|
208 | {
|
---|
209 | return mPostProcessSamples;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**********************************************************************/
|
---|
213 | /* BspViewCellsManager implementation */
|
---|
214 | /**********************************************************************/
|
---|
215 |
|
---|
216 | BspViewCellsManager::BspViewCellsManager(BspTree *bspTree, int constructionSamples):
|
---|
217 | ViewCellsManager(constructionSamples),
|
---|
218 | mBspTree(bspTree)
|
---|
219 | {
|
---|
220 | mRenderSimulator = new BspRenderSimulator(bspTree);
|
---|
221 | InitRenderSimulator();
|
---|
222 | }
|
---|
223 |
|
---|
224 | bool BspViewCellsManager::ViewCellsConstructed() const
|
---|
225 | {
|
---|
226 | return mBspTree->GetRoot() != NULL;
|
---|
227 | }
|
---|
228 |
|
---|
229 | ViewCell *BspViewCellsManager::GenerateViewCell(Mesh *mesh) const
|
---|
230 | {
|
---|
231 | return new BspViewCell(mesh);
|
---|
232 | }
|
---|
233 |
|
---|
234 | int BspViewCellsManager::Construct(const ObjectContainer &objects,
|
---|
235 | const VssRayContainer &rays,
|
---|
236 | AxisAlignedBox3 *sceneBbox)
|
---|
237 | {
|
---|
238 | // if view cells were already constructed
|
---|
239 | if (ViewCellsConstructed())
|
---|
240 | return 0;
|
---|
241 |
|
---|
242 | Debug << "Constructing bsp view cells" << endl;
|
---|
243 |
|
---|
244 | int sampleContributions = 0;
|
---|
245 |
|
---|
246 | RayContainer sampleRays;
|
---|
247 |
|
---|
248 | int limit = min (mConstructionSamples, (int)rays.size());
|
---|
249 |
|
---|
250 | for (int i = 0; i < limit; ++ i)
|
---|
251 | sampleRays.push_back(new Ray(*rays[i]));
|
---|
252 |
|
---|
253 | if (mViewCells.empty()) // no view cells loaded
|
---|
254 | mBspTree->Construct(objects, sampleRays);
|
---|
255 | else
|
---|
256 | mBspTree->Construct(mViewCells);
|
---|
257 |
|
---|
258 | Debug << mBspTree->GetStatistics() << endl;
|
---|
259 | CLEAR_CONTAINER(sampleRays);
|
---|
260 |
|
---|
261 | return sampleContributions;
|
---|
262 | }
|
---|
263 |
|
---|
264 | int BspViewCellsManager::ComputeSampleContributions(Ray &ray, const bool castRay)
|
---|
265 | {
|
---|
266 | // view cells not yet constructed
|
---|
267 | if (!ViewCellsConstructed())
|
---|
268 | return 0;
|
---|
269 |
|
---|
270 | int contributingSamples = 0;
|
---|
271 |
|
---|
272 | if (castRay)
|
---|
273 | mBspTree->CastRay(ray);
|
---|
274 |
|
---|
275 | //if (mBspTree->bspIntersections.empty()) return 0;
|
---|
276 |
|
---|
277 | Intersectable *tObject =
|
---|
278 | !ray.intersections.empty() ? ray.intersections[0].mObject : NULL;
|
---|
279 |
|
---|
280 | Intersectable *sObject = ray.sourceObject.mObject;
|
---|
281 |
|
---|
282 | if (sObject || tObject)
|
---|
283 | {
|
---|
284 | // object can be seen from the view cell => add to view cell pvs
|
---|
285 | for (int j = 0; j < (int)ray.bspIntersections.size(); ++ j)
|
---|
286 | {
|
---|
287 | BspLeaf *leaf = ray.bspIntersections[j].mLeaf;
|
---|
288 |
|
---|
289 | // if ray not in unbounded space
|
---|
290 | if (leaf->GetViewCell() != mBspTree->GetRootCell())
|
---|
291 | {
|
---|
292 | if (sObject)
|
---|
293 | {
|
---|
294 | contributingSamples +=
|
---|
295 | leaf->GetViewCell()->GetPvs().AddSample(sObject);
|
---|
296 | }
|
---|
297 |
|
---|
298 | if (tObject)
|
---|
299 | {
|
---|
300 | contributingSamples +=
|
---|
301 | leaf->GetViewCell()->GetPvs().AddSample(tObject);
|
---|
302 | }
|
---|
303 | }
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | // rays passing through this viewcell
|
---|
308 | if (0)
|
---|
309 | for (int j = 1; j < ((int)ray.bspIntersections.size() - 1); ++ j)
|
---|
310 | {
|
---|
311 | BspLeaf *leaf = ray.bspIntersections[j].mLeaf;
|
---|
312 |
|
---|
313 | if (leaf->GetViewCell() != mBspTree->GetRootCell())
|
---|
314 | leaf->GetViewCell()->
|
---|
315 | AddPassingRay(ray, contributingSamples ? 1 : 0);
|
---|
316 | }
|
---|
317 |
|
---|
318 | return contributingSamples;
|
---|
319 | }
|
---|
320 |
|
---|
321 | int BspViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
322 | const RayContainer &rays)
|
---|
323 | {
|
---|
324 | if (!ViewCellsConstructed())
|
---|
325 | {
|
---|
326 | Debug << "view cells not constructed" << endl;
|
---|
327 | return 0;
|
---|
328 | }
|
---|
329 |
|
---|
330 |
|
---|
331 | //-- post processing of bsp view cells
|
---|
332 | int vcSize = 0;
|
---|
333 | int pvsSize = 0;
|
---|
334 |
|
---|
335 | BspViewCellsStatistics stat;
|
---|
336 | mBspTree->EvaluateViewCellsStats(stat);
|
---|
337 | Debug << "original view cell partition:\n" << stat << endl;
|
---|
338 |
|
---|
339 | if (1) // export view cells
|
---|
340 | {
|
---|
341 | cout << "exporting initial view cells (=leaves) ... ";
|
---|
342 | Exporter *exporter = Exporter::GetExporter("view_cells.x3d");
|
---|
343 |
|
---|
344 | if (exporter)
|
---|
345 | {
|
---|
346 | exporter->SetWireframe();
|
---|
347 | exporter->ExportBspLeaves(*mBspTree, stat.maxPvs);
|
---|
348 | //exporter->ExportBspViewCellPartition(*mBspTree, 0);
|
---|
349 |
|
---|
350 | if (0)
|
---|
351 | {
|
---|
352 | Material m;//= RandomMaterial();
|
---|
353 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
354 | exporter->SetForcedMaterial(m);
|
---|
355 | exporter->SetWireframe();
|
---|
356 |
|
---|
357 | exporter->ExportGeometry(objects);
|
---|
358 | }
|
---|
359 |
|
---|
360 | delete exporter;
|
---|
361 | }
|
---|
362 | cout << "finished" << endl;
|
---|
363 | }
|
---|
364 |
|
---|
365 | cout << "starting post processing using " << mPostProcessSamples << " samples ... ";
|
---|
366 |
|
---|
367 | long startTime = GetTime();
|
---|
368 |
|
---|
369 |
|
---|
370 | //-- merge or subdivide view cells
|
---|
371 | int merged = 0;
|
---|
372 |
|
---|
373 | RayContainer::const_iterator rit, rit_end = rays.end();
|
---|
374 | vector<Ray::BspIntersection>::const_iterator iit;
|
---|
375 |
|
---|
376 | int limit = min((int)rays.size(), mPostProcessSamples);
|
---|
377 |
|
---|
378 | for (int i = 0; i < limit; ++ i)
|
---|
379 | {
|
---|
380 | Ray *ray = rays[i];
|
---|
381 |
|
---|
382 | // traverse leaves stored in the rays and compare and merge consecutive
|
---|
383 | // leaves (i.e., the neighbors in the tree)
|
---|
384 | if (ray->bspIntersections.size() < 2)
|
---|
385 | continue;
|
---|
386 |
|
---|
387 | iit = ray->bspIntersections.begin();
|
---|
388 |
|
---|
389 | BspLeaf *previousLeaf = (*iit).mLeaf;
|
---|
390 | ++ iit;
|
---|
391 |
|
---|
392 | for (; iit != ray->bspIntersections.end(); ++ iit)
|
---|
393 | {
|
---|
394 | BspLeaf *leaf = (*iit).mLeaf;
|
---|
395 |
|
---|
396 | if (ShouldMerge(leaf, previousLeaf))
|
---|
397 | {
|
---|
398 | MergeBspLeafViewCells(leaf, previousLeaf);
|
---|
399 |
|
---|
400 | ++ merged;
|
---|
401 | }
|
---|
402 |
|
---|
403 | previousLeaf = leaf;
|
---|
404 | }
|
---|
405 | }
|
---|
406 |
|
---|
407 | //-- stats and visualizations
|
---|
408 | cout << "finished" << endl;
|
---|
409 | cout << "merged " << merged << " view cells in "
|
---|
410 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
|
---|
411 |
|
---|
412 | return merged;
|
---|
413 | }
|
---|
414 |
|
---|
415 | int BspViewCellsManager::GetType() const
|
---|
416 | {
|
---|
417 | return BSP;
|
---|
418 | }
|
---|
419 |
|
---|
420 |
|
---|
421 | void BspViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
422 | const RayContainer &sampleRays)
|
---|
423 | {
|
---|
424 | if (!ViewCellsConstructed())
|
---|
425 | return;
|
---|
426 |
|
---|
427 | //-- recount pvs
|
---|
428 | BspViewCellsStatistics vcStats;
|
---|
429 | mBspTree->EvaluateViewCellsStats(vcStats);
|
---|
430 |
|
---|
431 | if (1) // export view cells
|
---|
432 | {
|
---|
433 | cout << "exporting view cells after merge ... ";
|
---|
434 | Exporter *exporter = Exporter::GetExporter("merged_view_cells.x3d");
|
---|
435 |
|
---|
436 | if (exporter)
|
---|
437 | {
|
---|
438 | exporter->ExportBspViewCellPartition(*mBspTree, vcStats.maxPvs);
|
---|
439 | //exporter->ExportBspViewCellPartition(*mBspTree, 0);
|
---|
440 | delete exporter;
|
---|
441 | }
|
---|
442 |
|
---|
443 | cout << "finished" << endl;
|
---|
444 | }
|
---|
445 |
|
---|
446 | //-- visualization of the BSP splits
|
---|
447 | bool exportSplits = false;
|
---|
448 | environment->GetBoolValue("BspTree.Visualization.exportSplits", exportSplits);
|
---|
449 |
|
---|
450 | if (exportSplits)
|
---|
451 | {
|
---|
452 | cout << "exporting splits ... ";
|
---|
453 | ExportSplits(objects, sampleRays);
|
---|
454 | cout << "finished" << endl;
|
---|
455 | }
|
---|
456 |
|
---|
457 | ExportBspPvs(objects, sampleRays);
|
---|
458 | }
|
---|
459 |
|
---|
460 |
|
---|
461 | inline bool vc_gt(ViewCell *a, ViewCell *b)
|
---|
462 | {
|
---|
463 | return a->GetPvs().GetSize() > b->GetPvs().GetSize();
|
---|
464 | }
|
---|
465 |
|
---|
466 | void BspViewCellsManager::ExportSplits(const ObjectContainer &objects,
|
---|
467 | const RayContainer &sampleRays)
|
---|
468 | {
|
---|
469 | Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
|
---|
470 |
|
---|
471 | if (exporter)
|
---|
472 | {
|
---|
473 | Material m;
|
---|
474 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
475 | exporter->SetForcedMaterial(m);
|
---|
476 | exporter->SetWireframe();
|
---|
477 | exporter->ExportBspSplits(*mBspTree, true);
|
---|
478 |
|
---|
479 | // take forced material, else big scenes cannot be viewed
|
---|
480 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
481 | exporter->SetForcedMaterial(m);
|
---|
482 | exporter->SetFilled();
|
---|
483 |
|
---|
484 | exporter->ResetForcedMaterial();
|
---|
485 |
|
---|
486 | // export rays
|
---|
487 | if (0)
|
---|
488 | {
|
---|
489 | RayContainer outRays;
|
---|
490 |
|
---|
491 | int raysSize = min((int)sampleRays.size(), mVisualizationSamples);
|
---|
492 |
|
---|
493 | for (int i = 0; i < raysSize; ++ i)
|
---|
494 | {
|
---|
495 | // only rays piercing geometry
|
---|
496 | if (!sampleRays[i]->intersections.empty())
|
---|
497 | outRays.push_back(sampleRays[i]);
|
---|
498 | }
|
---|
499 |
|
---|
500 | // export rays
|
---|
501 | exporter->ExportRays(outRays, 1000, RgbColor(1, 1, 0));
|
---|
502 | }
|
---|
503 |
|
---|
504 | if (0)
|
---|
505 | exporter->ExportGeometry(objects);
|
---|
506 |
|
---|
507 | delete exporter;
|
---|
508 | }
|
---|
509 | }
|
---|
510 |
|
---|
511 | void BspViewCellsManager::ExportBspPvs(const ObjectContainer &objects,
|
---|
512 | const RayContainer &sampleRays)
|
---|
513 | {
|
---|
514 | const int leafOut = 10;
|
---|
515 |
|
---|
516 | ViewCell::NewMail();
|
---|
517 |
|
---|
518 | //-- some rays for output
|
---|
519 | const int raysOut = min((int)sampleRays.size(), mVisualizationSamples);
|
---|
520 | cout << "visualization using " << mVisualizationSamples << " samples" << endl;
|
---|
521 | vector<Ray *> vcRays[leafOut];
|
---|
522 |
|
---|
523 | if (0)
|
---|
524 | {
|
---|
525 | //-- some random view cells and rays for output
|
---|
526 | vector<BspLeaf *> bspLeaves;
|
---|
527 |
|
---|
528 | for (int i = 0; i < leafOut; ++ i)
|
---|
529 | bspLeaves.push_back(mBspTree->GetRandomLeaf());
|
---|
530 |
|
---|
531 | for (int i = 0; i < bspLeaves.size(); ++ i)
|
---|
532 | {
|
---|
533 | cout << "creating output for view cell " << i << " ... ";
|
---|
534 | // check whether we can add the current ray to the output rays
|
---|
535 | for (int k = 0; k < raysOut; ++ k)
|
---|
536 | {
|
---|
537 | Ray *ray = sampleRays[k];
|
---|
538 |
|
---|
539 | for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
|
---|
540 | {
|
---|
541 | BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
|
---|
542 |
|
---|
543 | if (bspLeaves[i]->GetViewCell() == leaf->GetViewCell())
|
---|
544 | {
|
---|
545 | vcRays[i].push_back(ray);
|
---|
546 | }
|
---|
547 | }
|
---|
548 | }
|
---|
549 |
|
---|
550 | Intersectable::NewMail();
|
---|
551 |
|
---|
552 | BspViewCell *vc = dynamic_cast<BspViewCell *>(bspLeaves[i]->GetViewCell());
|
---|
553 |
|
---|
554 | //bspLeaves[j]->Mail();
|
---|
555 | char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
|
---|
556 |
|
---|
557 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
558 | exporter->SetFilled();
|
---|
559 |
|
---|
560 | ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin();
|
---|
561 |
|
---|
562 | exporter->SetWireframe();
|
---|
563 | //exporter->SetFilled();
|
---|
564 |
|
---|
565 | Material m;//= RandomMaterial();
|
---|
566 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
567 | exporter->SetForcedMaterial(m);
|
---|
568 |
|
---|
569 | if (vc->GetMesh())
|
---|
570 | exporter->ExportViewCell(vc);
|
---|
571 | else
|
---|
572 | {
|
---|
573 | PolygonContainer cell;
|
---|
574 | // export view cell geometry
|
---|
575 | mBspTree->ConstructGeometry(vc, cell);
|
---|
576 | exporter->ExportPolygons(cell);
|
---|
577 | CLEAR_CONTAINER(cell);
|
---|
578 | }
|
---|
579 |
|
---|
580 | Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize()
|
---|
581 | << ", piercing rays=" << (int)vcRays[i].size() << endl;
|
---|
582 |
|
---|
583 | // export rays piercing this view cell
|
---|
584 | exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0));
|
---|
585 |
|
---|
586 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
587 | exporter->SetForcedMaterial(m);
|
---|
588 |
|
---|
589 | // exporter->SetWireframe();
|
---|
590 | exporter->SetFilled();
|
---|
591 |
|
---|
592 | // output PVS of view cell
|
---|
593 | for (; it != vc->GetPvs().mEntries.end(); ++ it)
|
---|
594 | {
|
---|
595 | Intersectable *intersect = (*it).first;
|
---|
596 | if (!intersect->Mailed())
|
---|
597 | {
|
---|
598 | exporter->ExportIntersectable(intersect);
|
---|
599 | intersect->Mail();
|
---|
600 | }
|
---|
601 | }
|
---|
602 |
|
---|
603 | // output rest of the objects
|
---|
604 | if (0)
|
---|
605 | {
|
---|
606 | Material m;//= RandomMaterial();
|
---|
607 | m.mDiffuseColor = RgbColor(0, 0, 1);
|
---|
608 | exporter->SetForcedMaterial(m);
|
---|
609 |
|
---|
610 | for (int j = 0; j < objects.size(); ++ j)
|
---|
611 | if (!objects[j]->Mailed())
|
---|
612 | {
|
---|
613 | exporter->SetForcedMaterial(m);
|
---|
614 | exporter->ExportIntersectable(objects[j]);
|
---|
615 | objects[j]->Mail();
|
---|
616 | }
|
---|
617 | }
|
---|
618 | DEL_PTR(exporter);
|
---|
619 | cout << "finished" << endl;
|
---|
620 | }
|
---|
621 | }
|
---|
622 | else
|
---|
623 | {
|
---|
624 | ViewCellContainer viewCells;
|
---|
625 |
|
---|
626 | mBspTree->CollectViewCells(viewCells);
|
---|
627 | stable_sort(viewCells.begin(), viewCells.end(), vc_gt);
|
---|
628 |
|
---|
629 | int limit = min(leafOut, (int)viewCells.size());
|
---|
630 |
|
---|
631 | for (int i = 0; i < limit; ++ i)
|
---|
632 | {
|
---|
633 | cout << "creating output for view cell " << i << " ... ";
|
---|
634 |
|
---|
635 | Intersectable::NewMail();
|
---|
636 | BspViewCell *vc = dynamic_cast<BspViewCell *>(viewCells[i]);
|
---|
637 |
|
---|
638 | cout << "creating output for view cell " << i << " ... ";
|
---|
639 | // check whether we can add the current ray to the output rays
|
---|
640 | for (int k = 0; k < raysOut; ++ k)
|
---|
641 | {
|
---|
642 | Ray *ray = sampleRays[k];
|
---|
643 |
|
---|
644 | for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
|
---|
645 | {
|
---|
646 | BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
|
---|
647 |
|
---|
648 | if (vc == leaf->GetViewCell())
|
---|
649 | {
|
---|
650 | vcRays[i].push_back(ray);
|
---|
651 | }
|
---|
652 | }
|
---|
653 | }
|
---|
654 |
|
---|
655 | //bspLeaves[j]->Mail();
|
---|
656 | char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
|
---|
657 |
|
---|
658 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
659 |
|
---|
660 | exporter->SetWireframe();
|
---|
661 |
|
---|
662 | Material m;//= RandomMaterial();
|
---|
663 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
664 | exporter->SetForcedMaterial(m);
|
---|
665 |
|
---|
666 | if (vc->GetMesh())
|
---|
667 | exporter->ExportViewCell(vc);
|
---|
668 | else
|
---|
669 | {
|
---|
670 | PolygonContainer cell;
|
---|
671 | // export view cell
|
---|
672 | mBspTree->ConstructGeometry(vc, cell);
|
---|
673 | exporter->ExportPolygons(cell);
|
---|
674 | CLEAR_CONTAINER(cell);
|
---|
675 | }
|
---|
676 |
|
---|
677 |
|
---|
678 | Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize()
|
---|
679 | << ", piercing rays=" << (int)vcRays[i].size() << endl;
|
---|
680 |
|
---|
681 |
|
---|
682 | // export rays piercing this view cell
|
---|
683 | exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0));
|
---|
684 |
|
---|
685 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
686 | exporter->SetForcedMaterial(m);
|
---|
687 |
|
---|
688 | ViewCellPvsMap::const_iterator it,
|
---|
689 | it_end = vc->GetPvs().mEntries.end();
|
---|
690 |
|
---|
691 | // output PVS of view cell
|
---|
692 | for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it)
|
---|
693 | {
|
---|
694 | Intersectable *intersect = (*it).first;
|
---|
695 | if (!intersect->Mailed())
|
---|
696 | {
|
---|
697 | Material m = RandomMaterial();
|
---|
698 |
|
---|
699 | exporter->SetForcedMaterial(m);
|
---|
700 |
|
---|
701 | exporter->ExportIntersectable(intersect);
|
---|
702 | intersect->Mail();
|
---|
703 | }
|
---|
704 | }
|
---|
705 |
|
---|
706 | DEL_PTR(exporter);
|
---|
707 | cout << "finished" << endl;
|
---|
708 | }
|
---|
709 | }
|
---|
710 | }
|
---|
711 |
|
---|
712 |
|
---|
713 | bool BspViewCellsManager::MergeBspLeafViewCells(BspLeaf *front, BspLeaf *back) const |
---|
714 | { |
---|
715 | BspViewCell *viewCell = |
---|
716 | dynamic_cast<BspViewCell *>(MergeViewCells(*front->GetViewCell(), *back->GetViewCell())); |
---|
717 | |
---|
718 | if (!viewCell) |
---|
719 | return false; |
---|
720 | |
---|
721 | // change view cells of all leaves associated with the |
---|
722 | // previous view cells |
---|
723 | |
---|
724 | BspViewCell *fVc = front->GetViewCell(); |
---|
725 | BspViewCell *bVc = back->GetViewCell(); |
---|
726 | |
---|
727 | vector<BspLeaf *> fLeaves = fVc->mBspLeaves; |
---|
728 | vector<BspLeaf *> bLeaves = bVc->mBspLeaves; |
---|
729 | |
---|
730 | vector<BspLeaf *>::const_iterator it; |
---|
731 | |
---|
732 | for (it = fLeaves.begin(); it != fLeaves.end(); ++ it) |
---|
733 | { |
---|
734 | (*it)->SetViewCell(viewCell); |
---|
735 | viewCell->mBspLeaves.push_back(*it); |
---|
736 | } |
---|
737 | for (it = bLeaves.begin(); it != bLeaves.end(); ++ it) |
---|
738 | { |
---|
739 | (*it)->SetViewCell(viewCell); |
---|
740 | viewCell->mBspLeaves.push_back(*it); |
---|
741 | } |
---|
742 | |
---|
743 | DEL_PTR(fVc); |
---|
744 | DEL_PTR(bVc); |
---|
745 | |
---|
746 | return true; |
---|
747 | } |
---|
748 | |
---|
749 | bool BspViewCellsManager::ShouldMerge(BspLeaf *front, BspLeaf *back) const |
---|
750 | { |
---|
751 | ViewCell *fvc = front->GetViewCell(); |
---|
752 | ViewCell *bvc = back->GetViewCell(); |
---|
753 | |
---|
754 | if ((fvc == mBspTree->GetRootCell()) || (bvc == mBspTree->GetRootCell()) || (fvc == bvc)) |
---|
755 | return false; |
---|
756 | |
---|
757 | int fdiff = fvc->GetPvs().Diff(bvc->GetPvs()); |
---|
758 | |
---|
759 | if (fvc->GetPvs().GetSize() + fdiff < mMaxPvs) |
---|
760 | { |
---|
761 | if ((fvc->GetPvs().GetSize() < mMinPvs) || |
---|
762 | (bvc->GetPvs().GetSize() < mMinPvs) || |
---|
763 | ((fdiff < mMinPvsDif) && (bvc->GetPvs().Diff(fvc->GetPvs()) < mMinPvsDif))) |
---|
764 | { |
---|
765 | return true; |
---|
766 | } |
---|
767 | } |
---|
768 | |
---|
769 | return false; |
---|
770 | } |
---|
771 | |
---|
772 |
|
---|
773 |
|
---|
774 | /**********************************************************************/
|
---|
775 | /* KdViewCellsManager implementation */
|
---|
776 | /**********************************************************************/
|
---|
777 |
|
---|
778 | KdViewCellsManager::KdViewCellsManager(KdTree *kdTree):
|
---|
779 | ViewCellsManager(),
|
---|
780 | mKdTree(kdTree),
|
---|
781 | mKdPvsDepth(100)
|
---|
782 | {
|
---|
783 | mRenderSimulator = new KdRenderSimulator(mKdTree);
|
---|
784 | InitRenderSimulator();
|
---|
785 | }
|
---|
786 |
|
---|
787 | int KdViewCellsManager::Construct(const ObjectContainer &objects,
|
---|
788 | const VssRayContainer &rays,
|
---|
789 | AxisAlignedBox3 *sceneBbox)
|
---|
790 | {
|
---|
791 | // if view cells already constructed
|
---|
792 | if (ViewCellsConstructed())
|
---|
793 | return 0;
|
---|
794 |
|
---|
795 | Debug << "Constructing bsp view cells" << endl;
|
---|
796 |
|
---|
797 | mKdTree->Construct();
|
---|
798 |
|
---|
799 | return 0;
|
---|
800 | }
|
---|
801 |
|
---|
802 | bool KdViewCellsManager::ViewCellsConstructed() const
|
---|
803 | {
|
---|
804 | return mKdTree->GetRoot() != NULL;
|
---|
805 | }
|
---|
806 |
|
---|
807 | int KdViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
808 | const RayContainer &rays)
|
---|
809 | {
|
---|
810 | return 0;
|
---|
811 | }
|
---|
812 |
|
---|
813 | void KdViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
814 | const RayContainer &sampleRays)
|
---|
815 | {
|
---|
816 | if (!ViewCellsConstructed())
|
---|
817 | return;
|
---|
818 |
|
---|
819 | const int pvsOut = min((int)objects.size(), 10);
|
---|
820 |
|
---|
821 | int limit = min(mVisualizationSamples, (int)sampleRays.size());
|
---|
822 |
|
---|
823 | RayContainer *rays = new RayContainer[pvsOut];
|
---|
824 |
|
---|
825 | for (int i = 0; i < limit; ++ i)
|
---|
826 | {
|
---|
827 | Ray *ray = sampleRays[i];
|
---|
828 |
|
---|
829 | if (!ray->intersections.empty())
|
---|
830 | {
|
---|
831 | // check whether we can add this to the rays
|
---|
832 | for (int j = 0; j < pvsOut; j++)
|
---|
833 | {
|
---|
834 | if (objects[j] == ray->intersections[0].mObject)
|
---|
835 | {
|
---|
836 | rays[j].push_back(ray);
|
---|
837 | }
|
---|
838 | }
|
---|
839 | }
|
---|
840 | }
|
---|
841 |
|
---|
842 | bool exportRays = false;
|
---|
843 | if (exportRays) {
|
---|
844 | Exporter *exporter = NULL;
|
---|
845 | exporter = Exporter::GetExporter("sample-rays.x3d");
|
---|
846 | exporter->SetWireframe();
|
---|
847 | exporter->ExportKdTree(*mKdTree);
|
---|
848 |
|
---|
849 | for (i=0; i < pvsOut; i++)
|
---|
850 | exporter->ExportRays(rays[i], 1000, RgbColor(1, 0, 0));
|
---|
851 | exporter->SetFilled();
|
---|
852 |
|
---|
853 | delete exporter;
|
---|
854 | }
|
---|
855 |
|
---|
856 | for (int k=0; k < pvsOut; k++)
|
---|
857 | {
|
---|
858 | Intersectable *object = objects[k];
|
---|
859 | char s[64];
|
---|
860 | sprintf(s, "sample-pvs%04d.x3d", k);
|
---|
861 |
|
---|
862 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
863 | exporter->SetWireframe();
|
---|
864 |
|
---|
865 | KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
|
---|
866 | Intersectable::NewMail();
|
---|
867 |
|
---|
868 | // avoid adding the object to the list
|
---|
869 | object->Mail();
|
---|
870 | ObjectContainer visibleObjects;
|
---|
871 |
|
---|
872 | for (; i != object->mKdPvs.mEntries.end(); i++)
|
---|
873 | {
|
---|
874 | KdNode *node = (*i).first;
|
---|
875 | exporter->ExportBox(mKdTree->GetBox(node));
|
---|
876 | mKdTree->CollectObjects(node, visibleObjects);
|
---|
877 | }
|
---|
878 |
|
---|
879 | exporter->ExportRays(rays[k], 1000, RgbColor(0, 1, 0));
|
---|
880 | exporter->SetFilled();
|
---|
881 |
|
---|
882 | for (int j = 0; j < visibleObjects.size(); j++)
|
---|
883 | exporter->ExportIntersectable(visibleObjects[j]);
|
---|
884 |
|
---|
885 | Material m;
|
---|
886 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
887 | exporter->SetForcedMaterial(m);
|
---|
888 | exporter->ExportIntersectable(object);
|
---|
889 |
|
---|
890 | delete exporter;
|
---|
891 | }
|
---|
892 | DEL_PTR(rays);
|
---|
893 | }
|
---|
894 |
|
---|
895 | int KdViewCellsManager::GetType() const
|
---|
896 | {
|
---|
897 | return ViewCellsManager::KD;
|
---|
898 | }
|
---|
899 |
|
---|
900 | int KdViewCellsManager::ComputeSampleContributions(Ray &ray, const bool castRay)
|
---|
901 | {
|
---|
902 | // view cells not yet constructed
|
---|
903 | if (!ViewCellsConstructed())
|
---|
904 | return 0;
|
---|
905 |
|
---|
906 | if (castRay)
|
---|
907 | mKdTree->CastRay(ray);
|
---|
908 |
|
---|
909 | if (ray.kdLeaves.empty())
|
---|
910 | return 0;
|
---|
911 |
|
---|
912 | Intersectable *tObject =
|
---|
913 | !ray.intersections.empty() ? ray.intersections[0].mObject : NULL;
|
---|
914 |
|
---|
915 | Intersectable *sObject =
|
---|
916 | ray.sourceObject.mObject;
|
---|
917 |
|
---|
918 | int contributingSamples = 0;
|
---|
919 |
|
---|
920 | int objects = 0;
|
---|
921 |
|
---|
922 | if (sObject)
|
---|
923 | objects++;
|
---|
924 | if (tObject)
|
---|
925 | objects++;
|
---|
926 |
|
---|
927 | for (int j = 1; j < ((int)ray.kdLeaves.size() - 1); ++ j)
|
---|
928 | {
|
---|
929 | ray.kdLeaves[j]->AddPassingRay2(ray, objects,
|
---|
930 | (int)ray.kdLeaves.size());
|
---|
931 | }
|
---|
932 |
|
---|
933 | if (!objects)
|
---|
934 | return 0;
|
---|
935 |
|
---|
936 | for (int j=0; j < ray.kdLeaves.size(); j++)
|
---|
937 | {
|
---|
938 | KdNode *node = GetNodeForPvs(ray.kdLeaves[j]);
|
---|
939 |
|
---|
940 | if (sObject)
|
---|
941 | contributingSamples += sObject->mKdPvs.AddSample(node);
|
---|
942 | if (tObject)
|
---|
943 | contributingSamples += tObject->mKdPvs.AddSample(node);
|
---|
944 | }
|
---|
945 |
|
---|
946 | return contributingSamples;
|
---|
947 | }
|
---|
948 |
|
---|
949 |
|
---|
950 | KdNode *KdViewCellsManager::GetNodeForPvs(KdLeaf *leaf)
|
---|
951 | {
|
---|
952 | KdNode *node = leaf;
|
---|
953 |
|
---|
954 | while (node->mParent && node->mDepth > mKdPvsDepth)
|
---|
955 | node = node->mParent;
|
---|
956 | return node;
|
---|
957 | }
|
---|
958 |
|
---|
959 |
|
---|
960 |
|
---|
961 | /**********************************************************************/
|
---|
962 | /* VspKdViewCellsManager implementation */
|
---|
963 | /**********************************************************************/
|
---|
964 |
|
---|
965 | VspKdViewCellsManager::VspKdViewCellsManager(VspKdTree *vspKdTree, int constructionSamples):
|
---|
966 | ViewCellsManager(constructionSamples),
|
---|
967 | mVspKdTree(vspKdTree)
|
---|
968 | {
|
---|
969 | mRenderSimulator = NULL; // TODO: new VspKdRenderSimulator(vspKdTree);
|
---|
970 | InitRenderSimulator();
|
---|
971 | }
|
---|
972 |
|
---|
973 | int VspKdViewCellsManager::Construct(const ObjectContainer &objects,
|
---|
974 | const VssRayContainer &rays,
|
---|
975 | AxisAlignedBox3 *sceneBbox)
|
---|
976 | {
|
---|
977 | // if view cells already constructed
|
---|
978 | if (ViewCellsConstructed())
|
---|
979 | return 0;
|
---|
980 |
|
---|
981 | mVspKdTree->Construct(rays, sceneBbox);
|
---|
982 | return 0;
|
---|
983 | }
|
---|
984 |
|
---|
985 | bool VspKdViewCellsManager::ViewCellsConstructed() const
|
---|
986 | {
|
---|
987 | return mVspKdTree->GetRoot() != NULL;
|
---|
988 | }
|
---|
989 |
|
---|
990 | int VspKdViewCellsManager::ComputeSampleContributions(Ray &ray, const bool castRay)
|
---|
991 | {
|
---|
992 | // view cells not yet constructed
|
---|
993 | if (!ViewCellsConstructed())
|
---|
994 | return 0;
|
---|
995 |
|
---|
996 | //if (castRay) // TODO
|
---|
997 |
|
---|
998 | int sampleContributions = 0;
|
---|
999 |
|
---|
1000 | /*
|
---|
1001 | for (int i = 0; i < (int)rays.size(); ++ i)
|
---|
1002 | {
|
---|
1003 | Ray *ray = rays[i];
|
---|
1004 |
|
---|
1005 | mBspTree->CastRay(*ray);
|
---|
1006 |
|
---|
1007 | Intersectable *term =
|
---|
1008 | !ray->intersections.empty() ? ray->intersections[0].mObject : NULL
|
---|
1009 | ;
|
---|
1010 | sampleContributions +=
|
---|
1011 | AddSampleContributions(*ray, ray->sourceObject.mObject, term);
|
---|
1012 |
|
---|
1013 | }
|
---|
1014 | */
|
---|
1015 | return sampleContributions;
|
---|
1016 | }
|
---|
1017 |
|
---|
1018 | int VspKdViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
1019 | const RayContainer &rays)
|
---|
1020 | {
|
---|
1021 | if (!ViewCellsConstructed())
|
---|
1022 | return 0;
|
---|
1023 |
|
---|
1024 | return 0;
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | void VspKdViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
1028 | const RayContainer &sampleRays)
|
---|
1029 | {
|
---|
1030 | if (!ViewCellsConstructed())
|
---|
1031 | return;
|
---|
1032 |
|
---|
1033 | if (1)
|
---|
1034 | {
|
---|
1035 | Exporter *exporter = Exporter::GetExporter("vspkdtree.x3d");
|
---|
1036 | //exporter->SetWireframe();
|
---|
1037 | exporter->ExportVspKdTree(*mVspKdTree, mVspKdTree->GetStatistics().maxPvsSize);
|
---|
1038 |
|
---|
1039 | Debug << "average PVS size: " << mVspKdTree->GetAvgPvsSize() << endl;
|
---|
1040 |
|
---|
1041 | if (0)
|
---|
1042 | exporter->ExportGeometry(objects);
|
---|
1043 |
|
---|
1044 | bool exportRays = true;
|
---|
1045 |
|
---|
1046 | if (exportRays)
|
---|
1047 | {
|
---|
1048 | int raysSize = 2000;
|
---|
1049 | float prob = raysSize / (float)sampleRays.size();
|
---|
1050 |
|
---|
1051 | exporter->SetWireframe();
|
---|
1052 |
|
---|
1053 | RayContainer rays;
|
---|
1054 |
|
---|
1055 | for (int i = 0; i < sampleRays.size(); ++ i)
|
---|
1056 | {
|
---|
1057 | if (RandomValue(0,1) < prob)
|
---|
1058 | rays.push_back(sampleRays[i]);
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | exporter->ExportRays(rays, 1000, RgbColor(1, 0, 0));
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | delete exporter;
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | if (1)
|
---|
1068 | {
|
---|
1069 | vector<VspKdTreeLeaf *> leafContainer;
|
---|
1070 |
|
---|
1071 | mVspKdTree->CollectLeaves(leafContainer);
|
---|
1072 |
|
---|
1073 | for (int i = 0; i < 10; ++ i)
|
---|
1074 | {
|
---|
1075 | char s[64];
|
---|
1076 | sprintf(s, "vsp_leaves%04d.x3d", i);
|
---|
1077 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
1078 |
|
---|
1079 | // export geometry
|
---|
1080 | VspKdTreeLeaf *leaf = leafContainer[Random((int)leafContainer.size())];
|
---|
1081 | AxisAlignedBox3 box = mVspKdTree->GetBBox(leaf);
|
---|
1082 |
|
---|
1083 | Material m;
|
---|
1084 | m.mDiffuseColor = RgbColor(0, 1, 1);
|
---|
1085 | exporter->SetForcedMaterial(m);
|
---|
1086 | exporter->SetWireframe();
|
---|
1087 | exporter->ExportBox(box);
|
---|
1088 |
|
---|
1089 | //-- export stored rays
|
---|
1090 | VssRayContainer vssRays;
|
---|
1091 | leaf->GetRays(vssRays);
|
---|
1092 |
|
---|
1093 | VssRayContainer rays;
|
---|
1094 | VssRayContainer::const_iterator it, it_end = vssRays.end();
|
---|
1095 |
|
---|
1096 | for (it = vssRays.begin(); it != it_end; ++ it)
|
---|
1097 | {
|
---|
1098 | //if (!(*it)->mOriginObject && !(*it)->mTerminationObject)
|
---|
1099 | rays.push_back(*it);
|
---|
1100 |
|
---|
1101 | //if (!(*it)->mOriginObject && !(*it)->mTerminationObject)
|
---|
1102 | // Debug << "ERR: " << (*it)->mOrigin << " " << (*it)->mTermination << endl;
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
1106 |
|
---|
1107 | //-- export stored PVS
|
---|
1108 |
|
---|
1109 | ObjectContainer pvsObj;
|
---|
1110 | leaf->ExtractPvs(pvsObj);
|
---|
1111 |
|
---|
1112 | exporter->ExportGeometry(pvsObj);
|
---|
1113 |
|
---|
1114 | delete exporter;
|
---|
1115 | }
|
---|
1116 | }
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | int VspKdViewCellsManager::GetType() const
|
---|
1120 | {
|
---|
1121 | return VSP_KD;
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 |
|
---|
1125 | /**********************************************************************/
|
---|
1126 | /* VspBspViewCellsManager implementation */
|
---|
1127 | /**********************************************************************/
|
---|
1128 |
|
---|
1129 | VspBspViewCellsManager::VspBspViewCellsManager(VspBspTree *vspBspTree,
|
---|
1130 | int constructionSamples):
|
---|
1131 | ViewCellsManager(constructionSamples),
|
---|
1132 | mVspBspTree(vspBspTree)
|
---|
1133 | {
|
---|
1134 | mRenderSimulator = NULL;// TODO new VspBspRenderSimulator(bspTree);
|
---|
1135 | //InitRenderSimulator();
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | bool VspBspViewCellsManager::ViewCellsConstructed() const
|
---|
1139 | {
|
---|
1140 | return mVspBspTree->GetRoot() != NULL;
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 | ViewCell *VspBspViewCellsManager::GenerateViewCell(Mesh *mesh) const
|
---|
1144 | {
|
---|
1145 | return new BspViewCell(mesh);
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | int VspBspViewCellsManager::Construct(const ObjectContainer &objects,
|
---|
1149 | const VssRayContainer &rays,
|
---|
1150 | AxisAlignedBox3 *sceneBbox)
|
---|
1151 | {
|
---|
1152 | // if view cells were already constructed
|
---|
1153 | if (ViewCellsConstructed())
|
---|
1154 | return 0;
|
---|
1155 |
|
---|
1156 | Debug << "Constructing bsp view cells" << endl;
|
---|
1157 |
|
---|
1158 | int sampleContributions = 0;
|
---|
1159 |
|
---|
1160 | VssRayContainer sampleRays;
|
---|
1161 |
|
---|
1162 | int limit = min (mConstructionSamples, (int)rays.size());
|
---|
1163 |
|
---|
1164 | for (int i = 0; i < limit; ++ i)
|
---|
1165 | sampleRays.push_back(rays[i]);
|
---|
1166 |
|
---|
1167 | mVspBspTree->Construct(sampleRays);
|
---|
1168 |
|
---|
1169 | Debug << mVspBspTree->GetStatistics() << endl;
|
---|
1170 |
|
---|
1171 | return sampleContributions;
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | int VspBspViewCellsManager::ComputeSampleContributions(Ray &ray, const bool castRay)
|
---|
1175 | {
|
---|
1176 | // view cells not yet constructed
|
---|
1177 | if (!ViewCellsConstructed())
|
---|
1178 | return 0;
|
---|
1179 |
|
---|
1180 | int contributingSamples = 0;
|
---|
1181 |
|
---|
1182 | if (castRay)
|
---|
1183 | mVspBspTree->CastRay(ray);
|
---|
1184 |
|
---|
1185 | Intersectable *tObject =
|
---|
1186 | !ray.intersections.empty() ? ray.intersections[0].mObject : NULL;
|
---|
1187 |
|
---|
1188 | Intersectable *sObject = ray.sourceObject.mObject;
|
---|
1189 |
|
---|
1190 | if (sObject || tObject)
|
---|
1191 | {
|
---|
1192 | // object can be seen from the view cell => add to view cell pvs
|
---|
1193 | for (int j = 0; j < (int)ray.bspIntersections.size(); ++ j)
|
---|
1194 | {
|
---|
1195 | VspBspLeaf *leaf = NULL; //TODO ray.bspIntersections[j].mLeaf;
|
---|
1196 |
|
---|
1197 | // if ray not in unbounded space
|
---|
1198 | if (leaf->GetViewCell() != mVspBspTree->GetRootCell())
|
---|
1199 | {
|
---|
1200 | if (sObject)
|
---|
1201 | {
|
---|
1202 | contributingSamples +=
|
---|
1203 | leaf->GetViewCell()->GetPvs().AddSample(sObject);
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 | if (tObject)
|
---|
1207 | {
|
---|
1208 | contributingSamples +=
|
---|
1209 | leaf->GetViewCell()->GetPvs().AddSample(tObject);
|
---|
1210 | }
|
---|
1211 | }
|
---|
1212 | }
|
---|
1213 | }
|
---|
1214 |
|
---|
1215 | // rays passing through this viewcell
|
---|
1216 | if (0)
|
---|
1217 | for (int j = 1; j < ((int)ray.bspIntersections.size() - 1); ++ j)
|
---|
1218 | {
|
---|
1219 | VspBspLeaf *leaf = NULL;// TODO = ray.bspIntersections[j].mLeaf;
|
---|
1220 |
|
---|
1221 | if (leaf->GetViewCell() != mVspBspTree->GetRootCell())
|
---|
1222 | leaf->GetViewCell()->
|
---|
1223 | AddPassingRay(ray, contributingSamples ? 1 : 0);
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 | return contributingSamples;
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | int VspBspViewCellsManager::PostProcess(const ObjectContainer &objects,
|
---|
1230 | const RayContainer &rays)
|
---|
1231 | {
|
---|
1232 | return 0;// TODO
|
---|
1233 | if (!ViewCellsConstructed())
|
---|
1234 | {
|
---|
1235 | Debug << "view cells not constructed" << endl;
|
---|
1236 | return 0;
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 |
|
---|
1240 | //-- post processing of bsp view cells
|
---|
1241 | int vcSize = 0;
|
---|
1242 | int pvsSize = 0;
|
---|
1243 |
|
---|
1244 | VspBspViewCellsStatistics vcStats;
|
---|
1245 | mVspBspTree->EvaluateViewCellsStats(vcStats);
|
---|
1246 | Debug << "original view cell partition:\n" << vcStats << endl;
|
---|
1247 |
|
---|
1248 | if (1) // export view cells
|
---|
1249 | {
|
---|
1250 | cout << "exporting initial view cells (=leaves) ... ";
|
---|
1251 | Exporter *exporter = Exporter::GetExporter("view_cells.x3d");
|
---|
1252 |
|
---|
1253 | if (exporter)
|
---|
1254 | {
|
---|
1255 | exporter->SetWireframe();
|
---|
1256 | //exporter->ExportBspLeaves(*mVspBspTree, stat.maxPvs);
|
---|
1257 |
|
---|
1258 | if (0)
|
---|
1259 | {
|
---|
1260 | Material m;
|
---|
1261 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
1262 | exporter->SetForcedMaterial(m);
|
---|
1263 | exporter->SetWireframe();
|
---|
1264 |
|
---|
1265 | exporter->ExportGeometry(objects);
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | delete exporter;
|
---|
1269 | }
|
---|
1270 | cout << "finished" << endl;
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | cout << "starting post processing using " << mPostProcessSamples << " samples ... ";
|
---|
1274 |
|
---|
1275 | long startTime = GetTime();
|
---|
1276 |
|
---|
1277 |
|
---|
1278 | //-- merge or subdivide view cells
|
---|
1279 | int merged = 0;
|
---|
1280 |
|
---|
1281 | RayContainer::const_iterator rit, rit_end = rays.end();
|
---|
1282 | vector<Ray::BspIntersection>::const_iterator iit;
|
---|
1283 |
|
---|
1284 | int limit = min((int)rays.size(), mPostProcessSamples);
|
---|
1285 |
|
---|
1286 | for (int i = 0; i < limit; ++ i)
|
---|
1287 | {
|
---|
1288 | Ray *ray = rays[i];
|
---|
1289 |
|
---|
1290 | // traverse leaves stored in the rays and compare and merge consecutive
|
---|
1291 | // leaves (i.e., the neighbors in the tree)
|
---|
1292 | if (ray->bspIntersections.size() < 2)
|
---|
1293 | continue;
|
---|
1294 |
|
---|
1295 | iit = ray->bspIntersections.begin();
|
---|
1296 |
|
---|
1297 | VspBspLeaf *previousLeaf = NULL;//TODO(*iit).mLeaf;
|
---|
1298 | ++ iit;
|
---|
1299 |
|
---|
1300 | for (; iit != ray->bspIntersections.end(); ++ iit)
|
---|
1301 | {
|
---|
1302 | VspBspLeaf *leaf = NULL;//TODO(*iit).mLeaf;
|
---|
1303 |
|
---|
1304 | if (ShouldMerge(leaf, previousLeaf))
|
---|
1305 | {
|
---|
1306 | MergeVspBspLeafViewCells(leaf, previousLeaf);
|
---|
1307 |
|
---|
1308 | ++ merged;
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | previousLeaf = leaf;
|
---|
1312 | }
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | //-- stats and visualizations
|
---|
1316 | cout << "finished" << endl;
|
---|
1317 | cout << "merged " << merged << " view cells in "
|
---|
1318 | << TimeDiff(startTime, GetTime()) *1e-3 << " secs" << endl;
|
---|
1319 |
|
---|
1320 | return merged;
|
---|
1321 | }
|
---|
1322 |
|
---|
1323 | int VspBspViewCellsManager::GetType() const
|
---|
1324 | {
|
---|
1325 | return VSP_BSP;
|
---|
1326 | }
|
---|
1327 |
|
---|
1328 |
|
---|
1329 | void VspBspViewCellsManager::Visualize(const ObjectContainer &objects,
|
---|
1330 | const RayContainer &sampleRays)
|
---|
1331 | {
|
---|
1332 | return; // TODO
|
---|
1333 | if (!ViewCellsConstructed())
|
---|
1334 | return;
|
---|
1335 |
|
---|
1336 | //-- recount pvs
|
---|
1337 | VspBspViewCellsStatistics vcStats;
|
---|
1338 | mVspBspTree->EvaluateViewCellsStats(vcStats);
|
---|
1339 |
|
---|
1340 | if (1) // export view cells
|
---|
1341 | {
|
---|
1342 | cout << "exporting view cells after merge ... ";
|
---|
1343 | /*Exporter *exporter = Exporter::GetExporter("merged_view_cells.x3d");
|
---|
1344 |
|
---|
1345 | if (exporter)
|
---|
1346 | {
|
---|
1347 | exporter->ExportBspViewCellPartition(*mVspBspTree, vcStats.maxPvs);
|
---|
1348 | delete exporter;
|
---|
1349 | }
|
---|
1350 | */
|
---|
1351 | cout << "finished" << endl;
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 | //-- visualization of the BSP splits
|
---|
1355 | bool exportSplits = false;
|
---|
1356 | environment->GetBoolValue("BspTree.Visualization.exportSplits", exportSplits);
|
---|
1357 |
|
---|
1358 | if (exportSplits)
|
---|
1359 | {
|
---|
1360 | cout << "exporting splits ... ";
|
---|
1361 | ExportSplits(objects, sampleRays);
|
---|
1362 | cout << "finished" << endl;
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | //TODO ExportVspBspPvs(objects, sampleRays);
|
---|
1366 | }
|
---|
1367 |
|
---|
1368 |
|
---|
1369 | void VspBspViewCellsManager::ExportSplits(const ObjectContainer &objects,
|
---|
1370 | const RayContainer &sampleRays)
|
---|
1371 | {
|
---|
1372 | Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
|
---|
1373 |
|
---|
1374 | if (exporter)
|
---|
1375 | {
|
---|
1376 | Material m;
|
---|
1377 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
1378 | exporter->SetForcedMaterial(m);
|
---|
1379 | exporter->SetWireframe();
|
---|
1380 | // TODO
|
---|
1381 | //exporter->ExportBspSplits(*mVspBspTree, true);
|
---|
1382 |
|
---|
1383 | // take forced material, else big scenes cannot be viewed
|
---|
1384 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
1385 | exporter->SetForcedMaterial(m);
|
---|
1386 | exporter->SetFilled();
|
---|
1387 |
|
---|
1388 | exporter->ResetForcedMaterial();
|
---|
1389 |
|
---|
1390 | // export rays
|
---|
1391 | if (0)
|
---|
1392 | {
|
---|
1393 | RayContainer outRays;
|
---|
1394 |
|
---|
1395 | int raysSize = min((int)sampleRays.size(), mVisualizationSamples);
|
---|
1396 |
|
---|
1397 | for (int i = 0; i < raysSize; ++ i)
|
---|
1398 | {
|
---|
1399 | // only rays piercing geometry
|
---|
1400 | if (!sampleRays[i]->intersections.empty())
|
---|
1401 | outRays.push_back(sampleRays[i]);
|
---|
1402 | }
|
---|
1403 |
|
---|
1404 | // export rays
|
---|
1405 | exporter->ExportRays(outRays, 1000, RgbColor(1, 1, 0));
|
---|
1406 | }
|
---|
1407 |
|
---|
1408 | if (0)
|
---|
1409 | exporter->ExportGeometry(objects);
|
---|
1410 |
|
---|
1411 | delete exporter;
|
---|
1412 | }
|
---|
1413 | }
|
---|
1414 |
|
---|
1415 | void VspBspViewCellsManager::ExportBspPvs(const ObjectContainer &objects,
|
---|
1416 | const RayContainer &sampleRays)
|
---|
1417 | {
|
---|
1418 | const int leafOut = 10;
|
---|
1419 |
|
---|
1420 | ViewCell::NewMail();
|
---|
1421 |
|
---|
1422 | //-- some rays for output
|
---|
1423 | const int raysOut = min((int)sampleRays.size(), mVisualizationSamples);
|
---|
1424 | cout << "visualization using " << mVisualizationSamples << " samples" << endl;
|
---|
1425 | vector<Ray *> vcRays[leafOut];
|
---|
1426 |
|
---|
1427 | if (0)
|
---|
1428 | {
|
---|
1429 | //-- some random view cells and rays for output
|
---|
1430 | vector<VspBspLeaf *> vspBspLeaves;
|
---|
1431 |
|
---|
1432 | for (int i = 0; i < leafOut; ++ i)
|
---|
1433 | vspBspLeaves.push_back(mVspBspTree->GetRandomLeaf());
|
---|
1434 |
|
---|
1435 | for (int i = 0; i < (int)vspBspLeaves.size(); ++ i)
|
---|
1436 | {
|
---|
1437 | cout << "creating output for view cell " << i << " ... ";
|
---|
1438 | // check whether we can add the current ray to the output rays
|
---|
1439 | for (int k = 0; k < raysOut; ++ k)
|
---|
1440 | {
|
---|
1441 | Ray *ray = sampleRays[k];
|
---|
1442 |
|
---|
1443 | for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
|
---|
1444 | {
|
---|
1445 | BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
|
---|
1446 |
|
---|
1447 | if (vspBspLeaves[i]->GetViewCell() == leaf->GetViewCell())
|
---|
1448 | {
|
---|
1449 | vcRays[i].push_back(ray);
|
---|
1450 | }
|
---|
1451 | }
|
---|
1452 | }
|
---|
1453 |
|
---|
1454 | Intersectable::NewMail();
|
---|
1455 |
|
---|
1456 | BspViewCell *vc = dynamic_cast<BspViewCell *>
|
---|
1457 | (vspBspLeaves[i]->GetViewCell());
|
---|
1458 |
|
---|
1459 | //bspLeaves[j]->Mail();
|
---|
1460 | char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
|
---|
1461 |
|
---|
1462 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
1463 | exporter->SetFilled();
|
---|
1464 |
|
---|
1465 | ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin();
|
---|
1466 |
|
---|
1467 | exporter->SetWireframe();
|
---|
1468 | //exporter->SetFilled();
|
---|
1469 |
|
---|
1470 | Material m;//= RandomMaterial();
|
---|
1471 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
1472 | exporter->SetForcedMaterial(m);
|
---|
1473 |
|
---|
1474 | if (vc->GetMesh())
|
---|
1475 | exporter->ExportViewCell(vc);
|
---|
1476 | else
|
---|
1477 | {
|
---|
1478 | PolygonContainer vcGeom;
|
---|
1479 |
|
---|
1480 | // export view cell geometry
|
---|
1481 | mVspBspTree->ConstructGeometry(vc, vcGeom);
|
---|
1482 | exporter->ExportPolygons(vcGeom);
|
---|
1483 | CLEAR_CONTAINER(vcGeom);
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 | Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize()
|
---|
1487 | << ", piercing rays=" << (int)vcRays[i].size() << endl;
|
---|
1488 |
|
---|
1489 | // export rays piercing this view cell
|
---|
1490 | exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0));
|
---|
1491 |
|
---|
1492 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
1493 | exporter->SetForcedMaterial(m);
|
---|
1494 |
|
---|
1495 | // exporter->SetWireframe();
|
---|
1496 | exporter->SetFilled();
|
---|
1497 |
|
---|
1498 | // output PVS of view cell
|
---|
1499 | for (; it != vc->GetPvs().mEntries.end(); ++ it)
|
---|
1500 | {
|
---|
1501 | Intersectable *intersect = (*it).first;
|
---|
1502 | if (!intersect->Mailed())
|
---|
1503 | {
|
---|
1504 | exporter->ExportIntersectable(intersect);
|
---|
1505 | intersect->Mail();
|
---|
1506 | }
|
---|
1507 | }
|
---|
1508 |
|
---|
1509 | // output rest of the objects
|
---|
1510 | if (0)
|
---|
1511 | {
|
---|
1512 | Material m;//= RandomMaterial();
|
---|
1513 | m.mDiffuseColor = RgbColor(0, 0, 1);
|
---|
1514 | exporter->SetForcedMaterial(m);
|
---|
1515 |
|
---|
1516 | for (int j = 0; j < objects.size(); ++ j)
|
---|
1517 | if (!objects[j]->Mailed())
|
---|
1518 | {
|
---|
1519 | exporter->SetForcedMaterial(m);
|
---|
1520 | exporter->ExportIntersectable(objects[j]);
|
---|
1521 | objects[j]->Mail();
|
---|
1522 | }
|
---|
1523 | }
|
---|
1524 | DEL_PTR(exporter);
|
---|
1525 | cout << "finished" << endl;
|
---|
1526 | }
|
---|
1527 | }
|
---|
1528 | else
|
---|
1529 | {
|
---|
1530 | ViewCellContainer viewCells;
|
---|
1531 |
|
---|
1532 | mVspBspTree->CollectViewCells(viewCells);
|
---|
1533 | stable_sort(viewCells.begin(), viewCells.end(), vc_gt);
|
---|
1534 |
|
---|
1535 | int limit = min(leafOut, (int)viewCells.size());
|
---|
1536 |
|
---|
1537 | for (int i = 0; i < limit; ++ i)
|
---|
1538 | {
|
---|
1539 | cout << "creating output for view cell " << i << " ... ";
|
---|
1540 |
|
---|
1541 | Intersectable::NewMail();
|
---|
1542 | BspViewCell *vc = dynamic_cast<BspViewCell *>(viewCells[i]);
|
---|
1543 |
|
---|
1544 | cout << "creating output for view cell " << i << " ... ";
|
---|
1545 | // check whether we can add the current ray to the output rays
|
---|
1546 | for (int k = 0; k < raysOut; ++ k)
|
---|
1547 | {
|
---|
1548 | Ray *ray = sampleRays[k];
|
---|
1549 |
|
---|
1550 | for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
|
---|
1551 | {
|
---|
1552 | VspBspLeaf *leaf = NULL; // TODO = ray->bspIntersections[j].mLeaf;
|
---|
1553 |
|
---|
1554 | if (vc == leaf->GetViewCell())
|
---|
1555 | {
|
---|
1556 | vcRays[i].push_back(ray);
|
---|
1557 | }
|
---|
1558 | }
|
---|
1559 | }
|
---|
1560 |
|
---|
1561 | //bspLeaves[j]->Mail();
|
---|
1562 | char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
|
---|
1563 |
|
---|
1564 | Exporter *exporter = Exporter::GetExporter(s);
|
---|
1565 |
|
---|
1566 | exporter->SetWireframe();
|
---|
1567 |
|
---|
1568 | Material m;//= RandomMaterial();
|
---|
1569 | m.mDiffuseColor = RgbColor(0, 1, 0);
|
---|
1570 | exporter->SetForcedMaterial(m);
|
---|
1571 |
|
---|
1572 | if (vc->GetMesh())
|
---|
1573 | exporter->ExportViewCell(vc);
|
---|
1574 | else
|
---|
1575 | {
|
---|
1576 | PolygonContainer vcGeom;
|
---|
1577 | // export view cell
|
---|
1578 | mVspBspTree->ConstructGeometry(vc, vcGeom);
|
---|
1579 | exporter->ExportPolygons(vcGeom);
|
---|
1580 | CLEAR_CONTAINER(vcGeom);
|
---|
1581 | }
|
---|
1582 |
|
---|
1583 |
|
---|
1584 | Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize()
|
---|
1585 | << ", piercing rays=" << (int)vcRays[i].size() << endl;
|
---|
1586 |
|
---|
1587 |
|
---|
1588 | // export rays piercing this view cell
|
---|
1589 | exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0));
|
---|
1590 |
|
---|
1591 | m.mDiffuseColor = RgbColor(1, 0, 0);
|
---|
1592 | exporter->SetForcedMaterial(m);
|
---|
1593 |
|
---|
1594 | ViewCellPvsMap::const_iterator it,
|
---|
1595 | it_end = vc->GetPvs().mEntries.end();
|
---|
1596 |
|
---|
1597 | // output PVS of view cell
|
---|
1598 | for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it)
|
---|
1599 | {
|
---|
1600 | Intersectable *intersect = (*it).first;
|
---|
1601 |
|
---|
1602 | if (!intersect->Mailed())
|
---|
1603 | {
|
---|
1604 | Material m = RandomMaterial();
|
---|
1605 |
|
---|
1606 | exporter->SetForcedMaterial(m);
|
---|
1607 |
|
---|
1608 | exporter->ExportIntersectable(intersect);
|
---|
1609 | intersect->Mail();
|
---|
1610 | }
|
---|
1611 | }
|
---|
1612 |
|
---|
1613 | DEL_PTR(exporter);
|
---|
1614 | cout << "finished" << endl;
|
---|
1615 | }
|
---|
1616 | }
|
---|
1617 | }
|
---|
1618 |
|
---|
1619 |
|
---|
1620 | bool VspBspViewCellsManager::MergeVspBspLeafViewCells(VspBspLeaf *front, |
---|
1621 | VspBspLeaf *back) const |
---|
1622 | { |
---|
1623 | BspViewCell *viewCell = |
---|
1624 | dynamic_cast<BspViewCell *>(MergeViewCells(*front->GetViewCell(), |
---|
1625 | *back->GetViewCell())); |
---|
1626 | |
---|
1627 | if (!viewCell) |
---|
1628 | return false; |
---|
1629 | |
---|
1630 | // change view cells of all leaves associated with the |
---|
1631 | // previous view cells |
---|
1632 | |
---|
1633 | BspViewCell *fVc = front->GetViewCell(); |
---|
1634 | BspViewCell *bVc = back->GetViewCell(); |
---|
1635 | |
---|
1636 | // TODO |
---|
1637 | /*vector<VspBspLeaf *> fLeaves = fVc->mBspLeaves; |
---|
1638 | vector<VspBspLeaf *> bLeaves = bVc->mBspLeaves; |
---|
1639 | |
---|
1640 | vector<VspBspLeaf *>::const_iterator it; |
---|
1641 | |
---|
1642 | for (it = fLeaves.begin(); it != fLeaves.end(); ++ it) |
---|
1643 | { |
---|
1644 | (*it)->SetViewCell(viewCell); |
---|
1645 | viewCell->mBspLeaves.push_back(*it); |
---|
1646 | } |
---|
1647 | for (it = bLeaves.begin(); it != bLeaves.end(); ++ it) |
---|
1648 | { |
---|
1649 | (*it)->SetViewCell(viewCell); |
---|
1650 | viewCell->mBspLeaves.push_back(*it); |
---|
1651 | } |
---|
1652 | |
---|
1653 | DEL_PTR(fVc); |
---|
1654 | DEL_PTR(bVc); |
---|
1655 | */ |
---|
1656 | return true; |
---|
1657 | } |
---|
1658 | |
---|
1659 | bool VspBspViewCellsManager::ShouldMerge(VspBspLeaf *front, VspBspLeaf *back) const |
---|
1660 | { |
---|
1661 | ViewCell *fvc = front->GetViewCell(); |
---|
1662 | ViewCell *bvc = back->GetViewCell(); |
---|
1663 | |
---|
1664 | if ((fvc == mVspBspTree->GetRootCell()) || |
---|
1665 | (bvc == mVspBspTree->GetRootCell()) || |
---|
1666 | (fvc == bvc)) |
---|
1667 | return false; |
---|
1668 | |
---|
1669 | int fdiff = fvc->GetPvs().Diff(bvc->GetPvs()); |
---|
1670 | |
---|
1671 | if (fvc->GetPvs().GetSize() + fdiff < mMaxPvs) |
---|
1672 | { |
---|
1673 | if ((fvc->GetPvs().GetSize() < mMinPvs) || |
---|
1674 | (bvc->GetPvs().GetSize() < mMinPvs) || |
---|
1675 | ((fdiff < mMinPvsDif) && (bvc->GetPvs().Diff(fvc->GetPvs()) < mMinPvsDif))) |
---|
1676 | { |
---|
1677 | return true; |
---|
1678 | } |
---|
1679 | } |
---|
1680 | |
---|
1681 | return false; |
---|
1682 | } |
---|
1683 | |
---|