1 |
|
---|
2 | #include "SceneGraph.h"
|
---|
3 | #include "Exporter.h"
|
---|
4 | #include "UnigraphicsParser.h"
|
---|
5 | #include "X3dParser.h"
|
---|
6 | #include "Preprocessor.h"
|
---|
7 | #include "ViewCell.h"
|
---|
8 | #include "Environment.h"
|
---|
9 | #include "ViewCellsManager.h"
|
---|
10 | #include "ViewCellBsp.h"
|
---|
11 | #include "VspBspTree.h"
|
---|
12 | #include "RenderSimulator.h"
|
---|
13 | #include "GlRenderer.h"
|
---|
14 | #include "PlyParser.h"
|
---|
15 | #include "SamplingStrategy.h"
|
---|
16 | #include "VspTree.h"
|
---|
17 | #include "OspTree.h"
|
---|
18 | #include "ObjParser.h"
|
---|
19 | #include "BvHierarchy.h"
|
---|
20 | #include "HierarchyManager.h"
|
---|
21 | #include "VssRay.h"
|
---|
22 | #include "IntelRayCaster.h"
|
---|
23 | #include "HavranRayCaster.h"
|
---|
24 | #include "InternalRayCaster.h"
|
---|
25 | #include "GlobalLinesRenderer.h"
|
---|
26 | #include "ObjectsParser.h"
|
---|
27 |
|
---|
28 |
|
---|
29 | #define DEBUG_RAYCAST 0
|
---|
30 | #define SHOW_RAYCAST_TIMING 1
|
---|
31 |
|
---|
32 | using namespace std;
|
---|
33 |
|
---|
34 | namespace GtpVisibilityPreprocessor {
|
---|
35 |
|
---|
36 |
|
---|
37 | inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
38 | {
|
---|
39 | return obj1->mId < obj2->mId;
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | Preprocessor *preprocessor = NULL;
|
---|
44 |
|
---|
45 |
|
---|
46 | Preprocessor::Preprocessor():
|
---|
47 | mKdTree(NULL),
|
---|
48 | mBspTree(NULL),
|
---|
49 | mVspBspTree(NULL),
|
---|
50 | mViewCellsManager(NULL),
|
---|
51 | mRenderSimulator(NULL),
|
---|
52 | mPass(0),
|
---|
53 | mSceneGraph(NULL),
|
---|
54 | mRayCaster(NULL),
|
---|
55 | mStopComputation(false),
|
---|
56 | mThread(NULL),
|
---|
57 | mGlobalLinesRenderer(NULL),
|
---|
58 | mUseHwGlobalLines(false),
|
---|
59 | mTotalRaysCast(0)
|
---|
60 | {
|
---|
61 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
|
---|
62 |
|
---|
63 | // renderer will be constructed when the scene graph and viewcell manager will be known
|
---|
64 | renderer = NULL;
|
---|
65 |
|
---|
66 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlDebugger", mUseGlDebugger);
|
---|
67 | Environment::GetSingleton()->GetBoolValue("Preprocessor.loadMeshes", mLoadMeshes);
|
---|
68 | Environment::GetSingleton()->GetBoolValue("Preprocessor.quitOnFinish", mQuitOnFinish);
|
---|
69 | Environment::GetSingleton()->GetBoolValue("Preprocessor.computeVisibility", mComputeVisibility);
|
---|
70 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
71 | Environment::GetSingleton()->GetBoolValue("Preprocessor.exportVisibility", mExportVisibility );
|
---|
72 |
|
---|
73 | char buffer[256];
|
---|
74 | Environment::GetSingleton()->GetStringValue("Preprocessor.visibilityFile", buffer);
|
---|
75 | mVisibilityFileName = buffer;
|
---|
76 |
|
---|
77 | Environment::GetSingleton()->GetStringValue("Preprocessor.stats", buffer);
|
---|
78 | mStats.open(buffer);
|
---|
79 |
|
---|
80 | Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", mApplyVisibilityFilter);
|
---|
81 | Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
|
---|
82 | mApplyVisibilitySpatialFilter );
|
---|
83 | Environment::GetSingleton()->GetFloatValue("Preprocessor.visibilityFilterWidth", mVisibilityFilterWidth);
|
---|
84 |
|
---|
85 | Environment::GetSingleton()->GetBoolValue("Preprocessor.exportObj", mExportObj);
|
---|
86 |
|
---|
87 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useViewSpaceBox", mUseViewSpaceBox);
|
---|
88 |
|
---|
89 | Environment::GetSingleton()->GetBoolValue("Preprocessor.Export.rays", mExportRays);
|
---|
90 | Environment::GetSingleton()->GetBoolValue("Preprocessor.Export.animation", mExportAnimation);
|
---|
91 | Environment::GetSingleton()->GetIntValue("Preprocessor.Export.numRays", mExportNumRays);
|
---|
92 |
|
---|
93 | Environment::GetSingleton()->GetIntValue("Preprocessor.samplesPerPass", mSamplesPerPass);
|
---|
94 | Environment::GetSingleton()->GetIntValue("Preprocessor.totalSamples", mTotalSamples);
|
---|
95 | Environment::GetSingleton()->GetIntValue("Preprocessor.totalTime", mTotalTime);
|
---|
96 | Environment::GetSingleton()->GetIntValue("Preprocessor.samplesPerEvaluation",
|
---|
97 | mSamplesPerEvaluation);
|
---|
98 |
|
---|
99 | Debug << "******* Preprocessor Options **********" << endl;
|
---|
100 | Debug << "detect empty view space=" << mDetectEmptyViewSpace << endl;
|
---|
101 | Debug << "load meshes: " << mLoadMeshes << endl;
|
---|
102 | Debug << "load meshes: " << mLoadMeshes << endl;
|
---|
103 | Debug << "export obj: " << mExportObj << endl;
|
---|
104 | Debug << "use view space box: " << mUseViewSpaceBox << endl;
|
---|
105 |
|
---|
106 | cout << "samples per pass " << mSamplesPerPass << endl;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | Preprocessor::~Preprocessor()
|
---|
111 | {
|
---|
112 | cout << "cleaning up" << endl;
|
---|
113 |
|
---|
114 | cout << "Deleting view cells manager ... \n";
|
---|
115 | DEL_PTR(mViewCellsManager);
|
---|
116 | cout << "done.\n";
|
---|
117 |
|
---|
118 | cout << "Deleting bsp tree ... \n";
|
---|
119 | DEL_PTR(mBspTree);
|
---|
120 | cout << "done.\n";
|
---|
121 |
|
---|
122 | cout << "Deleting kd tree ...\n";
|
---|
123 | DEL_PTR(mKdTree);
|
---|
124 | cout << "done.\n";
|
---|
125 |
|
---|
126 | cout << "Deleting vspbsp tree ... \n";
|
---|
127 | DEL_PTR(mVspBspTree);
|
---|
128 | cout << "done.\n";
|
---|
129 |
|
---|
130 | cout << "Deleting scene graph ... \n";
|
---|
131 | DEL_PTR(mSceneGraph);
|
---|
132 | cout << "done.\n";
|
---|
133 |
|
---|
134 | cout << "deleting render simulator ... \n";
|
---|
135 | DEL_PTR(mRenderSimulator);
|
---|
136 | mRenderSimulator = NULL;
|
---|
137 |
|
---|
138 | cout << "deleting renderer ... \n";
|
---|
139 | DEL_PTR(renderer);
|
---|
140 | renderer = NULL;
|
---|
141 |
|
---|
142 | cout << "deleting ray caster ... \n";
|
---|
143 | DEL_PTR(mRayCaster);
|
---|
144 |
|
---|
145 | #ifdef USE_CG
|
---|
146 | cout << "deleting global lines renderer ... \n";
|
---|
147 | DEL_PTR(mGlobalLinesRenderer);
|
---|
148 | #endif
|
---|
149 | cout << "finished" << endl;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | GlRendererBuffer *Preprocessor::GetRenderer()
|
---|
154 | {
|
---|
155 | return renderer;
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 |
|
---|
160 |
|
---|
161 | void Preprocessor::SetThread(PreprocessorThread *t)
|
---|
162 | {
|
---|
163 | mThread = t;
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | PreprocessorThread *Preprocessor::GetThread() const
|
---|
168 | {
|
---|
169 | return mThread;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | bool Preprocessor::LoadBinaryObj(const string &filename,
|
---|
174 | SceneGraphLeaf *root,
|
---|
175 | vector<FaceParentInfo> *parents,
|
---|
176 | float scale)
|
---|
177 | {
|
---|
178 | //ifstream inStream(filename, ios::binary);
|
---|
179 | igzstream inStream(filename.c_str());
|
---|
180 |
|
---|
181 | if (!inStream.is_open())
|
---|
182 | return false;
|
---|
183 |
|
---|
184 | cout << "binary obj dump available, loading " << filename.c_str() << endl;
|
---|
185 |
|
---|
186 | // read in triangle size
|
---|
187 | int numTriangles;
|
---|
188 |
|
---|
189 | const int t = 500000;
|
---|
190 | inStream.read(reinterpret_cast<char *>(&numTriangles), sizeof(int));
|
---|
191 | root->mGeometry.reserve(numTriangles);
|
---|
192 | cout << "loading " << numTriangles << " triangles (" << numTriangles *
|
---|
193 | (sizeof(TriangleIntersectable) + sizeof(TriangleIntersectable *)) /
|
---|
194 | (1024 * 1024) << " MB)" << endl;
|
---|
195 |
|
---|
196 | int i = 0;
|
---|
197 |
|
---|
198 | while (1)
|
---|
199 | {
|
---|
200 | Triangle3 tri;
|
---|
201 |
|
---|
202 | inStream.read(reinterpret_cast<char *>(tri.mVertices + 0), sizeof(Vector3));
|
---|
203 | inStream.read(reinterpret_cast<char *>(tri.mVertices + 1), sizeof(Vector3));
|
---|
204 | inStream.read(reinterpret_cast<char *>(tri.mVertices + 2), sizeof(Vector3));
|
---|
205 |
|
---|
206 | if (scale > 0.0f)
|
---|
207 | {
|
---|
208 | tri.mVertices[0] *= scale;
|
---|
209 | tri.mVertices[1] *= scale;
|
---|
210 | tri.mVertices[2] *= scale;
|
---|
211 | }
|
---|
212 |
|
---|
213 | // end of file reached
|
---|
214 | if (inStream.eof())
|
---|
215 | break;
|
---|
216 |
|
---|
217 | TriangleIntersectable *obj = new TriangleIntersectable(tri);
|
---|
218 | root->mGeometry.push_back(obj);
|
---|
219 |
|
---|
220 | if ((i ++) % t == t)
|
---|
221 | cout<<"\r"<<i<<"/"<<numTriangles<<"\r";
|
---|
222 | }
|
---|
223 |
|
---|
224 | if (i != numTriangles)
|
---|
225 | {
|
---|
226 | cout << "warning: triangle size does not match with loaded triangle size" << endl;
|
---|
227 | return false;
|
---|
228 | }
|
---|
229 |
|
---|
230 | cout << "loaded " << numTriangles << " triangles" << endl;
|
---|
231 |
|
---|
232 | return true;
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | bool Preprocessor::ExportBinaryObj(const string &filename, SceneGraphLeaf *root)
|
---|
237 | {
|
---|
238 | ogzstream samplesOut(filename.c_str());
|
---|
239 |
|
---|
240 | if (!samplesOut.is_open())
|
---|
241 | return false;
|
---|
242 |
|
---|
243 | int numTriangles = (int)root->mGeometry.size();
|
---|
244 |
|
---|
245 | samplesOut.write(reinterpret_cast<char *>(&numTriangles), sizeof(int));
|
---|
246 |
|
---|
247 | ObjectContainer::const_iterator oit, oit_end = root->mGeometry.end();
|
---|
248 |
|
---|
249 | for (oit = root->mGeometry.begin(); oit != oit_end; ++ oit)
|
---|
250 | {
|
---|
251 | Intersectable *obj = *oit;
|
---|
252 |
|
---|
253 | if (obj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
254 | {
|
---|
255 | Triangle3 tri = static_cast<TriangleIntersectable *>(obj)->GetItem();
|
---|
256 | //tri.mVertices[0] *= 10; tri.mVertices[1] *= 10; tri.mVertices[2] *= 10;
|
---|
257 |
|
---|
258 | samplesOut.write(reinterpret_cast<char *>(tri.mVertices + 0), sizeof(Vector3));
|
---|
259 | samplesOut.write(reinterpret_cast<char *>(tri.mVertices + 1), sizeof(Vector3));
|
---|
260 | samplesOut.write(reinterpret_cast<char *>(tri.mVertices + 2), sizeof(Vector3));
|
---|
261 | }
|
---|
262 | else
|
---|
263 | {
|
---|
264 | cout << "not implemented intersectable type " << obj->Type() << endl;
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | cout << "exported " << numTriangles << " triangles" << endl;
|
---|
269 |
|
---|
270 | return true;
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | bool Preprocessor::ExportObj(const string &filename, const ObjectContainer &objects)
|
---|
275 | {
|
---|
276 | ofstream samplesOut(filename.c_str());
|
---|
277 |
|
---|
278 | if (!samplesOut.is_open())
|
---|
279 | return false;
|
---|
280 |
|
---|
281 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
282 |
|
---|
283 | //AxisAlignedBox3 bbox = mSceneGraph->GetBox(); bbox.Enlarge(30.0);
|
---|
284 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
285 | {
|
---|
286 | Intersectable *obj = *oit;
|
---|
287 |
|
---|
288 | if (obj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
289 | {
|
---|
290 | Triangle3 tri = static_cast<TriangleIntersectable *>(obj)->GetItem();
|
---|
291 | //if (!(bbox.IsInside(tri.mVertices[0]) && bbox.IsInside(tri.mVertices[1]) && bbox.IsInside(tri.mVertices[2])))continue;
|
---|
292 |
|
---|
293 | samplesOut << "v " << tri.mVertices[0].x << " " << tri.mVertices[0].y << " " << tri.mVertices[0].z << endl;
|
---|
294 | samplesOut << "v " << tri.mVertices[1].x << " " << tri.mVertices[1].y << " " << tri.mVertices[1].z << endl;
|
---|
295 | samplesOut << "v " << tri.mVertices[2].x << " " << tri.mVertices[2].y << " " << tri.mVertices[2].z << endl;
|
---|
296 | //}
|
---|
297 | }
|
---|
298 | else
|
---|
299 | {
|
---|
300 | cout << "not implemented intersectable type " << obj->Type() << endl;
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | // write faces
|
---|
305 | int i = 1;
|
---|
306 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
307 | {
|
---|
308 | Intersectable *obj = *oit;
|
---|
309 | if (obj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
|
---|
310 | {
|
---|
311 | //Triangle3 tri = static_cast<TriangleIntersectable *>(obj)->GetItem();
|
---|
312 | //if (!(bbox.IsInside(tri.mVertices[0]) && bbox.IsInside(tri.mVertices[1]) && bbox.IsInside(tri.mVertices[2]))) continue;
|
---|
313 |
|
---|
314 | Triangle3 tri = static_cast<TriangleIntersectable *>(obj)->GetItem();
|
---|
315 | samplesOut << "f " << i << " " << i + 1 << " " << i + 2 << endl;
|
---|
316 | i += 3;
|
---|
317 | }
|
---|
318 | else
|
---|
319 | {
|
---|
320 | cout << "not implemented intersectable type " << obj->Type() << endl;
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | return true;
|
---|
325 |
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 |
|
---|
330 | Intersectable *Preprocessor::GetParentObject(const int index) const
|
---|
331 | {
|
---|
332 | if (index < 0)
|
---|
333 | {
|
---|
334 | //cerr << "Warning: triangle index smaller zero! " << index << endl;
|
---|
335 | return NULL;
|
---|
336 | }
|
---|
337 |
|
---|
338 | if (!mFaceParents.empty())
|
---|
339 | {
|
---|
340 | if (index >= (int)mFaceParents.size())
|
---|
341 | {
|
---|
342 | cerr << "Warning: triangle index out of range! " << index << endl;
|
---|
343 | return NULL;
|
---|
344 | }
|
---|
345 | else
|
---|
346 | {
|
---|
347 | return mFaceParents[index].mObject;
|
---|
348 | }
|
---|
349 | }
|
---|
350 | else
|
---|
351 | {
|
---|
352 | if (index >= (int)mObjects.size())
|
---|
353 | {
|
---|
354 | cerr<<"Warning: triangle index out of range! " << index << " of " << (int)mObjects.size() << endl;
|
---|
355 | return NULL;
|
---|
356 | }
|
---|
357 | else
|
---|
358 | {
|
---|
359 | return mObjects[index];
|
---|
360 | }
|
---|
361 | }
|
---|
362 | }
|
---|
363 |
|
---|
364 |
|
---|
365 | Vector3 Preprocessor::GetParentNormal(const int index) const
|
---|
366 | {
|
---|
367 | if (!mFaceParents.empty())
|
---|
368 | {
|
---|
369 | return mFaceParents[index].mObject->GetNormal(mFaceParents[index].mFaceIndex);
|
---|
370 | }
|
---|
371 | else
|
---|
372 | {
|
---|
373 | return mObjects[index]->GetNormal(0);
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 |
|
---|
378 | bool
|
---|
379 | Preprocessor::LoadScene(const string &filename)
|
---|
380 | {
|
---|
381 | // use leaf nodes of the original spatial hierarchy as occludees
|
---|
382 | mSceneGraph = new SceneGraph;
|
---|
383 |
|
---|
384 | Parser *parser;
|
---|
385 | vector<string> filenames;
|
---|
386 | const int files = SplitFilenames(filename, filenames);
|
---|
387 | cout << "number of input files: " << files << endl;
|
---|
388 |
|
---|
389 | bool result = false;
|
---|
390 | bool isObj = false;
|
---|
391 |
|
---|
392 | // root for different files
|
---|
393 | mSceneGraph->SetRoot(new SceneGraphInterior());
|
---|
394 |
|
---|
395 | // intel ray caster can only trace triangles
|
---|
396 | int rayCastMethod;
|
---|
397 | Environment::GetSingleton()->GetIntValue("Preprocessor.rayCastMethod",
|
---|
398 | rayCastMethod);
|
---|
399 |
|
---|
400 | vector<FaceParentInfo> *fi =
|
---|
401 | ((rayCastMethod == RayCaster::INTEL_RAYCASTER ||
|
---|
402 | rayCastMethod == RayCaster::HAVRAN_RAYCASTER
|
---|
403 | ) && mLoadMeshes) ?
|
---|
404 | &mFaceParents : NULL;
|
---|
405 |
|
---|
406 | if (files == 1)
|
---|
407 | {
|
---|
408 | SceneGraphLeaf *leaf = new SceneGraphLeaf();
|
---|
409 |
|
---|
410 | if (strstr(filename.c_str(), ".x3d"))
|
---|
411 | {
|
---|
412 | parser = new X3dParser;
|
---|
413 |
|
---|
414 | result = parser->ParseFile(filename,
|
---|
415 | leaf,
|
---|
416 | mLoadMeshes,
|
---|
417 | fi);
|
---|
418 | delete parser;
|
---|
419 | }
|
---|
420 | else if (strstr(filename.c_str(), ".ply") || strstr(filename.c_str(), ".plb"))
|
---|
421 | {
|
---|
422 | parser = new PlyParser;
|
---|
423 |
|
---|
424 | result = parser->ParseFile(filename,
|
---|
425 | leaf,
|
---|
426 | mLoadMeshes,
|
---|
427 | fi);
|
---|
428 | delete parser;
|
---|
429 | }
|
---|
430 | else if (strstr(filename.c_str(), ".obj"))
|
---|
431 | {
|
---|
432 | isObj = true;
|
---|
433 |
|
---|
434 | // hack: load binary dump
|
---|
435 | const string bnFile = ReplaceSuffix(filename, ".obj", ".bn");
|
---|
436 |
|
---|
437 | if (!mLoadMeshes)
|
---|
438 | {
|
---|
439 | result = LoadBinaryObj(bnFile, leaf, fi);
|
---|
440 | }
|
---|
441 |
|
---|
442 | // parse obj
|
---|
443 | if (!result)
|
---|
444 | {
|
---|
445 | cout << "no binary dump available or loading full meshes, parsing file" << endl;
|
---|
446 | parser = new ObjParser;
|
---|
447 |
|
---|
448 | result = parser->ParseFile(filename, leaf, mLoadMeshes, fi);
|
---|
449 |
|
---|
450 | cout << "loaded " << (int)leaf->mGeometry.size() << " entities" << endl;
|
---|
451 |
|
---|
452 | // only works for triangles
|
---|
453 | if (result && !mLoadMeshes)
|
---|
454 | {
|
---|
455 | cout << "exporting binary obj to " << bnFile << "... " << endl;
|
---|
456 |
|
---|
457 | ExportBinaryObj(bnFile, leaf);
|
---|
458 |
|
---|
459 | cout << "finished" << endl;
|
---|
460 | }
|
---|
461 |
|
---|
462 | delete parser;
|
---|
463 | }
|
---|
464 | }
|
---|
465 | else
|
---|
466 | {
|
---|
467 | parser = new UnigraphicsParser;
|
---|
468 | result = parser->ParseFile(filename, leaf, mLoadMeshes, fi);
|
---|
469 | delete parser;
|
---|
470 | }
|
---|
471 |
|
---|
472 | if (result)
|
---|
473 | {
|
---|
474 | mSceneGraph->GetRoot()->mChildren.push_back(leaf);
|
---|
475 | }
|
---|
476 |
|
---|
477 | cout << filename << endl;
|
---|
478 | }
|
---|
479 | else
|
---|
480 | {
|
---|
481 | vector<string>::const_iterator fit, fit_end = filenames.end();
|
---|
482 |
|
---|
483 | for (fit = filenames.begin(); fit != fit_end; ++ fit)
|
---|
484 | {
|
---|
485 | const string filename = *fit;
|
---|
486 |
|
---|
487 | cout << "parsing file " << filename.c_str() << endl;
|
---|
488 | if (strstr(filename.c_str(), ".x3d"))
|
---|
489 | parser = new X3dParser;
|
---|
490 | else
|
---|
491 | parser = new UnigraphicsParser;
|
---|
492 |
|
---|
493 | SceneGraphLeaf *node = new SceneGraphLeaf();
|
---|
494 |
|
---|
495 | const bool success =
|
---|
496 | parser->ParseFile(filename, node, mLoadMeshes, fi);
|
---|
497 |
|
---|
498 | if (success)
|
---|
499 | {
|
---|
500 | mSceneGraph->GetRoot()->mChildren.push_back(node);
|
---|
501 | result = true; // at least one file parsed
|
---|
502 | }
|
---|
503 |
|
---|
504 | // temporare hack
|
---|
505 | //if (!strstr(filename.c_str(), "plane")) mSceneGraph->GetRoot()->UpdateBox();
|
---|
506 |
|
---|
507 | delete parser;
|
---|
508 | }
|
---|
509 | }
|
---|
510 |
|
---|
511 | if (result)
|
---|
512 | {
|
---|
513 | int intersectables, faces;
|
---|
514 | mSceneGraph->GetStatistics(intersectables, faces);
|
---|
515 |
|
---|
516 | cout<<filename<<" parsed successfully."<<endl;
|
---|
517 | cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
|
---|
518 | cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
|
---|
519 |
|
---|
520 | mObjects.reserve(intersectables);
|
---|
521 | mSceneGraph->CollectObjects(mObjects);
|
---|
522 |
|
---|
523 | mSceneGraph->AssignObjectIds();
|
---|
524 |
|
---|
525 | mSceneGraph->GetRoot()->UpdateBox();
|
---|
526 |
|
---|
527 | cout << "finished loading" << endl;
|
---|
528 | }
|
---|
529 |
|
---|
530 | return result;
|
---|
531 | }
|
---|
532 |
|
---|
533 | bool
|
---|
534 | Preprocessor::ExportPreprocessedData(const string &filename)
|
---|
535 | {
|
---|
536 | mViewCellsManager->ExportViewCells(filename, true, mObjects);
|
---|
537 | return true;
|
---|
538 | }
|
---|
539 |
|
---|
540 |
|
---|
541 | bool
|
---|
542 | Preprocessor::PostProcessVisibility()
|
---|
543 | {
|
---|
544 |
|
---|
545 | if (mApplyVisibilityFilter || mApplyVisibilitySpatialFilter) {
|
---|
546 | cout<<"Applying visibility filter ...";
|
---|
547 | cout<<"filter width = " << mVisibilityFilterWidth << endl;
|
---|
548 |
|
---|
549 | if (!mViewCellsManager)
|
---|
550 | return false;
|
---|
551 |
|
---|
552 |
|
---|
553 | mViewCellsManager->ApplyFilter(mKdTree,
|
---|
554 | mApplyVisibilityFilter ?
|
---|
555 | mVisibilityFilterWidth : -1.0f,
|
---|
556 | mApplyVisibilitySpatialFilter ?
|
---|
557 | mVisibilityFilterWidth : -1.0f);
|
---|
558 | cout << "done." << endl;
|
---|
559 | }
|
---|
560 |
|
---|
561 | // export the preprocessed information to a file
|
---|
562 | if (1 && mExportVisibility)
|
---|
563 | {
|
---|
564 | ExportPreprocessedData(mVisibilityFileName);
|
---|
565 | }
|
---|
566 |
|
---|
567 | return true;
|
---|
568 | }
|
---|
569 |
|
---|
570 |
|
---|
571 | bool
|
---|
572 | Preprocessor::BuildKdTree()
|
---|
573 | {
|
---|
574 | mKdTree = new KdTree;
|
---|
575 |
|
---|
576 | // add mesh instances of the scene graph to the root of the tree
|
---|
577 | KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
|
---|
578 |
|
---|
579 | mSceneGraph->CollectObjects(root->mObjects);
|
---|
580 |
|
---|
581 | const long startTime = GetTime();
|
---|
582 | cout << "building kd tree ... " << endl;
|
---|
583 |
|
---|
584 | mKdTree->Construct();
|
---|
585 | sceneBox = mKdTree->GetBox();
|
---|
586 |
|
---|
587 | cout << "finished kd tree construction in " << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
588 | << " secs " << endl;
|
---|
589 |
|
---|
590 | return true;
|
---|
591 | }
|
---|
592 |
|
---|
593 |
|
---|
594 | void
|
---|
595 | Preprocessor::KdTreeStatistics(ostream &s)
|
---|
596 | {
|
---|
597 | s<<mKdTree->GetStatistics();
|
---|
598 | }
|
---|
599 |
|
---|
600 | void
|
---|
601 | Preprocessor::BspTreeStatistics(ostream &s)
|
---|
602 | {
|
---|
603 | s << mBspTree->GetStatistics();
|
---|
604 | }
|
---|
605 |
|
---|
606 | bool
|
---|
607 | Preprocessor::Export( const string &filename,
|
---|
608 | const bool scene,
|
---|
609 | const bool kdtree
|
---|
610 | )
|
---|
611 | {
|
---|
612 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
613 |
|
---|
614 | if (exporter) {
|
---|
615 | if (2 && scene)
|
---|
616 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
617 |
|
---|
618 | if (1 && kdtree) {
|
---|
619 | exporter->SetWireframe();
|
---|
620 | exporter->ExportKdTree(*mKdTree);
|
---|
621 | }
|
---|
622 |
|
---|
623 | delete exporter;
|
---|
624 | return true;
|
---|
625 | }
|
---|
626 |
|
---|
627 | return false;
|
---|
628 | }
|
---|
629 |
|
---|
630 |
|
---|
631 | bool Preprocessor::PrepareViewCells()
|
---|
632 | {
|
---|
633 | #if 0
|
---|
634 | // load the view cells assigning the found objects to the pvss
|
---|
635 | cerr << "loading binary view cells" << endl;
|
---|
636 | ViewCellsManager *dummyViewCellsManager =
|
---|
637 | LoadViewCellsBinary("test.vc", mObjects, false, NULL);
|
---|
638 |
|
---|
639 | //cerr << "reexporting the binary view cells" << endl;
|
---|
640 | //dummyViewCellsManager->ExportViewCellsBinary("outvc.xml.gz", true, mObjects);
|
---|
641 |
|
---|
642 | return false;
|
---|
643 | #endif
|
---|
644 |
|
---|
645 | ///////
|
---|
646 | //-- parse view cells construction method
|
---|
647 |
|
---|
648 | Environment::GetSingleton()->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells);
|
---|
649 | char buf[100];
|
---|
650 |
|
---|
651 | if (mLoadViewCells)
|
---|
652 | {
|
---|
653 |
|
---|
654 | #ifdef USE_BIT_PVS
|
---|
655 | // HACK: for kd pvs, set pvs size to maximal number of kd nodes
|
---|
656 | vector<KdLeaf *> leaves;
|
---|
657 | preprocessor->mKdTree->CollectLeaves(leaves);
|
---|
658 |
|
---|
659 | ObjectPvs::SetPvsSize((int)leaves.size());
|
---|
660 | #endif
|
---|
661 |
|
---|
662 | Environment::GetSingleton()->GetStringValue("ViewCells.filename", buf);
|
---|
663 | cout << "loading objects from " << buf << endl;
|
---|
664 |
|
---|
665 | // load scene objects which are the entities used as pvs entries
|
---|
666 | ObjectContainer pvsObjects;
|
---|
667 | if (0) LoadObjects(buf, pvsObjects, mObjects);
|
---|
668 |
|
---|
669 | const bool finalizeViewCells = false;
|
---|
670 | cout << "loading view cells from " << buf << endl;
|
---|
671 |
|
---|
672 | mViewCellsManager = ViewCellsManager::LoadViewCells(buf,
|
---|
673 | pvsObjects,
|
---|
674 | mObjects,
|
---|
675 | finalizeViewCells,
|
---|
676 | NULL);
|
---|
677 |
|
---|
678 | cout << "view cells loaded." << endl<<flush;
|
---|
679 |
|
---|
680 | if (!mViewCellsManager)
|
---|
681 | {
|
---|
682 | cerr << "no view cells manager could be loaded" << endl;
|
---|
683 | return false;
|
---|
684 | }
|
---|
685 | }
|
---|
686 | else
|
---|
687 | {
|
---|
688 | // parse type of view cells manager
|
---|
689 | Environment::GetSingleton()->GetStringValue("ViewCells.type", buf);
|
---|
690 | mViewCellsManager = CreateViewCellsManager(buf);
|
---|
691 |
|
---|
692 | if (mUseViewSpaceBox)
|
---|
693 | {
|
---|
694 | // default view space is the extent of the scene
|
---|
695 | AxisAlignedBox3 viewSpaceBox = mSceneGraph->GetBox();
|
---|
696 |
|
---|
697 | // use a small box outside of the scene
|
---|
698 | viewSpaceBox.Scale(Vector3(0.15f, 0.3f, 0.5f));
|
---|
699 | viewSpaceBox.Translate(Vector3(Magnitude(mSceneGraph->GetBox().Size()) * 0.3f, 0, 0));
|
---|
700 | mViewCellsManager->SetViewSpaceBox(viewSpaceBox);
|
---|
701 | }
|
---|
702 |
|
---|
703 |
|
---|
704 | bool loadVcGeometry;
|
---|
705 | Environment::GetSingleton()->GetBoolValue("ViewCells.loadGeometry", loadVcGeometry);
|
---|
706 |
|
---|
707 | bool extrudeBaseTriangles;
|
---|
708 | Environment::GetSingleton()->GetBoolValue("ViewCells.useBaseTrianglesAsGeometry", extrudeBaseTriangles);
|
---|
709 |
|
---|
710 | char vcGeomFilename[100];
|
---|
711 | Environment::GetSingleton()->GetStringValue("ViewCells.geometryFilename", vcGeomFilename);
|
---|
712 |
|
---|
713 | // create view cells from specified geometry
|
---|
714 | if (loadVcGeometry)
|
---|
715 | {
|
---|
716 | if (mViewCellsManager->GetType() == ViewCellsManager::BSP)
|
---|
717 | {
|
---|
718 | if (!mViewCellsManager->LoadViewCellsGeometry(vcGeomFilename, extrudeBaseTriangles))
|
---|
719 | cerr << "loading view cells geometry failed" << endl;
|
---|
720 | }
|
---|
721 | else
|
---|
722 | {
|
---|
723 | cerr << "loading view cells geometry is not implemented for this manager" << endl;
|
---|
724 | }
|
---|
725 | }
|
---|
726 | }
|
---|
727 |
|
---|
728 | ////////
|
---|
729 | //-- evaluation of render cost heuristics
|
---|
730 |
|
---|
731 | float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
|
---|
732 |
|
---|
733 | Environment::GetSingleton()->GetFloatValue("Simulation.objRenderCost",objRenderCost);
|
---|
734 | Environment::GetSingleton()->GetFloatValue("Simulation.vcOverhead", vcOverhead);
|
---|
735 | Environment::GetSingleton()->GetFloatValue("Simulation.moveSpeed", moveSpeed);
|
---|
736 |
|
---|
737 | mRenderSimulator =
|
---|
738 | new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
|
---|
739 |
|
---|
740 | mViewCellsManager->SetRenderer(mRenderSimulator);
|
---|
741 |
|
---|
742 | mViewCellsManager->SetPreprocessor(this);
|
---|
743 |
|
---|
744 | return true;
|
---|
745 | }
|
---|
746 |
|
---|
747 |
|
---|
748 | bool Preprocessor::ConstructViewCells()
|
---|
749 | {
|
---|
750 | // construct view cells using it's own set of samples
|
---|
751 | mViewCellsManager->Construct(this);
|
---|
752 |
|
---|
753 | // visualizations and statistics
|
---|
754 | Debug << "finished view cells:" << endl;
|
---|
755 | mViewCellsManager->PrintStatistics(Debug);
|
---|
756 |
|
---|
757 | return true;
|
---|
758 | }
|
---|
759 |
|
---|
760 |
|
---|
761 | ViewCellsManager *Preprocessor::CreateViewCellsManager(const char *name)
|
---|
762 | {
|
---|
763 | ViewCellsTree *vcTree = new ViewCellsTree;
|
---|
764 |
|
---|
765 | if (strcmp(name, "kdTree") == 0)
|
---|
766 | {
|
---|
767 | mViewCellsManager = new KdViewCellsManager(vcTree, mKdTree);
|
---|
768 | }
|
---|
769 | else if (strcmp(name, "bspTree") == 0)
|
---|
770 | {
|
---|
771 | Debug << "view cell type: Bsp" << endl;
|
---|
772 |
|
---|
773 | mBspTree = new BspTree();
|
---|
774 | mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
|
---|
775 | }
|
---|
776 | else if (strcmp(name, "vspBspTree") == 0)
|
---|
777 | {
|
---|
778 | Debug << "view cell type: VspBsp" << endl;
|
---|
779 |
|
---|
780 | mVspBspTree = new VspBspTree();
|
---|
781 | mViewCellsManager = new VspBspViewCellsManager(vcTree, mVspBspTree);
|
---|
782 | }
|
---|
783 | else if (strcmp(name, "vspOspTree") == 0)
|
---|
784 | {
|
---|
785 | Debug << "view cell type: VspOsp" << endl;
|
---|
786 | char buf[100];
|
---|
787 | Environment::GetSingleton()->GetStringValue("Hierarchy.type", buf);
|
---|
788 |
|
---|
789 | mViewCellsManager = new VspOspViewCellsManager(vcTree, buf);
|
---|
790 | }
|
---|
791 | else if (strcmp(name, "sceneDependent") == 0) //TODO
|
---|
792 | {
|
---|
793 | Debug << "view cell type: Bsp" << endl;
|
---|
794 |
|
---|
795 | mBspTree = new BspTree();
|
---|
796 | mViewCellsManager = new BspViewCellsManager(vcTree, mBspTree);
|
---|
797 | }
|
---|
798 | else
|
---|
799 | {
|
---|
800 | cerr << "Wrong view cells type " << name << "!!!" << endl;
|
---|
801 | exit(1);
|
---|
802 | }
|
---|
803 |
|
---|
804 | return mViewCellsManager;
|
---|
805 | }
|
---|
806 |
|
---|
807 |
|
---|
808 | // use ascii format to store rays
|
---|
809 | #define USE_ASCII 0
|
---|
810 |
|
---|
811 |
|
---|
812 | bool Preprocessor::LoadKdTree(const string &filename)
|
---|
813 | {
|
---|
814 | mKdTree = new KdTree();
|
---|
815 | return mKdTree->ImportBinTree(filename.c_str(), mObjects);
|
---|
816 | }
|
---|
817 |
|
---|
818 |
|
---|
819 | bool Preprocessor::ExportKdTree(const string &filename)
|
---|
820 | {
|
---|
821 | return mKdTree->ExportBinTree(filename.c_str());
|
---|
822 | }
|
---|
823 |
|
---|
824 |
|
---|
825 | bool Preprocessor::LoadSamples(VssRayContainer &samples,
|
---|
826 | ObjectContainer &objects) const
|
---|
827 | {
|
---|
828 | std::stable_sort(objects.begin(), objects.end(), ilt);
|
---|
829 | char fileName[100];
|
---|
830 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
831 |
|
---|
832 | Vector3 origin, termination;
|
---|
833 | // HACK: needed only for lower_bound algorithm to find the
|
---|
834 | // intersected objects
|
---|
835 | MeshInstance sObj(NULL);
|
---|
836 | MeshInstance tObj(NULL);
|
---|
837 |
|
---|
838 | #if USE_ASCII
|
---|
839 | ifstream inStream(fileName);
|
---|
840 | if (!inStream.is_open())
|
---|
841 | return false;
|
---|
842 |
|
---|
843 | string buf;
|
---|
844 | while (!(getline(inStream, buf)).eof())
|
---|
845 | {
|
---|
846 | sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
|
---|
847 | &origin.x, &origin.y, &origin.z,
|
---|
848 | &termination.x, &termination.y, &termination.z,
|
---|
849 | &(sObj.mId), &(tObj.mId));
|
---|
850 |
|
---|
851 | Intersectable *sourceObj = NULL;
|
---|
852 | Intersectable *termObj = NULL;
|
---|
853 |
|
---|
854 | if (sObj.mId >= 0)
|
---|
855 | {
|
---|
856 | ObjectContainer::iterator oit =
|
---|
857 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
858 | sourceObj = *oit;
|
---|
859 | }
|
---|
860 |
|
---|
861 | if (tObj.mId >= 0)
|
---|
862 | {
|
---|
863 | ObjectContainer::iterator oit =
|
---|
864 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
865 | termObj = *oit;
|
---|
866 | }
|
---|
867 |
|
---|
868 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
869 | }
|
---|
870 | #else
|
---|
871 | ifstream inStream(fileName, ios::binary);
|
---|
872 | if (!inStream.is_open())
|
---|
873 | return false;
|
---|
874 |
|
---|
875 | while (1)
|
---|
876 | {
|
---|
877 | inStream.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
878 | inStream.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
879 | inStream.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
|
---|
880 | inStream.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
|
---|
881 |
|
---|
882 | if (inStream.eof())
|
---|
883 | break;
|
---|
884 |
|
---|
885 | Intersectable *sourceObj = NULL;
|
---|
886 | Intersectable *termObj = NULL;
|
---|
887 |
|
---|
888 | if (sObj.mId >= 0)
|
---|
889 | {
|
---|
890 | ObjectContainer::iterator oit =
|
---|
891 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
892 | sourceObj = *oit;
|
---|
893 | }
|
---|
894 |
|
---|
895 | if (tObj.mId >= 0)
|
---|
896 | {
|
---|
897 | ObjectContainer::iterator oit =
|
---|
898 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
899 | termObj = *oit;
|
---|
900 | }
|
---|
901 |
|
---|
902 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
903 | }
|
---|
904 | #endif
|
---|
905 |
|
---|
906 | inStream.close();
|
---|
907 |
|
---|
908 | return true;
|
---|
909 | }
|
---|
910 |
|
---|
911 |
|
---|
912 | bool Preprocessor::ExportSamples(const VssRayContainer &samples) const
|
---|
913 | {
|
---|
914 | char fileName[100];
|
---|
915 | Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
916 |
|
---|
917 |
|
---|
918 | VssRayContainer::const_iterator it, it_end = samples.end();
|
---|
919 |
|
---|
920 | #if USE_ASCII
|
---|
921 | ofstream samplesOut(fileName);
|
---|
922 | if (!samplesOut.is_open())
|
---|
923 | return false;
|
---|
924 |
|
---|
925 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
926 | {
|
---|
927 | VssRay *ray = *it;
|
---|
928 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
929 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
930 |
|
---|
931 | samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
|
---|
932 | << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
|
---|
933 | << sourceid << " " << termid << "\n";
|
---|
934 | }
|
---|
935 | #else
|
---|
936 | ofstream samplesOut(fileName, ios::binary);
|
---|
937 | if (!samplesOut.is_open())
|
---|
938 | return false;
|
---|
939 |
|
---|
940 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
941 | {
|
---|
942 | VssRay *ray = *it;
|
---|
943 | Vector3 origin(ray->GetOrigin());
|
---|
944 | Vector3 termination(ray->GetTermination());
|
---|
945 |
|
---|
946 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
947 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
948 |
|
---|
949 | samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
950 | samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
951 | samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
|
---|
952 | samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
|
---|
953 | }
|
---|
954 | #endif
|
---|
955 | samplesOut.close();
|
---|
956 |
|
---|
957 | return true;
|
---|
958 | }
|
---|
959 |
|
---|
960 |
|
---|
961 | int
|
---|
962 | Preprocessor::GenerateRays(const int number,
|
---|
963 | SamplingStrategy &strategy,
|
---|
964 | SimpleRayContainer &rays)
|
---|
965 | {
|
---|
966 | return strategy.GenerateSamples(number, rays);
|
---|
967 | }
|
---|
968 |
|
---|
969 |
|
---|
970 | int
|
---|
971 | Preprocessor::GenerateRays(const int number,
|
---|
972 | const int sampleType,
|
---|
973 | SimpleRayContainer &rays)
|
---|
974 | {
|
---|
975 | const int startSize = (int)rays.size();
|
---|
976 | SamplingStrategy *strategy = GenerateSamplingStrategy(sampleType);
|
---|
977 | int castRays = 0;
|
---|
978 |
|
---|
979 | if (!strategy)
|
---|
980 | {
|
---|
981 | return 0;
|
---|
982 | }
|
---|
983 |
|
---|
984 | #if 1
|
---|
985 | castRays = strategy->GenerateSamples(number, rays);
|
---|
986 | #else
|
---|
987 | GenerateRayBundle(rays, newRay, 16, 0);
|
---|
988 | castRays += 16;
|
---|
989 | #endif
|
---|
990 |
|
---|
991 | delete strategy;
|
---|
992 | return castRays;
|
---|
993 | }
|
---|
994 |
|
---|
995 |
|
---|
996 | SamplingStrategy *Preprocessor::GenerateSamplingStrategy(const int strategyId)
|
---|
997 | {
|
---|
998 | switch (strategyId)
|
---|
999 | {
|
---|
1000 | case SamplingStrategy::OBJECT_BASED_DISTRIBUTION:
|
---|
1001 | return new ObjectBasedDistribution(*this);
|
---|
1002 | case SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION:
|
---|
1003 | return new ObjectDirectionBasedDistribution(*this);
|
---|
1004 | case SamplingStrategy::DIRECTION_BASED_DISTRIBUTION:
|
---|
1005 | return new DirectionBasedDistribution(*this);
|
---|
1006 | case SamplingStrategy::DIRECTION_BOX_BASED_DISTRIBUTION:
|
---|
1007 | return new DirectionBoxBasedDistribution(*this);
|
---|
1008 | case SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION:
|
---|
1009 | return new SpatialBoxBasedDistribution(*this);
|
---|
1010 | case SamplingStrategy::REVERSE_OBJECT_BASED_DISTRIBUTION:
|
---|
1011 | return new ReverseObjectBasedDistribution(*this);
|
---|
1012 | //case SamplingStrategy::VIEWCELL_BORDER_BASED_DISTRIBUTION:
|
---|
1013 | // return new ViewCellBorderBasedDistribution(*this);
|
---|
1014 | case SamplingStrategy::REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION:
|
---|
1015 | return new ReverseViewSpaceBorderBasedDistribution(*this);
|
---|
1016 | case SamplingStrategy::GLOBAL_LINES_DISTRIBUTION:
|
---|
1017 | return new GlobalLinesDistribution(*this);
|
---|
1018 |
|
---|
1019 | //case OBJECTS_INTERIOR_DISTRIBUTION:
|
---|
1020 | // return new ObjectsInteriorDistribution(*this);
|
---|
1021 | default: // no valid strategy
|
---|
1022 | Debug << "warning: no valid sampling strategy" << endl;
|
---|
1023 | return NULL;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | return NULL; // should never come here
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 |
|
---|
1030 | bool Preprocessor::LoadInternKdTree(const string &internKdTree)
|
---|
1031 | {
|
---|
1032 | bool mUseKdTree = true;
|
---|
1033 |
|
---|
1034 |
|
---|
1035 | int rayCastMethod;
|
---|
1036 | Environment::GetSingleton()->
|
---|
1037 | GetIntValue("Preprocessor.rayCastMethod", rayCastMethod);
|
---|
1038 |
|
---|
1039 | // first buildup the tree for havran ray caster if it is used
|
---|
1040 | if ((rayCastMethod == 2) || (rayCastMethod == 3))
|
---|
1041 | {
|
---|
1042 | HavranRayCaster *hr = 0;
|
---|
1043 |
|
---|
1044 | if (rayCastMethod == 3)
|
---|
1045 | hr = reinterpret_cast<HavranDynRayCaster*>(mRayCaster);
|
---|
1046 | else
|
---|
1047 | hr = reinterpret_cast<HavranRayCaster*>(mRayCaster);
|
---|
1048 |
|
---|
1049 | hr->Build(this->mObjects);
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | // #ifdef USE_HAVRAN_RAYCASTER
|
---|
1053 | // if ((rayCastMethod == 2) || (rayCastMethod == 3))
|
---|
1054 | // {
|
---|
1055 | // HavranRayCaster *hr = 0;
|
---|
1056 |
|
---|
1057 | // if (rayCastMethod == 3)
|
---|
1058 | // hr = reinterpret_cast<HavranDynRayCaster*>(mRayCaster);
|
---|
1059 | // else
|
---|
1060 | // hr = reinterpret_cast<HavranRayCaster*>(mRayCaster);
|
---|
1061 |
|
---|
1062 | // string ktbFilename = internKdTree;
|
---|
1063 | // int l = (int)ktbFilename.length();
|
---|
1064 | // ktbFilename[l-1] = 't';
|
---|
1065 | // ktbFilename[l-2] = 'b';
|
---|
1066 | // ktbFilename[l-3] = 'k';
|
---|
1067 |
|
---|
1068 | // cout << "Trying to load tree from file " << ktbFilename << endl;
|
---|
1069 | // if (hr->ImportBinTree(ktbFilename, this->mObjects)) {
|
---|
1070 | // cout << "Loading failed - building kd-tree" << endl;
|
---|
1071 | // hr->Build(this->mObjects);
|
---|
1072 | // cout << "Exporting kd-tree to file " << ktbFilename << endl;
|
---|
1073 | // hr->ExportBinTree(ktbFilename);
|
---|
1074 | // }
|
---|
1075 | // else
|
---|
1076 | // cout << " done." << endl;
|
---|
1077 | // }
|
---|
1078 | // #endif
|
---|
1079 |
|
---|
1080 |
|
---|
1081 | if (!mUseKdTree) {
|
---|
1082 | // create just a dummy KdTree
|
---|
1083 | mKdTree = new KdTree;
|
---|
1084 | return true;
|
---|
1085 | }
|
---|
1086 |
|
---|
1087 |
|
---|
1088 |
|
---|
1089 | // always try to load the kd tree
|
---|
1090 | cout << "loading kd tree file " << internKdTree << " ... " << endl;
|
---|
1091 |
|
---|
1092 | if (!LoadKdTree(internKdTree)) {
|
---|
1093 | cout << "error loading kd tree with filename "
|
---|
1094 | << internKdTree << ", rebuilding it instead ... " << endl;
|
---|
1095 | // build new kd tree from scene geometry
|
---|
1096 | BuildKdTree();
|
---|
1097 |
|
---|
1098 | // export kd tree?
|
---|
1099 | const long startTime = GetTime();
|
---|
1100 | cout << "exporting kd tree ... ";
|
---|
1101 |
|
---|
1102 | if (!ExportKdTree(internKdTree))
|
---|
1103 | {
|
---|
1104 | cout << " error exporting kd tree with filename "
|
---|
1105 | << internKdTree << endl;
|
---|
1106 | }
|
---|
1107 | else
|
---|
1108 | {
|
---|
1109 | cout << "finished in "
|
---|
1110 | << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
1111 | << " secs" << endl;
|
---|
1112 | }
|
---|
1113 | }
|
---|
1114 |
|
---|
1115 | KdTreeStatistics(cout);
|
---|
1116 | sceneBox = mKdTree->GetBox();
|
---|
1117 |
|
---|
1118 | cout << mKdTree->GetBox() << endl;
|
---|
1119 |
|
---|
1120 | return true;
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 |
|
---|
1124 | bool Preprocessor::InitRayCast(const string &externKdTree,
|
---|
1125 | const string &internKdTree)
|
---|
1126 | {
|
---|
1127 | int rayCastMethod;
|
---|
1128 | Environment::GetSingleton()->
|
---|
1129 | GetIntValue("Preprocessor.rayCastMethod", rayCastMethod);
|
---|
1130 |
|
---|
1131 | if (rayCastMethod == 0)
|
---|
1132 | {
|
---|
1133 | cout << "ray cast method: internal" << endl;
|
---|
1134 | mRayCaster = new InternalRayCaster(*this);
|
---|
1135 | }
|
---|
1136 | if (rayCastMethod == 1)
|
---|
1137 | {
|
---|
1138 | #ifdef GTP_INTERNAL
|
---|
1139 | cout << "ray cast method: intel" << endl;
|
---|
1140 | mRayCaster = new IntelRayCaster(*this, externKdTree);
|
---|
1141 | #endif
|
---|
1142 | }
|
---|
1143 | if (rayCastMethod == 2)
|
---|
1144 | {
|
---|
1145 | #ifdef USE_HAVRAN_RAYCASTER
|
---|
1146 | cout << "ray cast method: havran" << endl <<flush;
|
---|
1147 | mRayCaster = new GALIGN16 HavranRayCaster(*this);
|
---|
1148 | #endif
|
---|
1149 | }
|
---|
1150 | if (rayCastMethod == 3)
|
---|
1151 | {
|
---|
1152 | #ifdef USE_HAVRAN_RAYCASTER
|
---|
1153 | cout << "ray cast method: havran - dyn" << endl <<flush;
|
---|
1154 | mRayCaster = new GALIGN16 HavranDynRayCaster(*this);
|
---|
1155 | #endif
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 |
|
---|
1159 | /////////////////
|
---|
1160 | //-- reserve constant block of rays
|
---|
1161 |
|
---|
1162 | // hack: If we dont't use view cells loading, there must be at least as much rays
|
---|
1163 | // as are needed for the view cells construction
|
---|
1164 | bool loadViewCells;
|
---|
1165 | Environment::GetSingleton()->GetBoolValue("ViewCells.loadFromFile", loadViewCells);
|
---|
1166 |
|
---|
1167 | int reserveRays;
|
---|
1168 | int constructionSamples;
|
---|
1169 |
|
---|
1170 | if (!loadViewCells)
|
---|
1171 | {
|
---|
1172 | cout << "hack: setting ray pool size to view cell construction or evaluation size" << endl;
|
---|
1173 |
|
---|
1174 | constructionSamples = 1000000;
|
---|
1175 |
|
---|
1176 | char buf[100];
|
---|
1177 | Environment::GetSingleton()->GetStringValue("ViewCells.type", buf);
|
---|
1178 |
|
---|
1179 | if (strcmp(buf, "vspBspTree") == 0)
|
---|
1180 | {
|
---|
1181 | Environment::GetSingleton()->GetIntValue("VspBspTree.Construction.samples", constructionSamples);
|
---|
1182 |
|
---|
1183 | }
|
---|
1184 | else if (strcmp(buf, "vspOspTree") == 0)
|
---|
1185 | {
|
---|
1186 | Environment::GetSingleton()->GetIntValue("Hierarchy.Construction.samples", constructionSamples);
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | int evalSamplesPerPass;
|
---|
1190 |
|
---|
1191 | Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samplesPerPass", evalSamplesPerPass);
|
---|
1192 |
|
---|
1193 | reserveRays = max(constructionSamples, evalSamplesPerPass);
|
---|
1194 | reserveRays *= 2;
|
---|
1195 | }
|
---|
1196 | else
|
---|
1197 | {
|
---|
1198 | const int n = 4;
|
---|
1199 | cout << "hack: setting ray pool size to multiple " << n << " of samples per pass" << endl;
|
---|
1200 | reserveRays = mSamplesPerPass * n;
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | cout << "======================" << endl;
|
---|
1204 | cout << "reserving " << reserveRays << " rays " << endl;
|
---|
1205 | mRayCaster->ReserveVssRayPool(reserveRays);
|
---|
1206 | cout<<"done."<<endl<<flush;
|
---|
1207 | return true;
|
---|
1208 | }
|
---|
1209 |
|
---|
1210 |
|
---|
1211 | void
|
---|
1212 | Preprocessor::CastRays(
|
---|
1213 | SimpleRayContainer &rays,
|
---|
1214 | VssRayContainer &vssRays,
|
---|
1215 | const bool castDoubleRays,
|
---|
1216 | const bool pruneInvalidRays
|
---|
1217 | )
|
---|
1218 | {
|
---|
1219 | const long t1 = GetTime();
|
---|
1220 |
|
---|
1221 | // !!!!!!!!!!!!!!!! VH no sorting
|
---|
1222 | if (
|
---|
1223 | rays.size() > 10000
|
---|
1224 | )
|
---|
1225 | {
|
---|
1226 | mRayCaster->SortRays(rays);
|
---|
1227 | cout<<"Rays sorted in "<<TimeDiff(t1, GetTime())<<" ms."<<endl;
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 | int numTransformed = 0;
|
---|
1231 |
|
---|
1232 |
|
---|
1233 | if (mUseHwGlobalLines)
|
---|
1234 | {
|
---|
1235 | CastRaysWithHwGlobalLines(
|
---|
1236 | rays,
|
---|
1237 | vssRays,
|
---|
1238 | castDoubleRays,
|
---|
1239 | pruneInvalidRays
|
---|
1240 | );
|
---|
1241 | }
|
---|
1242 | else
|
---|
1243 | {
|
---|
1244 | mRayCaster->CastRays(
|
---|
1245 | rays,
|
---|
1246 | vssRays,
|
---|
1247 | mViewCellsManager->GetViewSpaceBox(),
|
---|
1248 | castDoubleRays,
|
---|
1249 | pruneInvalidRays);
|
---|
1250 |
|
---|
1251 | // disabled not neccessary
|
---|
1252 | UpdateDynamicObjects();
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 |
|
---|
1256 | if (rays.size() > 10000)
|
---|
1257 | {
|
---|
1258 | cout << endl;
|
---|
1259 | long t2 = GetTime();
|
---|
1260 |
|
---|
1261 | #if SHOW_RAYCAST_TIMING
|
---|
1262 | if (castDoubleRays)
|
---|
1263 | cout << 2 * rays.size() / (1e3f * TimeDiff(t1, t2)) << "M double rays/s" << endl;
|
---|
1264 | else
|
---|
1265 | cout << rays.size() / (1e3f * TimeDiff(t1, t2)) << "M single rays/s" << endl;
|
---|
1266 | #endif
|
---|
1267 |
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 | DeterminePvsObjects(vssRays);
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 |
|
---|
1274 | void
|
---|
1275 | Preprocessor::CastRaysWithHwGlobalLines(
|
---|
1276 | SimpleRayContainer &rays,
|
---|
1277 | VssRayContainer &vssRays,
|
---|
1278 | const bool castDoubleRays,
|
---|
1279 | const bool pruneInvalidRays)
|
---|
1280 | {
|
---|
1281 | SimpleRayContainer::const_iterator rit, rit_end = rays.end();
|
---|
1282 | SimpleRayContainer rayBucket;
|
---|
1283 | int i = 0;
|
---|
1284 | for (rit = rays.begin(); rit != rit_end; ++ rit, ++ i)
|
---|
1285 | {
|
---|
1286 | SimpleRay ray = *rit;
|
---|
1287 | #ifdef USE_CG
|
---|
1288 | // HACK: global lines must be treated special
|
---|
1289 | if (ray.mDistribution == SamplingStrategy::HW_GLOBAL_LINES_DISTRIBUTION)
|
---|
1290 | {
|
---|
1291 | mGlobalLinesRenderer->CastGlobalLines(ray, vssRays);
|
---|
1292 | continue;
|
---|
1293 | }
|
---|
1294 | #endif
|
---|
1295 | rayBucket.push_back(ray);
|
---|
1296 |
|
---|
1297 | // 16 rays gathered => do ray casting
|
---|
1298 | if (rayBucket.size() >= 16)
|
---|
1299 | {
|
---|
1300 | mRayCaster->CastRays16(
|
---|
1301 | rayBucket,
|
---|
1302 | vssRays,
|
---|
1303 | mViewCellsManager->GetViewSpaceBox(),
|
---|
1304 | castDoubleRays,
|
---|
1305 | pruneInvalidRays);
|
---|
1306 |
|
---|
1307 | rayBucket.clear();
|
---|
1308 | }
|
---|
1309 |
|
---|
1310 | if (rays.size() > 100000 && i % 100000 == 0)
|
---|
1311 | cout<<"\r"<<i<<"/"<<(int)rays.size()<<"\r";
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | // cast rest of rays
|
---|
1315 | SimpleRayContainer::const_iterator sit, sit_end = rayBucket.end();
|
---|
1316 |
|
---|
1317 | for (sit = rayBucket.begin(); sit != sit_end; ++ sit)
|
---|
1318 | {
|
---|
1319 | SimpleRay ray = *sit;
|
---|
1320 |
|
---|
1321 | #ifdef USE_CG
|
---|
1322 | // HACK: global lines must be treated special
|
---|
1323 | if (ray.mDistribution == SamplingStrategy::HW_GLOBAL_LINES_DISTRIBUTION)
|
---|
1324 | {
|
---|
1325 | mGlobalLinesRenderer->CastGlobalLines(ray, vssRays);
|
---|
1326 | continue;
|
---|
1327 | }
|
---|
1328 | #endif
|
---|
1329 | mRayCaster->CastRay(
|
---|
1330 | ray,
|
---|
1331 | vssRays,
|
---|
1332 | mViewCellsManager->GetViewSpaceBox(),
|
---|
1333 | castDoubleRays,
|
---|
1334 | pruneInvalidRays);
|
---|
1335 |
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 |
|
---|
1341 | bool Preprocessor::GenerateRayBundle(SimpleRayContainer &rayBundle,
|
---|
1342 | const SimpleRay &mainRay,
|
---|
1343 | const int number,
|
---|
1344 | const int pertubType) const
|
---|
1345 | {
|
---|
1346 | rayBundle.push_back(mainRay);
|
---|
1347 |
|
---|
1348 | const float pertubOrigin = 0.0f;
|
---|
1349 | const float pertubDir = 0.2f;
|
---|
1350 |
|
---|
1351 | for (int i = 0; i < number - 1; ++ i)
|
---|
1352 | {
|
---|
1353 | Vector3 pertub;
|
---|
1354 |
|
---|
1355 | pertub.x = RandomValue(0.0f, pertubDir);
|
---|
1356 | pertub.y = RandomValue(0.0f, pertubDir);
|
---|
1357 | pertub.z = RandomValue(0.0f, pertubDir);
|
---|
1358 |
|
---|
1359 | const Vector3 newDir = mainRay.mDirection + pertub;
|
---|
1360 | //const Vector3 newDir = mainRay.mDirection;
|
---|
1361 |
|
---|
1362 | pertub.x = RandomValue(0.0f, pertubOrigin);
|
---|
1363 | pertub.y = RandomValue(0.0f, pertubOrigin);
|
---|
1364 | pertub.z = RandomValue(0.0f, pertubOrigin);
|
---|
1365 |
|
---|
1366 | const Vector3 newOrigin = mainRay.mOrigin + pertub;
|
---|
1367 | //const Vector3 newOrigin = mainRay.mOrigin;
|
---|
1368 |
|
---|
1369 | rayBundle.push_back(SimpleRay(newOrigin, newDir, 0, 1.0f));
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | return true;
|
---|
1373 | }
|
---|
1374 |
|
---|
1375 |
|
---|
1376 | void Preprocessor::SetupRay(Ray &ray,
|
---|
1377 | const Vector3 &point,
|
---|
1378 | const Vector3 &direction) const
|
---|
1379 | {
|
---|
1380 | ray.Clear();
|
---|
1381 | // do not store anything else then intersections at the ray
|
---|
1382 | ray.Init(point, direction, Ray::LOCAL_RAY);
|
---|
1383 | }
|
---|
1384 |
|
---|
1385 |
|
---|
1386 | void Preprocessor::EvalViewCellHistogram()
|
---|
1387 | {
|
---|
1388 | char filename[256];
|
---|
1389 | Environment::GetSingleton()->GetStringValue("Preprocessor.histogram.file", filename);
|
---|
1390 |
|
---|
1391 | // mViewCellsManager->EvalViewCellHistogram(filename, 1000000);
|
---|
1392 | mViewCellsManager->EvalViewCellHistogramForPvsSize(filename, 1000000);
|
---|
1393 | }
|
---|
1394 |
|
---|
1395 |
|
---|
1396 | bool
|
---|
1397 | Preprocessor::ExportRays(const char *filename,
|
---|
1398 | const VssRayContainer &vssRays,
|
---|
1399 | const int number,
|
---|
1400 | const bool exportScene
|
---|
1401 | )
|
---|
1402 | {
|
---|
1403 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
1404 |
|
---|
1405 | Exporter *exporter = NULL;
|
---|
1406 | exporter = Exporter::GetExporter(filename);
|
---|
1407 |
|
---|
1408 | if (0) {
|
---|
1409 | exporter->SetWireframe();
|
---|
1410 | exporter->ExportKdTree(*mKdTree);
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | exporter->SetFilled();
|
---|
1414 | // $$JB temporarily do not export the scene
|
---|
1415 | if (exportScene)
|
---|
1416 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
1417 |
|
---|
1418 | exporter->SetWireframe();
|
---|
1419 |
|
---|
1420 | if (1) {
|
---|
1421 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
1422 | exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
|
---|
1423 | exporter->ResetForcedMaterial();
|
---|
1424 | }
|
---|
1425 |
|
---|
1426 | VssRayContainer rays;
|
---|
1427 | vssRays.SelectRays(number, rays);
|
---|
1428 | exporter->ExportRays(rays, RgbColor(1, 0, 0));
|
---|
1429 | delete exporter;
|
---|
1430 | cout<<"done."<<endl<<flush;
|
---|
1431 |
|
---|
1432 | return true;
|
---|
1433 | }
|
---|
1434 |
|
---|
1435 | bool
|
---|
1436 | Preprocessor::ExportRayAnimation(const char *filename,
|
---|
1437 | const vector<VssRayContainer> &vssRays
|
---|
1438 | )
|
---|
1439 | {
|
---|
1440 | cout<<"Exporting vss rays..."<<endl<<flush;
|
---|
1441 |
|
---|
1442 | Exporter *exporter = NULL;
|
---|
1443 | exporter = Exporter::GetExporter(filename);
|
---|
1444 | if (0) {
|
---|
1445 | exporter->SetWireframe();
|
---|
1446 | exporter->ExportKdTree(*mKdTree);
|
---|
1447 | }
|
---|
1448 | exporter->SetFilled();
|
---|
1449 | // $$JB temporarily do not export the scene
|
---|
1450 | if (0)
|
---|
1451 | exporter->ExportScene(mSceneGraph->GetRoot());
|
---|
1452 | exporter->SetWireframe();
|
---|
1453 |
|
---|
1454 | if (1) {
|
---|
1455 | exporter->SetForcedMaterial(RgbColor(1,0,1));
|
---|
1456 | exporter->ExportBox(mViewCellsManager->GetViewSpaceBox());
|
---|
1457 | exporter->ResetForcedMaterial();
|
---|
1458 | }
|
---|
1459 |
|
---|
1460 | exporter->ExportRaySets(vssRays, RgbColor(1, 0, 0));
|
---|
1461 |
|
---|
1462 | delete exporter;
|
---|
1463 |
|
---|
1464 | cout<<"done."<<endl<<flush;
|
---|
1465 |
|
---|
1466 | return true;
|
---|
1467 | }
|
---|
1468 |
|
---|
1469 | void
|
---|
1470 | Preprocessor::ComputeRenderError()
|
---|
1471 | {
|
---|
1472 | // compute rendering error
|
---|
1473 |
|
---|
1474 | if (renderer && renderer->mPvsStatFrames) {
|
---|
1475 |
|
---|
1476 | if (!mViewCellsManager->GetViewCellPointsList()->empty())
|
---|
1477 | {
|
---|
1478 |
|
---|
1479 | ViewCellPointsList *vcPoints = mViewCellsManager->GetViewCellPointsList();
|
---|
1480 |
|
---|
1481 | ViewCellPointsList::const_iterator
|
---|
1482 | vit = vcPoints->begin(),
|
---|
1483 | vit_end = vcPoints->end();
|
---|
1484 |
|
---|
1485 | SimpleRayContainer viewPoints;
|
---|
1486 |
|
---|
1487 | for (; vit != vit_end; ++ vit) {
|
---|
1488 | ViewCellPoints *vp = *vit;
|
---|
1489 |
|
---|
1490 | SimpleRayContainer::const_iterator rit = vp->second.begin(), rit_end = vp->second.end();
|
---|
1491 | for (; rit!=rit_end; ++rit)
|
---|
1492 | viewPoints.push_back(*rit);
|
---|
1493 | }
|
---|
1494 |
|
---|
1495 | if (viewPoints.size() != renderer->mPvsErrorBuffer.size()) {
|
---|
1496 | renderer->mPvsErrorBuffer.resize(viewPoints.size());
|
---|
1497 | renderer->ClearErrorBuffer();
|
---|
1498 | }
|
---|
1499 |
|
---|
1500 | cout << "evaluating list of " << viewPoints.size() << " pts" << endl;
|
---|
1501 | renderer->EvalPvsStat(viewPoints);
|
---|
1502 | } else
|
---|
1503 | {
|
---|
1504 | cout << "evaluating random points" << endl;
|
---|
1505 | renderer->EvalPvsStat();
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | mStats <<
|
---|
1509 | "#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError()<<endl<<
|
---|
1510 | "#AvgPixelError\n" <<renderer->GetAvgPixelError()<<endl<<
|
---|
1511 | "#MaxPixelError\n" <<renderer->GetMaxPixelError()<<endl<<
|
---|
1512 | "#MaxPvsRenderError\n" <<renderer->mPvsStat.GetMaxError()<<endl<<
|
---|
1513 | "#ErrorFreeFrames\n" <<renderer->mPvsStat.GetErrorFreeFrames()<<endl<<
|
---|
1514 | "#AvgRenderPvs\n" <<renderer->mPvsStat.GetAvgPvs()<<endl;
|
---|
1515 | }
|
---|
1516 | }
|
---|
1517 |
|
---|
1518 |
|
---|
1519 | Intersectable *Preprocessor::GetObjectById(const int id)
|
---|
1520 | {
|
---|
1521 | #if 1
|
---|
1522 | // create a dummy mesh instance to be able to use stl
|
---|
1523 | MeshInstance object(NULL);
|
---|
1524 | object.SetId(id);
|
---|
1525 |
|
---|
1526 | ObjectContainer::const_iterator oit =
|
---|
1527 | lower_bound(mObjects.begin(), mObjects.end(), &object, ilt);
|
---|
1528 |
|
---|
1529 | // objects sorted by id
|
---|
1530 | if ((oit != mObjects.end()) && ((*oit)->GetId() == object.GetId()))
|
---|
1531 | {
|
---|
1532 | return (*oit);
|
---|
1533 | }
|
---|
1534 | else
|
---|
1535 | {
|
---|
1536 | return NULL;
|
---|
1537 | }
|
---|
1538 | #else
|
---|
1539 | return mObjects[id];
|
---|
1540 | #endif
|
---|
1541 | }
|
---|
1542 |
|
---|
1543 |
|
---|
1544 | void Preprocessor::PrepareHwGlobalLines()
|
---|
1545 | {
|
---|
1546 | int texHeight, texWidth;
|
---|
1547 | float eps;
|
---|
1548 | int maxDepth;
|
---|
1549 | bool sampleReverse;
|
---|
1550 |
|
---|
1551 | Environment::GetSingleton()->GetIntValue("Preprocessor.HwGlobalLines.texHeight", texHeight);
|
---|
1552 | Environment::GetSingleton()->GetIntValue("Preprocessor.HwGlobalLines.texWidth", texWidth);
|
---|
1553 | Environment::GetSingleton()->GetFloatValue("Preprocessor.HwGlobalLines.stepSize", eps);
|
---|
1554 | Environment::GetSingleton()->GetIntValue("Preprocessor.HwGlobalLines.maxDepth", maxDepth);
|
---|
1555 | Environment::GetSingleton()->GetBoolValue("Preprocessor.HwGlobalLines.sampleReverse", sampleReverse);
|
---|
1556 |
|
---|
1557 | Debug << "****** hw global line options *******" << endl;
|
---|
1558 | Debug << "texWidth: " << texWidth << endl;
|
---|
1559 | Debug << "texHeight: " << texHeight << endl;
|
---|
1560 | Debug << "sampleReverse: " << sampleReverse << endl;
|
---|
1561 | Debug << "max depth: " << maxDepth << endl;
|
---|
1562 | Debug << "step size: " << eps << endl;
|
---|
1563 | Debug << endl;
|
---|
1564 |
|
---|
1565 | #ifdef USE_CG
|
---|
1566 | globalLinesRenderer = mGlobalLinesRenderer =
|
---|
1567 | new GlobalLinesRenderer(this,
|
---|
1568 | texHeight,
|
---|
1569 | texWidth,
|
---|
1570 | eps,
|
---|
1571 | maxDepth,
|
---|
1572 | sampleReverse);
|
---|
1573 |
|
---|
1574 | mGlobalLinesRenderer->InitGl();
|
---|
1575 |
|
---|
1576 | #endif
|
---|
1577 | }
|
---|
1578 |
|
---|
1579 |
|
---|
1580 | void Preprocessor::DeterminePvsObjects(VssRayContainer &rays)
|
---|
1581 | {
|
---|
1582 | mViewCellsManager->DeterminePvsObjects(rays, false);
|
---|
1583 | }
|
---|
1584 |
|
---|
1585 |
|
---|
1586 | bool Preprocessor::LoadObjects(const string &filename,
|
---|
1587 | ObjectContainer &pvsObjects,
|
---|
1588 | const ObjectContainer &preprocessorObjects)
|
---|
1589 | {
|
---|
1590 | ObjectsParser parser;
|
---|
1591 |
|
---|
1592 | const bool success = parser.ParseObjects(filename,
|
---|
1593 | pvsObjects,
|
---|
1594 | preprocessorObjects);
|
---|
1595 |
|
---|
1596 | if (!success)
|
---|
1597 | {
|
---|
1598 | Debug << "Error: loading objects failed!" << endl;
|
---|
1599 | }
|
---|
1600 |
|
---|
1601 | // hack: no bvh object could be found => take preprocessor objects
|
---|
1602 | if (pvsObjects.empty())
|
---|
1603 | {
|
---|
1604 | Debug << "no objects" << endl;
|
---|
1605 | pvsObjects = preprocessorObjects;
|
---|
1606 | }
|
---|
1607 |
|
---|
1608 | return success;
|
---|
1609 | }
|
---|
1610 |
|
---|
1611 |
|
---|
1612 | SceneGraphLeaf *Preprocessor::RegisterDynamicGeometryWithRayCaster(SceneGraphLeaf *leaf)
|
---|
1613 | {
|
---|
1614 | if (mRayCaster)
|
---|
1615 | mRayCaster->AddDynamicObjecs(leaf->mGeometry, leaf->GetTransformation());
|
---|
1616 | // $$ JB in order to compile
|
---|
1617 | return leaf;
|
---|
1618 | }
|
---|
1619 |
|
---|
1620 |
|
---|
1621 | SceneGraphLeaf *Preprocessor::LoadDynamicGeometry(const string &filename)
|
---|
1622 | {
|
---|
1623 | #if DYNAMIC_OBJECTS_HACK
|
---|
1624 |
|
---|
1625 | const bool dynamic = true;
|
---|
1626 | SceneGraphLeaf *leaf = new SceneGraphLeaf(dynamic);
|
---|
1627 |
|
---|
1628 | bool parsed = false;
|
---|
1629 |
|
---|
1630 | if (strstr(filename.c_str(), ".obj"))
|
---|
1631 | {
|
---|
1632 | cout<<"parsing obj file.."<<endl;
|
---|
1633 | ObjParser *p = new ObjParser;
|
---|
1634 | parsed = p->ParseFile(filename, leaf, false);
|
---|
1635 | }
|
---|
1636 | else
|
---|
1637 | {
|
---|
1638 | cout<<"parsing binary obj file ... " << endl;
|
---|
1639 | parsed = LoadBinaryObj(filename, leaf, NULL, 100);
|
---|
1640 | }
|
---|
1641 |
|
---|
1642 | if (parsed)
|
---|
1643 | {
|
---|
1644 | const float scale = 0.01f;
|
---|
1645 |
|
---|
1646 | ObjectContainer::const_iterator it, it_end = leaf->mGeometry.end();
|
---|
1647 |
|
---|
1648 | for (it = leaf->mGeometry.begin(); it != it_end; ++ it)
|
---|
1649 | {
|
---|
1650 | TriangleIntersectable *tri = static_cast<TriangleIntersectable *>(*it);
|
---|
1651 |
|
---|
1652 | Triangle3 t = tri->GetItem();
|
---|
1653 |
|
---|
1654 | // scale object appropriately
|
---|
1655 | t.mVertices[0] *= scale;
|
---|
1656 | t.mVertices[1] *= scale;
|
---|
1657 | t.mVertices[2] *= scale;
|
---|
1658 |
|
---|
1659 | tri->SetItem(t);
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | //leaf->ApplyTransform(ScaleMatrix(s, s, s));
|
---|
1663 | //leaf->ApplyTransform(TranslationMatrix(Vector3(0, 10, 0)));
|
---|
1664 |
|
---|
1665 | leaf->UpdateBox();
|
---|
1666 | mSceneGraph->GetRoot()->mChildren.push_back(leaf);
|
---|
1667 | //mDynamicObjects.push_back(leaf);
|
---|
1668 |
|
---|
1669 | cout<<"Dynamic object loaded successfully: " << leaf->GetBox() << endl;
|
---|
1670 |
|
---|
1671 | return leaf;
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 |
|
---|
1675 | #endif
|
---|
1676 | cout<<"Dynamic object loading failed."<<endl;
|
---|
1677 | return NULL;
|
---|
1678 | }
|
---|
1679 |
|
---|
1680 |
|
---|
1681 | void Preprocessor::ObjectMoved(SceneGraphLeaf *object)
|
---|
1682 | {
|
---|
1683 | // first invalidate all PVS from which this object is visible
|
---|
1684 | ViewCellContainer::const_iterator vit, vit_end = mViewCellsManager->GetViewCells().end();
|
---|
1685 |
|
---|
1686 | AxisAlignedBox3 box = object->GetBox();
|
---|
1687 |
|
---|
1688 | ObjectContainer objects;
|
---|
1689 |
|
---|
1690 | if (0)
|
---|
1691 | {
|
---|
1692 | // simplified computation taking only the objects intersecting the box
|
---|
1693 | KdNode::NewMail();
|
---|
1694 |
|
---|
1695 | // first mail all kDObjects which are intersected by the object
|
---|
1696 | preprocessor->mKdTree->CollectKdObjects(box, objects);
|
---|
1697 |
|
---|
1698 | }
|
---|
1699 | else
|
---|
1700 | {
|
---|
1701 | ViewCellContainer viewCells;
|
---|
1702 | mViewCellsManager->ComputeBoxIntersections(box, viewCells);
|
---|
1703 |
|
---|
1704 | // add object to all viewcells that intersect the bounding box
|
---|
1705 | ObjectPvs pvs;
|
---|
1706 |
|
---|
1707 | ViewCellContainer::const_iterator it = viewCells.begin(), it_end = viewCells.end();
|
---|
1708 |
|
---|
1709 | for (int i = 0; it != it_end; ++ it, ++ i)
|
---|
1710 | {
|
---|
1711 | //cout<<"v"<<i<<" pvs="<<(*it)->GetPvs().mEntries.size()<<endl;
|
---|
1712 | pvs.MergeInPlace((*it)->GetPvs());
|
---|
1713 | }
|
---|
1714 |
|
---|
1715 | ObjectPvsIterator pit = pvs.GetIterator();
|
---|
1716 | while (pit.HasMoreEntries())
|
---|
1717 | {
|
---|
1718 | PvsData pvsData;
|
---|
1719 | Intersectable *object = pit.Next(pvsData);
|
---|
1720 | objects.push_back(object);
|
---|
1721 | }
|
---|
1722 | }
|
---|
1723 |
|
---|
1724 | int pvsCounter = 0;
|
---|
1725 |
|
---|
1726 | // now search for pvss which contained any mailed node
|
---|
1727 | for (vit = mViewCellsManager->GetViewCells().begin(); vit != vit_end; ++ vit)
|
---|
1728 | {
|
---|
1729 | ObjectPvs &pvs = (*vit)->GetPvs();
|
---|
1730 |
|
---|
1731 | for (int i=0; i < objects.size(); i++)
|
---|
1732 | {
|
---|
1733 | vector<PvsEntry<Intersectable *,PvsData> >::iterator v;
|
---|
1734 |
|
---|
1735 | if (pvs.Find(objects[i], v)) {
|
---|
1736 | // clear the pvs
|
---|
1737 | pvs.Clear();
|
---|
1738 | pvsCounter++;
|
---|
1739 | break;
|
---|
1740 | }
|
---|
1741 | }
|
---|
1742 | }
|
---|
1743 |
|
---|
1744 | cout<<"Number of affected objects "<<objects.size()<<endl;
|
---|
1745 | cout<<"Cleared "<<pvsCounter<<" PVSs ("<<mViewCellsManager->GetViewCells().size()/
|
---|
1746 | (float)pvsCounter*100.0f<<"%) "<<endl;
|
---|
1747 |
|
---|
1748 | }
|
---|
1749 |
|
---|
1750 |
|
---|
1751 | void Preprocessor::UpdateDynamicObjects()
|
---|
1752 | {
|
---|
1753 | if (mUpdateDynamicObjects)
|
---|
1754 | {
|
---|
1755 | // delete ALL dynamic stuff and rebuild using the new trafos
|
---|
1756 | preprocessor->mRayCaster->DeleteDynamicObjects();
|
---|
1757 |
|
---|
1758 | for (size_t i=0; i < mDynamicObjects.size(); ++ i)
|
---|
1759 | {
|
---|
1760 | SceneGraphLeaf *l = mDynamicObjects[i];
|
---|
1761 |
|
---|
1762 | cout<<"Updating dynamic objects in ray caster..."<<endl;
|
---|
1763 |
|
---|
1764 | mRayCaster->AddDynamicObjecs(l->mGeometry, l->GetTransformation());
|
---|
1765 |
|
---|
1766 | cout<<"done."<<endl;
|
---|
1767 |
|
---|
1768 | cout<<"Updating affected PVSs..."<<endl;
|
---|
1769 | preprocessor->ObjectMoved(l);
|
---|
1770 | cout<<"done."<<endl;
|
---|
1771 | }
|
---|
1772 |
|
---|
1773 | mUpdateDynamicObjects = false;
|
---|
1774 |
|
---|
1775 | }
|
---|
1776 | }
|
---|
1777 |
|
---|
1778 |
|
---|
1779 | void Preprocessor::ScheduleUpdateDynamicObjects()
|
---|
1780 | {
|
---|
1781 | mUpdateDynamicObjects = true;
|
---|
1782 | }
|
---|
1783 |
|
---|
1784 |
|
---|
1785 | }
|
---|