- Timestamp:
- 08/27/08 15:09:04 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp
r2871 r2873 15 15 16 16 // number of ssao samples 17 #define NUM_SAMPLES 1 617 #define NUM_SAMPLES 10 18 18 19 19 20 20 static CGprogram sCgSsaoProgram = NULL; 21 static CGprogram sCgDeferredProgram2 = NULL; 21 static CGprogram sCgGiProgram = NULL; 22 23 static CGprogram sCgDeferredProgram = NULL; 22 24 static CGprogram sCgAntiAliasingProgram = NULL; 23 25 static CGprogram sCgCombineProgram = NULL; … … 30 32 static CGparameter sNormalsTexDeferredParam; 31 33 34 35 ///////////////////////////////////////7 36 37 32 38 static CGparameter sColorsTexParam; 33 39 static CGparameter sPositionsTexParam; 34 40 static CGparameter sNormalsTexParam; 35 36 41 37 42 static CGparameter sOldModelViewProjMatrixParam; … … 43 48 static CGparameter sExpFactorParam; 44 49 50 51 /////////////////////////////////////// 52 53 54 static CGparameter sColorsTexGiParam; 55 static CGparameter sPositionsTexGiParam; 56 static CGparameter sNormalsTexGiParam; 57 58 59 static CGparameter sOldModelViewProjMatrixGiParam; 60 static CGparameter sMaxDepthGiParam; 61 static CGparameter sSamplesGiParam; 62 static CGparameter sOldSsaoTexGiParam; 63 static CGparameter sOldIllumTexGiParam; 64 static CGparameter sNoiseTexGiParam; 65 static CGparameter sNoiseMultiplierGiParam; 66 static CGparameter sExpFactorGiParam; 67 68 69 //////////// 70 45 71 static CGparameter sColorsTexAntiAliasingParam; 46 72 static CGparameter sNormalsTexAntiAliasingParam; … … 88 114 mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 89 115 mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 116 mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 90 117 91 118 mOldFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); … … 93 120 mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 94 121 mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 122 mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 95 123 96 124 mFbo3 = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); … … 98 126 mFbo3->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 99 127 100 mFbo4 = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);101 // the diffuse color buffer102 mFbo4->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);103 128 104 129 // create noise texture for ssao … … 121 146 void SsaoShader::Init(CGcontext context) 122 147 { 123 sCgDeferredProgram 2=148 sCgDeferredProgram = 124 149 cgCreateProgramFromFile(context, 125 150 CG_SOURCE, 126 151 "src/shaders/deferred.cg", 127 152 RenderState::sCgFragmentProfile, 128 "main 2",153 "main", 129 154 NULL); 130 155 131 if (sCgDeferredProgram 2!= NULL)132 { 133 cgGLLoadProgram(sCgDeferredProgram 2);156 if (sCgDeferredProgram != NULL) 157 { 158 cgGLLoadProgram(sCgDeferredProgram); 134 159 135 160 // we need size of texture for scaling 136 sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram 2, "positions");137 sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram 2, "colors");138 sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram 2, "normals");161 sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions"); 162 sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors"); 163 sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals"); 139 164 } 140 165 else 141 166 cerr << "deferred program failed to load" << endl; 142 167 143 144 /*sCgCombineProgram =145 cgCreateProgramFromFile(context,146 CG_SOURCE,147 "src/shaders/ssao.cg",148 RenderState::sCgFragmentProfile,149 "combined",150 NULL);151 152 if (sCgCombineProgram != NULL)153 {154 cgGLLoadProgram(sCgCombineProgram);155 156 // we need size of texture for scaling157 sColorsTexCombineParam = cgGetNamedParameter(sCgCombineProgram, "colors");158 sSsaoTexCombineParam = cgGetNamedParameter(sCgCombineProgram, "ssaoTex");159 }160 else161 cerr << "combined program failed to load" << endl;162 */163 168 164 169 /////////////// … … 189 194 sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples"); 190 195 sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex"); 196 191 197 192 198 // generate samples for ssao kernel … … 196 202 else 197 203 cerr << "ssao program failed to load" << endl; 204 205 sCgGiProgram = 206 cgCreateProgramFromFile(context, 207 CG_SOURCE, 208 "src/shaders/globillum.cg", 209 RenderState::sCgFragmentProfile, 210 "main", 211 NULL); 212 213 if (sCgGiProgram != NULL) 214 { 215 cgGLLoadProgram(sCgGiProgram); 216 217 // we need size of texture for scaling 218 sPositionsTexGiParam = cgGetNamedParameter(sCgGiProgram, "positions"); 219 sColorsTexGiParam = cgGetNamedParameter(sCgGiProgram, "colors"); 220 sNormalsTexGiParam = cgGetNamedParameter(sCgGiProgram, "normals"); 221 sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture"); 222 sNoiseMultiplierGiParam = cgGetNamedParameter(sCgGiProgram, "noiseMultiplier"); 223 224 sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgGiProgram, "oldModelViewProj"); 225 sMaxDepthGiParam = cgGetNamedParameter(sCgGiProgram, "maxDepth"); 226 sExpFactorGiParam = cgGetNamedParameter(sCgGiProgram, "expFactor"); 227 228 sSamplesGiParam = cgGetNamedParameter(sCgGiProgram, "samples"); 229 230 sOldSsaoTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldSsaoTex"); 231 sOldIllumTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldIllumTex"); 232 233 // generate samples for ssao kernel 234 GenerateSamples(); 235 cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples); 236 } 237 else 238 cerr << "globillum program failed to load" << endl; 198 239 199 240 sCgAntiAliasingProgram = … … 258 299 259 300 FirstPass(fbo); 301 260 302 ComputeSsao(fbo, expFactor); 303 //ComputeGlobIllum(fbo, expFactor); 304 261 305 //Combine(fbo); 262 306 AntiAliasing(fbo); … … 323 367 324 368 325 //GenerateSamples(); 369 // q: should we generate new samples or only rotate the old ones? 370 // in the first case, the sample patterns look nicer, but the kernel 371 // needs longer to converge 372 GenerateSamples(); 326 373 cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples); 327 374 … … 499 546 cgGLEnableProfile(RenderState::sCgFragmentProfile); 500 547 501 cgGLBindProgram(sCgDeferredProgram 2);548 cgGLBindProgram(sCgDeferredProgram); 502 549 503 550 cgGLSetTextureParameter(sColorsTexDeferredParam, colorsTex); … … 535 582 536 583 537 void SsaoShader::Com bine(FrameBufferObject *fbo)584 void SsaoShader::ComputeGlobIllum(FrameBufferObject *fbo, float expFactor) 538 585 { 539 586 GLuint colorsTex = mFbo3->GetColorBuffer(0)->GetTexture(); 540 GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture(); 541 542 mFbo4->Bind(); 543 //mNewFbo->Bind(); 587 GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 588 GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 589 590 if (1) 591 { 592 // generate mip map levels for position texture 593 glBindTexture(GL_TEXTURE_2D, positionsTex); 594 glGenerateMipmapEXT(GL_TEXTURE_2D); 595 596 // generate mip map levels for position texture 597 glBindTexture(GL_TEXTURE_2D, colorsTex); 598 glGenerateMipmapEXT(GL_TEXTURE_2D); 599 } 600 601 602 // read the second buffer, write to the first buffer 603 mNewFbo->Bind(); 604 glDrawBuffers(3, mymrt); 605 606 GLuint oldSsaoTex = mOldFbo->GetColorBuffer(0)->GetTexture(); 607 GLuint oldIllumTex = mOldFbo->GetColorBuffer(2)->GetTexture(); 544 608 545 609 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 546 547 glDrawBuffers(1, mymrt);548 610 549 611 cgGLEnableProfile(RenderState::sCgFragmentProfile); 550 551 cgGLBindProgram(sCgCombineProgram); 552 553 cgGLSetTextureParameter(sColorsTexCombineParam, colorsTex); 554 cgGLEnableTextureParameter(sColorsTexCombineParam); 555 556 cgGLSetTextureParameter(sSsaoTexCombineParam, ssaoTex); 557 cgGLEnableTextureParameter(sSsaoTexCombineParam); 612 cgGLBindProgram(sCgGiProgram); 613 614 cgGLSetTextureParameter(sPositionsTexGiParam, positionsTex); 615 cgGLEnableTextureParameter(sPositionsTexGiParam); 616 617 cgGLSetTextureParameter(sColorsTexGiParam, colorsTex); 618 cgGLEnableTextureParameter(sColorsTexGiParam); 619 620 cgGLSetTextureParameter(sNormalsTexGiParam, normalsTex); 621 cgGLEnableTextureParameter(sNormalsTexGiParam); 622 623 cgGLSetTextureParameter(sNoiseTexGiParam, noiseTex); 624 cgGLEnableTextureParameter(sNoiseTexGiParam); 625 626 cgGLSetTextureParameter(sOldSsaoTexGiParam, oldSsaoTex); 627 cgGLEnableTextureParameter(sOldSsaoTexGiParam); 628 629 cgGLSetTextureParameter(sOldIllumTexGiParam, oldIllumTex); 630 cgGLEnableTextureParameter(sOldIllumTexGiParam); 631 632 cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f)); 633 634 cgGLSetParameter1f(sMaxDepthGiParam, mScaleFactor); 635 cgGLSetParameter1f(sExpFactorGiParam, expFactor); 636 637 638 // q: should we generate new samples or only rotate the old ones? 639 // in the first case, the sample patterns look nicer, but the kernel 640 // needs longer to converge 641 GenerateSamples(); 642 cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples); 643 644 Vector3 tl, tr, bl, br; 645 ComputeViewVectors(tl, tr, bl, br); 558 646 559 647 glColor3f(1.0f, 1.0f, 1.0f); 560 648 561 const float offs = 0.5f;562 563 649 glBegin(GL_QUADS); 564 650 565 glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f); 566 glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f); 567 glTexCoord2f(1, 1); glVertex3f( offs, offs, -0.5f); 568 glTexCoord2f(0, 1); glVertex3f(-offs, offs, -0.5f); 651 // note: slightly larger texture hides ambient occlusion error on border but costs resolution 652 //const float new_offs = 0.55f; 653 const float new_offs = 0.5f; 654 655 glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-new_offs, -new_offs, -0.5f); 656 glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( new_offs, -new_offs, -0.5f); 657 glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( new_offs, new_offs, -0.5f); 658 glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-new_offs, new_offs, -0.5f); 569 659 570 660 glEnd(); 571 661 572 cgGLDisableTextureParameter(sColorsTexCombineParam); 573 cgGLDisableTextureParameter(sSsaoTexCombineParam); 574 575 cgGLDisableProfile(RenderState::sCgFragmentProfile); 662 cgGLDisableTextureParameter(sColorsTexGiParam); 663 cgGLDisableTextureParameter(sPositionsTexGiParam); 664 cgGLDisableTextureParameter(sNormalsTexGiParam); 665 cgGLDisableTextureParameter(sNoiseTexGiParam); 666 cgGLDisableTextureParameter(sOldSsaoTexGiParam); 667 cgGLDisableTextureParameter(sOldIllumTexGiParam); 576 668 577 669 FrameBufferObject::Release(); 578 670 579 PrintGLerror(" deferred shading");671 PrintGLerror("globillum first pass"); 580 672 } 581 673
Note: See TracChangeset
for help on using the changeset viewer.