- Timestamp:
- 11/25/05 14:45:50 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
r433 r434 11 11 12 12 13 bool useViewSpaceBox = false;//true;13 bool useViewSpaceBox = true;//true; 14 14 bool use2dSampling = false; 15 bool useViewspacePlane = false;//true;15 bool useViewspacePlane = true;//true; 16 16 17 17 VssPreprocessor::VssPreprocessor(): 18 19 18 mPass(0), 19 mVssRays() 20 20 { 21 21 // this should increase coherence of the samples … … 32 32 VssPreprocessor::~VssPreprocessor() 33 33 { 34 34 CLEAR_CONTAINER(mVssRays); 35 35 } 36 36 37 37 void 38 38 VssPreprocessor::SetupRay(Ray &ray, 39 40 41 39 const Vector3 &point, 40 const Vector3 &direction 41 ) 42 42 { 43 43 ray.intersections.clear(); 44 44 // do not store anything else then intersections at the ray 45 45 ray.Init(point, direction, Ray::LOCAL_RAY); 46 46 } … … 48 48 int 49 49 VssPreprocessor::CastRay( 50 51 52 53 54 { 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 50 Vector3 &viewPoint, 51 Vector3 &direction, 52 VssRayContainer &vssRays 53 ) 54 { 55 int hits = 0; 56 static Ray ray; 57 AxisAlignedBox3 box = mKdTree->GetBox(); 58 59 AxisAlignedBox3 sbox = box; 60 sbox.Enlarge(Vector3(-Limits::Small)); 61 if (!sbox.IsInside(viewPoint)) 62 return 0; 63 64 SetupRay(ray, viewPoint, direction); 65 // cast ray to KD tree to find intersection with other objects 66 Intersectable *objectA, *objectB; 67 Vector3 pointA, pointB; 68 float bsize = Magnitude(box.Size()); 69 if (mKdTree->CastRay(ray)) { 70 objectA = ray.intersections[0].mObject; 71 pointA = ray.Extrap(ray.intersections[0].mT); 72 } else { 73 objectA = NULL; 74 // compute intersection with the scene bounding box 75 float tmin, tmax; 76 box.ComputeMinMaxT(ray, &tmin, &tmax); 77 if (tmax > bsize) { 78 // cerr<<"Warning: tmax > box size tmax="<<tmax<<" tmin="<<tmin<<" size="<<bsize<<endl; 79 // cerr<<"ray"<<ray<<endl; 80 } 81 pointA = ray.Extrap(tmax); 82 83 } 84 85 bool detectEmptyViewSpace = true; 86 87 if (detectEmptyViewSpace) { 88 SetupRay(ray, pointA, -direction); 89 } else 90 SetupRay(ray, viewPoint, -direction); 91 92 93 if (mKdTree->CastRay(ray)) { 94 95 objectB = ray.intersections[0].mObject; 96 pointB = ray.Extrap(ray.intersections[0].mT); 97 98 } else { 99 objectB = NULL; 100 float tmin, tmax; 101 box.ComputeMinMaxT(ray, &tmin, &tmax); 102 if (tmax > bsize) { 103 // cerr<<"Warning: tmax > box size tmax="<<tmax<<" tmin="<<tmin<<" size="<<bsize<<endl; 104 // cerr<<"ray"<<ray<<endl; 105 } 106 107 pointB = ray.Extrap(tmax); 108 } 109 110 VssRay *vssRay = NULL; 111 112 bool validSample = true; 113 if (detectEmptyViewSpace) { 114 if (Distance(pointA, pointB) < 115 Distance(viewPoint, pointA) + Distance(viewPoint, pointB) - Limits::Small) { 116 validSample = false; 117 } 118 } 119 120 if (validSample) { 121 if (objectA) { 122 vssRay = new VssRay(pointB, 123 pointA, 124 objectB, 125 objectA); 126 vssRays.push_back(vssRay); 127 hits ++; 128 } 129 130 if (objectB) { 131 vssRay = new VssRay(pointA, 132 pointB, 133 objectA, 134 objectB); 135 vssRays.push_back(vssRay); 136 hits ++; 137 } 138 } 139 140 return hits; 141 141 } 142 142 … … 145 145 VssPreprocessor::GetViewpoint(AxisAlignedBox3 *viewSpaceBox) 146 146 { 147 148 149 150 151 152 153 154 155 147 AxisAlignedBox3 box; 148 149 if (viewSpaceBox) 150 box =*viewSpaceBox; 151 else 152 box = mKdTree->GetBox(); 153 154 // shrink the box in the y direction 155 return box.GetRandomPoint(); 156 156 } 157 157 158 158 Vector3 159 159 VssPreprocessor::GetDirection(const Vector3 &viewpoint, 160 161 162 { 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 160 AxisAlignedBox3 *viewSpaceBox 161 ) 162 { 163 Vector3 point; 164 if (!use2dSampling) { 165 Vector3 normal; 166 int i = RandomValue(0, mObjects.size()-1); 167 Intersectable *object = mObjects[i]; 168 object->GetRandomSurfacePoint(point, normal); 169 } else { 170 AxisAlignedBox3 box; 171 172 if (viewSpaceBox) 173 box =*viewSpaceBox; 174 else 175 box = mKdTree->GetBox(); 176 177 point = box.GetRandomPoint(); 178 point.y = viewpoint.y; 179 } 180 181 return point - viewpoint; 182 182 } 183 183 184 184 int 185 185 VssPreprocessor::GenerateImportanceRays(VssTree *vssTree, 186 187 188 189 { 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 186 const int desiredSamples, 187 SimpleRayContainer &rays 188 ) 189 { 190 int num; 191 if (0) { 192 float minRayContribution; 193 float maxRayContribution; 194 float avgRayContribution; 195 196 vssTree->GetRayContributionStatistics(minRayContribution, 197 maxRayContribution, 198 avgRayContribution); 199 200 cout<< 201 "#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<< 202 "#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<< 203 "#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl; 204 205 float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves()); 206 num = vssTree->GenerateRays(p, rays); 207 } else { 208 int leaves = vssTree->stat.Leaves()/2; 209 num = vssTree->GenerateRays(desiredSamples, leaves, rays); 210 } 211 212 cout<<"Generated "<<num<<" rays."<<endl; 213 214 return num; 215 215 } 216 216 … … 218 218 bool 219 219 VssPreprocessor::ExportRays(const char *filename, 220 const VssRayContainer &vssRays, 221 const int number 222 ) 223 { 224 cout<<"Exporting vss rays..."<<endl<<flush; 225 226 float prob = number/(float)vssRays.size(); 227 228 229 Exporter *exporter = NULL; 230 exporter = Exporter::GetExporter(filename); 231 // exporter->SetWireframe(); 232 // exporter->ExportKdTree(*mKdTree); 233 exporter->SetFilled(); 234 exporter->ExportScene(mSceneGraph->mRoot); 235 exporter->SetWireframe(); 236 237 if (mViewSpaceBox) { 238 exporter->SetForcedMaterial(RgbColor(1,0,0)); 239 exporter->ExportBox(*mViewSpaceBox); 240 exporter->ResetForcedMaterial(); 241 } 242 243 VssRayContainer rays; for (int i=0; i < vssRays.size(); i++) 244 if (RandomValue(0,1) < prob) 245 rays.push_back(vssRays[i]); 246 247 exporter->ExportRays(rays, RgbColor(1, 0, 0)); 248 249 delete exporter; 250 251 cout<<"done."<<endl<<flush; 252 253 return true; 254 } 255 220 const VssRayContainer &vssRays, 221 const int number 222 ) 223 { 224 cout<<"Exporting vss rays..."<<endl<<flush; 225 226 float prob = number/(float)vssRays.size(); 227 228 229 Exporter *exporter = NULL; 230 exporter = Exporter::GetExporter(filename); 231 // exporter->SetWireframe(); 232 // exporter->ExportKdTree(*mKdTree); 233 exporter->SetFilled(); 234 exporter->ExportScene(mSceneGraph->mRoot); 235 exporter->SetWireframe(); 236 237 if (mViewSpaceBox) { 238 exporter->SetForcedMaterial(RgbColor(1,0,0)); 239 exporter->ExportBox(*mViewSpaceBox); 240 exporter->ResetForcedMaterial(); 241 } 242 243 VssRayContainer rays; for (int i=0; i < vssRays.size(); i++) 244 if (RandomValue(0,1) < prob) 245 rays.push_back(vssRays[i]); 246 247 exporter->ExportRays(rays, RgbColor(1, 0, 0)); 248 249 delete exporter; 250 251 cout<<"done."<<endl<<flush; 252 253 return true; 254 } 255 256 257 bool 258 VssPreprocessor::ExportVssTree(char *filename, 259 VssTree *tree) 260 { 261 Exporter *exporter = Exporter::GetExporter(filename); 262 exporter->SetFilled(); 263 exporter->ExportScene(mSceneGraph->mRoot); 264 exporter->SetWireframe(); 265 bool result = exporter->ExportVssTree(*tree); 266 delete exporter; 267 return result; 268 } 256 269 257 270 bool 258 271 VssPreprocessor::ExportVssTreeLeaf(char *filename, 259 VssTree *tree, 260 VssTreeLeaf *leaf) 261 { 262 Exporter *exporter = NULL; 263 exporter = Exporter::GetExporter(filename); 264 exporter->SetWireframe(); 265 exporter->ExportKdTree(*mKdTree); 266 267 if (mViewSpaceBox) { 268 exporter->SetForcedMaterial(RgbColor(1,0,0)); 269 exporter->ExportBox(*mViewSpaceBox); 270 exporter->ResetForcedMaterial(); 271 } 272 273 exporter->SetForcedMaterial(RgbColor(0,0,1)); 274 exporter->ExportBox(tree->GetBBox(leaf)); 272 VssTree *tree, 273 VssTreeLeaf *leaf) 274 { 275 Exporter *exporter = NULL; 276 exporter = Exporter::GetExporter(filename); 277 exporter->SetWireframe(); 278 exporter->ExportKdTree(*mKdTree); 279 280 if (mViewSpaceBox) { 281 exporter->SetForcedMaterial(RgbColor(1,0,0)); 282 exporter->ExportBox(*mViewSpaceBox); 275 283 exporter->ResetForcedMaterial(); 276 277 VssRayContainer rays[4]; 278 for (int i=0; i < leaf->rays.size(); i++) { 279 int k = leaf->rays[i].GetRayClass(); 280 rays[k].push_back(leaf->rays[i].mRay); 281 } 282 283 // SOURCE RAY 284 exporter->ExportRays(rays[0], RgbColor(1, 0, 0)); 285 // TERMINATION RAY 286 exporter->ExportRays(rays[1], RgbColor(1, 1, 1)); 287 // PASSING_RAY 288 exporter->ExportRays(rays[2], RgbColor(1, 1, 0)); 289 // CONTAINED_RAY 290 exporter->ExportRays(rays[3], RgbColor(0, 0, 1)); 291 292 delete exporter; 293 return true; 284 } 285 286 exporter->SetForcedMaterial(RgbColor(0,0,1)); 287 exporter->ExportBox(tree->GetBBox(leaf)); 288 exporter->ResetForcedMaterial(); 289 290 VssRayContainer rays[4]; 291 for (int i=0; i < leaf->rays.size(); i++) { 292 int k = leaf->rays[i].GetRayClass(); 293 rays[k].push_back(leaf->rays[i].mRay); 294 } 295 296 // SOURCE RAY 297 exporter->ExportRays(rays[0], RgbColor(1, 0, 0)); 298 // TERMINATION RAY 299 exporter->ExportRays(rays[1], RgbColor(1, 1, 1)); 300 // PASSING_RAY 301 exporter->ExportRays(rays[2], RgbColor(1, 1, 0)); 302 // CONTAINED_RAY 303 exporter->ExportRays(rays[3], RgbColor(0, 0, 1)); 304 305 delete exporter; 306 return true; 294 307 } 295 308 … … 297 310 VssPreprocessor::ExportVssTreeLeaves(VssTree *tree, const int number) 298 311 { 299 vector<VssTreeLeaf *> leaves; 300 tree->CollectLeaves(leaves); 301 302 int num = 0; 303 int i; 304 float p = number / (float)leaves.size(); 305 for (i=0; i < leaves.size(); i++) { 306 if (RandomValue(0,1) < p) { 307 char filename[64]; 308 sprintf(filename, "vss-leaf-%04d.x3d", num); 309 ExportVssTreeLeaf(filename, tree, leaves[i]); 310 num++; 311 } 312 if (num >= number) 313 break; 314 } 312 vector<VssTreeLeaf *> leaves; 313 tree->CollectLeaves(leaves); 314 315 int num = 0; 316 int i; 317 float p = number / (float)leaves.size(); 318 for (i=0; i < leaves.size(); i++) { 319 if (RandomValue(0,1) < p) { 320 char filename[64]; 321 sprintf(filename, "vss-leaf-%04d.x3d", num); 322 ExportVssTreeLeaf(filename, tree, leaves[i]); 323 num++; 324 } 325 if (num >= number) 326 break; 327 } 328 } 329 330 331 float 332 VssPreprocessor::GetAvgPvsSize(VssTree *tree, 333 const vector<AxisAlignedBox3> &viewcells 334 ) 335 { 336 vector<AxisAlignedBox3>::const_iterator it, it_end = viewcells.end(); 337 338 int sum = 0; 339 for (it = viewcells.begin(); it != it_end; ++ it) 340 sum += tree->GetPvsSize(*it); 341 342 return sum/(float)viewcells.size(); 315 343 } 316 344 … … 326 354 327 355 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 356 AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox()); 357 358 if (!useViewspacePlane) { 359 float size = 0.01f; 360 float s = 0.5f - size; 361 float olds = Magnitude(box->Size()); 362 box->Enlarge(box->Size()*Vector3(-s)); 363 Vector3 translation = Vector3(-olds*0.1f, 0, 0); 364 box->SetMin(box->Min() + translation); 365 box->SetMax(box->Max() + translation); 366 } else { 367 368 // sample city like heights 369 box->SetMin(1, box->Min(1) + box->Size(1)*0.1); 370 box->SetMax(1, box->Min(1) + box->Size(1)*0.2); 371 } 372 373 if (use2dSampling) 374 box->SetMax(1, box->Min(1)); 375 376 if (useViewSpaceBox) 377 mViewSpaceBox = box; 378 else 379 mViewSpaceBox = NULL; 380 381 382 VssTree *vssTree = NULL; 383 384 RayContainer bspRays; 357 385 358 386 while (totalSamples < mInitialSamples) { 359 360 361 362 363 364 365 366 367 387 int passContributingSamples = 0; 388 int passSampleContributions = 0; 389 int passSamples = 0; 390 int index = 0; 391 392 int sampleContributions; 393 394 int s = Min(mSamplesPerPass, mInitialSamples); 395 for (int k=0; k < s; k++) { 368 396 369 370 397 Vector3 viewpoint = GetViewpoint(mViewSpaceBox); 398 Vector3 direction = GetDirection(viewpoint, mViewSpaceBox); 371 399 372 400 sampleContributions = CastRay(viewpoint, direction, mVssRays); 373 401 374 402 375 376 377 378 379 380 381 382 403 //-- CORR matt: put block inside loop 404 if (sampleContributions) { 405 passContributingSamples ++; 406 passSampleContributions += sampleContributions; 407 } 408 passSamples++; 409 totalSamples++; 410 } 383 411 384 mPass++; 385 386 int pvsSize = 0; 387 float avgRayContrib = (passContributingSamples > 0) ? 388 passSampleContributions/(float)passContributingSamples : 0; 389 390 cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 391 cout << "#TotalSamples=" << totalSamples/1000 392 << "k #SampleContributions=" << passSampleContributions << " (" 393 << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS=" 394 << pvsSize/(float)mObjects.size() << endl 395 << "avg ray contrib=" << avgRayContrib << endl; 396 397 mStats << 398 "#Pass\n" <<mPass<<endl<< 399 "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<< 400 "#TotalSamples\n" << totalSamples<< endl<< 401 "#SampleContributions\n" << passSampleContributions << endl << 402 "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl << 403 "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl << 404 "#AvgRayContrib\n" << avgRayContrib << endl; 405 406 407 408 409 } 410 411 cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl; 412 cout << "#totalRayStackSize=" << mVssRays.size() << endl <<flush; 413 414 415 int numExportRays = 10000; 416 //int numExportRays = 0; 412 mPass++; 413 414 int pvsSize = 0; 415 float avgRayContrib = (passContributingSamples > 0) ? 416 passSampleContributions/(float)passContributingSamples : 0; 417 418 cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; 419 cout << "#TotalSamples=" << totalSamples/1000 420 << "k #SampleContributions=" << passSampleContributions << " (" 421 << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS=" 422 << pvsSize/(float)mObjects.size() << endl 423 << "avg ray contrib=" << avgRayContrib << endl; 424 425 mStats << 426 "#Pass\n" <<mPass<<endl<< 427 "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl<< 428 "#TotalSamples\n" << totalSamples<< endl<< 429 "#SampleContributions\n" << passSampleContributions << endl << 430 "#PContributingSamples\n"<<100*passContributingSamples/(float)passSamples<<endl << 431 "#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl << 432 "#AvgRayContrib\n" << avgRayContrib << endl; 433 434 435 436 437 } 438 439 cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl; 440 cout << "#totalRayStackSize=" << mVssRays.size() << endl <<flush; 441 442 int numExportRays = 10000; 443 //int numExportRays = 0; 444 445 if (numExportRays) { 446 char filename[64]; 447 sprintf(filename, "vss-rays-initial.x3d"); 448 ExportRays(filename, mVssRays, numExportRays); 449 } 450 451 //-- construct BSP view cells 452 if (ViewCell::sHierarchy == ViewCell::BSP) 453 { 454 const int bspSamples = min((int)mVssRays.size(), mBspConstructionSamples); 455 456 for (int i = 0; i < bspSamples; ++ i) 457 bspRays.push_back(new Ray(*mVssRays[i])); 458 459 //-- construct BSP tree using the samples 460 mBspTree = new BspTree(&mUnbounded); 461 462 mBspTree->SetGenerateViewCells(true); 463 mBspTree->Construct(bspRays); 464 465 Exporter *exporter = Exporter::GetExporter("vccbsprays.x3d"); 466 467 // export rays piercing this view cell 468 exporter->ExportRays(bspRays, 1000, RgbColor(0, 1, 0)); 469 470 // cast remaining initial rays into BSP tree 471 for (int i = bspSamples; i < (int)mVssRays.size(); ++ i) 472 CastRay(*mBspTree, *mVssRays[i]); 473 } 474 475 vssTree = new VssTree; 476 // viewcells = Construct(mVssRays); 477 478 vssTree->Construct(mVssRays, mViewSpaceBox); 479 cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl; 480 481 ExportVssTree("vss-tree.x3d", vssTree); 482 483 ExportVssTreeLeaves(vssTree, 10); 484 485 // viewcells->UpdatePVS(newVssRays); 486 // get viewcells as kd tree boxes 487 vector<AxisAlignedBox3> kdViewcells; 488 if (0) { 489 vector<KdLeaf *> leaves; 490 mKdTree->CollectLeaves(leaves); 491 vector<KdLeaf *>::const_iterator it; 492 int targetLeaves = 50; 493 float prob = targetLeaves/(float)leaves.size(); 494 for (it = leaves.begin(); it != leaves.end(); ++it) 495 if (RandomValue(0.0f,1.0f) < prob) 496 kdViewcells.push_back(mKdTree->GetBox(*it)); 497 498 float avgPvs = GetAvgPvsSize(vssTree, kdViewcells); 499 cout<<"Initial average PVS size = "<<avgPvs<<endl; 500 } 501 502 503 int samples = 0; 504 int pass = 0; 505 while (1) { 506 int num = mVssSamplesPerPass; 507 SimpleRayContainer rays; 508 VssRayContainer vssRays; 509 510 if (!mUseImportanceSampling) { 511 for (int j=0; j < num; j++) { 512 Vector3 viewpoint = GetViewpoint(mViewSpaceBox); 513 Vector3 direction = GetDirection(viewpoint, mViewSpaceBox); 514 rays.push_back(SimpleRay(viewpoint, direction)); 515 } 516 } else { 517 num = GenerateImportanceRays(vssTree, num, rays); 518 } 519 520 521 for (int i=0; i < rays.size(); i++) 522 CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays); 523 524 vssTree->AddRays(vssRays); 525 526 if (0) { 527 int subdivided = vssTree->UpdateSubdivision(); 528 cout<<"subdivided leafs = "<<subdivided<<endl; 529 } 530 531 float avgPvs = GetAvgPvsSize(vssTree, kdViewcells); 532 cout<<"Average PVS size = "<<avgPvs<<endl; 417 533 418 534 if (numExportRays) { 419 char filename[64]; 420 sprintf(filename, "vss-rays-initial.x3d"); 421 ExportRays(filename, mVssRays, numExportRays); 422 } 423 424 //-- construct BSP view cells 535 char filename[64]; 536 if (mUseImportanceSampling) 537 sprintf(filename, "vss-rays-i%04d.x3d", pass); 538 else 539 sprintf(filename, "vss-rays-%04d.x3d", pass); 540 541 ExportRays(filename, vssRays, numExportRays); 542 } 543 544 // cast rays into BSP tree 425 545 if (ViewCell::sHierarchy == ViewCell::BSP) 546 { 547 for (int i = 0; i < (int)vssRays.size(); ++ i) 548 { 549 CastRay(*mBspTree, *vssRays[i]); 550 } 551 } 552 553 samples+=num; 554 float pvs = vssTree->GetAvgPvsSize(); 555 cout<<"*****************************\n"; 556 cout<<samples<<" avgPVS ="<<pvs<<endl; 557 cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl; 558 cout<<"*****************************\n"; 559 if (samples >= mVssSamples) 560 break; 561 pass++; 562 } 563 564 delete vssTree; 565 566 if (ViewCell::sHierarchy == ViewCell::BSP) 426 567 { 427 const int bspSamples = min((int)mVssRays.size(), mBspConstructionSamples); 428 429 for (int i = 0; i < bspSamples; ++ i) 430 bspRays.push_back(new Ray(*mVssRays[i])); 431 432 //-- construct BSP tree using the samples 433 mBspTree = new BspTree(&mUnbounded); 434 435 mBspTree->SetGenerateViewCells(true); 436 mBspTree->Construct(bspRays); 437 438 Exporter *exporter = Exporter::GetExporter("vccbsprays.x3d"); 568 Debug << mBspTree->GetStatistics(); 569 570 ObjectContainer objects; 571 ExportSplits(objects, bspRays, 10000); 572 ExportBspPvs(objects, bspRays, 10000); 573 574 BspViewCellsStatistics stat; 575 mBspTree->EvaluateViewCellsStats(stat); 576 Debug << "original view cell partition:\n" << stat << endl; 577 578 // clear BSP samples 579 CLEAR_CONTAINER(bspRays); 580 } 581 582 return true; 583 } 584 585 void VssPreprocessor::CastRay(const BspTree &tree, const VssRay & vssRay) 586 { 587 //-- cast ray to BSP tree to get intersection with view cells 588 Ray ray(vssRay); 589 590 Debug << ray << endl; 591 if (ray.intersections.empty()) 592 Debug << "empty ray" << endl; 593 else 594 Debug << "intersection: " << ray.intersections[0].mT << " " << ray.intersections[0].mObject << endl; 595 596 mBspTree->CastRay(ray); 597 598 //if (0 && ray.sourceObject.mObject) 599 //sampleContributions += 600 //AddObjectSamples(ray.sourceObject.mObject, ray); 439 601 440 // export rays piercing this view cell 441 exporter->ExportRays(bspRays, 1000, RgbColor(0, 1, 0)); 442 443 // cast remaining initial rays into BSP tree 444 for (int i = bspSamples; i < (int)mVssRays.size(); ++ i) 445 CastRay(*mBspTree, *mVssRays[i]); 446 } 447 448 vssTree = new VssTree; 449 450 vssTree->Construct(mVssRays, mViewSpaceBox); 451 452 cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl; 453 454 ExportVssTreeLeaves(vssTree, 10); 455 456 int samples = 0; 457 int pass = 0; 458 while (1) { 459 int num = mVssSamplesPerPass; 460 SimpleRayContainer rays; 461 VssRayContainer vssRays; 462 463 if (!mUseImportanceSampling) { 464 for (int j=0; j < num; j++) { 465 Vector3 viewpoint = GetViewpoint(mViewSpaceBox); 466 Vector3 direction = GetDirection(viewpoint, mViewSpaceBox); 467 rays.push_back(SimpleRay(viewpoint, direction)); 468 } 469 } else { 470 num = GenerateImportanceRays(vssTree, num, rays); 471 } 472 473 474 for (int i=0; i < rays.size(); i++) 475 CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays); 476 477 vssTree->AddRays(vssRays); 478 479 if (1) { 480 int subdivided = vssTree->UpdateSubdivision(); 481 cout<<"subdivided leafs = "<<subdivided<<endl; 482 } 483 484 if (numExportRays) { 485 char filename[64]; 486 if (mUseImportanceSampling) 487 sprintf(filename, "vss-rays-i%04d.x3d", pass); 488 else 489 sprintf(filename, "vss-rays-%04d.x3d", pass); 490 491 ExportRays(filename, vssRays, numExportRays); 492 } 493 494 // cast rays into BSP tree 495 if (ViewCell::sHierarchy == ViewCell::BSP) 496 { 497 for (int i = 0; i < (int)vssRays.size(); ++ i) 498 { 499 CastRay(*mBspTree, *vssRays[i]); 500 } 501 } 502 503 samples+=num; 504 float pvs = vssTree->GetAvgPvsSize(); 505 cout<<"*****************************\n"; 506 cout<<samples<<" avgPVS ="<<pvs<<endl; 507 cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl; 508 cout<<"*****************************\n"; 509 if (samples >= mVssSamples) 510 break; 511 pass++; 512 } 513 514 delete vssTree; 515 516 if (ViewCell::sHierarchy == ViewCell::BSP) 602 if (!ray.intersections.empty()) // second intersection found 517 603 { 518 Debug << mBspTree->GetStatistics(); 519 520 ObjectContainer objects; 521 ExportSplits(objects, bspRays, 10000); 522 ExportBspPvs(objects, bspRays, 10000); 523 524 BspViewCellsStatistics stat; 525 mBspTree->EvaluateViewCellsStats(stat); 526 Debug << "original view cell partition:\n" << stat << endl; 527 528 // clear BSP samples 529 CLEAR_CONTAINER(bspRays); 530 } 531 532 return true; 533 } 534 535 void VssPreprocessor::CastRay(const BspTree &tree, const VssRay & vssRay) 536 { 537 //-- cast ray to BSP tree to get intersection with view cells 538 Ray ray(vssRay); 539 540 Debug << ray << endl; 541 if (ray.intersections.empty()) 542 Debug << "empty ray" << endl; 543 else 544 Debug << "intersection: " << ray.intersections[0].mT << " " << ray.intersections[0].mObject << endl; 545 546 mBspTree->CastRay(ray); 547 548 //if (0 && ray.sourceObject.mObject) 549 //sampleContributions += 550 //AddObjectSamples(ray.sourceObject.mObject, ray); 551 552 if (!ray.intersections.empty()) // second intersection found 553 { 554 //sampleContributions += 555 AddObjectSamples(ray.intersections[0].mObject, ray); 556 } 604 //sampleContributions += 605 AddObjectSamples(ray.intersections[0].mObject, ray); 606 } 557 607 } 558 608 559 609 int VssPreprocessor::AddObjectSamples(Intersectable *obj, const Ray &ray) 560 610 { 561 562 611 int contributingSamples = 0; 612 int j; 563 613 564 565 614 // object can be seen from the view cell => add to view cell pvs 615 for (j=0; j < ray.bspIntersections.size(); ++ j) 566 616 { 617 BspLeaf *leaf = ray.bspIntersections[j].mLeaf; 618 // if ray not in unbounded space 619 if (leaf->GetViewCell() != &mUnbounded) 620 contributingSamples += 621 leaf->GetViewCell()->GetPvs().AddSample(obj); 622 } 623 624 // rays passing through this viewcell 625 if (mPass > 1) 626 for (j=1; j < ((int)ray.bspIntersections.size() - 1); ++ j) 627 { 567 628 BspLeaf *leaf = ray.bspIntersections[j].mLeaf; 568 // if ray not in unbounded space629 569 630 if (leaf->GetViewCell() != &mUnbounded) 570 contributingSamples += 571 leaf->GetViewCell()->GetPvs().AddSample(obj); 572 } 573 574 // rays passing through this viewcell 575 if (mPass > 1) 576 for (j=1; j < ((int)ray.bspIntersections.size() - 1); ++ j) 577 { 578 BspLeaf *leaf = ray.bspIntersections[j].mLeaf; 579 580 if (leaf->GetViewCell() != &mUnbounded) 581 leaf->GetViewCell()-> 582 AddPassingRay(ray, contributingSamples ? 1 : 0); 583 } 584 585 return contributingSamples; 586 } 631 leaf->GetViewCell()-> 632 AddPassingRay(ray, contributingSamples ? 1 : 0); 633 } 634 635 return contributingSamples; 636 }
Note: See TracChangeset
for help on using the changeset viewer.