Changeset 2895 for GTP/trunk/App
- Timestamp:
- 09/02/08 15:29:20 (16 years ago)
- Location:
- GTP/trunk/App/Demos/Vis/FriendlyCulling
- Files:
-
- 7 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj
r2887 r2895 216 216 </File> 217 217 <File 218 RelativePath=".\src\DeferredShader.cpp"219 >220 </File>221 <File222 RelativePath=".\src\DeferredShader.h"223 >224 </File>225 <File226 218 RelativePath=".\src\Environment.cpp" 227 219 > … … 321 313 <File 322 314 RelativePath=".\src\ShadowMapping.h" 323 >324 </File>325 <File326 RelativePath=".\src\SsaoShader.cpp"327 >328 </File>329 <File330 RelativePath=".\src\SsaoShader.h"331 315 > 332 316 </File> … … 544 528 <File 545 529 RelativePath=".\src\Camera.h" 530 > 531 </File> 532 <File 533 RelativePath=".\src\DeferredRenderer.cpp" 546 534 > 547 535 </File> -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
r2894 r2895 24 24 using namespace std; 25 25 26 27 int BvhNode::sCurrentState = 0; 26 28 27 29 … … 75 77 mAxis(-1), 76 78 mDepth(0), 77 mPlaneMask(0),78 mPreferredPlane(0),79 79 mLastRenderedFrame(-999), 80 80 mFirst(-1), … … 87 87 mIsVirtualLeaf(false) 88 88 { 89 for (int i = 0; i < NUM_STATES; ++ i) 90 { 91 mPlaneMask[i] = 0; 92 mPreferredPlane[i]= 0; 93 } 89 94 } 90 95 … … 97 102 void BvhNode::ResetVisibility() 98 103 { 99 mVisibility.Reset(); 104 for (int i = 0; i < NUM_STATES; ++ i) 105 { 106 mVisibility[i].Reset(); 107 } 108 100 109 mLastRenderedFrame = -999; 101 110 } … … 301 310 { 302 311 // do the test only if necessary 303 if (!(node->mPlaneMask & (1 << i)))312 if (!(node->mPlaneMask[BvhNode::sCurrentState] & (1 << i))) 304 313 return true; 305 314 … … 310 319 { 311 320 // outside 312 node->mPreferredPlane = i;321 node->mPreferredPlane[BvhNode::sCurrentState] = i; 313 322 return false; 314 323 } … … 320 329 { 321 330 // completely inside: children don't need to check against this plane no more 322 node->mPlaneMask ^= 1 << i;331 node->mPlaneMask[BvhNode::sCurrentState] ^= 1 << i; 323 332 } 324 333 else … … 336 345 337 346 if (node->GetParent()) 338 node->mPlaneMask = node->GetParent()->mPlaneMask; 339 340 341 //for (int i = 0; i < 6; ++ i) 342 // if (!TestPlane(node, i, bIntersect)) return 0; 343 347 node->mPlaneMask[BvhNode::sCurrentState] = node->GetParent()->mPlaneMask[BvhNode::sCurrentState]; 344 348 345 349 //////// 346 350 //-- apply frustum culling for the planes [mPreferredPlane - 5] 347 351 348 for (int i = node->mPreferredPlane ; i < 6; ++ i)352 for (int i = node->mPreferredPlane[BvhNode::sCurrentState]; i < 6; ++ i) 349 353 if (!TestPlane(node, i, bIntersect)) return 0; 350 354 355 351 356 ////////// 352 357 //-- do the view frustum culling for the planes [0 - m_iPreferredPlane) 353 358 354 for (int i = 0; i < node->mPreferredPlane ; ++ i)359 for (int i = 0; i < node->mPreferredPlane[BvhNode::sCurrentState]; ++ i) 355 360 if (!TestPlane(node, i, bIntersect)) return 0; 356 361 … … 362 367 { 363 368 // = 0011 1111 which means that at the beginning, all six planes have to frustum culled 364 mRoot->mPlaneMask = 0x3f;369 mRoot->mPlaneMask[BvhNode::sCurrentState] = 0x3f; 365 370 366 371 mCamera->CalcFrustum(sFrustum); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h
r2894 r2895 12 12 namespace CHCDemoEngine 13 13 { 14 15 // the number of visibility states 16 #define NUM_STATES 2 14 17 15 18 //////// … … 149 152 inline int CountPrimitives() const; 150 153 154 static void SetCurrentState(int _state) { sCurrentState = _state; } 155 151 156 152 157 protected: 153 158 154 /////////////155 156 159 /// the depth of this node 157 160 unsigned char mDepth; … … 160 163 /// the parent node 161 164 BvhNode *mParent; 162 /// stores the visibility related info 163 VisibilityInfo mVisibility; 165 166 167 ////////////// 168 //-- these members define the current state 169 170 /// stores the visibility related info 171 VisibilityInfo mVisibility[NUM_STATES]; 164 172 165 173 ///////// 166 174 //-- used for view frustum culling 167 175 168 int mPlaneMask; 169 int mPreferredPlane; 176 int mPlaneMask[NUM_STATES]; 177 int mPreferredPlane[NUM_STATES]; 178 179 // the current state 180 static int sCurrentState; 181 182 183 //////////////////// 170 184 171 185 … … 221 235 int BvhNode::GetLastVisitedFrame() const 222 236 { 223 return mVisibility .mLastVisitedFrame;237 return mVisibility[sCurrentState].mLastVisitedFrame; 224 238 } 225 239 … … 227 241 void BvhNode::SetLastVisitedFrame(const int lastVisited) 228 242 { 229 mVisibility .mLastVisitedFrame = lastVisited;243 mVisibility[sCurrentState].mLastVisitedFrame = lastVisited; 230 244 } 231 245 … … 233 247 bool BvhNode::IsVisible() const 234 248 { 235 return mVisibility .mIsVisible;249 return mVisibility[sCurrentState].mIsVisible; 236 250 } 237 251 … … 239 253 void BvhNode::SetVisible(bool visible) 240 254 { 241 mVisibility .mIsVisible = visible;255 mVisibility[sCurrentState].mIsVisible = visible; 242 256 } 243 257 … … 245 259 void BvhNode::IncTimesTestedInvisible() 246 260 { 247 ++ mVisibility .mTimesInvisible;261 ++ mVisibility[sCurrentState].mTimesInvisible; 248 262 } 249 263 … … 251 265 int BvhNode::GetTimesTestedInvisible() const 252 266 { 253 return mVisibility .mTimesInvisible;267 return mVisibility[sCurrentState].mTimesInvisible; 254 268 } 255 269 … … 257 271 void BvhNode::SetTimesTestedInvisible(int t) 258 272 { 259 mVisibility .mTimesInvisible = t;273 mVisibility[sCurrentState].mTimesInvisible = t; 260 274 } 261 275 … … 263 277 bool BvhNode::IsViewFrustumCulled() const 264 278 { 265 return mVisibility .mIsFrustumCulled;279 return mVisibility[sCurrentState].mIsFrustumCulled; 266 280 } 267 281 … … 269 283 void BvhNode::SetViewFrustumCulled(bool frustumCulled) 270 284 { 271 mVisibility .mIsFrustumCulled = frustumCulled;285 mVisibility[sCurrentState].mIsFrustumCulled = frustumCulled; 272 286 } 273 287 … … 275 289 bool BvhNode::IsNew() const 276 290 { 277 return mVisibility .mIsNew;291 return mVisibility[sCurrentState].mIsNew; 278 292 } 279 293 … … 281 295 void BvhNode::SetIsNew(bool isNew) 282 296 { 283 mVisibility.mIsNew = isNew; 284 } 285 297 mVisibility[sCurrentState].mIsNew = isNew; 298 } 299 300 301 void BvhNode::SetAssumedVisibleFrameId(int t) 302 { 303 mVisibility[sCurrentState].mAssumedVisibleFrameId = t; 304 } 305 306 307 int BvhNode::GetAssumedVisibleFrameId() const 308 { 309 return mVisibility[sCurrentState].mAssumedVisibleFrameId; 310 } 311 312 313 int BvhNode::GetLastQueriedFrame() const 314 { 315 return mVisibility[sCurrentState].mLastQueriedFrame; 316 } 317 318 319 void BvhNode::SetLastQueriedFrame(int lastTested) 320 { 321 mVisibility[sCurrentState].mLastQueriedFrame = lastTested; 322 } 286 323 287 324 int BvhNode::GetLastRenderedFrame() const … … 309 346 310 347 311 void BvhNode::SetAssumedVisibleFrameId(int t)312 {313 mVisibility.mAssumedVisibleFrameId = t;314 }315 316 317 int BvhNode::GetAssumedVisibleFrameId() const318 {319 return mVisibility.mAssumedVisibleFrameId;320 }321 322 323 int BvhNode::GetLastQueriedFrame() const324 {325 return mVisibility.mLastQueriedFrame;326 }327 328 329 void BvhNode::SetLastQueriedFrame(int lastTested)330 {331 mVisibility.mLastQueriedFrame = lastTested;332 }333 348 334 349 -
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 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp
r2894 r2895 47 47 static CGparameter sShadowMatrixParam; 48 48 static CGparameter sMaxDepthParam; 49 49 static CGparameter sSampleWidthParam; 50 50 51 51 … … 122 122 sPositionsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "positions"); 123 123 sColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "colors"); 124 sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals"); 124 sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals"); 125 125 126 sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap"); 126 127 sMaxDepthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "maxDepth"); 128 sSampleWidthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "sampleWidth"); 127 129 sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix"); 128 130 } … … 156 158 glOrtho(-offs, offs, -offs, offs, 0, 1); 157 159 158 159 160 FirstPass(fbo); 160 161 AntiAliasing(fbo); 161 162 162 163 glEnable(GL_LIGHTING); 163 glDisable(GL_TEXTURE_2D); 164 164 165 165 glMatrixMode(GL_PROJECTION); 166 166 glPopMatrix(); … … 172 172 173 173 cgGLDisableProfile(RenderState::sCgFragmentProfile); 174 FrameBufferObject::Release(); 174 175 } 175 176 … … 178 179 ShadowMap *shadowMap) 179 180 { 181 FrameBufferObject::Release(); 182 180 183 cgGLEnableProfile(RenderState::sCgFragmentProfile); 181 184 … … 183 186 glDisable(GL_TEXTURE_2D); 184 187 glDisable(GL_LIGHTING); 185 glDepthMask(GL_FALSE);188 //glDepthMask(GL_FALSE); 186 189 187 190 … … 204 207 205 208 glEnable(GL_LIGHTING); 206 glEnable(GL_TEXTURE_2D);207 glDepthMask(GL_TRUE);209 //glEnable(GL_TEXTURE_2D); 210 //glDepthMask(GL_TRUE); 208 211 209 212 glMatrixMode(GL_PROJECTION); … … 216 219 217 220 cgGLDisableProfile(RenderState::sCgFragmentProfile); 221 FrameBufferObject::Release(); 218 222 } 219 223 … … 258 262 cgGLDisableTextureParameter(sPositionsTexParam); 259 263 cgGLDisableTextureParameter(sNormalsTexParam); 260 261 cgGLDisableProfile(RenderState::sCgFragmentProfile);262 264 263 265 FrameBufferObject::Release(); … … 290 292 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 291 293 292 cgGLEnableProfile(RenderState::sCgFragmentProfile);293 294 294 cgGLBindProgram(sCgAntiAliasingProgram); 295 295 … … 301 301 302 302 glColor3f(1.0f, 1.0f, 1.0f); 303 304 float offs2 = 0.5f; 305 303 306 304 glBegin(GL_QUADS); 307 305 … … 320 318 cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam); 321 319 322 cgGLDisableProfile(RenderState::sCgFragmentProfile);323 324 320 PrintGLerror("antialiasing"); 325 321 } … … 343 339 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 344 340 345 cgGLEnableProfile(RenderState::sCgFragmentProfile);346 347 341 cgGLBindProgram(sCgDeferredShadowProgram); 348 342 … … 360 354 361 355 cgGLSetParameter1f(sMaxDepthParam, mScaleFactor); 362 356 cgGLSetParameter1f(sSampleWidthParam, 1.0f / shadowMap->GetSize()); 357 363 358 cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x); 364 359 … … 381 376 cgGLDisableTextureParameter(sShadowMapParam); 382 377 383 cgGLDisableProfile(RenderState::sCgFragmentProfile);384 385 378 FrameBufferObject::Release(); 386 379 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
r2894 r2895 46 46 */ 47 47 void GetTextureMatrix(Matrix4x4 &m) const; 48 48 /** Return shadow size. 49 */ 50 int GetSize() const { return mSize; } 49 51 50 52 protected: -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
r2894 r2895 34 34 #include "FrameBufferObject.h" 35 35 #include "SsaoShader.h" 36 #include "DeferredShader.h"37 36 #include "ShadowMapping.h" 38 37 #include "Light.h" … … 95 94 static int winHeight = 768; 96 95 97 //static int winWidth = 512;98 //static int winHeight = 512;99 100 96 101 97 static float winAspectRatio = 1.0f; … … 172 168 bool useLODs = true; 173 169 174 int samplingMethod = SsaoShader::POISSON; 170 SsaoShader::SAMPLING_METHOD samplingMethod = SsaoShader::POISSON; 171 SsaoShader::SHADING_METHOD shadingMethod = SsaoShader::DEFAULT; 172 173 bool showShadowMap = false; 174 bool shadowChanged = true; 175 175 176 176 static Matrix4x4 matProjectionView = IdentityMatrix(); … … 179 179 ShadowMap *shadowMap = NULL; 180 180 Light *light = NULL; 181 181 182 182 183 // function forward declarations … … 225 226 226 227 SsaoShader *ssaoShader = NULL; 227 DeferredShader *deferredShader = NULL;228 228 229 229 static GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT}; … … 431 431 InitCg(); 432 432 433 DeferredShader::Init(sCgContext);434 433 SsaoShader::Init(sCgContext); 435 434 … … 442 441 sceneQuery = new SceneQuery(bvh->GetBox(), traverser); 443 442 444 Vector3 lightDir = Normalize(Vector3(0, 1, -1)); 443 //Vector3 lightDir = Normalize(Vector3(0, 1, -1)); 444 Vector3 lightDir = Normalize(-Vector3(0.8f, -1.0f, 0.7f)); 445 445 446 light = new Light(lightDir, RgbaColor(1, 1, 1, 1)); 446 447 447 const float shadowSize = 4096; 448 shadowMap = new ShadowMap(shadowSize, bvh->GetBox(), camera); 449 448 450 449 // frame time is restarted every frame 451 450 frameTimer.Start(); … … 867 866 } 868 867 868 if (showShadowMap && !shadowMap) 869 { 870 const float shadowSize = 4096; 871 shadowMap = new ShadowMap(shadowSize, bvh->GetBox(), camera); 872 } 873 869 874 // render without shading 870 875 switch (renderType) … … 897 902 898 903 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 899 900 904 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 901 905 … … 904 908 case RenderState::DEFERRED: 905 909 906 shadowMap->ComputeShadowMap(light, traverser); 910 // change CHC state (must be done for each change of camera) 911 BvhNode::SetCurrentState(1); 912 913 if (showShadowMap && shadowChanged) 914 { 915 shadowChanged = false; 916 917 cgGLDisableProfile(RenderState::sCgFragmentProfile); 918 cgGLDisableProfile(RenderState::sCgVertexProfile); 919 920 state.SetRenderType(RenderState::DEPTH_PASS); 921 922 // the scene is rendered withouth any shading 923 shadowMap->ComputeShadowMap(light, traverser); 924 925 // change back state 926 BvhNode::SetCurrentState(0); 927 928 glEnable(GL_LIGHTING); 929 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 930 } 907 931 908 932 if (!fbo) InitFBO(); 933 909 934 910 935 // multisampling does not work with deferred shading … … 989 1014 cgGLDisableProfile(RenderState::sCgFragmentProfile); 990 1015 991 if (useSsao) 992 { 993 if (!ssaoShader) ssaoShader = new SsaoShader(texWidth, texHeight, camera, myfar / 10.0f); 994 ssaoShader->Render(fbo, oldViewProjMatrix, ssaoExpFactor); 995 996 } 997 else 998 { 999 if (!deferredShader) deferredShader = new DeferredShader(texWidth, texHeight, myfar / 10.0f); 1000 1001 deferredShader->Render(fbo, shadowMap); 1002 } 1016 if (!ssaoShader) ssaoShader = new SsaoShader(texWidth, texHeight, camera, myfar / 10.0f); 1017 1018 ssaoShader->SetShadingMethod(shadingMethod); 1019 ssaoShader->SetSamplingMethod(samplingMethod); 1020 ssaoShader->SetUseTemporalCoherence(useTemporalCoherence); 1021 1022 ShadowMap *sm = showShadowMap ? shadowMap : NULL; 1023 ssaoShader->Render(fbo, oldViewProjMatrix, ssaoExpFactor, sm); 1003 1024 } 1004 1025 … … 1118 1139 samplingMethod = SsaoShader::GAUSS; 1119 1140 1120 ssaoShader->SetSamplingMethod(samplingMethod); 1121 1141 break; 1142 case 'Y': 1143 case 'y': 1144 showShadowMap = !showShadowMap; 1122 1145 break; 1123 1146 case 'g': 1124 case 'G':1125 useGlobIllum = !useGlobIllum;1126 ssaoShader->SetUseGlobIllum(useGlobIllum);1127 break;1128 1147 case 't': 1129 1148 case 'T': 1130 1149 useTemporalCoherence = !useTemporalCoherence; 1131 ssaoShader->SetUseTemporalCoherence(useTemporalCoherence);1132 1150 break; 1133 1151 case 'o': … … 1262 1280 break; 1263 1281 case GLUT_KEY_F8: 1264 useSsao = !useSsao; 1282 shadingMethod = (SsaoShader::SHADING_METHOD)((shadingMethod + 1) % 3); 1283 1265 1284 break; 1266 1285 case GLUT_KEY_F9: … … 1562 1581 DEL_PTR(fbo); 1563 1582 DEL_PTR(ssaoShader); 1564 DEL_PTR(deferredShader);1565 1583 1566 1584 if (sCgMrtVertexProgram) … … 1766 1784 } 1767 1785 1768 1786 // render visible object from depth pass 1769 1787 void RenderVisibleObjects() 1770 1788 { -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
r2893 r2895 81 81 uniform sampler2D shadowMap, 82 82 uniform float4x4 shadowMatrix, 83 uniform float maxDepth 83 uniform float maxDepth, 84 uniform float sampleWidth 84 85 ) 85 86 { … … 102 103 float4 lightSpacePos = mul(shadowMatrix, position); 103 104 104 float shadowDepth = tex2D(shadowMap, lightSpacePos.xy); 105 float shadowDepth[9]; 106 107 float w = sampleWidth; 108 109 // pcf sampling using 3 x 3 tab 110 111 shadowDepth[0] = tex2D(shadowMap, lightSpacePos.xy).x; 112 113 shadowDepth[1] = tex2D(shadowMap, lightSpacePos.xy + float2( w, w)).x; 114 shadowDepth[2] = tex2D(shadowMap, lightSpacePos.xy - float2( w, -w)).x; 115 shadowDepth[3] = tex2D(shadowMap, lightSpacePos.xy - float2(-w, -w)).x; 116 shadowDepth[4] = tex2D(shadowMap, lightSpacePos.xy - float2(-w, w)).x; 117 118 shadowDepth[5] = tex2D(shadowMap, lightSpacePos.xy - float2( w, 0)).x; 119 shadowDepth[6] = tex2D(shadowMap, lightSpacePos.xy - float2( 0, w)).x; 120 shadowDepth[7] = tex2D(shadowMap, lightSpacePos.xy - float2(-w, 0)).x; 121 shadowDepth[8] = tex2D(shadowMap, lightSpacePos.xy - float2( 0, -w)).x; 105 122 106 123 OUT.color = col; 107 124 108 // hack: prevent shadowing the sky 109 if ((amb < 0.9) && 125 float depth = lightSpacePos.z / lightSpacePos.w; 126 127 float d = 0.0f; 128 129 for (int i = 0; i < 9; ++ i) 130 { 131 d += step(shadowDepth[i], depth); 132 } 133 134 d /= 9.0f; 135 136 /*if ((amb < 0.9) && // hack: prevent shadowing the sky 110 137 (lightSpacePos.z / lightSpacePos.w > shadowDepth)) 111 138 { 112 139 OUT.color *= 0.1f; 140 }*/ 141 142 if (amb < 0.9) // hack: prevent shadowing the sky 143 { 144 const float x = 0.1f; 145 OUT.color *= x + (1.0f - x) * (1.0f - d); 113 146 } 114 147
Note: See TracChangeset
for help on using the changeset viewer.