- Timestamp:
- 09/11/08 17:31:36 (16 years ago)
- Location:
- GTP/trunk/App/Demos/Vis/FriendlyCulling/src
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp
r2928 r2931 337 337 338 338 339 340 } 341 339 Polyhedron *Camera::ComputeFrustum() const 340 { 341 Vector3 ftl, ftr, fbl, fbr; 342 Vector3 ntl, ntr, nbl, nbr; 343 344 VertexArray sides[6]; 345 346 ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr); 347 348 for (int i = 0; i < 6; ++ i) 349 sides[i].resize(4); 350 351 // left, right 352 sides[0][0] = ftl; sides[0][1] = fbl; sides[0][2] = nbl; sides[0][3] = ntl; 353 sides[1][0] = fbr; sides[1][1] = ftr; sides[1][2] = ntr; sides[1][3] = nbr; 354 // bottom, top 355 sides[2][0] = fbl; sides[2][1] = fbr; sides[2][2] = nbr; sides[2][3] = nbl; 356 sides[3][0] = ftr; sides[3][1] = ftl; sides[3][2] = ntl; sides[3][3] = ntr; 357 // near, far 358 sides[4][0] = ntr; sides[4][1] = ntl; sides[4][2] = nbl; sides[4][3] = nbr; 359 sides[5][0] = ftl; sides[5][1] = ftr; sides[5][2] = fbr; sides[5][3] = fbl; 360 361 362 ////////// 363 //-- compute polyhedron 364 365 PolygonContainer polygons; 366 367 for (int i = 0; i < 6; ++ i) 368 { 369 Polygon3 *poly = new Polygon3(sides[i]); 370 polygons.push_back(poly); 371 } 372 373 return new Polyhedron(polygons); 374 } 375 376 377 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.h
r2913 r2931 108 108 */ 109 109 void SetDirection(const Vector3 &direction); 110 /** Returns frustum as polyhedron. 111 */ 112 Polyhedron *Camera::ComputeFrustum() const; 110 113 111 114 protected: -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.cpp
r2929 r2931 15 15 16 16 17 /********************************************************** /18 /* class Polyhedron Implementation */19 /********************************************************** /17 /************************************************************/ 18 /* class Polyhedron Implementation */ 19 /************************************************************/ 20 20 21 21 … … 57 57 58 58 if (!oldPolyhedron->Valid()) cerr << "******************* not valid!! ************* " << endl; 59 60 //oldPolyhedron->mBox = sceneBox;61 59 62 60 vector<Plane3>::const_iterator it, it_end = planes.end(); … … 77 75 } 78 76 79 // hack: set bounding box temporarily to scene bounding box80 //newPolyhedron->mBox = sceneBox;81 82 77 DEL_PTR(oldPolyhedron); 83 78 oldPolyhedron = newPolyhedron; -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
r2929 r2931 17 17 { 18 18 19 static CGprogram sCgShadowProgram; 20 static CGparameter sShadowParam; 21 19 22 20 23 static Polyhedron *polyhedron = NULL; … … 35 38 36 39 37 38 static CGprogram sCgShadowProgram; 39 static CGparameter sShadowParam; 40 static Polyhedron *CreatePolyhedron(const Matrix4x4 &lightMatrix, const AxisAlignedBox3 &sceneBox) 41 { 42 Frustum frustum(lightMatrix); 43 44 vector<Plane3> clipPlanes; 45 46 for (int i = 0; i < 6; ++ i) 47 { 48 //////////// 49 //-- normalize the coefficients 50 51 // the clipping planes look outward the frustum, 52 // so distances > 0 mean that a point is outside 53 const float invLength = -1.0f / Magnitude(frustum.mClipPlanes[i].mNormal); 54 55 frustum.mClipPlanes[i].mD *= invLength; 56 frustum.mClipPlanes[i].mNormal *= invLength; 57 58 //clipPlanes.push_back(frustum.mClipPlanes[i]); 59 } 60 61 clipPlanes.push_back(frustum.mClipPlanes[5]); 62 clipPlanes.push_back(frustum.mClipPlanes[0]); 63 clipPlanes.push_back(frustum.mClipPlanes[1]); 64 clipPlanes.push_back(frustum.mClipPlanes[2]); 65 clipPlanes.push_back(frustum.mClipPlanes[3]); 66 clipPlanes.push_back(frustum.mClipPlanes[4]); 67 68 return Polyhedron::CreatePolyhedron(clipPlanes, sceneBox); 69 } 40 70 41 71 … … 182 212 //-- first find the free parameter values n, and P (the projection center), and the projection depth 183 213 184 //const float n = 1e 6f;185 const float n = 1e 3f;214 //const float n = 1e2f; 215 const float n = 1e6f; 186 216 //const float n = ComputeN(bounds_ls) * 100; 187 217 … … 431 461 Polyhedron *ShadowMap::CalcClippedFrustum(const AxisAlignedBox3 &box) const 432 462 { 433 Vector3 ftl, ftr, fbl, fbr; 434 Vector3 ntl, ntr, nbl, nbr; 435 436 VertexArray sides[6]; 437 438 mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr); 439 440 for (int i = 0; i < 6; ++ i) 441 sides[i].resize(4); 442 443 // left, right 444 sides[0][0] = ftl; sides[0][1] = fbl; sides[0][2] = nbl; sides[0][3] = ntl; 445 sides[1][0] = fbr; sides[1][1] = ftr; sides[1][2] = ntr; sides[1][3] = nbr; 446 // bottom, top 447 sides[2][0] = fbl; sides[2][1] = fbr; sides[2][2] = nbr; sides[2][3] = nbl; 448 sides[3][0] = ftr; sides[3][1] = ftl; sides[3][2] = ntl; sides[3][3] = ntr; 449 // near, far 450 sides[4][0] = ntr; sides[4][1] = ntl; sides[4][2] = nbl; sides[4][3] = nbr; 451 sides[5][0] = ftl; sides[5][1] = ftr; sides[5][2] = fbr; sides[5][3] = fbl; 452 453 ////////// 454 //-- compute polyhedron 455 456 PolygonContainer polygons; 457 458 for (int i = 0; i < 6; ++ i) 459 { 460 Polygon3 *poly = new Polygon3(sides[i]); 461 polygons.push_back(poly); 462 } 463 464 Polyhedron *p = new Polyhedron(polygons); 463 Polyhedron *p = mCamera->ComputeFrustum(); 464 465 465 Polyhedron *clippedPolyhedron = box.CalcIntersection(*p); 466 466 467 467 DEL_PTR(p); 468 468 469 470 469 return clippedPolyhedron; 470 } 471 472 473 //calculates the up vector for the light coordinate frame 474 static Vector3 CalcUpVec(const Vector3 viewDir, const Vector3 lightDir) 475 { 476 //we do what gluLookAt does... 477 //left is the normalized vector perpendicular to lightDir and viewDir 478 //this means left is the normalvector of the yz-plane from the paper 479 Vector3 left = CrossProd(lightDir, viewDir); 480 481 //we now can calculate the rotated(in the yz-plane) viewDir vector 482 //and use it as up vector in further transformations 483 Vector3 up = CrossProd(left, lightDir); 484 485 return Normalize(up); 471 486 } 472 487 … … 479 494 //const Vector3 dir = mLight->GetDirection(); 480 495 const Vector3 dir(0, 0, -1); 496 497 Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir); 498 mLightMatrix = LookAt(mCamera->GetPosition(), dir, upVec); 481 499 482 500 mShadowCam->SetDirection(dir); … … 526 544 Matrix4x4 lightView, lightProj; 527 545 528 mShadowCam->GetModelViewMatrix(lightView); 546 //mShadowCam->GetModelViewMatrix(lightView); 547 lightView = mLightMatrix; 548 529 549 CalcLightProjection(lightProj); 530 550 … … 535 555 mLightProjView = lightView * lightProj; 536 556 537 Frustum frustum(mLightProjView);538 //Frustum frustum(projView);539 540 557 DEL_PTR(lightPoly); 541 542 vector<Plane3> clipPlanes; 543 544 for (int i = 0; i < 6; ++ i) 545 { 546 frustum.mClipPlanes[i].mNormal *= -1; 547 frustum.mClipPlanes[i].mD *= -1; 548 549 clipPlanes.push_back(frustum.mClipPlanes[i]); 550 } 551 552 DEL_PTR(lightPoly); 553 lightPoly = Polyhedron::CreatePolyhedron(clipPlanes, mSceneBox); 558 lightPoly = CreatePolyhedron(mLightProjView, mSceneBox); 554 559 555 560 //cout << "new:\n" << lightProj << endl; … … 559 564 glLoadIdentity(); 560 565 561 mShadowCam->SetupCameraView();562 566 //mShadowCam->SetupCameraView(); 567 glLoadMatrixf((float *)mLightMatrix.x); 563 568 564 569 ////////////// … … 606 611 FrameBufferObject::Release(); 607 612 } 613 608 614 609 615 … … 661 667 662 668 669 void ShadowMap::RenderShadowView(RenderTraverser *renderer, const Matrix4x4 &projView) 670 { 671 //const Vector3 dir = mLight->GetDirection(); 672 const Vector3 dir(0, 0, -1); 673 674 mShadowCam->SetDirection(dir); 675 676 Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir); 677 mLightMatrix = LookAt(mCamera->GetPosition(), dir, upVec); 678 679 // set position so that we can see the whole scene 680 Vector3 pos = mSceneBox.Center(); 681 682 pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.5f); 683 mShadowCam->SetPosition(pos); 684 685 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 686 687 glEnable(GL_LIGHTING); 688 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 689 690 glPolygonOffset(1.0f, 2000.0f); 691 glEnable(GL_POLYGON_OFFSET_FILL); 692 glDisable(GL_CULL_FACE); 693 glEnable(GL_DEPTH_TEST); 694 695 Matrix4x4 lightView, lightProj; 696 697 //mShadowCam->GetModelViewMatrix(lightView); 698 lightView = mLightMatrix; 699 CalcLightProjection(lightProj); 700 701 glMatrixMode(GL_PROJECTION); 702 glPushMatrix(); 703 glLoadMatrixf((float *)lightProj.x); 704 705 mLightProjView = lightView * lightProj; 706 707 DEL_PTR(lightPoly); 708 lightPoly = CreatePolyhedron(mLightProjView, mSceneBox); 709 710 //cout << "new:\n" << lightProj << endl; 711 712 glMatrixMode(GL_MODELVIEW); 713 glPushMatrix(); 714 glLoadIdentity(); 715 716 //mShadowCam->SetupCameraView(); 717 glLoadMatrixf((float *)mLightMatrix.x); 718 719 720 ///////////// 721 //-- render scene into shadow map 722 723 renderer->RenderScene(); 724 725 726 glDisable(GL_POLYGON_OFFSET_FILL); 727 728 glPushAttrib(GL_CURRENT_BIT); 729 glDisable(GL_LIGHTING); 730 glDisable(GL_DEPTH_TEST); 731 glDepthMask(GL_FALSE); 732 glDisable(GL_CULL_FACE); 733 734 Polyhedron *hpoly = CreatePolyhedron(projView, mSceneBox); 735 //Polyhedron *hpoly = CalcClippedFrustum(mSceneBox); 736 737 DrawPoly(hpoly, Vector3(1, 1, 1)); 738 DEL_PTR(hpoly); 739 740 glEnable(GL_CULL_FACE); 741 742 glEnable(GL_DEPTH_TEST); 743 glDepthMask(GL_TRUE); 744 glPopAttrib(); 745 746 glMatrixMode(GL_MODELVIEW); 747 glPopMatrix(); 748 749 glMatrixMode(GL_PROJECTION); 750 glPopMatrix(); 751 752 753 #if 0 754 float *data = new float[mSize * mSize]; 755 756 GrabDepthBuffer(data, mFbo->GetDepthTex()); 757 ExportDepthBuffer(data, mSize); 758 759 delete [] data; 760 761 PrintGLerror("shadow map"); 762 #endif 763 FrameBufferObject::Release(); 764 } 765 766 767 768 663 769 } // namespace -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
r2929 r2931 56 56 Camera *GetShadowCamera() const { return mShadowCam; } 57 57 58 void RenderShadowView(RenderTraverser *renderer, const Matrix4x4 &projView); 59 58 60 59 61 static void DrawPolys(); … … 61 63 62 64 protected: 65 63 66 64 67 /** Calculates the intersection of the frustum with the box, … … 106 109 107 110 Matrix4x4 mLightProjView; 111 112 Matrix4x4 mLightMatrix; 108 113 }; 109 114 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Visualization.cpp
r2929 r2931 174 174 175 175 glDepthMask(GL_TRUE); 176 177 176 glPopAttrib(); 178 177 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
r2930 r2931 100 100 static int winHeight = 768; 101 101 102 const float shadowSize = 4096; 102 103 103 104 static float winAspectRatio = 1.0f; … … 179 180 180 181 static Matrix4x4 matProjectionView = IdentityMatrix(); 182 183 bool renderLightView = false; 184 181 185 182 186 … … 873 877 874 878 875 // render without shading 879 880 if ((!shadowMap || !shadowTraverser) && (showShadowMap || renderLightView)) 881 { 882 if (!shadowMap) 883 shadowMap = new ShadowMap(light, shadowSize, bvh->GetBox(), camera); 884 885 if (!shadowTraverser) 886 shadowTraverser = CreateTraverser(shadowMap->GetShadowCamera()); 887 888 } 889 890 // hack 891 int oldRenderType = renderType; 892 893 if (renderLightView) 894 renderType = RenderState::FIXED; 895 896 897 // render with the specified method (forward rendering, forward + depth, deferred) 876 898 switch (renderType) 877 899 { … … 909 931 case RenderState::DEFERRED: 910 932 911 if (showShadowMap )// && shadowChanged)933 if (showShadowMap && !renderLightView)// && shadowChanged) 912 934 { 913 if (!shadowMap)914 {915 const float shadowSize = 4096;916 //const float shadowSize = 512;917 shadowMap = new ShadowMap(light, shadowSize, bvh->GetBox(), camera);918 }919 920 // change CHC state (must be done for each change of camera)921 BvhNode::SetCurrentState(1);922 923 if (!shadowTraverser)924 shadowTraverser = CreateTraverser(shadowMap->GetShadowCamera());925 926 935 shadowChanged = false; 927 928 #if 0929 state.SetRenderType(RenderState::DEFERRED);930 931 fbo->Bind();932 933 glPushAttrib(GL_VIEWPORT_BIT);934 glViewport(0, 0, texWidth, texHeight);935 936 cgGLEnableProfile(RenderState::sCgFragmentProfile);937 cgGLBindProgram(RenderState::sCgMrtFragmentProgram);938 939 cgGLEnableProfile(RenderState::sCgVertexProfile);940 cgGLBindProgram(sCgMrtVertexProgram);941 #else942 936 943 937 cgGLDisableProfile(RenderState::sCgFragmentProfile); … … 945 939 946 940 state.SetRenderType(RenderState::DEPTH_PASS); 947 #endif 941 942 // change CHC++ set of state variables (must be done for each change of camera because 943 // otherwise the temporal coherency is broken 944 BvhNode::SetCurrentState(1); 945 948 946 // the scene is rendered withouth any shading 949 947 shadowMap->ComputeShadowMap(shadowTraverser, matProjectionView); … … 1002 1000 glLightfv(GL_LIGHT1, GL_POSITION, position1); 1003 1001 1004 // actually render the scene geometry using the specified algorithm 1005 traverser->RenderScene(); 1002 1003 if (renderLightView) 1004 { 1005 // change CHC++ set of state variables (must be done for each change of camera because 1006 // otherwise the temporal coherency is broken 1007 BvhNode::SetCurrentState(1); 1008 shadowMap->RenderShadowView(shadowTraverser, matProjectionView); 1009 BvhNode::SetCurrentState(0); 1010 } 1011 else 1012 // actually render the scene geometry using the specified algorithm 1013 traverser->RenderScene(); 1006 1014 1007 1015 … … 1068 1076 glDisableClientState(GL_VERTEX_ARRAY); 1069 1077 glDisableClientState(GL_NORMAL_ARRAY); 1070 1078 1079 renderType = oldRenderType; 1071 1080 1072 1081 … … 1114 1123 traverser = CreateTraverser(camera); 1115 1124 1116 if (shadowTraverser && shadowMap)1125 if (shadowTraverser) 1117 1126 { 1127 // shadow traverser has to be recomputed (this is done on demand) 1118 1128 DEL_PTR(shadowTraverser); 1119 //shadowTraverser = CreateTraverser(shadowMap->GetShadowCamera());1120 1129 } 1121 1130 … … 1202 1211 case 'a': 1203 1212 case 'A': 1204 1213 leftKeyPressed = true; 1205 1214 break; 1206 1215 case 'd': 1207 1216 case 'D': 1208 1217 rightKeyPressed = true; 1209 1218 break; 1210 1219 case 'w': 1211 1220 case 'W': 1212 1221 upKeyPressed = true; 1213 1222 break; 1214 1223 case 's': 1215 1224 case 'S': 1216 1225 downKeyPressed = true; 1217 1226 break; 1218 1227 case 'r': 1219 1228 case 'R': 1220 { 1221 useRenderQueue = !useRenderQueue; 1222 traverser->SetUseRenderQueue(useRenderQueue); 1223 } 1229 useRenderQueue = !useRenderQueue; 1230 traverser->SetUseRenderQueue(useRenderQueue); 1231 1224 1232 break; 1225 1233 case 'b': 1226 1234 case 'B': 1227 { 1228 useTightBounds = !useTightBounds; 1229 traverser->SetUseTightBounds(useTightBounds); 1230 } 1235 useTightBounds = !useTightBounds; 1236 traverser->SetUseTightBounds(useTightBounds); 1237 break; 1238 case 'l': 1239 case 'L': 1240 renderLightView = !renderLightView; 1231 1241 break; 1232 1242 default:
Note: See TracChangeset
for help on using the changeset viewer.