Changeset 3034
- Timestamp:
- 10/16/08 19:51:16 (16 years ago)
- Location:
- GTP/trunk/App/Demos/Vis/FriendlyCulling/src
- Files:
-
- 17 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
r3028 r3034 319 319 InitProgram(&sCgLogLumProgram, context, "tonemap", "CalcAvgLogLum"); 320 320 321 int i; 322 323 i = 0; 324 325 sCgSsaoProgram->AddParameter("colors", i ++); 326 sCgSsaoProgram->AddParameter("colors", i ++); 327 sCgSsaoProgram->AddParameter("oldTex", i ++); 328 sCgSsaoProgram->AddParameter("noise", i ++); 329 sCgSsaoProgram->AddParameter("eyePos", i ++); 330 sCgSsaoProgram->AddParameter("temporalCoherence", i ++); 331 sCgSsaoProgram->AddParameter("modelViewProj", i ++); 332 sCgSsaoProgram->AddParameter("oldModelViewProj", i ++); 333 sCgSsaoProgram->AddParameter("samples", i ++); 334 sCgSsaoProgram->AddParameter("bl", i ++); 335 sCgSsaoProgram->AddParameter("br", i ++); 336 sCgSsaoProgram->AddParameter("tl", i ++); 337 sCgSsaoProgram->AddParameter("tr", i ++); 338 339 i = 0; 340 341 sCgGiProgram->AddParameter("colors", i ++); 342 sCgGiProgram->AddParameter("colors", i ++); 343 sCgGiProgram->AddParameter("noise", i ++); 344 sCgGiProgram->AddParameter("oldSsaoTex", i ++); 345 sCgGiProgram->AddParameter("oldIllumTex", i ++); 346 sCgGiProgram->AddParameter("eyePos", i ++); 347 sCgGiProgram->AddParameter("temporalCoherence", i ++); 348 sCgGiProgram->AddParameter("samples", i ++); 349 sCgGiProgram->AddParameter("bl", i ++); 350 sCgGiProgram->AddParameter("br", i ++); 351 sCgGiProgram->AddParameter("tl", i ++); 352 sCgGiProgram->AddParameter("tr", i ++); 353 354 sCgAntiAliasingProgram->AddParameter("colors", 0); 355 sCgAntiAliasingProgram->AddParameter("normals", 1); 356 357 sCgDeferredProgram->AddParameter("colors", 0); 358 sCgDeferredProgram->AddParameter("normals", 1); 359 sCgDeferredProgram->AddParameter("lightDir", 2); 360 361 321 362 float filterWeights[NUM_PCF_TABS]; 322 363 PoissonDiscSampleGenerator2 poisson(NUM_PCF_TABS, 1.0f); … … 451 492 glDrawBuffers(1, mrt + mIllumFboIndex); 452 493 494 sCgSsaoProgram->SetTexture(0, colorsTex); 495 sCgSsaoProgram->SetTexture(1, normalsTex); 496 sCgSsaoProgram->SetTexture(2, oldTex); 497 sCgSsaoProgram->SetTexture(3, noiseTex); 498 453 499 const Vector3 pos = mCamera->GetPosition(); 454 sCgSsaoProgram->SetValue3f("eyePos", pos.x, pos.y, pos.z); 455 456 sCgSsaoProgram->SetTexture("colors", colorsTex); 457 sCgSsaoProgram->SetTexture("normals", normalsTex); 458 sCgSsaoProgram->SetTexture("oldTex", oldTex); 459 sCgSsaoProgram->SetTexture("noise", noiseTex); 460 461 sCgSsaoProgram->SetValue1f("temporalCoherence", (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0); 462 463 sCgSsaoProgram->SetMatrix("modelViewProj", projViewMatrix); 464 sCgSsaoProgram->SetMatrix("oldModelViewProj", oldProjViewMatrix); 500 sCgSsaoProgram->SetValue3f(4, pos.x, pos.y, pos.z); 501 502 sCgSsaoProgram->SetValue1f(5, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0); 503 sCgSsaoProgram->SetMatrix(6, projViewMatrix); 504 sCgSsaoProgram->SetMatrix(7, oldProjViewMatrix); 465 505 466 506 if (mUseTemporalCoherence || mRegenerateSamples) … … 472 512 // needs longer to converge 473 513 GenerateSamples(mSamplingMethod); 474 sCgSsaoProgram->SetArray2f( "samples", (float *)samples2, NUM_SAMPLES);514 sCgSsaoProgram->SetArray2f(8, (float *)samples2, NUM_SAMPLES); 475 515 } 476 516 … … 478 518 ComputeViewVectors(tl, tr, bl, br); 479 519 480 sCgSsaoProgram->SetValue3f( "bl", bl.x, bl.y, bl.z);481 sCgSsaoProgram->SetValue3f( "br", br.x, br.y, br.z);482 sCgSsaoProgram->SetValue3f( "tl", tl.x, tl.y, tl.z);483 sCgSsaoProgram->SetValue3f( "tr", tr.x, tr.y, tr.z);520 sCgSsaoProgram->SetValue3f(9, bl.x, bl.y, bl.z); 521 sCgSsaoProgram->SetValue3f(10, br.x, br.y, br.z); 522 sCgSsaoProgram->SetValue3f(11, tl.x, tl.y, tl.z); 523 sCgSsaoProgram->SetValue3f(12, tr.x, tr.y, tr.z); 484 524 485 525 DrawQuad(sCgSsaoProgram); … … 528 568 GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 529 569 530 sCgAntiAliasingProgram->SetTexture( "colors", colorsTex);531 sCgAntiAliasingProgram->SetTexture( "normals", normalsTex);570 sCgAntiAliasingProgram->SetTexture(0, colorsTex); 571 sCgAntiAliasingProgram->SetTexture(1, normalsTex); 532 572 533 573 sCgAntiAliasingProgram->Bind(); … … 565 605 const Vector3 lightDir = -light->GetDirection(); 566 606 567 sCgDeferredProgram->SetTexture( "colors", colorsTex);568 sCgDeferredProgram->SetTexture( "normals", normalsTex);569 sCgDeferredProgram->SetValue3f( "lightDir", lightDir.x, lightDir.y, lightDir.z);607 sCgDeferredProgram->SetTexture(0, colorsTex); 608 sCgDeferredProgram->SetTexture(1, normalsTex); 609 sCgDeferredProgram->SetValue3f(2, lightDir.x, lightDir.y, lightDir.z); 570 610 571 611 DrawQuad(sCgDeferredProgram); … … 603 643 604 644 605 sCgGiProgram->SetTexture("colors", colorsTex); 606 sCgGiProgram->SetTexture("normals", normalsTex); 607 sCgGiProgram->SetTexture("noise", noiseTex); 608 sCgGiProgram->SetTexture("oldSsaoTex", oldSsaoTex); 609 sCgGiProgram->SetTexture("oldIllumTex", oldIllumTex); 610 611 sCgGiProgram->SetValue1f("temporalCoherence", 645 sCgGiProgram->SetTexture(0, colorsTex); 646 sCgGiProgram->SetTexture(1, normalsTex); 647 sCgGiProgram->SetTexture(2, noiseTex); 648 sCgGiProgram->SetTexture(3, oldSsaoTex); 649 sCgGiProgram->SetTexture(4, oldIllumTex); 650 651 const Vector3 pos = mCamera->GetPosition(); 652 sCgGiProgram->SetValue3f(5, pos.x, pos.y, pos.z); 653 654 655 sCgGiProgram->SetValue1f(6, 612 656 (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0); 613 657 … … 621 665 GenerateSamples(mSamplingMethod); 622 666 623 sCgGiProgram->SetArray2f( "samples", (float *)samples2, NUM_SAMPLES);667 sCgGiProgram->SetArray2f(7, (float *)samples2, NUM_SAMPLES); 624 668 } 625 669 … … 628 672 ComputeViewVectors(tl, tr, bl, br); 629 673 630 const Vector3 pos = mCamera->GetPosition(); 631 sCgGiProgram->SetValue3f("eyePos", pos.x, pos.y, pos.z); 632 633 sCgGiProgram->SetValue3f("bl", bl.x, bl.y, bl.z); 634 sCgGiProgram->SetValue3f("br", br.x, br.y, br.z); 635 sCgGiProgram->SetValue3f("tl", tl.x, tl.y, tl.z); 636 sCgGiProgram->SetValue3f("tr", tr.x, tr.y, tr.z); 674 sCgGiProgram->SetValue3f(8, bl.x, bl.y, bl.z); 675 sCgGiProgram->SetValue3f(9, br.x, br.y, br.z); 676 sCgGiProgram->SetValue3f(10, tl.x, tl.y, tl.z); 677 sCgGiProgram->SetValue3f(11, tr.x, tr.y, tr.z); 637 678 638 679 DrawQuad(sCgGiProgram); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp
r3031 r3034 4 4 #include "glInterface.h" 5 5 #include "RenderState.h" 6 #include "ShaderProgram.h" 7 8 using namespace std; 6 9 7 10 … … 20 23 mAlphaTestEnabled = false; 21 24 mCullFaceEnabled = true; 22 23 mAmbientColor = RgbaColor(0.2f, 0.2f, 0.2f, 1.0f); 24 mDiffuseColor = RgbaColor(1.0f, 1.0f, 1.0f, 1.0f); 25 mSpecularColor = RgbaColor(.0f, .0f, .0f, 1.0f); 26 mEmmisiveColor = RgbaColor(.0f, .0f, .0f, 1.0f); 25 26 mGPUVertexParameters = NULL; 27 mVertexProgram = NULL; 28 29 mGPUFragmentParameters = NULL; 30 mFragmentProgram = NULL; 27 31 } 28 32 29 33 30 Material::Material() 34 Material::Material(): 35 mAmbientColor(RgbaColor(0.2f, 0.2f, 0.2f, 1.0f)), 36 mDiffuseColor(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f)), 37 mSpecularColor(RgbaColor(.0f, .0f, .0f, 1.0f)), 38 mEmmisiveColor(RgbaColor(.0f, .0f, .0f, 1.0f)) 31 39 { 32 40 InitMaterial(); 41 } 42 43 44 Material::~Material() 45 { 46 DEL_PTR(mGPUFragmentParameters); 47 DEL_PTR(mGPUVertexParameters); 33 48 } 34 49 … … 40 55 mTexture(NULL) 41 56 { 57 InitMaterial(); 42 58 } 43 59 … … 57 73 void Material::Render(RenderState *state) 58 74 { 59 state->SetState( mTexture, mAlphaTestEnabled, mCullFaceEnabled);75 state->SetState(this); 60 76 61 77 glMaterialfv(GL_FRONT, GL_AMBIENT, (float *)&mAmbientColor.r); … … 63 79 glMaterialfv(GL_FRONT, GL_EMISSION, (float *)&mEmmisiveColor.r); 64 80 glMaterialfv(GL_FRONT, GL_SPECULAR, (float *)&mSpecularColor.r); 81 82 83 if (mVertexProgram) 84 { 85 mGPUVertexParameters->UpdateParameters(); 86 } 87 88 if (mFragmentProgram) 89 { 90 mGPUFragmentParameters->UpdateParameters(); 91 } 65 92 } 66 93 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.h
r2968 r3034 11 11 class Texture; 12 12 class RenderState; 13 class GPUProgramParameters; 14 class ShaderProgram; 13 15 14 16 … … 45 47 Material(); 46 48 49 ~Material(); 47 50 /** Sets ambient and diffuse color to color 48 51 */ … … 70 73 inline bool IsAlphaTestEnabled() const { return mAlphaTestEnabled; } 71 74 inline bool IsCullFaceEnabled() const { return mCullFaceEnabled; } 72 73 75 /** Renders this material. 74 76 */ … … 77 79 */ 78 80 void InitMaterial(); 81 82 GPUProgramParameters *mGPUVertexParameters; 83 GPUProgramParameters *mGPUFragmentParameters; 84 85 ShaderProgram *mFragmentProgram; 86 ShaderProgram *mVertexProgram; 79 87 80 88 protected: -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp
r3031 r3034 25 25 26 26 RenderState::RenderState(): 27 mAlphaTestEnabled(false),28 mCullFaceEnabled(true),29 mTexturesEnabled(false),30 mMode(RENDER),31 27 mRenderType(FIXED), 32 28 mUseAlphaToCoverage(true), … … 62 58 glDepthMask(GL_FALSE); 63 59 64 SetState(false, false, false); 60 static Material defaultMat; 61 SetState(&defaultMat); 65 62 } 66 63 else // mode returns to render … … 88 85 89 86 90 void RenderState::SetState(Texture *tex, bool alphaTest, bool cullFace) 91 { 87 void RenderState::SetState(Material *mat) 88 { 89 Texture *tex = mat->GetTexture(); 90 92 91 const bool texturing = (tex != NULL); 92 93 bool alphaTest = mat->IsAlphaTestEnabled(); 94 bool cullFace = mat->IsCullFaceEnabled(); 93 95 94 96 if (!mLockCullFaceEnabled) … … 129 131 mTexturesEnabled = false; 130 132 131 if (mRenderType == DEFERRED) 132 sCgMrtFragmentProgram->Bind(); 133 else 133 if (mRenderType != DEFERRED)//!mat->mFragmentProgram) 134 134 glDisable(GL_TEXTURE_2D); 135 135 … … 143 143 mTexturesEnabled = true; 144 144 145 if (mRenderType == DEFERRED) 146 sCgMrtFragmentTexProgram->Bind(); 147 else 145 if (mRenderType != DEFERRED)//!mat->mFragmentProgram) 148 146 glEnable(GL_TEXTURE_2D); 149 147 … … 152 150 } 153 151 154 SetTexture(tex); 152 if (mRenderType == DEFERRED) 153 { 154 if (mat->mFragmentProgram && (mat->mFragmentProgram != mCurrentFragmentProgram)) 155 { 156 mCurrentFragmentProgram = mat->mFragmentProgram; 157 mCurrentFragmentProgram->Bind(); 158 } 159 160 if (mat->mVertexProgram && (mat->mVertexProgram != mCurrentVertexProgram)) 161 { 162 mCurrentVertexProgram = mat->mVertexProgram; 163 mCurrentVertexProgram->Bind(); 164 } 165 } 166 else 167 { 168 // set fixed texture 169 if (tex) 170 { 171 if (mCurrentTexId != tex->GetId()) 172 { 173 tex->Bind(); 174 mCurrentTexId = tex->GetId(); 175 } 176 } 177 else 178 { 179 mCurrentTexId = 0; 180 glBindTexture(GL_TEXTURE_2D, 0); 181 } 182 } 155 183 } 156 184 … … 158 186 void RenderState::Reset() 159 187 { 188 SetMode(RENDER); 189 160 190 mCurrentVboId = -1; 191 mCurrentTexId = 0; 161 192 162 193 mCullFaceEnabled = true; 194 mAlphaTestEnabled = false; 195 mTexturesEnabled = false; 196 197 mCurrentVertexProgram = NULL; 198 mCurrentFragmentProgram = NULL; 199 163 200 glEnable(GL_CULL_FACE); 164 165 mAlphaTestEnabled = false; 166 201 167 202 glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB); 168 203 glDisable(GL_ALPHA_TEST); 169 204 170 mTexturesEnabled = false;171 172 205 glDisable(GL_TEXTURE_2D); 173 206 glDisableClientState(GL_TEXTURE_COORD_ARRAY); 174 175 SetMode(RENDER);176 207 } 177 208 … … 201 232 202 233 203 void RenderState::SetTexture(Texture *tex) 204 { 205 if (GetRenderPassType() == RenderState::DEFERRED) 206 { 207 if (tex) 208 RenderState::sCgMrtFragmentTexProgram->SetTexture("tex", tex->GetId()); 209 } 210 else 211 { 212 if (tex) 213 tex->Bind(); 214 else 215 glBindTexture(GL_TEXTURE_2D, 0); 216 } 217 } 218 219 220 } 234 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h
r3031 r3034 14 14 class ShaderProgram; 15 15 class Texture; 16 class Material; 16 17 17 18 /** The current render state. … … 47 48 /** Sets the current render state. 48 49 */ 49 void SetState( Texture *tex, bool alphaTest, bool cullFace);50 void SetState(Material *mat); 50 51 /** Returns either query or render mode 51 52 */ … … 60 61 */ 61 62 inline int GetCurrentVboId() const { return mCurrentVboId; } 62 /** If alpha to coverage is instead of alpha testing63 */64 void SetUseAlphaToCoverage(bool useAlphaToCoverage);65 63 /** Sets the type of rendering used (foreward, depth pass, deferred) 66 64 */ … … 69 67 */ 70 68 RenderPassType GetRenderPassType() const; 69 /** If alpha to coverage is instead of alpha testing 70 */ 71 void SetUseAlphaToCoverage(bool useAlphaToCoverage); 71 72 /** If true, the current value of cull face enabled / disable will 72 73 not be changed by the render state. 73 74 */ 74 75 void LockCullFaceEnabled(bool lockCull); 75 /** Set a new texture. 76 */ 77 void SetTexture(Texture *tex); 78 79 76 77 80 78 /////////////////// 81 79 //-- for deferred shading, we have to switch shaders on and off … … 93 91 protected: 94 92 93 ////////////////// 94 95 95 bool mAlphaTestEnabled; 96 96 bool mCullFaceEnabled; … … 106 106 107 107 bool mLockCullFaceEnabled; 108 109 int mCurrentTexId; 110 111 ShaderProgram *mCurrentVertexProgram; 112 ShaderProgram *mCurrentFragmentProgram; 108 113 }; 109 114 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp
r3021 r3034 11 11 #include "Shape.h" 12 12 #include "LODInfo.h" 13 #include "RenderState.h" 14 #include "ShaderProgram.h" 13 15 14 16 … … 185 187 str.read(reinterpret_cast<char *>(&mat->mCullFaceEnabled), sizeof(bool)); 186 188 187 // material188 bool has Material;189 str.read(reinterpret_cast<char *>(&has Material), sizeof(bool));190 191 if (has Material)189 // gl material 190 bool hasGlMaterial; 191 str.read(reinterpret_cast<char *>(&hasGlMaterial), sizeof(bool)); 192 193 if (hasGlMaterial) 192 194 { 193 195 // only write rgb part of the material … … 198 200 } 199 201 202 mat->mVertexProgram = RenderState::sCgMrtVertexProgram; 203 204 if (mat->GetTexture()) 205 { 206 mat->mFragmentProgram = RenderState::sCgMrtFragmentTexProgram; 207 208 mat->mGPUFragmentParameters = new GPUProgramParameters(mat->mFragmentProgram); 209 mat->mGPUFragmentParameters->SetTexture(0, mat->GetTexture()->GetId()); 210 } 211 else 212 { 213 mat->mFragmentProgram = RenderState::sCgMrtFragmentProgram; 214 mat->mGPUFragmentParameters = new GPUProgramParameters(mat->mFragmentProgram); 215 } 216 217 mat->mGPUVertexParameters = new GPUProgramParameters(RenderState::sCgMrtVertexProgram); 218 200 219 return mat; 201 220 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp
r2961 r3034 84 84 85 85 86 void SceneEntity::GetLODLevel(int level, 86 void SceneEntity::GetLODLevel(int level, 87 87 ShapeContainer::iterator &start, 88 88 ShapeContainer::iterator &end) -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.cpp
r3033 r3034 46 46 void GPUProgramParameters::SetTexture(int idx, unsigned int tex) 47 47 { 48 mTextures.push_back(IntParam(idx, tex)); 48 49 } 49 50 … … 56 57 void GPUProgramParameters::SetValue1f(const string &name, float val) 57 58 { 58 SetValue1f(mProgram->GetParam eter(name), val);59 } 60 61 62 void ShaderProgram::SetValue2f(const string &name, float val1, float val2)63 { 64 SetValue2f(mProgram->GetParam eter(name), val1, val2);59 SetValue1f(mProgram->GetParamIdxByName(name), val); 60 } 61 62 63 void GPUProgramParameters::SetValue2f(const string &name, float val1, float val2) 64 { 65 SetValue2f(mProgram->GetParamIdxByName(name), val1, val2); 65 66 } 66 67 … … 68 69 void GPUProgramParameters::SetValue3f(const string &name, float val1, float val2, float val3) 69 70 { 70 SetValue3f(mProgram->GetParam eter(name), val1, val2, val3);71 SetValue3f(mProgram->GetParamIdxByName(name), val1, val2, val3); 71 72 } 72 73 … … 74 75 void GPUProgramParameters::SetArray1f(const string &name, float *vals, int numElements) 75 76 { 76 SetArray1f(mProgram->GetParam eter(name), numElements, (const float *)vals);77 } 78 79 80 void GPUProgramParameters:: GetParameter(const string &name, float *vals, int numElements)81 { 82 SetArray2f(mProgram->GetParam eter(name, numElements, (const float *)vals);77 SetArray1f(mProgram->GetParamIdxByName(name), vals, numElements); 78 } 79 80 81 void GPUProgramParameters::SetArray2f(const string &name, float *vals, int numElements) 82 { 83 SetArray2f(mProgram->GetParamIdxByName(name), vals, numElements); 83 84 } 84 85 … … 86 87 void GPUProgramParameters::SetArray3f(const string &name, float *vals, int numElements) 87 88 { 88 SetArray3f(mProgram->GetParam eter(name), numElements, (const float *)vals);89 SetArray3f(mProgram->GetParamIdxByName(name), vals, numElements); 89 90 } 90 91 … … 92 93 void GPUProgramParameters::SetMatrix(const string &name, Matrix4x4 *mat) 93 94 { 94 SetMatrix(mProgram->GetParam eter(name), mat);95 SetMatrix(mProgram->GetParamIdxByName(name), mat); 95 96 } 96 97 … … 98 99 void GPUProgramParameters::SetTexture(const string &name, unsigned int tex) 99 100 { 100 SetTexture(mProgram->GetOrCreateParameter(name), tex); 101 } 101 SetTexture(mProgram->GetParamIdxByName(name), tex); 102 } 103 104 105 void GPUProgramParameters::UpdateParameters() 106 { 107 for (size_t i = 0; i < mFloats.size(); ++ i) 108 { 109 const FloatParam &p = mFloats[i]; 110 111 switch (p.mNumComponents) 112 { 113 case 1: 114 mProgram->SetValue1f(p.mIndex, p.mValues[0]); 115 break; 116 case 2: 117 mProgram->SetValue2f(p.mIndex, p.mValues[0], p.mValues[1]); 118 break; 119 case 3: 120 mProgram->SetValue3f(p.mIndex, p.mValues[0], p.mValues[1], p.mValues[2]); 121 break; 122 default: 123 mProgram->SetValue1f(p.mIndex, p.mValues[0]); 124 } 125 } 126 127 for (size_t i = 0; i < mTextures.size(); ++ i) 128 { 129 const IntParam &p = mTextures[i]; 130 mProgram->SetTexture(p.mIndex, p.mValue); 131 } 132 133 for (size_t i = 0; i < mMatrices.size(); ++ i) 134 { 135 const MatrixParam &p = mMatrices[i]; 136 mProgram->SetMatrix(p.mIndex, *p.mValue); 137 } 138 } 139 140 141 142 /********************************************************/ 143 /* ShaderProgram implementation */ 144 /********************************************************/ 102 145 103 146 … … 116 159 if (mProgram) cgGLLoadProgram(mProgram); 117 160 118 mParameters.resize(64); 119 120 for (int i = 0; i < 64; ++ i) 161 const int maxParams = 64; 162 mParameters.resize(maxParams); 163 164 for (int i = 0; i < maxParams; ++ i) 121 165 { 122 166 mParameters[i] = NULL; … … 151 195 CGparameter p = GetOrCreateParameter(name); 152 196 197 mParamHash[name] = idx; 153 198 mParameters[idx] = p; 199 154 200 return p; 155 201 } … … 164 210 { 165 211 p = cgGetNamedParameter(mProgram, name.c_str()); 166 mParamHash[name] = i;167 212 } 168 213 else … … 178 223 CGparameter ShaderProgram::GetParameter(int idx) const 179 224 { 180 CGparameter p = mParameters[idx]; 181 return p; 225 return mParameters[idx]; 182 226 } 183 227 … … 231 275 cgGLSetTextureParameter(p, tex); 232 276 cgGLEnableTextureParameter(p); 277 } 278 279 280 void ShaderProgram::SetValue1f(const string &name, float val) 281 { 282 cgGLSetParameter1f(GetOrCreateParameter(name), val); 283 } 284 285 286 void ShaderProgram::SetValue2f(const string &name, float val1, float val2) 287 { 288 cgGLSetParameter2f(GetOrCreateParameter(name), val1, val2); 289 } 290 291 292 void ShaderProgram::SetValue3f(const string &name, float val1, float val2, float val3) 293 { 294 cgGLSetParameter3f(GetOrCreateParameter(name), val1, val2, val3); 295 } 296 297 298 void ShaderProgram::SetArray1f(const string &name, float *vals, int numElements) 299 { 300 cgGLSetParameterArray1f(GetOrCreateParameter(name), 0, numElements, (const float *)vals); 301 } 302 303 304 void ShaderProgram::SetArray2f(const string &name, float *vals, int numElements) 305 { 306 cgGLSetParameterArray2f(GetOrCreateParameter(name), 0, numElements, (const float *)vals); 307 } 308 309 310 void ShaderProgram::SetArray3f(const string &name, float *vals, int numElements) 311 { 312 cgGLSetParameterArray3f(GetOrCreateParameter(name), 0, numElements, (const float *)vals); 313 } 314 315 316 void ShaderProgram::SetMatrix(const string &name, const Matrix4x4 &mat) 317 { 318 cgGLSetMatrixParameterfc(GetOrCreateParameter(name), (const float *)mat.x); 319 } 320 321 322 void ShaderProgram::SetTexture(const string &name, unsigned int tex) 323 { 324 CGparameter p = GetOrCreateParameter(name); 325 326 cgGLSetTextureParameter(p, tex); 327 cgGLEnableTextureParameter(p); 233 328 234 329 mTextureParams.push_back(p); … … 236 331 237 332 238 int ShaderProgram::GetParameter(const std::string &name) 239 { 240 return mParamHash[name]; 333 int ShaderProgram::GetParamIdxByName(const std::string &name) const 334 { 335 CGParameterMap::const_iterator it = mParamHash.find(name); 336 return (*it).second; 241 337 } 242 338 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h
r3033 r3034 34 34 class GPUProgramParameters 35 35 { 36 public: 37 38 /////////// 39 //-- set parameters by index 40 36 41 void SetValue1f(int idx, float value); 37 42 void SetValue2f(int idx, float val1, float val2); … … 49 54 void SetMatrix(int idx, Matrix4x4 *mat); 50 55 56 57 /////////// 58 //-- set parameters by name (slower!) 59 60 51 61 void SetValue1f(const std::string &name, float value); 52 62 void SetValue2f(const std::string &name, float val1, float val2); … … 56 66 void SetArray2f(const std::string &name, float *vals, int numElements); 57 67 void SetArray3f(const std::string &name, float *vals, int numElements); 68 58 69 /** Sets the texture parameter. 59 70 */ … … 62 73 */ 63 74 void SetMatrix(const std::string &name, Matrix4x4 *mat); 64 65 struct ParameterFloat 75 /** Feeds the shader program with the parameter values. 76 */ 77 void UpdateParameters(); 78 79 80 GPUProgramParameters(ShaderProgram *p); 81 82 83 protected: 84 85 struct FloatParam 66 86 { 67 87 int mIndex; 68 88 float mValues[4]; 69 float mNumComponents; 70 }; 71 72 struct ParameterValueInt 73 { 89 int mNumComponents; 90 }; 91 92 struct IntParam 93 { 94 IntParam(int idx, float val): mIndex(idx), mValue(val) {} 95 74 96 int mIndex; 75 97 float mValue; 76 98 }; 77 99 78 struct ParameterValueMatrix100 struct MatrixParam 79 101 { 80 102 int mIndex; … … 82 104 }; 83 105 84 struct ParameterValueArray106 struct ArrayParam 85 107 { 86 108 int mIndex; … … 89 111 int mNumEntries; 90 112 91 float mNumComponents; 92 }; 93 94 95 GPUProgramParameters(ShaderProgram *p); 96 97 98 protected: 113 int mNumComponents; 114 }; 115 99 116 100 117 ShaderProgram *mProgram; 101 118 102 std::vector< ParameterFloat> mFloats;103 104 std::vector< ParameterValueInt> mTextures;105 std::vector< ParameterValueMatrix> mMatrices;119 std::vector<FloatParam> mFloats; 120 std::vector<IntParam> mTextures; 121 std::vector<MatrixParam> mMatrices; 122 std::vector<ArrayParam> mArrays; 106 123 }; 107 124 … … 134 151 void SetTexture(int idx, unsigned int tex); 135 152 153 void SetValue1f(const std::string &name, float value); 154 void SetValue2f(const std::string &name, float val1, float val2); 155 void SetValue3f(const std::string &name, float val1, float val, float val3); 156 157 void SetArray1f(const std::string &name, float *vals, int numElements); 158 void SetArray2f(const std::string &name, float *vals, int numElements); 159 void SetArray3f(const std::string &name, float *vals, int numElements); 160 /** Sets the matrix parameter. 161 */ 162 void SetMatrix(const std::string &name, const Matrix4x4 &mat); 163 /** Sets the texture parameter. 164 */ 165 void SetTexture(const std::string &name, unsigned int tex); 166 136 167 CGparameter AddParameter(const std::string &name, int idx); 137 168 /** Binds the program. 169 */ 170 void Bind(); 171 /** Returns true if this program is valid. 172 */ 173 inline bool IsValid() const { return mProgram != NULL; } 138 174 /** Enable / disable a texture parameter. 139 175 */ … … 141 177 //void DisableTexture(const std::string &name); 142 178 143 /** Binds the program.144 */145 void Bind();146 /** Returns true if this program is valid.147 */148 inline bool IsValid() const { return mProgram != NULL; }149 150 inline int GetParameter(const std::string &name);151 179 152 180 153 181 protected: 154 182 155 inline CGparameter GetParameter(int idx); 183 inline int GetParamIdxByName(const std::string &name) const; 184 185 inline CGparameter GetParameter(int idx) const; 156 186 157 187 CGparameter GetOrCreateParameter(const std::string &name); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
r2965 r3034 110 110 /// size of the shadow map 111 111 int mSize; 112 112 /// the shadow view 113 113 Camera *mShadowCam; 114 114 /// the texture matrix -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.h
r2848 r3034 68 68 Material *mMaterial; 69 69 SceneEntity *mParent; 70 70 71 /// pointer to the renderqueue bucket this entity belongs to 71 72 RenderQueueBucket *mRenderQueueBucket; -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Transform3.cpp
r3031 r3034 25 25 { 26 26 if (mIsIdentity) return; 27 28 // note: would not need to pass the model matrix,29 // as all required information is avaiabl in the glstate30 // however, we require it compute the word positions,31 // as computing the world position by use the inverse of the viewprojection32 // matrix turned out to be too inaccurate for temporal reprojection33 34 if (state->GetRenderPassType() == RenderState::DEFERRED)35 {36 //cgGLSetMatrixParameterfc(sModelMatrixParam, (const float *)mMatrix.x);37 RenderState::sCgMrtVertexProgram->SetMatrix("modelView", mMatrix);38 }39 27 40 28 glPushMatrix(); … … 46 34 { 47 35 if (mIsIdentity) return; 48 49 // reset model matrix50 if (state->GetRenderPassType() == RenderState::DEFERRED)51 {52 static Matrix4x4 identity = IdentityMatrix();53 RenderState::sCgMrtVertexProgram->SetMatrix("modelView", identity);54 }55 36 56 37 glPopMatrix(); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
r3031 r3034 452 452 MiddleMotion(0, 0); 453 453 454 // init cg shader programs 455 InitCg(); 456 454 457 perfGraph = new PerformanceGraph(1000); 455 458 … … 458 461 const string filename = string(model_path + "city.dem"); 459 462 460 461 463 if (loader->Load(filename, sceneEntities)) 462 464 cout << "model " << filename << " loaded" << endl; … … 486 488 camera->SetFar(Magnitude(bvh->GetBox().Diagonal())); 487 489 488 InitCg();489 490 490 Vector3 cubeCenter(470.398f, 240.364f, 182.5f); 491 491 … … 510 510 skyDome = sceneEntities.back(); 511 511 512 //InitCg(); 512 513 513 514 const float turbitiy = 5.0f; … … 537 538 void InitCg(void) 538 539 { 539 // Setup Cg540 // setup cg 540 541 cgSetErrorCallback(cgErrorCallback); 541 542 542 // Create cgContext.543 // create context. 543 544 sCgContext = cgCreateContext(); 544 545 545 // get the best profile for this hardware546 // get the optimal profile 546 547 RenderState::sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT); 547 548 cgGLSetOptimalOptions(RenderState::sCgFragmentProfile); … … 550 551 cgGLSetOptimalOptions(RenderState::sCgVertexProfile); 551 552 552 cgGLSetManageTextureParameters(sCgContext, true); 553 // set textures to auto load 554 cgGLSetManageTextureParameters(sCgContext, false); 553 555 554 556 RenderState::sCgMrtVertexProgram = … … 561 563 new ShaderProgram(sCgContext, "src/shaders/mrt.cg", RenderState::sCgFragmentProfile, "fragtex"); 562 564 565 563 566 if (!RenderState::sCgMrtFragmentTexProgram->IsValid()) 564 567 cerr << "fragment tex program failed to load" << endl; 568 569 RenderState::sCgMrtFragmentTexProgram->AddParameter("tex", 0); 565 570 566 571 RenderState::sCgMrtFragmentProgram = … … 851 856 // store matrix for later use 852 857 viewProjMat = matViewing * matProjection; 853 854 if ((renderMethod == RENDER_DEFERRED) || (renderMethod == RENDER_DEPTH_PASS_DEFERRED))855 {856 RenderState::sCgMrtVertexProgram->SetMatrix("modelView", IdentityMatrix());857 }858 858 } 859 859 … … 912 912 913 913 const Vector3 pos = camera->GetPosition(); 914 915 RenderState::sCgMrtFragmentProgram->SetValue3f("eyePos", pos.x, pos.y, pos.z);916 RenderState::sCgMrtFragmentTexProgram->SetValue3f("eyePos", pos.x, pos.y, pos.z);917 914 918 915 … … 972 969 973 970 } 974 975 971 976 972 // bring eye modelview matrix up-to-date -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
r3026 r3034 175 175 const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w; 176 176 177 float4 position; 178 position.xyz = eyePos - viewDir * eyeDepth; 179 177 const float4 worldPos = float4(eyePos - viewDir * eyeDepth, 1); 180 178 181 179 // diffuse intensity … … 193 191 ) 194 192 { 195 position.w = 1.0f; 196 197 float4 lightSpacePos = mul(shadowMatrix, position); 193 float4 lightSpacePos = mul(shadowMatrix, worldPos); 198 194 lightSpacePos /= lightSpacePos.w; 199 195 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg
r3026 r3034 179 179 const float3 eyeSpacePos = -viewDir * eyeDepth; 180 180 181 const float3 centerPosition = eyePos + eyeSpacePos; 182 const float4 realPos = float4(centerPosition, 1.0f); 181 const float4 worldPos = float4(eyePos + eyeSpacePos, 1.0f); 183 182 184 183 // calculcate the current projected depth for next frame 185 float4 currentPos = mul(modelViewProj, realPos); 184 float4 currentPos = mul(modelViewProj, worldPos); 185 186 186 const float w = SAMPLE_RADIUS / currentPos.w; 187 187 currentPos /= currentPos.w; 188 188 189 const float currentDepth = currentPos.z * 1e-3f; 189 190 … … 197 198 //-- compute temporally smoothing 198 199 199 200 200 // reprojection new frame into old one 201 201 // calculate projected depth 202 float4 projPos = mul(oldModelViewProj, realPos);202 float4 projPos = mul(oldModelViewProj, worldPos); 203 203 projPos /= projPos.w; 204 204 … … 210 210 211 211 // retrieve the sample from the last frame 212 float3 oldSsao = tex2D(oldSsaoTex, tex) ;213 float3 oldIllum = tex2D(oldIllumTex, tex) ;212 float3 oldSsao = tex2D(oldSsaoTex, tex).xyz; 213 float3 oldIllum = tex2D(oldIllumTex, tex).xyz; 214 214 215 215 const float oldDepth = oldSsao.z; -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg
r3031 r3034 15 15 16 16 float4 color: COLOR0; 17 float4 worldPos: TEXCOORD1; // worldposition17 float4 eyePos: TEXCOORD1; // eye position 18 18 float3 normal: TEXCOORD2; 19 19 }; … … 24 24 { 25 25 float4 color: COLOR0; 26 float4 position: POSITION; // eye space26 //float4 position: POSITION; 27 27 float4 texCoord: TEXCOORD0; 28 28 29 29 float4 winPos: WPOS; 30 float4 worldPos: TEXCOORD1; // worldposition30 float4 eyePos: TEXCOORD1; // eye position 31 31 float3 normal: TEXCOORD2; 32 32 }; … … 40 40 }; 41 41 42 42 43 #pragma position_invariant vtx 43 44 44 vtxout vtx(vtxin IN, 45 uniform float4x4 modelView) 45 vtxout vtx(vtxin IN) 46 46 { 47 47 vtxout OUT; … … 50 50 OUT.texCoord = IN.texCoord; 51 51 52 //OUT.worldPos = mul(glstate.matrix.inverse.projection, OUT.position);53 OUT.worldPos = mul(modelView, IN.position);54 52 // transform the vertex position into eye space 53 OUT.eyePos = mul(glstate.matrix.modelview[0], IN.position); 54 // transform the vertex position into post projection space 55 55 OUT.position = mul(glstate.matrix.mvp, IN.position); 56 56 … … 60 60 } 61 61 62 63 // bilinear interpolation64 inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)65 {66 float3 x1 = lerp(bl, tl, w.y);67 float3 x2 = lerp(br, tr, w.y);68 float3 v = lerp(x1, x2, w.x);69 70 return v;71 }72 73 62 //#pragma position_invariant fragtex 74 63 75 64 pixel fragtex(fragin IN, 76 uniform sampler2D tex, 77 uniform float3 eyePos, 78 uniform float3 bl, 79 uniform float3 br, 80 uniform float3 tl, 81 uniform float3 tr 65 uniform sampler2D tex 82 66 ) 83 67 { … … 99 83 100 84 // compute eye linear depth 101 pix.col.w = length( eyePos - IN.worldPos.xyz);85 pix.col.w = length(IN.eyePos.xyz); 102 86 103 87 return pix; … … 105 89 106 90 107 pixel frag(fragin IN, 108 uniform float3 eyePos, 109 uniform float3 bl, 110 uniform float3 br, 111 uniform float3 tl, 112 uniform float3 tr) 91 pixel frag(fragin IN) 113 92 { 114 93 pixel pix; … … 119 98 pix.norm = IN.normal; 120 99 100 pix.col.w = length(IN.eyePos.xyz); 101 121 102 // hack: squeeze some information about the ambient term into the target 122 103 //pix.col.w = glstate.material.emission.x; 123 pix.col.w = length(eyePos - IN.worldPos.xyz);124 104 125 105 return pix; -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
r3028 r3034 159 159 const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w; 160 160 const float3 eyeSpacePos = -viewDir * eyeDepth; 161 const float3 centerPosition = eyePos + eyeSpacePos; 162 163 const float4 realPos = float4(centerPosition, 1.0f); 161 const float4 worldPos = float4(eyePos + eyeSpacePos, 1.0f); 164 162 165 163 … … 167 165 //-- calculcate the current projected depth for next frame 168 166 169 float4 currentPos = mul(modelViewProj, realPos);167 float4 currentPos = mul(modelViewProj, worldPos); 170 168 171 169 const float w = SAMPLE_RADIUS / currentPos.w; … … 185 183 186 184 // calculate projected depth 187 float4 projPos = mul(oldModelViewProj, realPos);185 float4 projPos = mul(oldModelViewProj, worldPos); 188 186 projPos /= projPos.w; 189 187 190 188 // the current depth projected into the old frame 191 189 const float projDepth = projPos.z * precisionScale;
Note: See TracChangeset
for help on using the changeset viewer.