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