- Timestamp:
- 11/11/08 12:18:07 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
r3115 r3117 234 234 235 235 236 static void InitBuffer(FrameBufferObject *fbo, int index)237 {238 // read the second buffer, write to the first buffer239 fbo->Bind();240 glDrawBuffers(1, mrt + index);241 242 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);243 244 FrameBufferObject::Release();245 }246 247 248 236 DeferredRenderer::DeferredRenderer(int w, int h, PerspectiveCamera *cam): 249 237 mWidth(w), mHeight(h), … … 270 258 { 271 259 mIllumFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 272 InitBuffer(mIllumFbo, i); 273 } 260 FrameBufferObject::InitBuffer(mIllumFbo, i); 261 } 262 263 264 /////////////// 265 //-- the downsampled ssao + color bleeding textures: as gi is inherently low frequency, we can use these to improve performance 274 266 275 267 mDownSampleFbo = new FrameBufferObject(dsw, dsh, FrameBufferObject::DEPTH_NONE); … … 278 270 // downsample buffer for the normal texture 279 271 mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 272 // downsample buffer for the offset texture 273 mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 274 275 for (int i = 0; i < 3; ++ i) 276 { 277 FrameBufferObject::InitBuffer(mDownSampleFbo, i); 278 } 280 279 281 280 mFBOs.push_back(mDownSampleFbo); … … 453 452 // normals 454 453 DownSample(fbo, 1, mDownSampleFbo, 1); 454 // offsets 455 DownSample(fbo, 2, mDownSampleFbo, 2); 455 456 } 456 457 … … 458 459 { 459 460 case SSAO: 460 461 461 ComputeSsao(fbo, tempCohFactor); 462 462 CombineSsao(fbo); … … 501 501 float tempCohFactor) 502 502 { 503 GLuint colorsTex, normalsTex, attribsTex; 504 505 if (0) 506 { 507 colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture(); 508 normalsTex = fbo->GetColorBuffer(1)->GetTexture(); 509 attribsTex = fbo->GetColorBuffer(2)->GetTexture(); 510 } 511 else 512 { 513 colorsTex = mDownSampleFbo->GetColorBuffer(0)->GetTexture(); 514 normalsTex = mDownSampleFbo->GetColorBuffer(1)->GetTexture(); 515 attribsTex = mDownSampleFbo->GetColorBuffer(2)->GetTexture(); 516 } 517 518 // flip flop between illumination buffers 519 GLuint oldTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture(); 520 521 cout << colorBufferIdx; 522 523 glPushAttrib(GL_VIEWPORT_BIT); 524 glViewport(0, 0, mIllumFbo->GetWidth(), mIllumFbo->GetHeight()); 525 526 // read the second buffer, write to the first buffer 527 mIllumFbo->Bind(); 528 glDrawBuffers(1, mrt + mIllumFboIndex); 529 530 int i = 0; 531 532 sCgSsaoProgram->SetTexture(i ++, colorsTex); 533 sCgSsaoProgram->SetTexture(i ++, normalsTex); 534 sCgSsaoProgram->SetTexture(i ++, oldTex); 535 sCgSsaoProgram->SetTexture(i ++, noiseTex); 536 537 sCgSsaoProgram->SetValue1f(i ++, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0); 538 539 if (mUseTemporalCoherence || mRegenerateSamples) 540 { 541 mRegenerateSamples = false; 542 543 // q: should we generate new samples or only rotate the old ones? 544 // in the first case, the sample patterns look nicer, but the kernel 545 // needs longer to converge 546 GenerateSamples(mSamplingMethod); 547 sCgSsaoProgram->SetArray2f(i, (float *)samples2, NUM_SAMPLES); 548 } 549 550 ++ i; 551 552 for (int j = 0; j < 4; ++ j, ++ i) 553 sCgSsaoProgram->SetValue3f(i, mCornersView[j].x, mCornersView[j].y, mCornersView[j].z); 554 555 sCgSsaoProgram->SetMatrix(i ++, mProjViewMatrix); 556 sCgSsaoProgram->SetMatrix(i ++, mOldProjViewMatrix); 557 558 Vector3 de; 559 de.x = mOldEyePos.x - mEyePos.x; 560 de.y = mOldEyePos.y - mEyePos.y; 561 de.z = mOldEyePos.z - mEyePos.z; 562 563 sCgSsaoProgram->SetValue3f(i ++, de.x, de.y, de.z); 564 565 for (int j = 0; j < 4; ++ j, ++ i) 566 sCgSsaoProgram->SetValue3f(i, mOldCornersView[j].x, mOldCornersView[j].y, mOldCornersView[j].z); 567 568 sCgSsaoProgram->SetTexture(i ++, attribsTex); 569 570 DrawQuad(sCgSsaoProgram); 571 572 glPopAttrib(); 573 574 PrintGLerror("ssao first pass"); 575 } 576 577 578 static void SetVertex(float x, float y, float x_offs, float y_offs) 579 { 580 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center 581 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top 582 glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom 583 glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top 584 glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom 585 586 glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right 587 glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom 588 589 //glVertex3f(x - 0.5f, y - 0.5f, -0.5f); 590 glVertex2f(x, y); 591 } 592 593 594 void DeferredRenderer::AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light) 595 { 596 FrameBufferObject::Release(); 597 598 ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx); 599 600 GLuint colorsTex = colorBuffer->GetTexture(); 601 GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 602 603 sCgAntiAliasingProgram->SetTexture(0, colorsTex); 604 sCgAntiAliasingProgram->SetTexture(1, normalsTex); 605 606 sCgAntiAliasingProgram->Bind(); 607 608 glBegin(GL_QUADS); 609 610 // the neighbouring texels 611 float x_offs = 1.0f / mWidth; 612 float y_offs = 1.0f / mHeight; 613 614 SetVertex(0, 0, x_offs, y_offs); 615 SetVertex(1, 0, x_offs, y_offs); 616 SetVertex(1, 1, x_offs, y_offs); 617 SetVertex(0, 1, x_offs, y_offs); 618 619 glEnd(); 620 621 PrintGLerror("antialiasing"); 622 } 623 624 625 void DeferredRenderer::FirstPass(FrameBufferObject *fbo, DirectionalLight *light) 626 { 627 GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture(); 628 GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture(); 629 630 fbo->Bind(); 631 632 colorBufferIdx = 3 - colorBufferIdx; 633 glDrawBuffers(1, mrt + colorBufferIdx); 634 635 const Vector3 lightDir = -light->GetDirection(); 636 637 sCgDeferredProgram->SetTexture(0, colorsTex); 638 sCgDeferredProgram->SetTexture(1, normalsTex); 639 sCgDeferredProgram->SetValue3f(2, lightDir.x, lightDir.y, lightDir.z); 640 641 DrawQuad(sCgDeferredProgram); 642 643 PrintGLerror("deferred shading"); 644 } 645 646 647 void DeferredRenderer::ComputeGlobIllum(FrameBufferObject *fbo, 648 float tempCohFactor) 649 { 503 650 #if 0 504 651 GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture(); … … 509 656 #endif 510 657 511 // flip flop between illumination buffers512 GLuint oldTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();513 514 658 glPushAttrib(GL_VIEWPORT_BIT); 515 659 glViewport(0, 0, mIllumFbo->GetWidth(), mIllumFbo->GetHeight()); … … 517 661 // read the second buffer, write to the first buffer 518 662 mIllumFbo->Bind(); 519 glDrawBuffers(1, mrt + mIllumFboIndex);520 521 int i = 0;522 523 sCgSsaoProgram->SetTexture(i ++, colorsTex);524 sCgSsaoProgram->SetTexture(i ++, normalsTex);525 sCgSsaoProgram->SetTexture(i ++, oldTex);526 sCgSsaoProgram->SetTexture(i ++, noiseTex);527 528 sCgSsaoProgram->SetValue1f(i ++, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);529 530 if (mUseTemporalCoherence || mRegenerateSamples)531 {532 mRegenerateSamples = false;533 534 // q: should we generate new samples or only rotate the old ones?535 // in the first case, the sample patterns look nicer, but the kernel536 // needs longer to converge537 GenerateSamples(mSamplingMethod);538 sCgSsaoProgram->SetArray2f(i, (float *)samples2, NUM_SAMPLES);539 }540 541 ++ i;542 543 for (int j = 0; j < 4; ++ j, ++ i)544 sCgSsaoProgram->SetValue3f(i, mCornersView[j].x, mCornersView[j].y, mCornersView[j].z);545 546 sCgSsaoProgram->SetMatrix(i ++, mProjViewMatrix);547 sCgSsaoProgram->SetMatrix(i ++, mOldProjViewMatrix);548 549 //Vector3 de = mOldEyePos - mEyePos;550 Vector3 de;551 de.x = mOldEyePos.x - mEyePos.x;552 de.y = mOldEyePos.y - mEyePos.y;553 de.z = mOldEyePos.z - mEyePos.z;554 //Vector3 de = mEyePos - mOldEyePos;555 556 sCgSsaoProgram->SetValue3f(i ++, de.x, de.y, de.z);557 558 for (int j = 0; j < 4; ++ j, ++ i)559 sCgSsaoProgram->SetValue3f(i, mOldCornersView[j].x, mOldCornersView[j].y, mOldCornersView[j].z);560 561 GLuint attribsTex = fbo->GetColorBuffer(2)->GetTexture();562 sCgSsaoProgram->SetTexture(i ++, attribsTex);563 564 float trafos[32];565 for (int i = 0; i < 16; ++ i)566 trafos[i] = ((const float *)invTrafo.x)[i];567 568 static Matrix4x4 identity = IdentityMatrix();569 570 for (int i = 0; i < 16; ++ i)571 trafos[i+16] = ((const float *)identity.x)[i];572 573 //sCgSsaoProgram->SetMatrixArray(i ++, trafos, 2);574 //sCgSsaoProgram->SetMatrix(i ++, IdentityMatrix());575 576 DrawQuad(sCgSsaoProgram);577 578 glPopAttrib();579 580 PrintGLerror("ssao first pass");581 }582 583 584 static void SetVertex(float x, float y, float x_offs, float y_offs)585 {586 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center587 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top588 glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom589 glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top590 glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom591 592 glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right593 glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom594 595 //glVertex3f(x - 0.5f, y - 0.5f, -0.5f);596 glVertex2f(x, y);597 }598 599 600 void DeferredRenderer::AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light)601 {602 FrameBufferObject::Release();603 604 ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);605 606 GLuint colorsTex = colorBuffer->GetTexture();607 GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();608 609 sCgAntiAliasingProgram->SetTexture(0, colorsTex);610 sCgAntiAliasingProgram->SetTexture(1, normalsTex);611 612 sCgAntiAliasingProgram->Bind();613 614 glBegin(GL_QUADS);615 616 // the neighbouring texels617 float x_offs = 1.0f / mWidth;618 float y_offs = 1.0f / mHeight;619 620 SetVertex(0, 0, x_offs, y_offs);621 SetVertex(1, 0, x_offs, y_offs);622 SetVertex(1, 1, x_offs, y_offs);623 SetVertex(0, 1, x_offs, y_offs);624 625 glEnd();626 627 PrintGLerror("antialiasing");628 }629 630 631 void DeferredRenderer::FirstPass(FrameBufferObject *fbo, DirectionalLight *light)632 {633 GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();634 GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();635 636 fbo->Bind();637 638 colorBufferIdx = 3 - colorBufferIdx;639 glDrawBuffers(1, mrt + colorBufferIdx);640 641 const Vector3 lightDir = -light->GetDirection();642 643 sCgDeferredProgram->SetTexture(0, colorsTex);644 sCgDeferredProgram->SetTexture(1, normalsTex);645 sCgDeferredProgram->SetValue3f(2, lightDir.x, lightDir.y, lightDir.z);646 647 DrawQuad(sCgDeferredProgram);648 649 PrintGLerror("deferred shading");650 }651 652 653 void DeferredRenderer::ComputeGlobIllum(FrameBufferObject *fbo,654 float tempCohFactor)655 {656 #if 0657 GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();658 GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();659 #else660 GLuint colorsTex = mDownSampleFbo->GetColorBuffer(0)->GetTexture();661 GLuint normalsTex = mDownSampleFbo->GetColorBuffer(1)->GetTexture();662 #endif663 664 glPushAttrib(GL_VIEWPORT_BIT);665 glViewport(0, 0, mIllumFbo->GetWidth(), mIllumFbo->GetHeight());666 667 // read the second buffer, write to the first buffer668 mIllumFbo->Bind();669 663 670 664 glDrawBuffers(2, mrt + mIllumFboIndex); … … 672 666 GLuint oldSsaoTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture(); 673 667 GLuint oldIllumTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex + 1)->GetTexture(); 674 675 668 676 669 sCgGiProgram->SetTexture(0, colorsTex);
Note: See TracChangeset
for help on using the changeset viewer.