Changeset 2861 for GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
- Timestamp:
- 08/22/08 13:20:06 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
r2860 r2861 33 33 #include "SampleGenerator.h" 34 34 #include "FrameBufferObject.h" 35 #include "SsaoShader.h" 36 #include "DeferredShader.h" 35 37 36 38 … … 45 47 //-- fbos 46 48 47 GLuint fbo; 48 GLuint fbo1; 49 GLuint fbo2; 49 FrameBufferObject *fbo; 50 50 51 51 bool isFirstTexture = true; 52 53 /////////////54 //-- renderbuffers55 56 57 GLuint depthBuffer;58 59 GLuint positionsBuffer;60 GLuint colorsBuffer;61 GLuint colorsBuffer1;62 GLuint colorsBuffer2;63 GLuint normalsBuffer;64 65 66 /////////////67 //-- textures68 69 70 GLuint positionsTex;71 GLuint colorsTex;72 GLuint colorsTex1;73 GLuint colorsTex2;74 75 GLuint normalsTex;76 77 GLuint noiseTex;78 52 79 53 GLuint fontTex; … … 177 151 178 152 bool useSsao = false; 153 static float ssaoExpFactor = 0.1f; 179 154 180 155 bool showAlgorithmTime = false; … … 190 165 bool useFullScreen = false; 191 166 192 // exp factor for ssao193 float expFactor = 0.05f;194 195 // ssao number of samples196 //#define NUM_SAMPLES 8197 #define NUM_SAMPLES 16198 199 // ssao random spherical samples200 static float samples[NUM_SAMPLES * 2];201 202 167 static Matrix4x4 matProjectionView = IdentityMatrix(); 203 168 204 FrameBufferObject *myfbo = NULL;205 169 206 170 … … 241 205 242 206 void PlaceViewer(const Vector3 &oldPos); 243 void DisplayRenderTexture();244 207 245 208 … … 249 212 void InitFBO(); 250 213 251 void CreateNoiseTex2D(); 252 253 void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br); 254 255 void GenerateSamples(); 256 214 SsaoShader *ssaoShader = NULL; 215 DeferredShader *deferredShader = NULL; 257 216 258 217 GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT}; … … 264 223 static CGcontext sCgContext = NULL; 265 224 static CGprogram sCgMrtVertexProgram = NULL; 266 static CGprogram sCgSsaoProgram = NULL;267 static CGprogram sCgDeferredProgram = NULL;268 269 static CGparameter sColorsTexParam;270 static CGparameter sPositionsTexParam;271 static CGparameter sNormalsTexParam;272 273 static CGparameter sColorsTexParamSsao;274 static CGparameter sPositionsTexParamSsao;275 static CGparameter sNormalsTexParamSsao;276 static CGparameter sNoiseTexParamSsao;277 225 278 226 static CGparameter sModelViewProjMatrixParam; 279 static CGparameter sOldModelViewProjMatrixParam;280 227 static CGparameter sMaxDepthParam; 281 static CGparameter sMaxDepthParamSsao;282 228 static CGparameter sMaxDepthParamTex; 283 229 284 static CGparameter sSamplesParamSsao; 285 static CGparameter sOldTexParamSsao; 286 static CGparameter sNoiseMultiplierParam; 287 static CGparameter sExpFactorParamSsao; 230 static Matrix4x4 oldViewProjMatrix; 288 231 289 232 … … 296 239 printf("%s\n\n", cgGetErrorString(lastError)); 297 240 printf("%s\n", cgGetLastListing(sCgContext)); 241 298 242 printf("Cg error, exiting...\n"); 299 243 … … 343 287 344 288 env.GetBoolParam(string("useFullScreen"), useFullScreen); 345 env.GetFloatParam(string("expFactor"), expFactor);289 env.GetFloatParam(string("expFactor"), ssaoExpFactor); 346 290 env.GetVectorParam(string("camPosition"), camPos); 347 291 env.GetVectorParam(string("camDirection"), camDir); … … 360 304 cout << "useFullScreen: " << useFullScreen << endl; 361 305 cout << "camPosition: " << camPos << endl; 362 cout << "expFactor: " << expFactor << endl;306 cout << "expFactor: " << ssaoExpFactor << endl; 363 307 //cout << "model path: " << model_path << endl; 364 308 } … … 466 410 InitCg(); 467 411 468 // create noise texture for ssao 469 CreateNoiseTex2D(); 412 SsaoShader::Init(sCgContext); 413 DeferredShader::Init(sCgContext); 414 415 ssaoShader = new SsaoShader(texWidth, texHeight, camera, myfar / 10.0f); 416 deferredShader = new DeferredShader(texWidth, texHeight); 470 417 471 418 // initialize the render traverser … … 499 446 // get the best profile for this hardware 500 447 RenderState::sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT); 501 //assert(sCgFragmentProfile != CG_PROFILE_UNKNOWN);502 448 cgGLSetOptimalOptions(RenderState::sCgFragmentProfile); 503 449 … … 564 510 cerr << "fragment program failed to load" << endl; 565 511 566 PrintGLerror("test");567 568 569 ///////////////570 571 sCgSsaoProgram =572 cgCreateProgramFromFile(sCgContext,573 CG_SOURCE,574 "src/shaders/deferred.cg",575 RenderState::sCgFragmentProfile,576 "main_ssao",577 NULL);578 579 if (sCgSsaoProgram != NULL)580 {581 cgGLLoadProgram(sCgSsaoProgram);582 583 // we need size of texture for scaling584 sPositionsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "positions");585 sColorsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "colors");586 sNormalsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "normals");587 sNoiseTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");588 sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier");589 sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");590 sMaxDepthParamSsao = cgGetNamedParameter(sCgSsaoProgram, "maxDepth");591 sExpFactorParamSsao = cgGetNamedParameter(sCgSsaoProgram, "expFactor");592 593 cgGLSetParameter1f(sMaxDepthParamSsao, myfar / 10.0f);594 cgGLSetParameter1f(sExpFactorParamSsao, expFactor);595 596 sSamplesParamSsao = cgGetNamedParameter(sCgSsaoProgram, "samples");597 sOldTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "oldTex");598 599 GenerateSamples(); cgGLSetParameterArray2f(sSamplesParamSsao, 0, NUM_SAMPLES, (const float *)samples);600 }601 else602 cerr << "ssao program failed to load" << endl;603 604 605 sCgDeferredProgram =606 cgCreateProgramFromFile(sCgContext,607 CG_SOURCE,608 "src/shaders/deferred.cg",609 RenderState::sCgFragmentProfile,610 "main",611 NULL);612 613 if (sCgDeferredProgram != NULL)614 {615 cgGLLoadProgram(sCgDeferredProgram);616 617 // we need size of texture for scaling618 sPositionsTexParam = cgGetNamedParameter(sCgDeferredProgram, "positions");619 sColorsTexParam = cgGetNamedParameter(sCgDeferredProgram, "colors");620 sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals");621 }622 else623 cerr << "deferred program failed to load" << endl;624 625 626 512 PrintGLerror("init"); 627 513 628 514 cout << "cg initialization successful" << endl; 629 }630 631 632 void PrintFBOStatus(GLenum status)633 {634 switch(status)635 {636 case GL_FRAMEBUFFER_COMPLETE_EXT:637 cout << "frame buffer object created successfully" << endl;638 break;639 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:640 cerr << "incomplete attachment" << endl;641 break;642 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:643 cerr << "missing attachment" << endl;644 break;645 case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:646 cerr << "incomplete dimensions" << endl;647 break;648 case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:649 cerr << "incomplete formats" << endl;650 break;651 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:652 cerr << "incomplete draw buffer" << endl;653 break;654 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:655 cerr << "incomplete read buffer" << endl;656 break;657 case GL_FRAMEBUFFER_UNSUPPORTED_EXT:658 cerr << "framebuffer unsupported" << endl;659 break;660 default:661 cerr << "unknown status code " << status << endl;662 }663 515 } 664 516 … … 668 520 // this fbo basicly stores the scene information we get from standard rendering of a frame 669 521 // we store colors, normals, positions (for the ssao) 670 fbo = new FrameBufferObject(texWidth, texHeight, true, FrameBufferObject::DEPTH_24); 671 522 fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_24); 672 523 673 524 // the diffuse color buffer 674 525 fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false, false); 675 676 526 // the positions buffer 677 527 fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false, false); 678 679 528 // the normals buffer 680 529 fbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false, false); 681 682 683 684 ////////////////////////685 686 glGenFramebuffersEXT(1, &fbo);687 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);688 689 690 ////////////691 //-- colors buffer692 693 glGenRenderbuffersEXT(1, &colorsBuffer);694 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer);695 696 #if 1697 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight);698 #else699 int samples = 8;700 glEnable(GL_MULTISAMPLE_ARB);701 glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, samples, GL_RGBA8, texWidth, texHeight);702 #endif703 704 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer);705 706 glGenTextures(1, &colorsTex);707 glBindTexture(GL_TEXTURE_2D, colorsTex);708 709 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);710 //glGenerateMipmapEXT(GL_TEXTURE_2D);711 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex, 0);712 713 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);714 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);715 716 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);717 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);718 719 720 //////////721 //-- positions buffer722 723 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));724 glGenRenderbuffersEXT(1, &positionsBuffer);725 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, positionsBuffer);726 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight);727 728 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_RENDERBUFFER_EXT, positionsBuffer);729 730 glGenTextures(1, &positionsTex);731 glBindTexture(GL_TEXTURE_2D, positionsTex);732 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);733 //glGenerateMipmapEXT(GL_TEXTURE_2D);734 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, positionsTex, 0);735 736 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);737 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);738 739 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);740 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);741 742 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));743 744 745 ////////746 //-- normals buffer747 748 glGenRenderbuffersEXT(1, &normalsBuffer);749 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, normalsBuffer);750 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, texWidth, texHeight);751 752 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_RENDERBUFFER_EXT, normalsBuffer);753 754 glGenTextures(1, &normalsTex);755 glBindTexture(GL_TEXTURE_2D, normalsTex);756 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);757 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_2D, normalsTex, 0);758 759 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);760 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);761 762 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);763 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);764 765 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));766 767 768 ///////////769 //-- create depth buffer770 771 glGenRenderbuffersEXT(1, &depthBuffer);772 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthBuffer);773 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, texWidth, texHeight);774 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthBuffer);775 776 777 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));778 779 780 781 //////////////////////////////////782 783 784 785 glGenFramebuffersEXT(1, &fbo1);786 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo1);787 788 789 ////////////790 //-- colors buffer791 792 glGenRenderbuffersEXT(1, &colorsBuffer1);793 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer1);794 795 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight);796 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer1);797 798 glGenTextures(1, &colorsTex1);799 glBindTexture(GL_TEXTURE_2D, colorsTex1);800 801 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);802 //glGenerateMipmapEXT(GL_TEXTURE_2D);803 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex1, 0);804 805 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);806 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);807 808 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);809 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);810 811 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);812 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);813 814 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));815 816 817 818 /////////////////////////819 820 glGenFramebuffersEXT(1, &fbo2);821 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2);822 823 824 ////////////825 //-- colors buffer826 827 glGenRenderbuffersEXT(1, &colorsBuffer2);828 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer2);829 830 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight);831 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer2);832 833 glGenTextures(1, &colorsTex2);834 glBindTexture(GL_TEXTURE_2D, colorsTex2);835 836 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);837 //glGenerateMipmapEXT(GL_TEXTURE_2D);838 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex2, 0);839 840 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);841 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);842 843 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);844 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);845 846 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);847 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);848 849 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);850 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);851 852 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));853 854 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);855 530 856 531 PrintGLerror("fbo"); … … 1073 748 Matrix4x4 matViewing, matProjection; 1074 749 1075 if (renderType == RenderState::DEFERRED) 1076 { 1077 cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)matProjectionView.x); 1078 } 750 // store matrix of last frame 751 oldViewProjMatrix = matProjectionView; 1079 752 1080 753 glMatrixMode(GL_PROJECTION); … … 1170 843 //glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB); 1171 844 1172 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);845 fbo->Release(); 1173 846 1174 847 state.SetRenderType(RenderState::FIXED); … … 1189 862 //glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB); 1190 863 1191 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);864 fbo->Release(); 1192 865 1193 866 cgGLDisableProfile(RenderState::sCgFragmentProfile); … … 1215 888 state.SetRenderType(RenderState::DEFERRED); 1216 889 1217 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);890 fbo->Bind(); 1218 891 1219 892 glPushAttrib(GL_VIEWPORT_BIT); … … 1226 899 cgGLBindProgram(sCgMrtVertexProgram); 1227 900 901 /// draw to 3 color buffers 1228 902 glDrawBuffers(3, mrt); 1229 903 … … 1240 914 1241 915 916 // reset lod levels for current frame 1242 917 LODLevel::InitFrame(camera->GetPosition()); 1243 918 … … 1279 954 if (renderType == RenderState::DEFERRED) 1280 955 { 1281 //glPopAttrib(); 1282 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 956 fbo->Release(); 1283 957 1284 958 cgGLDisableProfile(RenderState::sCgVertexProfile); … … 1287 961 glDrawBuffers(1, mrt); 1288 962 1289 DisplayRenderTexture(); 1290 } 1291 963 if (useSsao) 964 ssaoShader->Render(fbo, oldViewProjMatrix, ssaoExpFactor); 965 else 966 deferredShader->Render(fbo); 967 } 1292 968 1293 969 /////////// … … 1379 1055 break; 1380 1056 case '7': 1381 expFactor *= 0.5f;1057 ssaoExpFactor *= 0.5f; 1382 1058 break; 1383 1059 case '8': 1384 expFactor *= 2.0f;1385 if ( expFactor > 1.0f) expFactor = 1.0f;1060 ssaoExpFactor *= 2.0f; 1061 if (ssaoExpFactor > 1.0f) ssaoExpFactor = 1.0f; 1386 1062 break; 1387 1063 case 'o': … … 1819 1495 if (RenderState::sCgMrtFragmentTexProgram) 1820 1496 cgDestroyProgram(RenderState::sCgMrtFragmentTexProgram); 1821 if (sCgSsaoProgram) 1822 cgDestroyProgram(sCgSsaoProgram); 1823 if (sCgDeferredProgram) 1824 cgDestroyProgram(sCgDeferredProgram); 1497 1825 1498 if (sCgContext) 1826 1499 cgDestroyContext(sCgContext); 1827 1828 glDeleteFramebuffersEXT(1, &fbo);1829 glDeleteRenderbuffersEXT(1, &depthBuffer);1830 glDeleteRenderbuffersEXT(1, &colorsBuffer);1831 glDeleteRenderbuffersEXT(1, &normalsBuffer);1832 glDeleteRenderbuffersEXT(1, &positionsBuffer);1833 1834 glDeleteTextures(1, &colorsTex);1835 glDeleteTextures(1, &normalsTex);1836 glDeleteTextures(1, &positionsTex);1837 glDeleteTextures(1, &noiseTex);1838 glDeleteTextures(1, &fontTex);1839 1500 } 1840 1501 … … 2065 1726 } 2066 1727 } 2067 2068 2069 void DisplayRenderTexture3()2070 {2071 glEnable(GL_TEXTURE_2D);2072 2073 if (isFirstTexture)2074 glBindTexture(GL_TEXTURE_2D, colorsTex1);2075 else2076 glBindTexture(GL_TEXTURE_2D, colorsTex2);2077 2078 glDisable(GL_LIGHTING);2079 2080 glMatrixMode(GL_PROJECTION);2081 glPushMatrix();2082 glLoadIdentity();2083 2084 glMatrixMode(GL_MODELVIEW);2085 glPushMatrix();2086 glLoadIdentity();2087 2088 const float offs = 0.5f;2089 glOrtho(-offs, offs, -offs, offs, 0, 1);2090 2091 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);2092 2093 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);2094 glBegin(GL_QUADS);2095 2096 glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);2097 glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);2098 glTexCoord2f(1, 1); glVertex3f( offs, offs, -0.5f);2099 glTexCoord2f(0, 1); glVertex3f(-offs, offs, -0.5f);2100 2101 glEnd();2102 2103 glEnable(GL_LIGHTING);2104 glDisable(GL_TEXTURE_2D);2105 2106 glMatrixMode(GL_PROJECTION);2107 glPopMatrix();2108 2109 glMatrixMode(GL_MODELVIEW);2110 glPopMatrix();2111 2112 PrintGLerror("displaytexture3");2113 }2114 2115 2116 void DisplayRenderTexture()2117 {2118 GLuint oldTex;2119 2120 if (isFirstTexture)2121 {2122 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo1);2123 oldTex = colorsTex2;2124 }2125 else2126 {2127 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2);2128 oldTex = colorsTex1;2129 }2130 2131 glPushAttrib(GL_VIEWPORT_BIT);2132 glViewport(0, 0, texWidth, texHeight);2133 2134 glDrawBuffers(1, mrt);2135 2136 glDisable(GL_ALPHA_TEST);2137 glDisable(GL_TEXTURE_2D);2138 glDisable(GL_LIGHTING);2139 2140 glMatrixMode(GL_PROJECTION);2141 glPushMatrix();2142 glLoadIdentity();2143 2144 glMatrixMode(GL_MODELVIEW);2145 glPushMatrix();2146 glLoadIdentity();2147 2148 const float offs = 0.5f;2149 2150 glOrtho(-offs, offs, -offs, offs, 0, 1);2151 2152 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);2153 2154 cgGLEnableProfile(RenderState::sCgFragmentProfile);2155 2156 if (useSsao)2157 {2158 cgGLBindProgram(sCgSsaoProgram);2159 2160 cgGLSetTextureParameter(sPositionsTexParamSsao, positionsTex);2161 cgGLEnableTextureParameter(sPositionsTexParamSsao);2162 2163 cgGLSetTextureParameter(sColorsTexParamSsao, colorsTex);2164 cgGLEnableTextureParameter(sColorsTexParamSsao);2165 2166 cgGLSetTextureParameter(sNormalsTexParamSsao, normalsTex);2167 cgGLEnableTextureParameter(sNormalsTexParamSsao);2168 2169 cgGLSetTextureParameter(sNoiseTexParamSsao, noiseTex);2170 cgGLEnableTextureParameter(sNoiseTexParamSsao);2171 2172 cgGLSetTextureParameter(sOldTexParamSsao, oldTex);2173 cgGLEnableTextureParameter(sOldTexParamSsao);2174 2175 cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));2176 cgGLSetParameter1f(sExpFactorParamSsao, expFactor);2177 2178 GenerateSamples(); cgGLSetParameterArray2f(sSamplesParamSsao, 0, NUM_SAMPLES, (const float *)samples);2179 }2180 else2181 {2182 cgGLBindProgram(sCgDeferredProgram);2183 2184 cgGLSetTextureParameter(sPositionsTexParam, positionsTex);2185 cgGLEnableTextureParameter(sPositionsTexParam);2186 2187 cgGLSetTextureParameter(sColorsTexParam, colorsTex);2188 cgGLEnableTextureParameter(sColorsTexParam);2189 2190 cgGLSetTextureParameter(sNormalsTexParam, normalsTex);2191 cgGLEnableTextureParameter(sNormalsTexParam);2192 }2193 2194 Vector3 tl, tr, bl, br;2195 ComputeViewVectors(tl, tr, bl, br);2196 2197 glColor3f(1.0f, 1.0f, 1.0f);2198 2199 glBegin(GL_QUADS);2200 2201 // note: slightly larger texture hides ambient occlusion error on border but costs resolution2202 //float offs2 = 0.55f;2203 float offs2 = 0.5f;2204 2205 glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);2206 glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);2207 glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);2208 glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);2209 2210 glEnd();2211 2212 2213 if (useSsao)2214 {2215 cgGLDisableTextureParameter(sColorsTexParamSsao);2216 cgGLDisableTextureParameter(sPositionsTexParamSsao);2217 cgGLDisableTextureParameter(sNormalsTexParamSsao);2218 cgGLDisableTextureParameter(sNoiseTexParamSsao);2219 cgGLDisableTextureParameter(sOldTexParamSsao);2220 }2221 else2222 {2223 cgGLDisableTextureParameter(sColorsTexParam);2224 cgGLDisableTextureParameter(sPositionsTexParam);2225 cgGLDisableTextureParameter(sNormalsTexParam);2226 }2227 2228 cgGLDisableProfile(RenderState::sCgFragmentProfile);2229 2230 glEnable(GL_LIGHTING);2231 glDisable(GL_TEXTURE_2D);2232 2233 glMatrixMode(GL_PROJECTION);2234 glPopMatrix();2235 2236 glMatrixMode(GL_MODELVIEW);2237 glPopMatrix();2238 2239 glPopAttrib();2240 2241 PrintGLerror("displaytexture");2242 2243 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);2244 2245 DisplayRenderTexture3();2246 }2247 2248 #if 02249 // kustls magic sample positions2250 void GenerateSamples()2251 {2252 static const float mysamples[] =2253 {2254 -0.326212f, -0.405805f,2255 -0.840144f, -0.07358f,2256 -0.695914f, 0.457137f,2257 -0.203345f, 0.620716,2258 0.96234f, -0.194983f,2259 0.473434f, -0.480026f,2260 0.519456, 0.767022f,2261 0.185461f, -0.893124f,2262 0.507431f, 0.064425f,2263 0.89642f, 0.412458f,2264 -0.32194f, -0.932615f,2265 -0.791559f, -0.597705f,2266 0.326212f, 0.405805f,2267 0.840144f, 0.07358f,2268 0.695914f, -0.457137f,2269 0.203345f, -0.620716,2270 -0.96234f, 0.194983f,2271 -0.473434f, 0.480026f,2272 -0.519456, -0.767022f,2273 -0.185461f, 0.893124f,2274 -0.507431f, -0.064425f,2275 -0.89642f, -0.412458f,2276 0.32194f, 0.932615f,2277 0.791559f, 0.597705f2278 };2279 2280 for (int i = 0; i < NUM_SAMPLES; ++ i)2281 {2282 samples[i] = mysamples[i];2283 }2284 }2285 2286 #else2287 2288 2289 void GenerateSamples()2290 {2291 /*static HaltonSequence halton;2292 2293 float r[2];2294 2295 // generates poisson distribution on disc2296 float minDist = 2.0f / sqrt((float)NUM_SAMPLES);2297 2298 //cout << "minDist before= " << minDist << endl;2299 2300 for (int i = 0; i < NUM_SAMPLES * 2; i += 2)2301 {2302 int tries = 0, totalTries = 0;2303 2304 // repeat until valid sample was found2305 while (1)2306 {2307 ++ tries;2308 ++ totalTries;2309 2310 halton.GetNext(2, r);2311 2312 //const float rx = RandomValue(-1, 1);2313 //const float ry = RandomValue(-1, 1);2314 2315 const float rx = r[0] * 2.0f - 1.0f;2316 const float ry = r[1] * 2.0f - 1.0f;2317 2318 // check if in disk, else exit early2319 if (rx * rx + ry * ry > 1)2320 continue;2321 2322 bool sampleValid = true;2323 2324 // check poisson property2325 for (int j = 0; ((j < i) && sampleValid); j += 2)2326 {2327 const float dist =2328 sqrt((samples[j] - rx) * (samples[j] - rx) +2329 (samples[j + 1] - ry) * (samples[j + 1] - ry));2330 2331 if (dist < minDist)2332 sampleValid = false;2333 }2334 2335 if (sampleValid)2336 {2337 samples[i] = rx;2338 samples[i + 1]= ry;2339 break;2340 }2341 2342 if (tries > 2000)2343 {2344 minDist *= 0.9f;2345 tries = 0;2346 }2347 }2348 }2349 */2350 2351 static PoissonDiscSampleGenerator poisson(NUM_SAMPLES, 1.0f);2352 poisson.Generate((Sample2 *)samples);2353 }2354 2355 2356 #endif2357 2358 2359 void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br)2360 {2361 float myfov = fov * M_PI / 180.0f;2362 2363 const float w_far = 2.0f * tan(myfov / 2.0f);2364 const float aspect = texWidth / texHeight;2365 2366 const float h_far = w_far / aspect;2367 2368 float t1 = h_far * 0.5f;2369 float t2 = w_far * 0.5f;2370 2371 #if 02372 bl = Normalize(-Vector3(-t1, -t2, 1.0f));2373 br = Normalize(-Vector3( t1, -t2, 1.0f));2374 tl = Normalize(-Vector3(-t1, t2, 1.0f));2375 tr = Normalize(-Vector3( t1, t2, 1.0f));2376 #else2377 bl = -Normalize(camera->GetDirection());2378 br = -Normalize(camera->GetDirection());2379 tl = -Normalize(camera->GetDirection());2380 tr = -Normalize(camera->GetDirection());2381 #endif2382 // normalize to 0 .. 12383 bl = bl * 0.5f + 0.5f;2384 br = br * 0.5f + 0.5f;2385 tl = tl * 0.5f + 0.5f;2386 tr = tr * 0.5f + 0.5f;2387 }2388 2389 2390 void CreateNoiseTex2D()2391 {2392 randomNormals = new GLubyte[texWidth * texHeight * 3];2393 2394 for (int i = 0; i < texWidth * texHeight * 3; i += 3)2395 {2396 // create random samples over sphere2397 const float rx = RandomValue(0, 1);2398 const float theta = 2.0f * acos(sqrt(1.0f - rx));2399 2400 randomNormals[i + 0] = (GLubyte)((cos(theta) * 0.5f + 0.5f) * 255.0f);2401 randomNormals[i + 1] = (GLubyte)((sin(theta) * 0.5f + 0.5f) * 255.0f);2402 randomNormals[i + 2] = 0;2403 }2404 2405 glEnable(GL_TEXTURE_2D);2406 glGenTextures(1, &noiseTex);2407 glBindTexture(GL_TEXTURE_2D, noiseTex);2408 2409 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);2410 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);2411 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);2412 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);2413 2414 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);2415 //gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, texWidth, texHeight, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);2416 2417 glBindTexture(GL_TEXTURE_2D, 0);2418 glDisable(GL_TEXTURE_2D);2419 2420 cout << "created noise texture" << endl;2421 PrintGLerror("noisetexture");2422 }
Note: See TracChangeset
for help on using the changeset viewer.