Changeset 2809 for GTP/trunk/App/Demos/Vis/FriendlyCulling/src
- Timestamp:
- 07/03/08 00:13:43 (17 years ago)
- Location:
- GTP/trunk/App/Demos/Vis/FriendlyCulling/src
- Files:
-
- 5 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.cpp
r2808 r2809 12 12 using namespace std; 13 13 14 int texWidth = 2048;15 int texHeight = 2048;14 static int texWidth = 2048; 15 static int texHeight = 2048; 16 16 17 /*static void cgErrorCallback()18 {19 CGerror lastError = cgGetError();20 21 if(lastError)22 {23 printf("%s\n\n", cgGetErrorString(lastError));24 printf("%s\n", cgGetLastListing(sCgContext));25 printf("Cg error, exiting...\n");26 27 exit(0);28 }29 }*/30 17 31 18 … … 137 124 cout << "Preparing scene queries" << endl; 138 125 139 RenderTexture *depthTexture = new RenderTexture(texWidth, texHeight, true, true);140 141 #ifdef ATI142 depthTexture->Initialize(true, true, false, false, false, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);143 #else144 depthTexture->Initialize(true, true, false, false, false, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);145 #endif146 147 PrintGLerror("Init");148 149 150 126 const float xlen = mSceneBox.Size().x * 0.5f; 151 127 const float ylen = mSceneBox.Size().y * 0.5f; … … 160 136 orthoCam->SetPosition(pos); 161 137 138 RenderTexture *depthTexture = new RenderTexture(texWidth, texHeight, true, true); 139 140 #ifdef ATI 141 depthTexture->Initialize(true, true, false, false, false, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE); 142 #else 143 depthTexture->Initialize(true, true, false, false, false, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE); 144 #endif 145 146 PrintGLerror("Init"); 147 162 148 depthTexture->BeginCapture(); 149 { 150 glViewport(0, 0, texWidth, texHeight); 163 151 164 glViewport(0, 0, texWidth, texHeight); 152 glClearColor(1, 1, 1, 1); 153 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 165 154 166 glClearColor(1, 1, 1, 1);167 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);155 glFrontFace(GL_CCW); 156 glCullFace(GL_BACK); 168 157 169 glFrontFace(GL_CCW); 170 glCullFace(GL_BACK); 158 glEnable(GL_CULL_FACE); 171 159 172 glDisable(GL_CULL_FACE);173 //glEnable(GL_CULL_FACE);160 glShadeModel(GL_FLAT); 161 glEnable(GL_DEPTH_TEST); 174 162 175 glShadeModel(GL_FLAT);176 glEnable(GL_DEPTH_TEST);163 glMatrixMode(GL_PROJECTION); 164 glLoadIdentity(); 177 165 178 glMatrixMode(GL_PROJECTION); 179 glLoadIdentity(); 180 181 glOrtho(-xlen, xlen, -ylen, ylen, 0.0f, mSceneBox.Size().z); 182 183 glMatrixMode(GL_MODELVIEW); 184 185 orthoCam->SetupCameraView(); 166 glOrtho(-xlen, xlen, -ylen, ylen, 0.0f, mSceneBox.Size().z); 186 167 187 mDepth = new float[texHeight * texWidth];168 glMatrixMode(GL_MODELVIEW); 188 169 189 //renderer->SetCamera(orthoCam); 190 renderer->RenderScene(); 170 orthoCam->SetupCameraView(); 191 171 172 mDepth = new float[texHeight * texWidth]; 173 174 //renderer->SetCamera(orthoCam); 175 renderer->RenderScene(); 176 } 192 177 depthTexture->EndCapture(); 193 178 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
r2808 r2809 27 27 28 28 29 29 30 using namespace std; 30 31 using namespace CHCDemoEngine; 31 32 32 33 34 GLuint fbo; 35 GLuint depthbuffer; 36 GLuint colorbuffer; 37 GLuint colorbuffer2; 38 GLuint depthTex; 39 GLuint colorTex; 40 GLuint colorTex2; 41 42 GLuint normalTex; 43 44 GLuint fbo2; 45 GLuint occlusionbuffer; 46 GLuint occlusionTex; 47 48 int texWidth2 = 512; 49 int texHeight2 = 512; 33 50 34 51 /// the renderable scene geometry … … 65 82 RenderQueue *renderQueue = NULL; 66 83 67 68 84 /// these values get scaled with the frame rate 69 const float keyForwardMotion = 50.0f; 70 const float keyRotation = 2.0f; 85 const static float keyForwardMotion = 50.0f; 86 const static float keyRotation = 2.0f; 87 71 88 /// elapsed time in seconds 72 89 double elapsedTime = 1.0f; 73 90 double algTime = 1.0f; 74 91 75 int winWidth = 1024;76 int winHeight = 768;77 float winAspectRatio = 1.0f;92 static int winWidth = 1024; 93 static int winHeight = 768; 94 static float winAspectRatio = 1.0f; 78 95 79 96 double accumulatedTime = 1000; 80 97 float fps = 1e3f; 98 99 float myfar = 0; 100 101 // rendertexture 102 const static int texWidth = 2048; 103 const static int texHeight = 2048; 81 104 82 105 int renderedObjects = 0; … … 98 121 99 122 // mouse navigation state 100 int xEyeBegin, yEyeBegin, yMotionBegin = 0; 101 int verticalMotionBegin, horizontalMotionBegin = 0; 123 int xEyeBegin = 0; 124 int yEyeBegin = 0; 125 int yMotionBegin = 0; 126 int verticalMotionBegin = 0; 127 int horizontalMotionBegin = 0; 102 128 103 129 bool useOptimization = false; … … 119 145 120 146 147 static Vector3 samples[32]; 148 149 150 // function forward declarations 121 151 void InitExtensions(); 122 152 void DisplayVisualization(); 123 153 void InitGLstate(); 154 void InitRenderTexture(); 155 void InitCg(); 124 156 void CleanUp(); 125 157 void SetupEyeView(); … … 152 184 153 185 void PlaceViewer(const Vector3 &oldPos); 186 void DisplayRenderTexture(); 154 187 155 188 … … 157 190 inline float KeyShift() { return keyForwardMotion * elapsedTime; } 158 191 192 void InitFBO(); 193 194 void GenerateSamples(); 195 196 void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br); 197 198 GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT}; 199 159 200 160 201 // cg stuff 161 202 static CGcontext sCgContext = NULL; 162 static CGprogram sCgDepthPeelingProgram = NULL; 163 static CGprogram sCgPassThroughProgram = NULL; 203 static CGprogram sCgLinearDepthProgram = NULL; 204 static CGprogram sCgSSAOProgram = NULL; 205 static CGprogram sCgTestProgram = NULL; 164 206 165 207 static CGprofile sCgFragmentProfile; 208 static CGprofile sCgVertexProfile; 209 166 210 static CGparameter sTextureParam; 167 static CGparameter sTexWidthParam; 168 static CGparameter sStepSizeParam; 169 211 static CGparameter sSceneTextureParam; 212 static CGparameter sNormalTextureParam; 213 214 static CGparameter sTexSizeParam; 215 static CGparameter sSamplesParam; 216 static CGparameter sEyeVecParam; 217 static CGparameter sRadiusParam; 218 219 //static CGparameter sPositionParam; 220 static CGparameter sModelViewMatrixParam; 221 static CGparameter sMaxDepthParam; 222 223 const static float radius = 1e-3f; 224 225 226 static void cgErrorCallback() 227 { 228 CGerror lastError = cgGetError(); 229 230 if(lastError) 231 { 232 printf("%s\n\n", cgGetErrorString(lastError)); 233 printf("%s\n", cgGetLastListing(sCgContext)); 234 printf("Cg error, exiting...\n"); 235 236 exit(0); 237 } 238 } 239 240 241 static void PrintGLerror(char *msg) 242 { 243 GLenum errCode; 244 const GLubyte *errStr; 245 246 if ((errCode = glGetError()) != GL_NO_ERROR) 247 { 248 errStr = gluErrorString(errCode); 249 fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg); 250 } 251 } 170 252 171 253 … … 202 284 InitExtensions(); 203 285 InitGLstate(); 286 //InitRenderTexture(); 287 InitFBO(); 204 288 205 289 LeftMotion(0, 0); … … 240 324 //bvh = bvhLoader.Load("data/city/model/city.bvh", sceneEntities); 241 325 326 myfar = 10.0f * Magnitude(bvh->GetBox().Diagonal()); 327 242 328 if (!bvh) 243 329 { … … 247 333 } 248 334 335 InitCg(); 249 336 250 337 bvh->SetCamera(camera); … … 266 353 267 354 return 0; 355 } 356 357 358 void InitCg(void) 359 { 360 GenerateSamples(); 361 362 // Setup Cg 363 cgSetErrorCallback(cgErrorCallback); 364 365 // Create cgContext. 366 sCgContext = cgCreateContext(); 367 368 // get the best profile for this hardware 369 sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT); 370 //assert(sCgFragmentProfile != CG_PROFILE_UNKNOWN); 371 cgGLSetOptimalOptions(sCgFragmentProfile); 372 373 sCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX); 374 cgGLSetOptimalOptions(sCgVertexProfile); 375 376 sCgLinearDepthProgram = 377 cgCreateProgramFromFile(sCgContext, 378 CG_SOURCE, 379 "src/lineardepth.cg", 380 sCgVertexProfile, 381 "main", 382 NULL); 383 384 if(sCgLinearDepthProgram != NULL) 385 { 386 cgGLLoadProgram(sCgLinearDepthProgram); 387 388 sModelViewMatrixParam = cgGetNamedParameter(sCgLinearDepthProgram, "ModelViewProj"); 389 } 390 391 sCgTestProgram = 392 cgCreateProgramFromFile(sCgContext, 393 CG_SOURCE, 394 "src/test2.cg", 395 sCgFragmentProfile, 396 "main", 397 NULL); 398 399 if(sCgTestProgram != NULL) 400 { 401 cgGLLoadProgram(sCgTestProgram); 402 403 sMaxDepthParam = cgGetNamedParameter(sCgTestProgram, "maxDepth"); 404 cgGLSetParameter1f(sMaxDepthParam, 1.0f / myfar); 405 } 406 else 407 cerr << "test program failed to load" << endl; 408 409 PrintGLerror("test"); 410 411 412 /////////////// 413 414 sCgSSAOProgram = 415 cgCreateProgramFromFile(sCgContext, 416 CG_SOURCE, 417 "src/ssao.cg", 418 //"src/test.cg", 419 sCgFragmentProfile, 420 NULL, 421 NULL); 422 423 if(sCgSSAOProgram != NULL) 424 { 425 cgGLLoadProgram(sCgSSAOProgram); 426 427 // we need size of texture for scaling 428 sTextureParam = cgGetNamedParameter(sCgSSAOProgram, "lindepth"); 429 sSceneTextureParam = cgGetNamedParameter(sCgSSAOProgram, "scene"); 430 sNormalTextureParam = cgGetNamedParameter(sCgSSAOProgram, "normal"); 431 sTexSizeParam = cgGetNamedParameter(sCgSSAOProgram, "invTexSize"); 432 sRadiusParam = cgGetNamedParameter(sCgSSAOProgram, "radius"); 433 sEyeVecParam = cgGetNamedParameter(sCgSSAOProgram, "eyevec"); 434 sSamplesParam = cgGetNamedParameter(sCgSSAOProgram, "samples"); 435 436 cgGLSetParameter1f(sTexSizeParam, 1.0f / (float)texWidth); 437 cgGLSetParameter1f(sRadiusParam, radius); 438 cgGLSetParameterArray3f(sSamplesParam, 0, 32, (const float *)samples); 439 } 440 else 441 cerr << "ssao program failed to load" << endl; 442 443 PrintGLerror("init"); 444 } 445 446 447 void PrintFBOStatus(GLenum status) 448 { 449 switch(status) 450 { 451 case GL_FRAMEBUFFER_COMPLETE_EXT: 452 cout << "frame buffer object created successfully" << endl; 453 break; 454 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: 455 cerr << "incomplete attachment" << endl; 456 break; 457 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: 458 cerr << "missing attachment" << endl; 459 break; 460 case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: 461 cerr << "incomplete dimensions" << endl; 462 break; 463 case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: 464 cerr << "incomplete formats" << endl; 465 break; 466 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: 467 cerr << "incomplete draw buffer" << endl; 468 break; 469 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: 470 cerr << "incomplete read buffer" << endl; 471 break; 472 case GL_FRAMEBUFFER_UNSUPPORTED_EXT: 473 cerr << "framebuffer unsupported" << endl; 474 break; 475 default: 476 cerr << "unknown status code " << status << endl; 477 } 478 } 479 480 481 void InitFBO() 482 { 483 glGenFramebuffersEXT(1, &fbo); 484 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); 485 486 // create color buffer 487 int samples = 8; 488 489 glGenRenderbuffersEXT(1, &colorbuffer); 490 //glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA16F_ARB, texWidth, texHeight); 491 glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, samples, GL_FLOAT_RGBA_NV, texWidth, texHeight); 492 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorbuffer); 493 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorbuffer); 494 495 glGenTextures(1, &colorTex); 496 glBindTexture(GL_TEXTURE_2D, colorTex); 497 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL); 498 glGenerateMipmapEXT(GL_TEXTURE_2D); 499 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorTex, 0); 500 501 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 502 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 503 504 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 505 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 506 507 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); 508 509 510 glGenRenderbuffersEXT(1, &colorbuffer2); 511 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA16F_ARB, texWidth, texHeight); 512 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorbuffer2); 513 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_RENDERBUFFER_EXT, colorbuffer2); 514 515 glGenTextures(1, &colorTex2); 516 glBindTexture(GL_TEXTURE_2D, colorTex2); 517 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL); 518 glGenerateMipmapEXT(GL_TEXTURE_2D); 519 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, colorTex2, 0); 520 521 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 522 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 523 524 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 525 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 526 527 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); 528 529 /////////// 530 // create depth buffer 531 532 glGenRenderbuffersEXT(1, &depthbuffer); 533 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthbuffer); 534 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, texWidth, texHeight); 535 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthbuffer); 536 537 538 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); 539 540 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 541 542 543 //////////////// 544 //-- second fbo 545 546 /*glGenFramebuffersEXT(1, &fbo2); 547 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2); 548 549 // create color buffer 550 glGenRenderbuffersEXT(1, &occlusionbuffer); 551 //glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA16F_ARB, texWidth, texHeight); 552 glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, samples, GL_FLOAT_RGBA_NV, texWidth2, texHeight2); 553 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, occlusionbuffer); 554 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, occlusionbuffer); 555 556 glGenTextures(1, &occlusionTex); 557 glBindTexture(GL_TEXTURE_2D, occlusionTex); 558 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, texWidth2, texHeight2, 0, GL_RGBA, GL_FLOAT, NULL); 559 glGenerateMipmapEXT(GL_TEXTURE_2D); 560 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorTex, 0); 561 562 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); 563 564 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);*/ 268 565 } 269 566 … … 470 767 glLoadIdentity(); 471 768 472 gluPerspective(fov, 1.0f / winAspectRatio, nearDist, 10.0f * Magnitude(bvh->GetBox().Diagonal()));769 gluPerspective(fov, 1.0f / winAspectRatio, nearDist, myfar); 473 770 474 771 glMatrixMode(GL_MODELVIEW); … … 535 832 algTimer.Start(); 536 833 834 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); 835 836 glPushAttrib(GL_VIEWPORT_BIT); 837 glViewport(0, 0, texWidth, texHeight); 838 839 cgGLEnableProfile(sCgFragmentProfile); 840 cgGLBindProgram(sCgTestProgram); 841 842 cgGLEnableProfile(sCgVertexProfile); 843 cgGLBindProgram(sCgLinearDepthProgram); 844 845 glDrawBuffers(2, mrt); 846 537 847 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 538 848 … … 556 866 SetupEyeView(); 557 867 868 Vector3 dir = camera->GetDirection(); 869 cgGLSetParameter3f(sEyeVecParam, dir.x, dir.y, dir.z); 870 871 // set modelview matrix for shaders 872 cgGLSetStateMatrixParameter(sModelViewMatrixParam, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY); 873 cgGLEnableProfile(sCgVertexProfile); 874 558 875 // actually render the scene geometry using one of the specified algorithms 559 876 traverser->RenderScene(); … … 561 878 562 879 ////// 563 //-- otherrendering880 //-- do the rest of the rendering 564 881 565 882 glEnableClientState(GL_VERTEX_ARRAY); … … 583 900 glDisableClientState(GL_VERTEX_ARRAY); 584 901 glDisableClientState(GL_NORMAL_ARRAY); 902 903 904 cgGLDisableProfile(sCgVertexProfile); 905 cgGLDisableProfile(sCgFragmentProfile); 906 907 glPopAttrib(); 908 909 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 910 911 DisplayRenderTexture(); 585 912 586 913 if (useGlFinish) glFinish(); … … 836 1163 glLoadIdentity(); 837 1164 838 gluPerspective(fov, 1.0f / winAspectRatio, nearDist, 10.0f * Magnitude(bvh->GetBox().Diagonal()));1165 gluPerspective(fov, 1.0f / winAspectRatio, nearDist, myfar); 839 1166 840 1167 glMatrixMode(GL_MODELVIEW); … … 1071 1398 DEL_PTR(loader); 1072 1399 DEL_PTR(renderQueue); 1400 1401 if (sCgLinearDepthProgram) 1402 cgDestroyProgram(sCgLinearDepthProgram); 1403 if (sCgSSAOProgram) 1404 cgDestroyProgram(sCgSSAOProgram); 1405 if (sCgContext) 1406 cgDestroyContext(sCgContext); 1073 1407 } 1074 1408 … … 1232 1566 } 1233 1567 } 1568 1569 1570 void DisplayRenderTexture2() 1571 { 1572 glEnable(GL_TEXTURE_2D); 1573 glBindTexture(GL_TEXTURE_2D, occlusionTex); 1574 1575 glDisable(GL_LIGHTING); 1576 1577 glMatrixMode(GL_PROJECTION); 1578 glPushMatrix(); 1579 glLoadIdentity(); 1580 1581 glMatrixMode(GL_MODELVIEW); 1582 glPushMatrix(); 1583 glLoadIdentity(); 1584 1585 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2); 1586 glPushAttrib(GL_VIEWPORT_BIT); 1587 glViewport(0, 0, texWidth2, texHeight2); 1588 1589 glOrtho(-0.5f, 0.5f, -0.5f, 0.5f, 0, 1); 1590 1591 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1592 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1593 1594 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1595 1596 glColor3f(1.0f, 1.0f, 1.0f); 1597 1598 glBegin(GL_QUADS); 1599 glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f); 1600 glTexCoord2f(1, 0); glVertex3f( 1, -1, -0.5f); 1601 glTexCoord2f(1, 1); glVertex3f( 1, 1, -0.5f); 1602 glTexCoord2f(0, 1); glVertex3f(-1, 1, -0.5f); 1603 glEnd(); 1604 1605 glEnable(GL_LIGHTING); 1606 glDisable(GL_TEXTURE_2D); 1607 1608 glMatrixMode(GL_PROJECTION); 1609 glPopMatrix(); 1610 1611 glMatrixMode(GL_MODELVIEW); 1612 glPopMatrix(); 1613 1614 glPopAttrib(); 1615 glBindTexture(GL_TEXTURE_2D, 0); 1616 1617 PrintGLerror("displaytexture2"); 1618 } 1619 1620 void DisplayRenderTexture() 1621 { 1622 glDisable(GL_TEXTURE_2D); 1623 glDisable(GL_LIGHTING); 1624 1625 glMatrixMode(GL_PROJECTION); 1626 glPushMatrix(); 1627 glLoadIdentity(); 1628 1629 glMatrixMode(GL_MODELVIEW); 1630 glPushMatrix(); 1631 glLoadIdentity(); 1632 1633 /*glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2); 1634 glPushAttrib(GL_VIEWPORT_BIT); 1635 glViewport(0, 0, texWidth2, texHeight2); 1636 */ 1637 glOrtho(-0.5f, 0.5f, -0.5f, 0.5f, 0, 1); 1638 1639 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1640 1641 cgGLEnableProfile(sCgFragmentProfile); 1642 cgGLBindProgram(sCgSSAOProgram); 1643 1644 cgGLEnableTextureParameter(sTextureParam); 1645 cgGLSetTextureParameter(sTextureParam, colorTex2); 1646 1647 cgGLEnableTextureParameter(sSceneTextureParam); 1648 cgGLSetTextureParameter(sSceneTextureParam, colorTex); 1649 1650 cgGLEnableTextureParameter(sNormalTextureParam); 1651 cgGLSetTextureParameter(sNormalTextureParam, normalTex); 1652 1653 Vector3 tl, tr, bl, br; 1654 ComputeViewVectors(tl, tr, bl, br); 1655 1656 glColor3f(1.0f, 1.0f, 1.0f); 1657 1658 glBegin(GL_QUADS); 1659 #if 1 1660 glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f); 1661 glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( 1, -1, -0.5f); 1662 glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( 1, 1, -0.5f); 1663 glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-1, 1, -0.5f); 1664 #else 1665 glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f); 1666 glTexCoord2f(1, 0); glVertex3f( 1, -1, -0.5f); 1667 glTexCoord2f(1, 1); glVertex3f( 1, 1, -0.5f); 1668 glTexCoord2f(0, 1); glVertex3f(-1, 1, -0.5f); 1669 #endif 1670 glEnd(); 1671 1672 cgGLDisableTextureParameter(sTextureParam); 1673 cgGLDisableTextureParameter(sSceneTextureParam); 1674 cgGLDisableProfile(sCgFragmentProfile); 1675 1676 glEnable(GL_LIGHTING); 1677 glDisable(GL_TEXTURE_2D); 1678 1679 glMatrixMode(GL_PROJECTION); 1680 glPopMatrix(); 1681 1682 glMatrixMode(GL_MODELVIEW); 1683 glPopMatrix(); 1684 1685 //glPopAttrib(); 1686 //glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2); 1687 1688 PrintGLerror("displaytexture"); 1689 //DisplayRenderTexture2(); 1690 } 1691 1692 1693 1694 void GenerateSamples() 1695 { 1696 // fill an n * n * 2 array with uniformly distributed spherical samples 1697 for (int i = 0; i < 32; ++ i) 1698 { 1699 // create stratified samples over sphere 1700 const float rx = RandomValue(0, 1); 1701 const float ry = RandomValue(0, 1); 1702 //const float l = RandomValue(0, 1); 1703 1704 const float theta = 2.0f * acos(sqrt(1.0f - rx)); 1705 const float phi = 2.0f * M_PI * ry; 1706 1707 samples[i] = Vector3(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta)); 1708 cout << samples[i] << endl; 1709 } 1710 } 1711 1712 1713 void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br) 1714 { 1715 float myfov = fov * M_PI / 180.0f; 1716 const float h_far = 2.0f * tan(myfov / 2.0f); 1717 const float w_far = h_far * texWidth / texHeight; 1718 1719 float t1 = h_far * 0.5f; 1720 float t2 = w_far * 0.5f; 1721 1722 bl = Normalize(Vector3(-t1, -t2, 1.0f)); 1723 br = Normalize(Vector3( t1, -t2, 1.0f)); 1724 tl = Normalize(Vector3(-t1, t2, 1.0f)); 1725 tr = Normalize(Vector3( t1, t2, 1.0f)); 1726 1727 // normalize to 0 .. 1 1728 bl = bl * 0.5f + 0.5f; 1729 br = br * 0.5f + 0.5f; 1730 tl = tl * 0.5f + 0.5f; 1731 tr = tr * 0.5f + 0.5f; 1732 1733 /* 1734 cout << "tl: " << tl << endl; 1735 cout << "tr: " << tr << endl; 1736 cout << "bl: " << bl << endl; 1737 cout << "br: " << bl << endl << endl;*/ 1738 } 1739 1740 1741 void CreateNormalMap() 1742 { 1743 Vector3 *data = new Vector3[texWidth * texHeight]; 1744 1745 for (int i = 0; i < texWidth * texHeight; ++ i) 1746 { 1747 // create random samples over sphere 1748 const float rx = RandomValue(0, 1); 1749 const float ry = RandomValue(0, 1); 1750 1751 const float theta = 2.0f * acos(sqrt(1.0f - rx)); 1752 const float phi = 2.0f * M_PI * ry; 1753 1754 data[i] = Vector3(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta)); 1755 data[i] += 1.0f; 1756 data[i] *= 0.5f; 1757 } 1758 1759 glGenTextures(1, &normalTex); 1760 glBindTexture(GL_TEXTURE_2D, normalTex); 1761 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, data); 1762 }
Note: See TracChangeset
for help on using the changeset viewer.