Ignore:
Timestamp:
10/16/08 19:51:16 (16 years ago)
Author:
mattausch
Message:
 
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  
    319319        InitProgram(&sCgLogLumProgram, context, "tonemap", "CalcAvgLogLum"); 
    320320 
     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 
    321362        float filterWeights[NUM_PCF_TABS]; 
    322363        PoissonDiscSampleGenerator2 poisson(NUM_PCF_TABS, 1.0f); 
     
    451492        glDrawBuffers(1, mrt + mIllumFboIndex); 
    452493 
     494        sCgSsaoProgram->SetTexture(0, colorsTex); 
     495        sCgSsaoProgram->SetTexture(1, normalsTex); 
     496        sCgSsaoProgram->SetTexture(2, oldTex); 
     497        sCgSsaoProgram->SetTexture(3, noiseTex); 
     498 
    453499        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); 
    465505 
    466506        if (mUseTemporalCoherence || mRegenerateSamples) 
     
    472512                // needs longer to converge 
    473513                GenerateSamples(mSamplingMethod);  
    474                 sCgSsaoProgram->SetArray2f("samples", (float *)samples2, NUM_SAMPLES); 
     514                sCgSsaoProgram->SetArray2f(8, (float *)samples2, NUM_SAMPLES); 
    475515        } 
    476516 
     
    478518        ComputeViewVectors(tl, tr, bl, br); 
    479519 
    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); 
    484524 
    485525        DrawQuad(sCgSsaoProgram); 
     
    528568        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
    529569 
    530         sCgAntiAliasingProgram->SetTexture("colors", colorsTex); 
    531         sCgAntiAliasingProgram->SetTexture("normals", normalsTex); 
     570        sCgAntiAliasingProgram->SetTexture(0, colorsTex); 
     571        sCgAntiAliasingProgram->SetTexture(1, normalsTex); 
    532572 
    533573        sCgAntiAliasingProgram->Bind(); 
     
    565605        const Vector3 lightDir = -light->GetDirection(); 
    566606 
    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); 
    570610         
    571611        DrawQuad(sCgDeferredProgram); 
     
    603643         
    604644 
    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,  
    612656                (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0); 
    613657 
     
    621665                GenerateSamples(mSamplingMethod);  
    622666 
    623                 sCgGiProgram->SetArray2f("samples", (float *)samples2, NUM_SAMPLES); 
     667                sCgGiProgram->SetArray2f(7, (float *)samples2, NUM_SAMPLES); 
    624668        } 
    625669 
     
    628672        ComputeViewVectors(tl, tr, bl, br); 
    629673         
    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); 
    637678 
    638679        DrawQuad(sCgGiProgram); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp

    r3031 r3034  
    44#include "glInterface.h" 
    55#include "RenderState.h" 
     6#include "ShaderProgram.h" 
     7 
     8using namespace std; 
    69 
    710 
     
    2023        mAlphaTestEnabled = false; 
    2124        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; 
    2731} 
    2832 
    2933 
    30 Material::Material() 
     34Material::Material(): 
     35mAmbientColor(RgbaColor(0.2f, 0.2f, 0.2f, 1.0f)), 
     36mDiffuseColor(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f)), 
     37mSpecularColor(RgbaColor(.0f, .0f, .0f, 1.0f)), 
     38mEmmisiveColor(RgbaColor(.0f, .0f, .0f, 1.0f)) 
    3139{ 
    3240        InitMaterial(); 
     41} 
     42 
     43 
     44Material::~Material() 
     45{ 
     46        DEL_PTR(mGPUFragmentParameters); 
     47        DEL_PTR(mGPUVertexParameters); 
    3348} 
    3449 
     
    4055mTexture(NULL) 
    4156{ 
     57        InitMaterial(); 
    4258} 
    4359 
     
    5773void Material::Render(RenderState *state) 
    5874{ 
    59         state->SetState(mTexture, mAlphaTestEnabled, mCullFaceEnabled); 
     75        state->SetState(this); 
    6076 
    6177        glMaterialfv(GL_FRONT, GL_AMBIENT, (float *)&mAmbientColor.r); 
     
    6379        glMaterialfv(GL_FRONT, GL_EMISSION, (float *)&mEmmisiveColor.r); 
    6480        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        } 
    6592} 
    6693 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.h

    r2968 r3034  
    1111class Texture; 
    1212class RenderState; 
     13class GPUProgramParameters; 
     14class ShaderProgram; 
    1315 
    1416 
     
    4547        Material(); 
    4648 
     49        ~Material(); 
    4750        /** Sets ambient and diffuse color to color 
    4851        */ 
     
    7073        inline bool IsAlphaTestEnabled() const { return mAlphaTestEnabled; }  
    7174        inline bool IsCullFaceEnabled() const { return mCullFaceEnabled; }  
    72  
    7375        /** Renders this material. 
    7476        */ 
     
    7779        */ 
    7880        void InitMaterial(); 
     81 
     82        GPUProgramParameters *mGPUVertexParameters; 
     83        GPUProgramParameters *mGPUFragmentParameters; 
     84 
     85        ShaderProgram *mFragmentProgram; 
     86        ShaderProgram *mVertexProgram; 
    7987 
    8088protected: 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp

    r3031 r3034  
    2525 
    2626RenderState::RenderState(): 
    27 mAlphaTestEnabled(false),  
    28 mCullFaceEnabled(true),  
    29 mTexturesEnabled(false),  
    30 mMode(RENDER), 
    3127mRenderType(FIXED), 
    3228mUseAlphaToCoverage(true), 
     
    6258                glDepthMask(GL_FALSE); 
    6359 
    64                 SetState(false, false, false); 
     60                static Material defaultMat; 
     61                SetState(&defaultMat); 
    6562        } 
    6663        else // mode returns to render 
     
    8885 
    8986         
    90 void RenderState::SetState(Texture *tex, bool alphaTest, bool cullFace) 
    91 { 
     87void RenderState::SetState(Material *mat) 
     88{ 
     89        Texture *tex = mat->GetTexture(); 
     90 
    9291        const bool texturing = (tex != NULL); 
     92 
     93        bool alphaTest = mat->IsAlphaTestEnabled(); 
     94        bool cullFace = mat->IsCullFaceEnabled(); 
    9395 
    9496        if (!mLockCullFaceEnabled) 
     
    129131                mTexturesEnabled = false; 
    130132 
    131                 if (mRenderType == DEFERRED) 
    132                         sCgMrtFragmentProgram->Bind(); 
    133                 else 
     133                if (mRenderType != DEFERRED)//!mat->mFragmentProgram) 
    134134                        glDisable(GL_TEXTURE_2D); 
    135135 
     
    143143                        mTexturesEnabled = true; 
    144144 
    145                         if (mRenderType == DEFERRED) 
    146                                 sCgMrtFragmentTexProgram->Bind(); 
    147                         else 
     145                        if (mRenderType != DEFERRED)//!mat->mFragmentProgram) 
    148146                                glEnable(GL_TEXTURE_2D); 
    149147 
     
    152150        } 
    153151 
    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        } 
    155183} 
    156184 
     
    158186void RenderState::Reset() 
    159187{ 
     188        SetMode(RENDER); 
     189 
    160190        mCurrentVboId = -1; 
     191        mCurrentTexId = 0; 
    161192 
    162193        mCullFaceEnabled = true; 
     194        mAlphaTestEnabled = false; 
     195        mTexturesEnabled = false; 
     196 
     197        mCurrentVertexProgram = NULL; 
     198        mCurrentFragmentProgram = NULL; 
     199 
    163200        glEnable(GL_CULL_FACE); 
    164          
    165         mAlphaTestEnabled = false; 
    166          
     201 
    167202        glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB); 
    168203        glDisable(GL_ALPHA_TEST); 
    169204 
    170         mTexturesEnabled = false; 
    171  
    172205        glDisable(GL_TEXTURE_2D); 
    173206        glDisableClientState(GL_TEXTURE_COORD_ARRAY); 
    174  
    175         SetMode(RENDER); 
    176207} 
    177208 
     
    201232 
    202233 
    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  
    1414class ShaderProgram; 
    1515class Texture; 
     16class Material; 
    1617 
    1718/** The current render state. 
     
    4748        /** Sets the current render state. 
    4849        */ 
    49         void SetState(Texture *tex, bool alphaTest, bool cullFace); 
     50        void SetState(Material *mat); 
    5051        /** Returns either query or render mode 
    5152        */ 
     
    6061        */ 
    6162        inline int GetCurrentVboId() const { return mCurrentVboId; } 
    62         /** If alpha to coverage is instead of alpha testing 
    63         */ 
    64         void SetUseAlphaToCoverage(bool useAlphaToCoverage); 
    6563        /** Sets the type of rendering used (foreward, depth pass, deferred) 
    6664        */ 
     
    6967        */ 
    7068        RenderPassType GetRenderPassType() const; 
     69        /** If alpha to coverage is instead of alpha testing 
     70        */ 
     71        void SetUseAlphaToCoverage(bool useAlphaToCoverage); 
    7172        /** If true, the current value of cull face enabled / disable will 
    7273                not be changed by the render state. 
    7374        */ 
    7475        void LockCullFaceEnabled(bool lockCull); 
    75         /** Set a new texture. 
    76         */ 
    77         void SetTexture(Texture *tex); 
    78  
    79  
     76         
     77         
    8078        /////////////////// 
    8179        //-- for deferred shading, we have to switch shaders on and off 
     
    9391protected: 
    9492 
     93        ////////////////// 
     94 
    9595        bool mAlphaTestEnabled; 
    9696        bool mCullFaceEnabled; 
     
    106106 
    107107        bool mLockCullFaceEnabled; 
     108 
     109        int mCurrentTexId; 
     110 
     111        ShaderProgram *mCurrentVertexProgram; 
     112        ShaderProgram *mCurrentFragmentProgram; 
    108113}; 
    109114 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp

    r3021 r3034  
    1111#include "Shape.h" 
    1212#include "LODInfo.h" 
     13#include "RenderState.h" 
     14#include "ShaderProgram.h" 
    1315 
    1416 
     
    185187        str.read(reinterpret_cast<char *>(&mat->mCullFaceEnabled), sizeof(bool)); 
    186188 
    187         // material 
    188         bool hasMaterial; 
    189         str.read(reinterpret_cast<char *>(&hasMaterial), sizeof(bool)); 
    190          
    191         if (hasMaterial) 
     189        // gl material 
     190        bool hasGlMaterial; 
     191        str.read(reinterpret_cast<char *>(&hasGlMaterial), sizeof(bool)); 
     192         
     193        if (hasGlMaterial) 
    192194        { 
    193195                // only write rgb part of the material 
     
    198200        } 
    199201 
     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 
    200219        return mat; 
    201220} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp

    r2961 r3034  
    8484 
    8585 
    86 void SceneEntity::GetLODLevel(int level,  
     86void SceneEntity::GetLODLevel(int level, 
    8787                                                          ShapeContainer::iterator &start,  
    8888                                                          ShapeContainer::iterator &end) 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.cpp

    r3033 r3034  
    4646void GPUProgramParameters::SetTexture(int idx, unsigned int tex) 
    4747{ 
     48        mTextures.push_back(IntParam(idx, tex)); 
    4849} 
    4950 
     
    5657void GPUProgramParameters::SetValue1f(const string &name, float val) 
    5758{ 
    58         SetValue1f(mProgram->GetParameter(name), val); 
    59 } 
    60  
    61  
    62 void ShaderProgram::SetValue2f(const string &name, float val1, float val2) 
    63 { 
    64         SetValue2f(mProgram->GetParameter(name), val1, val2); 
     59        SetValue1f(mProgram->GetParamIdxByName(name), val); 
     60} 
     61 
     62 
     63void GPUProgramParameters::SetValue2f(const string &name, float val1, float val2) 
     64{ 
     65        SetValue2f(mProgram->GetParamIdxByName(name), val1, val2); 
    6566} 
    6667 
     
    6869void GPUProgramParameters::SetValue3f(const string &name, float val1, float val2, float val3) 
    6970{ 
    70         SetValue3f(mProgram->GetParameter(name), val1, val2, val3); 
     71        SetValue3f(mProgram->GetParamIdxByName(name), val1, val2, val3); 
    7172} 
    7273 
     
    7475void GPUProgramParameters::SetArray1f(const string &name, float *vals, int numElements) 
    7576{ 
    76         SetArray1f(mProgram->GetParameter(name), numElements, (const float *)vals); 
    77 } 
    78  
    79  
    80 void GPUProgramParameters::GetParameter(const string &name, float *vals, int numElements) 
    81 { 
    82         SetArray2f(mProgram->GetParameter(name, numElements, (const float *)vals); 
     77        SetArray1f(mProgram->GetParamIdxByName(name), vals, numElements); 
     78} 
     79 
     80 
     81void GPUProgramParameters::SetArray2f(const string &name, float *vals, int numElements) 
     82{ 
     83        SetArray2f(mProgram->GetParamIdxByName(name), vals, numElements); 
    8384} 
    8485 
     
    8687void GPUProgramParameters::SetArray3f(const string &name, float *vals, int numElements) 
    8788{ 
    88         SetArray3f(mProgram->GetParameter(name), numElements, (const float *)vals); 
     89        SetArray3f(mProgram->GetParamIdxByName(name), vals, numElements); 
    8990} 
    9091 
     
    9293void GPUProgramParameters::SetMatrix(const string &name, Matrix4x4 *mat) 
    9394{ 
    94         SetMatrix(mProgram->GetParameter(name), mat); 
     95        SetMatrix(mProgram->GetParamIdxByName(name), mat); 
    9596} 
    9697 
     
    9899void GPUProgramParameters::SetTexture(const string &name, unsigned int tex) 
    99100{ 
    100         SetTexture(mProgram->GetOrCreateParameter(name), tex); 
    101 } 
     101        SetTexture(mProgram->GetParamIdxByName(name), tex); 
     102} 
     103 
     104 
     105void 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/********************************************************/ 
    102145 
    103146 
     
    116159        if (mProgram) cgGLLoadProgram(mProgram); 
    117160 
    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) 
    121165        { 
    122166                mParameters[i] = NULL; 
     
    151195        CGparameter p = GetOrCreateParameter(name); 
    152196 
     197        mParamHash[name] = idx; 
    153198        mParameters[idx] = p; 
     199 
    154200        return p; 
    155201} 
     
    164210        { 
    165211                p = cgGetNamedParameter(mProgram, name.c_str()); 
    166                 mParamHash[name] = i; 
    167212        } 
    168213        else 
     
    178223CGparameter ShaderProgram::GetParameter(int idx) const 
    179224{ 
    180         CGparameter p = mParameters[idx]; 
    181         return p; 
     225        return mParameters[idx]; 
    182226} 
    183227 
     
    231275        cgGLSetTextureParameter(p, tex); 
    232276        cgGLEnableTextureParameter(p); 
     277} 
     278 
     279 
     280void ShaderProgram::SetValue1f(const string &name, float val) 
     281{ 
     282        cgGLSetParameter1f(GetOrCreateParameter(name), val); 
     283} 
     284 
     285 
     286void ShaderProgram::SetValue2f(const string &name, float val1, float val2) 
     287{ 
     288        cgGLSetParameter2f(GetOrCreateParameter(name), val1, val2); 
     289} 
     290 
     291 
     292void ShaderProgram::SetValue3f(const string &name, float val1, float val2, float val3) 
     293{ 
     294        cgGLSetParameter3f(GetOrCreateParameter(name), val1, val2, val3); 
     295} 
     296 
     297 
     298void ShaderProgram::SetArray1f(const string &name, float *vals, int numElements) 
     299{ 
     300        cgGLSetParameterArray1f(GetOrCreateParameter(name), 0, numElements, (const float *)vals); 
     301} 
     302 
     303 
     304void ShaderProgram::SetArray2f(const string &name, float *vals, int numElements) 
     305{ 
     306        cgGLSetParameterArray2f(GetOrCreateParameter(name), 0, numElements, (const float *)vals); 
     307} 
     308 
     309 
     310void ShaderProgram::SetArray3f(const string &name, float *vals, int numElements) 
     311{ 
     312        cgGLSetParameterArray3f(GetOrCreateParameter(name), 0, numElements, (const float *)vals); 
     313} 
     314 
     315 
     316void ShaderProgram::SetMatrix(const string &name, const Matrix4x4 &mat) 
     317{ 
     318        cgGLSetMatrixParameterfc(GetOrCreateParameter(name), (const float *)mat.x); 
     319} 
     320 
     321 
     322void ShaderProgram::SetTexture(const string &name, unsigned int tex) 
     323{ 
     324        CGparameter p = GetOrCreateParameter(name); 
     325 
     326        cgGLSetTextureParameter(p, tex); 
     327        cgGLEnableTextureParameter(p); 
    233328 
    234329        mTextureParams.push_back(p); 
     
    236331 
    237332 
    238 int ShaderProgram::GetParameter(const std::string &name) 
    239 { 
    240         return mParamHash[name]; 
     333int ShaderProgram::GetParamIdxByName(const std::string &name) const 
     334{ 
     335        CGParameterMap::const_iterator it = mParamHash.find(name); 
     336        return (*it).second; 
    241337} 
    242338 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h

    r3033 r3034  
    3434class GPUProgramParameters 
    3535{ 
     36public: 
     37 
     38        /////////// 
     39        //-- set parameters by index 
     40 
    3641        void SetValue1f(int idx, float value); 
    3742        void SetValue2f(int idx, float val1, float val2); 
     
    4954        void SetMatrix(int idx, Matrix4x4 *mat); 
    5055 
     56 
     57        /////////// 
     58        //-- set parameters by name (slower!) 
     59 
     60 
    5161        void SetValue1f(const std::string &name, float value); 
    5262        void SetValue2f(const std::string &name, float val1, float val2); 
     
    5666        void SetArray2f(const std::string &name, float *vals, int numElements); 
    5767        void SetArray3f(const std::string &name, float *vals, int numElements); 
     68 
    5869        /** Sets the texture parameter. 
    5970        */ 
     
    6273        */ 
    6374        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 
     83protected: 
     84 
     85        struct FloatParam 
    6686        { 
    6787                int mIndex; 
    6888                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 
    7496                int mIndex; 
    7597                float mValue; 
    7698        }; 
    7799 
    78         struct ParameterValueMatrix 
     100        struct MatrixParam 
    79101        { 
    80102                int mIndex; 
     
    82104        }; 
    83105 
    84         struct ParameterValueArray 
     106        struct ArrayParam 
    85107        { 
    86108                int mIndex; 
     
    89111                int mNumEntries; 
    90112 
    91                 float mNumComponents; 
    92         }; 
    93  
    94  
    95         GPUProgramParameters(ShaderProgram *p); 
    96  
    97  
    98 protected: 
     113                int mNumComponents; 
     114        }; 
     115 
    99116 
    100117        ShaderProgram *mProgram; 
    101118 
    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; 
    106123}; 
    107124 
     
    134151        void SetTexture(int idx, unsigned int tex); 
    135152 
     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 
    136167        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; } 
    138174        /** Enable / disable a texture parameter. 
    139175        */ 
     
    141177        //void DisableTexture(const std::string &name); 
    142178 
    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); 
    151179 
    152180 
    153181protected: 
    154182 
    155         inline CGparameter GetParameter(int idx); 
     183        inline int GetParamIdxByName(const std::string &name) const; 
     184 
     185        inline CGparameter GetParameter(int idx) const; 
    156186 
    157187        CGparameter GetOrCreateParameter(const std::string &name); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h

    r2965 r3034  
    110110        /// size of the shadow map 
    111111        int mSize; 
    112  
     112        /// the shadow view 
    113113        Camera *mShadowCam; 
    114114        /// the texture matrix 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.h

    r2848 r3034  
    6868        Material *mMaterial; 
    6969        SceneEntity *mParent; 
     70 
    7071        /// pointer to the renderqueue bucket this entity belongs to 
    7172        RenderQueueBucket *mRenderQueueBucket; 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Transform3.cpp

    r3031 r3034  
    2525{ 
    2626        if (mIsIdentity) return; 
    27          
    28         // note: would not need to pass the model matrix,  
    29         // as all required information is avaiabl in the glstate 
    30         // however, we require it compute the word positions, 
    31         // as computing the world position by use the inverse of the viewprojection 
    32         // matrix turned out to be too inaccurate for temporal reprojection 
    33  
    34         if (state->GetRenderPassType() == RenderState::DEFERRED) 
    35         { 
    36                 //cgGLSetMatrixParameterfc(sModelMatrixParam, (const float *)mMatrix.x); 
    37                 RenderState::sCgMrtVertexProgram->SetMatrix("modelView", mMatrix); 
    38         } 
    3927 
    4028        glPushMatrix(); 
     
    4634{ 
    4735        if (mIsIdentity) return; 
    48  
    49         // reset model matrix 
    50         if (state->GetRenderPassType() == RenderState::DEFERRED) 
    51         { 
    52                 static Matrix4x4 identity = IdentityMatrix(); 
    53                 RenderState::sCgMrtVertexProgram->SetMatrix("modelView", identity); 
    54         } 
    5536 
    5637        glPopMatrix(); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r3031 r3034  
    452452        MiddleMotion(0, 0); 
    453453 
     454        // init cg shader programs 
     455        InitCg(); 
     456 
    454457        perfGraph = new PerformanceGraph(1000); 
    455458 
     
    458461        const string filename = string(model_path + "city.dem"); 
    459462         
    460  
    461463        if (loader->Load(filename, sceneEntities)) 
    462464                cout << "model " << filename << " loaded" << endl; 
     
    486488        camera->SetFar(Magnitude(bvh->GetBox().Diagonal())); 
    487489         
    488         InitCg(); 
    489  
    490490        Vector3 cubeCenter(470.398f, 240.364f, 182.5f); 
    491491         
     
    510510        skyDome = sceneEntities.back(); 
    511511 
     512        //InitCg(); 
    512513 
    513514        const float turbitiy = 5.0f; 
     
    537538void InitCg(void)  
    538539{ 
    539         // Setup Cg 
     540        // setup cg 
    540541        cgSetErrorCallback(cgErrorCallback); 
    541542 
    542         // Create cgContext. 
     543        // create context. 
    543544        sCgContext = cgCreateContext(); 
    544545 
    545         // get the best profile for this hardware 
     546        // get the optimal profile 
    546547        RenderState::sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT); 
    547548        cgGLSetOptimalOptions(RenderState::sCgFragmentProfile); 
     
    550551        cgGLSetOptimalOptions(RenderState::sCgVertexProfile); 
    551552 
    552         cgGLSetManageTextureParameters(sCgContext, true); 
     553        // set textures to auto load 
     554        cgGLSetManageTextureParameters(sCgContext, false); 
    553555 
    554556        RenderState::sCgMrtVertexProgram =  
     
    561563                new ShaderProgram(sCgContext, "src/shaders/mrt.cg", RenderState::sCgFragmentProfile, "fragtex"); 
    562564 
     565         
    563566        if (!RenderState::sCgMrtFragmentTexProgram->IsValid()) 
    564567                cerr << "fragment tex program failed to load" << endl; 
     568 
     569        RenderState::sCgMrtFragmentTexProgram->AddParameter("tex", 0); 
    565570 
    566571        RenderState::sCgMrtFragmentProgram =  
     
    851856        // store matrix for later use 
    852857        viewProjMat = matViewing * matProjection; 
    853          
    854         if ((renderMethod == RENDER_DEFERRED) || (renderMethod == RENDER_DEPTH_PASS_DEFERRED)) 
    855         { 
    856                 RenderState::sCgMrtVertexProgram->SetMatrix("modelView", IdentityMatrix()); 
    857         } 
    858858} 
    859859 
     
    912912 
    913913        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); 
    917914 
    918915 
     
    972969 
    973970        } 
    974  
    975971         
    976972        // bring eye modelview matrix up-to-date 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg

    r3026 r3034  
    175175        const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w; 
    176176 
    177         float4 position; 
    178         position.xyz = eyePos - viewDir * eyeDepth; 
    179          
     177        const float4 worldPos = float4(eyePos - viewDir * eyeDepth, 1); 
    180178         
    181179        // diffuse intensity 
     
    193191                ) 
    194192        { 
    195                 position.w = 1.0f; 
    196  
    197                 float4 lightSpacePos = mul(shadowMatrix, position); 
     193                float4 lightSpacePos = mul(shadowMatrix, worldPos); 
    198194                lightSpacePos /= lightSpacePos.w; 
    199195 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg

    r3026 r3034  
    179179        const float3 eyeSpacePos = -viewDir * eyeDepth; 
    180180 
    181         const float3 centerPosition = eyePos + eyeSpacePos; 
    182         const float4 realPos = float4(centerPosition, 1.0f); 
     181        const float4 worldPos = float4(eyePos + eyeSpacePos, 1.0f); 
    183182 
    184183        // calculcate the current projected depth for next frame 
    185         float4 currentPos = mul(modelViewProj, realPos); 
     184        float4 currentPos = mul(modelViewProj, worldPos); 
     185 
    186186        const float w = SAMPLE_RADIUS / currentPos.w; 
    187187        currentPos /= currentPos.w; 
     188         
    188189        const float currentDepth = currentPos.z * 1e-3f; 
    189190 
     
    197198        //-- compute temporally smoothing 
    198199 
    199          
    200200        // reprojection new frame into old one  
    201201        // calculate projected depth 
    202         float4 projPos = mul(oldModelViewProj, realPos); 
     202        float4 projPos = mul(oldModelViewProj, worldPos); 
    203203        projPos /= projPos.w; 
    204204 
     
    210210 
    211211        // 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; 
    214214 
    215215        const float oldDepth = oldSsao.z; 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg

    r3031 r3034  
    1515 
    1616  float4 color: COLOR0;   
    17   float4 worldPos: TEXCOORD1; // world position 
     17  float4 eyePos: TEXCOORD1; // eye position 
    1818  float3 normal: TEXCOORD2; 
    1919}; 
     
    2424{ 
    2525        float4 color: COLOR0;   
    26         float4 position: POSITION; // eye space 
     26        //float4 position: POSITION; 
    2727        float4 texCoord: TEXCOORD0;     
    2828 
    2929        float4 winPos: WPOS; 
    30         float4 worldPos: TEXCOORD1; // world position 
     30        float4 eyePos: TEXCOORD1; // eye position 
    3131        float3 normal: TEXCOORD2; 
    3232}; 
     
    4040}; 
    4141 
     42 
    4243#pragma position_invariant vtx 
    4344 
    44 vtxout vtx(vtxin IN,  
    45                    uniform float4x4 modelView) 
     45vtxout vtx(vtxin IN) 
    4646{ 
    4747        vtxout OUT; 
     
    5050        OUT.texCoord = IN.texCoord; 
    5151 
    52         //OUT.worldPos = mul(glstate.matrix.inverse.projection, OUT.position); 
    53         OUT.worldPos = mul(modelView, IN.position); 
    5452        // 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 
    5555        OUT.position = mul(glstate.matrix.mvp, IN.position); 
    5656 
     
    6060} 
    6161 
    62  
    63 // bilinear interpolation 
    64 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  
    7362//#pragma position_invariant fragtex 
    7463 
    7564pixel 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 
    8266                          ) 
    8367{ 
     
    9983 
    10084        // compute eye linear depth 
    101         pix.col.w = length(eyePos - IN.worldPos.xyz); 
     85        pix.col.w = length(IN.eyePos.xyz); 
    10286 
    10387        return pix; 
     
    10589 
    10690 
    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) 
     91pixel frag(fragin IN) 
    11392{ 
    11493        pixel pix; 
     
    11998        pix.norm = IN.normal; 
    12099 
     100        pix.col.w = length(IN.eyePos.xyz); 
     101 
    121102        // hack: squeeze some information about the ambient term into the target 
    122103        //pix.col.w = glstate.material.emission.x; 
    123         pix.col.w = length(eyePos - IN.worldPos.xyz); 
    124104 
    125105        return pix; 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg

    r3028 r3034  
    159159        const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w; 
    160160        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); 
    164162 
    165163 
     
    167165        //-- calculcate the current projected depth for next frame 
    168166         
    169         float4 currentPos = mul(modelViewProj, realPos); 
     167        float4 currentPos = mul(modelViewProj, worldPos); 
    170168         
    171169        const float w = SAMPLE_RADIUS / currentPos.w; 
     
    185183         
    186184        // calculate projected depth 
    187         float4 projPos = mul(oldModelViewProj, realPos); 
     185        float4 projPos = mul(oldModelViewProj, worldPos); 
    188186        projPos /= projPos.w; 
    189  
     187         
    190188        // the current depth projected into the old frame 
    191189        const float projDepth = projPos.z * precisionScale; 
Note: See TracChangeset for help on using the changeset viewer.