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