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