Changeset 2767 for GTP/trunk/App/Demos/Vis/CHC_revisited/chcdemo.cpp
- Timestamp:
- 06/17/08 15:20:32 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/CHC_revisited/chcdemo.cpp
r2765 r2767 17 17 #include "StopAndWaitTraverser.h" 18 18 #include "CHCTraverser.h" 19 #include "CHCPlusPlusTraverser.h" 20 #include "Visualization.h" 21 19 22 20 23 … … 27 30 SceneEntityContainer sceneEntities; 28 31 // traverses and renders the hierarchy 29 RenderTraverser *traverser ;32 RenderTraverser *traverser = NULL; 30 33 /// the hierarchy 31 Bvh *bvh ;34 Bvh *bvh = NULL; 32 35 /// the scene camera 33 Camera *camera ;34 /// the scene bounding box35 AxisAlignedBox3 sceneBox;36 Camera *camera = NULL; 37 /// the visualization 38 Visualization *visualization = NULL; 36 39 /// the current render state 37 40 RenderState state; … … 43 46 float nearDist = 0.1f; 44 47 48 const float keyForwardMotion = 1.0f; 49 const float keyRotation = 0.2f; 50 51 bool useRenderQueue = false; 52 45 53 int winWidth = 1024; 46 54 int winHeight = 768; … … 55 63 56 64 57 int currentFrame = -1;58 59 65 // visualisation view matrix 60 66 Matrix4x4 visView; … … 63 69 int xEyeBegin, yEyeBegin, yMotionBegin, verticalMotionBegin, horizontalMotionBegin = 0; 64 70 65 const int renderTimeSize = 100;66 long renderTimes[renderTimeSize];67 int renderTimesIdx = 0;68 int renderTimesValid = 0;69 70 71 bool useOptimization = true; 71 72 72 73 Vector3 amb[2];74 Vector3 dif[2];75 Vector3 spec[2];76 73 77 74 void InitExtensions(); … … 100 97 void setupVisView(void); 101 98 long calcRenderTime(void); 102 void resetTimer(void);103 99 void CalcDecimalPoint(std::string &str, int d); 104 100 void ResetTraverser(); … … 148 144 //bvh = bvhLoader.Load("house_test.bvh", sceneEntities); 149 145 150 sceneBox = bvh->GetBox();151 152 146 ResetTraverser(); 153 147 154 148 //camera->LookAtBox(sceneBox); 155 camera->LookInBox(sceneBox); 156 157 /// initialise rendertime array 158 for(int i=0; i<renderTimeSize; i++) 159 renderTimes[i] = 0; 149 //camera->LookInBox(bvh->GetBox()); 150 151 camera->SetDirection(Vector3(0.961829f, 0.273652f, 0.0f)); 152 camera->SetPosition(Vector3(483.398f, 242.364f, 186.078f)); 160 153 161 154 glutMainLoop(); … … 170 163 void InitGLstate(void) 171 164 { 172 glClearColor(0.5f, 0.5f, 0.8f, 0.0 );165 glClearColor(0.5f, 0.5f, 0.8f, 0.0f); 173 166 174 167 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); … … 185 178 glMaterialf(GL_FRONT, GL_SHININESS, 64); 186 179 glEnable(GL_NORMALIZE); 187 180 181 //glEnable(GL_ALPHA_TEST); 182 //glAlphaFunc(GL_GEQUAL, 0.01); 183 184 glDisable(GL_ALPHA_TEST); 185 188 186 glFrontFace(GL_CCW); 189 187 glCullFace(GL_BACK); … … 288 286 traverser = new CHCTraverser(); 289 287 break; 288 case RenderTraverser::CHCPLUSPLUS: 289 traverser = new CHCPlusPlusTraverser(); 290 break; 291 290 292 default: 291 293 traverser = new FrustumCullingTraverser(); 292 294 } 293 295 296 traverser->SetCamera(camera); 294 297 traverser->SetHierarchy(bvh); 295 298 traverser->SetRenderState(&state); … … 302 305 glEnable(GL_LIGHT0); 303 306 glEnable(GL_LIGHT1); 307 304 308 //glDisable(GL_LIGHT1); 305 309 //glDisable(GL_LIGHTING); … … 349 353 glLoadIdentity(); 350 354 351 gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude( sceneBox.Diagonal()));355 gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(bvh->GetBox().Diagonal())); 352 356 353 357 glMatrixMode(GL_MODELVIEW); … … 357 361 glLightfv(GL_LIGHT0, GL_POSITION, position); 358 362 359 GLfloat position1[] = {sceneBox.Center().x, sceneBox.Max().y, sceneBox.Center().z, 1.0f}; 360 //GLfloat position1[] = {-2.0f, 1.0f, 0.0f, 0.0f}; 363 GLfloat position1[] = {bvh->GetBox().Center().x, bvh->GetBox().Max().y, bvh->GetBox().Center().z, 1.0f}; 361 364 glLightfv(GL_LIGHT1, GL_POSITION, position1); 362 365 } … … 365 368 void display(void) 366 369 { 367 ++ currentFrame;368 369 370 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 370 371 … … 372 373 SetupEyeView(); 373 374 374 375 traverser->InitFrame(camera, currentFrame); 376 377 378 InitTiming(); 379 long t1, t2; 380 381 t1 = GetTime(); 382 383 glEnableClientState(GL_VERTEX_ARRAY); 384 glEnableClientState(GL_NORMAL_ARRAY); 385 386 traverser->Render(); 387 388 glDisableClientState(GL_VERTEX_ARRAY); 389 glDisableClientState(GL_NORMAL_ARRAY); 390 391 glDisable(GL_TEXTURE_2D); 392 glDisableClientState(GL_TEXTURE_COORD_ARRAY); 393 394 395 t2 = GetTime(); 396 long loop_time = TimeDiff(t1, t2); 397 398 // cycle through rendertime array 399 renderTimes[renderTimesIdx] = loop_time;// traverser.GetRenderTime(); 400 renderTimesIdx = (renderTimesIdx + 1) % renderTimeSize; 401 402 if(renderTimesIdx > renderTimesValid) 403 renderTimesValid = renderTimesIdx; 404 375 //cout << camera->GetDirection() << " " << camera->GetPosition() << endl; 376 // actually render the scene geometry using one of the specified algorithms 377 traverser->RenderScene(); 378 405 379 //if(visMode) displayVisualization(); 406 380 407 381 DisplayStats(); 382 408 383 glutSwapBuffers(); 409 384 } … … 413 388 void keyboard(const unsigned char c, const int x, const int y) 414 389 { 390 // used to avoid vertical motion with the keys 391 Vector3 hvec = Vector3(camera->GetDirection()[0], camera->GetDirection()[1], 0.0f); 392 Vector3 uvec = Vector3(0, 0, 1); 393 415 394 switch(c) 416 395 { … … 420 399 case 32: //space 421 400 renderMode = (renderMode + 1) % RenderTraverser::NUM_RENDERMODES; 422 resetTimer();423 401 ResetTraverser(); 424 402 break; … … 432 410 //HierarchyNode::SetRenderBoundingVolume(showBoundingVolumes); 433 411 break; 434 case ' s':435 case ' S':412 case 'O': 413 case 'o': 436 414 showStatistics = !showStatistics; 437 415 break; … … 449 427 visMode = !visMode; 450 428 break; 451 452 429 case '2': 453 430 visZoomFactor += 0.1; … … 460 437 setupVisView(); 461 438 break; 439 case '8': 440 { 441 Vector3 pos = camera->GetPosition(); 442 pos += uvec * keyForwardMotion; 443 camera->SetPosition(pos); 444 break; 445 } 446 case '9': 447 { 448 Vector3 pos = camera->GetPosition(); 449 pos -= uvec * keyForwardMotion; 450 camera->SetPosition(pos); 451 break; 452 } 462 453 case 'g': 463 454 case 'G': 464 455 useOptimization = !useOptimization; 465 456 traverser->SetUseOptimization(useOptimization); 457 break; 458 case 'a': 459 case 'A': 460 { 461 Vector3 viewDir = camera->GetDirection(); 462 // rotate view vector 463 Matrix4x4 rot = RotationZMatrix(keyRotation); 464 viewDir = rot * viewDir; 465 camera->SetDirection(viewDir); 466 } 467 break; 468 case 'd': 469 case 'D': 470 { 471 Vector3 viewDir = camera->GetDirection(); 472 // rotate view vector 473 Matrix4x4 rot = RotationZMatrix(-keyRotation); 474 viewDir = rot * viewDir; 475 camera->SetDirection(viewDir); 476 } 477 break; 478 case 'w': 479 case 'W': 480 { 481 Vector3 pos = camera->GetPosition(); 482 pos += hvec * keyForwardMotion; 483 camera->SetPosition(pos); 484 } 485 break; 486 case 'x': 487 case 'X': 488 { 489 Vector3 pos = camera->GetPosition(); 490 pos -= hvec * keyForwardMotion; 491 camera->SetPosition(pos); 492 } 493 break; 494 case 'r': 495 case 'R': 496 { 497 useRenderQueue = !useRenderQueue; 498 traverser->SetUseRenderQueue(useRenderQueue); 499 } 466 500 default: 467 501 return; … … 475 509 { 476 510 // used to avoid vertical motion with the keys 477 //Vector3 hvec = Vector3(viewDir[0], 0, viewDir[2]);511 Vector3 hvec = Vector3(camera->GetDirection()[0], camera->GetDirection()[1], 0.0f); 478 512 479 513 switch(c) … … 482 516 showHelp = !showHelp; 483 517 break; 484 case GLUT_KEY_F2:485 //numNextObjects -= 100;486 //if(numNextObjects < 100) numNextObjects = 100;487 break;488 case GLUT_KEY_F3:489 // numNextObjects += 100;490 break;491 case GLUT_KEY_F4:492 //zLength -= 1;493 //if(zLength < 0) zLength = 0;494 break;495 case GLUT_KEY_F5:496 //zLength += 1;497 break;498 case GLUT_KEY_F6:499 //objectSize -= 0.1;500 //if(objectSize < 0.1) objectSize = 0.1;501 break;502 case GLUT_KEY_F7:503 //objectSize += 0.1;504 break;505 case GLUT_KEY_F8:506 //nextObjectType = (nextObjectType + 1) % Geometry::NUM_OBJECTS;507 break;508 518 case GLUT_KEY_LEFT: 509 // rotateVectorY(viewDir, 0.2); 519 { 520 Vector3 viewDir = camera->GetDirection(); 521 // rotate view vector 522 Matrix4x4 rot = RotationZMatrix(keyRotation); 523 viewDir = rot * viewDir; 524 camera->SetDirection(viewDir); 525 } 510 526 break; 511 527 case GLUT_KEY_RIGHT: 512 //rotateVectorY(viewDir, -0.2); 528 { 529 Vector3 viewDir = camera->GetDirection(); 530 // rotate view vector 531 Matrix4x4 rot = RotationZMatrix(-keyRotation); 532 viewDir = rot * viewDir; 533 camera->SetDirection(viewDir); 534 } 513 535 break; 514 536 case GLUT_KEY_UP: 515 // linCombVector3(eyePos, eyePos, hvec, 0.6); 537 { 538 Vector3 pos = camera->GetPosition(); 539 pos += hvec * 0.6f; 540 camera->SetPosition(pos); 541 } 516 542 break; 517 543 case GLUT_KEY_DOWN: 518 //linCombVector3(eyePos, eyePos, hvec, -0.6); 544 { 545 Vector3 pos = camera->GetPosition(); 546 pos -= hvec * 0.6f; 547 camera->SetPosition(pos); 548 } 519 549 break; 520 550 default: … … 525 555 glutPostRedisplay(); 526 556 } 557 527 558 #pragma warning( default : 4100 ) 528 559 … … 542 573 glLoadIdentity(); 543 574 544 gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude( sceneBox.Diagonal()));575 gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(bvh->GetBox().Diagonal())); 545 576 glMatrixMode(GL_MODELVIEW); 546 577 … … 587 618 588 619 // don't move in the vertical direction 589 //Vector3 horView(viewDir[0], 0, viewDir[2]);590 620 Vector3 horView(viewDir[0], viewDir[1], 0); 591 621 … … 593 623 594 624 // rotate view vector 595 //Matrix4x4 rot = RotationYMatrix(eyeXAngle);596 625 Matrix4x4 rot = RotationZMatrix(eyeXAngle); 597 626 viewDir = rot * viewDir; … … 609 638 610 639 611 /** rotation for left /right mouse drag612 motion for up /down mouse drag640 /** rotation for left / right mouse drag 641 motion for up / down mouse drag 613 642 */ 614 643 void rightMotion(int x, int y) … … 707 736 void output(const int x, const int y, const char *string) 708 737 { 709 if (string != 0)710 { 711 int len, i;712 glRasterPos2f(x, y);713 len = (int)strlen(string);738 if (string != 0) 739 { 740 size_t len, i; 741 glRasterPos2f(x, y); 742 len = strlen(string); 714 743 715 for (i = 0; i < len; i++)744 for (i = 0; i < len; ++ i) 716 745 { 717 746 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, string[i]); … … 719 748 } 720 749 } 750 721 751 722 752 // displays the visualisation of the kd tree node culling … … 738 768 glClear(GL_DEPTH_BUFFER_BIT); 739 769 770 //////////// 740 771 // --- visualization of the occlusion culling 741 /*HierarchyNode::SetRenderBoundingVolume(true); 742 traverser.RenderVisualization(); 743 HierarchyNode::SetRenderBoundingVolume(showBoundingVolumes); 744 */ 772 visualization->Render(); 773 745 774 glPopMatrix(); 775 746 776 glViewport(0, 0, winWidth, winHeight); 747 777 } 778 748 779 749 780 /** Sets up view matrix in order to get a good position … … 752 783 void setupVisView() 753 784 { 754 const Vector3 up(0.0, 1.0, 0.0);785 const Vector3 up(0.0, 0.0, 1.0); 755 786 756 787 Vector3 visPos(24, 23, -6); … … 760 791 visPos[2] *= visZoomFactor; 761 792 762 Vector3 visDir = Vector3(-1.3, -1, -1); 763 764 //normalize(visDir); 765 //look(visView, visPos, visDir, up); 766 } 767 768 // we take a couple of measurements and compute the average 769 long calcRenderTime() 770 { 771 long result = 0; 772 773 for(int i=0; i<renderTimesValid; i++) 774 result += renderTimes[i]; 775 776 if(renderTimesValid) 777 result /= renderTimesValid; 778 779 return result; 780 } 781 782 783 // reset the timer array for a new traversal method 784 void resetTimer() 785 { 786 renderTimesValid = 0; 787 renderTimesIdx = 0; 793 Vector3 visDir = Vector3(-1.3f, -1, -1); 788 794 } 789 795 … … 796 802 797 803 DEL_PTR(bvh); 804 DEL_PTR(visualization); 798 805 } 799 806 … … 829 836 { 830 837 char *msg[] = {"Frustum Culling", "Stop and Wait", 831 "C oherent Culling"};838 "CHC", "CHC ++"}; 832 839 833 840 char msg2[200]; … … 843 850 844 851 float fps = 1e3f; 845 long renderTime = calcRenderTime(); 852 static long renderTime = traverser->GetStats().mRenderTime; 853 854 const float expFactor = 0.9f; 855 856 renderTime = renderTime * expFactor + (1.0f - expFactor) * traverser->GetStats().mRenderTime; 857 846 858 if (renderTime) fps /= (float)renderTime; 847 859 … … 868 880 else 869 881 { 870 glColor3f(1.0f ,1.0f ,1.0f);871 output( 20, winHeight - 10, msg[renderMode]);882 glColor3f(1.0f, 1.0f , 1.0f); 883 output(10, winHeight - 10, msg[renderMode]); 872 884 873 885 if(showStatistics)
Note: See TracChangeset
for help on using the changeset viewer.