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