- Timestamp:
- 09/02/08 15:29:20 (16 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
r2894 r2895 8 8 #include "RndGauss.h" 9 9 #include "Halton.h" 10 #include "ShadowMapping.h" 10 11 11 12 … … 23 24 static CGprogram sCgDeferredProgram = NULL; 24 25 static CGprogram sCgAntiAliasingProgram = NULL; 26 static CGprogram sCgDeferredShadowProgram = NULL; 25 27 26 28 static CGparameter sColorsTexCombineParam; … … 83 85 static CGparameter sNormalsTexAntiAliasingParam; 84 86 87 88 static CGparameter sShadowMapParam; 89 static CGparameter sPositionsTexShadowParam; 90 static CGparameter sColorsTexShadowParam; 91 static CGparameter sNormalsTexShadowParam; 92 93 static CGparameter sShadowMatrixParam; 94 static CGparameter sMaxDepthShadowParam; 95 static CGparameter sSampleWidthParam; 96 97 98 85 99 static GLuint noiseTex = 0; 86 100 … … 88 102 static Sample2 samples[NUM_SAMPLES]; 89 103 104 static int colorBufferIdx = 0; 90 105 91 106 static void PrintGLerror(char *msg) … … 192 207 mScaleFactor(scaleFactor), 193 208 mUseTemporalCoherence(true), 194 mUseGlobIllum(false), 195 mSampling(POISSON), 209 mRegenerateSamples(true), 210 mSamplingMethod(POISSON), 211 mShadingMethod(DEFAULT), 196 212 mFboIndex(0) 197 213 { … … 237 253 238 254 glDeleteTextures(1, &noiseTex); 239 }240 241 242 void SsaoShader::SetUseGlobIllum(bool useGlobIllum)243 {244 mUseGlobIllum = useGlobIllum;245 255 } 246 256 … … 303 313 304 314 cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f)); 305 306 // generate samples for ssao kernel307 //GenerateSamples(mSampling);308 309 315 sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples"); 310 //cgSetArraySize(sSamplesParam, NUM_SAMPLES);311 //cgCompileProgram(sCgSsaoProgram);312 313 //cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);314 316 } 315 317 else … … 344 346 sOldIllumTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldIllumTex"); 345 347 346 // generate samples for ssao kernel347 //GenerateSamples();348 //cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);349 350 348 cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f)); 351 349 } … … 410 408 cerr << "antialiasing program failed to load" << endl; 411 409 410 sCgDeferredShadowProgram = 411 cgCreateProgramFromFile(context, 412 CG_SOURCE, 413 "src/shaders/deferred.cg", 414 RenderState::sCgFragmentProfile, 415 "main_shadow", 416 NULL); 417 418 if (sCgDeferredShadowProgram != NULL) 419 { 420 cgGLLoadProgram(sCgDeferredShadowProgram); 421 422 // we need size of texture for scaling 423 sPositionsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "positions"); 424 sColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "colors"); 425 sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals"); 426 427 sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap"); 428 sMaxDepthShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "maxDepth"); 429 sSampleWidthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "sampleWidth"); 430 sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix"); 431 } 432 else 433 cerr << "deferred program failed to load" << endl; 412 434 PrintGLerror("init"); 413 435 } … … 416 438 void SsaoShader::Render(FrameBufferObject *fbo, 417 439 const Matrix4x4 &oldProjViewMatrix, 418 float expFactor) 440 float expFactor, 441 ShadowMap *shadowMap) 419 442 { 420 443 … … 447 470 glOrtho(-offs, offs, -offs, offs, 0, 1); 448 471 449 FirstPass(fbo); 450 451 if (!mUseGlobIllum) 452 { 472 if (shadowMap) 473 FirstPassShadow(fbo, shadowMap); 474 else 475 FirstPass(fbo); 476 477 switch (mShadingMethod) 478 { 479 case SSAO: 453 480 ComputeSsao(fbo, expFactor, oldProjViewMatrix); 454 481 CombineSsao(fbo); 455 } 456 else 457 { 482 break; 483 case GI: 458 484 ComputeGlobIllum(fbo, expFactor, oldProjViewMatrix); 459 485 CombineIllum(fbo); 486 break; 487 default: // DEFAULT 488 // do nothing: standard deferred shading 489 break; 460 490 } 461 491 … … 528 558 529 559 530 if (mUseTemporalCoherence) 531 { 560 if (mUseTemporalCoherence || mRegenerateSamples) 561 { 562 mRegenerateSamples = false; 532 563 cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f)); 533 cgGLSetParameter1f(sExpFactorParam, expFactor);534 564 535 565 // q: should we generate new samples or only rotate the old ones? 536 566 // in the first case, the sample patterns look nicer, but the kernel 537 567 // needs longer to converge 538 GenerateSamples(mSampling );568 GenerateSamples(mSamplingMethod); 539 569 cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples); 540 570 } 541 else 542 { 543 cgGLSetParameter1f(sExpFactorParam, 1.0f); 544 } 571 572 cgGLSetParameter1f(sExpFactorParam, mUseTemporalCoherence ? expFactor : 1.0f); 545 573 546 574 Vector3 tl, tr, bl, br; … … 604 632 void SsaoShader::AntiAliasing(FrameBufferObject *fbo) 605 633 { 606 GLuint colorsTex = fbo->GetColorBuffer( 0)->GetTexture();634 GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture(); 607 635 GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 608 636 … … 650 678 fbo->Bind(); 651 679 652 glDrawBuffers(1, mymrt + 3); 680 colorBufferIdx = 3; 681 glDrawBuffers(1, mymrt + colorBufferIdx); 653 682 654 683 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); … … 754 783 755 784 756 if (mUseTemporalCoherence) 757 { 785 if (mUseTemporalCoherence || mRegenerateSamples) 786 { 787 mRegenerateSamples = false; 758 788 cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f)); 759 789 cgGLSetParameter1f(sExpFactorGiParam, expFactor); … … 762 792 // in the first case, the sample patterns look nicer, but the kernel 763 793 // needs longer to converge 764 GenerateSamples(mSampling );794 GenerateSamples(mSamplingMethod); 765 795 cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples); 766 796 } 767 else 768 { 769 cgGLSetParameter1f(sExpFactorGiParam, 1.0f); 770 } 797 798 cgGLSetParameter1f(sExpFactorParam, mUseTemporalCoherence ? expFactor : 1.0f); 771 799 772 800 Vector3 tl, tr, bl, br; … … 812 840 813 841 // overwrite old color texture 814 glDrawBuffers(1, mymrt); 842 colorBufferIdx = 0; 843 glDrawBuffers(1, mymrt + colorBufferIdx); 815 844 816 845 cgGLEnableProfile(RenderState::sCgFragmentProfile); … … 863 892 fbo->Bind(); 864 893 865 // write into old color texture (not needed anymore) 866 glDrawBuffers(1, mymrt); 894 // overwrite old color texture 895 colorBufferIdx = 0; 896 glDrawBuffers(1, mymrt + colorBufferIdx); 867 897 868 898 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); … … 903 933 904 934 935 void SsaoShader::FirstPassShadow(FrameBufferObject *fbo, ShadowMap *shadowMap) 936 { 937 GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 938 939 GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 940 GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 941 GLuint shadowTex = shadowMap->GetShadowTexture(); 942 943 Matrix4x4 shadowMatrix; 944 shadowMap->GetTextureMatrix(shadowMatrix); 945 946 fbo->Bind(); 947 948 colorBufferIdx = 3; 949 glDrawBuffers(1, mymrt + colorBufferIdx); 950 951 952 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 953 954 cgGLBindProgram(sCgDeferredShadowProgram); 955 956 cgGLSetTextureParameter(sColorsTexShadowParam, colorsTex); 957 cgGLEnableTextureParameter(sColorsTexShadowParam); 958 959 cgGLSetTextureParameter(sPositionsTexShadowParam, positionsTex); 960 cgGLEnableTextureParameter(sPositionsTexShadowParam); 961 962 cgGLSetTextureParameter(sNormalsTexShadowParam, normalsTex); 963 cgGLEnableTextureParameter(sNormalsTexShadowParam); 964 965 cgGLSetTextureParameter(sShadowMapParam, shadowTex); 966 cgGLEnableTextureParameter(sShadowMapParam); 967 968 cgGLSetParameter1f(sMaxDepthShadowParam, mScaleFactor); 969 cgGLSetParameter1f(sSampleWidthParam, 1.0f / shadowMap->GetSize()); 970 971 cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x); 972 973 glColor3f(1.0f, 1.0f, 1.0f); 974 975 glBegin(GL_QUADS); 976 977 float offs2 = 0.5f; 978 979 glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f); 980 glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f); 981 glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f); 982 glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f); 983 984 glEnd(); 985 986 cgGLDisableTextureParameter(sColorsTexShadowParam); 987 cgGLDisableTextureParameter(sPositionsTexShadowParam); 988 cgGLDisableTextureParameter(sNormalsTexShadowParam); 989 cgGLDisableTextureParameter(sShadowMapParam); 990 991 FrameBufferObject::Release(); 992 993 PrintGLerror("deferred shading + shadows"); 994 } 995 996 997 void SsaoShader::SetSamplingMethod(SAMPLING_METHOD s) 998 { 999 if (s != mSamplingMethod) 1000 { 1001 mSamplingMethod = s; 1002 mRegenerateSamples = true; 1003 } 1004 } 905 1005 906 1006 } // namespace
Note: See TracChangeset
for help on using the changeset viewer.