Changeset 3046


Ignore:
Timestamp:
10/20/08 01:26:12 (16 years ago)
Author:
mattausch
Message:

added shader for forward shadin tree animation program

Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp

    r3045 r3046  
    4242static CGprofile sCgFragmentProfile; 
    4343static CGprofile sCgVertexProfile;  
    44 static ShaderProgram *sTreeAnimation; 
     44 
     45static ShaderProgram *sTreeAnimationProgramMrt; 
     46static ShaderProgram *sTreeAnimationProgram; 
     47 
    4548 
    4649// only instance of the resource manager 
     
    153156 
    154157        /////////// 
    155         //-- hack: tree animation 
     158        //-- hack: add tree animation 
    156159 
    157160        if (numLODs > 1) 
    158161        { 
    159                 for (int i = 0; i < min(numLODs, 2); ++ i) 
     162                //for (int i = 0; i < min(numLODs, 2); ++ i) 
     163                for (int i = 0; i < numLODs; ++ i) 
    160164                { 
    161165                        ShapeContainer::iterator sstart, send; 
     
    172176                                { 
    173177                                        Technique *tech = mat->GetTechnique(i); 
    174                                         tech->SetVertexProgram(sTreeAnimation); 
    175178 
    176179                                        GPUProgramParameters *vtxParams = tech->GetVertexProgramParameters(); 
    177180 
     181                                        if (i == 0) 
     182                                        { 
     183                                                tech->SetVertexProgram(sTreeAnimationProgram); 
     184                                                vtxParams->SetLightDirParam(5); 
     185                                        } 
     186                                        else 
     187                                        { 
     188                                                tech->SetVertexProgram(sTreeAnimationProgramMrt); 
     189                                        } 
     190 
     191                                        /// use a timer to simulate the moving of the tree in the wind 
    178192                                        vtxParams->SetTimerParam(0); 
    179193                                        // wind direction 
     
    352366 
    353367 
    354 void ResourceManager::LoadSceneEntities(igzstream &str, SceneEntityContainer &entities) 
     368void ResourceManager::LoadSceneEntities(igzstream &str,  
     369                                                                                SceneEntityContainer &entities) 
    355370{ 
    356371        int entityCount; 
     
    373388 
    374389 
    375 bool ResourceManager::Load(const std::string &filename, SceneEntityContainer &entities) 
     390bool ResourceManager::Load(const std::string &filename, 
     391                                                   SceneEntityContainer &entities) 
    376392{ 
    377393        igzstream istr(filename.c_str()); 
     
    468484        mMrtDefaultFragmentProgram->AddParameter("viewMatrix", 0); 
    469485        mMrtDefaultFragmentTexProgram->AddParameter("viewMatrix", 0); 
    470  
    471486        // add a texture parameter 
    472487        mMrtDefaultFragmentTexProgram->AddParameter("tex", 1); 
    473488 
    474         sTreeAnimation = CreateVertexProgram("treeanimation", "animateVtxMrt"); 
    475  
    476         sTreeAnimation->AddParameter("timer", 0); 
    477         sTreeAnimation->AddParameter("windDir", 1); 
    478         sTreeAnimation->AddParameter("windStrength", 2); 
    479         sTreeAnimation->AddParameter("minMaxPos", 3); 
    480         sTreeAnimation->AddParameter("frequency", 4); 
     489 
     490        //////////// 
     491        //-- hack: apply hardcoded tree animation (should be provided with a material script!) 
     492 
     493        sTreeAnimationProgram = CreateVertexProgram("treeanimation", "animateVtx"); 
     494        sTreeAnimationProgramMrt = CreateVertexProgram("treeanimation", "animateVtxMrt"); 
     495 
     496        sTreeAnimationProgram->AddParameter("timer", 0); 
     497        sTreeAnimationProgram->AddParameter("windDir", 1); 
     498        sTreeAnimationProgram->AddParameter("windStrength", 2); 
     499        sTreeAnimationProgram->AddParameter("minMaxPos", 3); 
     500        sTreeAnimationProgram->AddParameter("frequency", 4); 
     501        sTreeAnimationProgram->AddParameter("lightDir", 5); 
     502 
     503        sTreeAnimationProgramMrt->AddParameter("timer", 0); 
     504        sTreeAnimationProgramMrt->AddParameter("windDir", 1); 
     505        sTreeAnimationProgramMrt->AddParameter("windStrength", 2); 
     506        sTreeAnimationProgramMrt->AddParameter("minMaxPos", 3); 
     507        sTreeAnimationProgramMrt->AddParameter("frequency", 4); 
    481508 
    482509        cout << "cg initialization successful" << endl; 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.cpp

    r3045 r3046  
    44#include "Vector3.h" 
    55#include "Camera.h" 
     6#include "Light.h" 
    67 
    78 
     
    1314////////// 
    1415//-- changing shader parameters 
     16 
    1517static float sCurrentTimer = 0; 
    1618static Matrix4x4 sCurrentViewMatrix = IdentityMatrix(); 
    17 static Vector3 sCurrentViewVector = Vector3::UNIT_Y(); 
     19static Vector3 sCurrentViewDir = Vector3::UNIT_Y(); 
     20static Vector3 sCurrentLightDir = Vector3::UNIT_Y(); 
     21 
     22 
     23/*********************************************************/ 
     24/*         GPUProgramParameters implementation           */ 
     25/*********************************************************/ 
    1826 
    1927 
     
    2129mProgram(p),  
    2230mTimerParam(-1), 
    23 mViewVectorParam(-1), 
    24 mViewMatrixParam(-1) 
     31mViewDirParam(-1), 
     32mViewMatrixParam(-1), 
     33mLightDirParam(-1) 
    2534{ 
    2635} 
     
    3039mProgram(NULL), 
    3140mTimerParam(-1), 
    32 mViewVectorParam(-1), 
    33 mViewMatrixParam(-1) 
     41mViewDirParam(-1), 
     42mViewMatrixParam(-1), 
     43mLightDirParam(-1) 
    3444{ 
    3545} 
     
    4454 
    4555        mTimerParam = -1; 
    46         mViewVectorParam = -1; 
     56        mViewDirParam = -1; 
     57        mLightDirParam = -1; 
    4758        mViewMatrixParam = -1; 
    4859} 
     
    137148 
    138149 
    139 void GPUProgramParameters::SetViewVectorParam(int idx) 
    140 { 
    141         mViewVectorParam = idx; 
     150void GPUProgramParameters::SetViewDirParam(int idx) 
     151{ 
     152        mViewDirParam = idx; 
     153} 
     154 
     155 
     156void GPUProgramParameters::SetLightDirParam(int idx) 
     157{ 
     158        mLightDirParam = idx; 
    142159} 
    143160 
     
    258275                mProgram->SetValue1f(mTimerParam, sCurrentTimer); 
    259276 
    260         if (mViewVectorParam >= 0) 
    261                 mProgram->SetValue3f(mViewVectorParam, sCurrentViewVector.x, sCurrentViewVector.y, sCurrentViewVector.z); 
    262  
     277        if (mViewDirParam >= 0) 
     278                mProgram->SetValue3f(mViewDirParam, sCurrentViewDir.x, sCurrentViewDir.y, sCurrentViewDir.z); 
     279        if (mLightDirParam >= 0) 
     280                mProgram->SetValue3f(mLightDirParam, sCurrentLightDir.x, sCurrentLightDir.y, sCurrentLightDir.z); 
    263281        if (mViewMatrixParam >= 0) 
    264282                mProgram->SetMatrix(mViewMatrixParam, sCurrentViewMatrix); 
     
    266284 
    267285 
    268 void GPUProgramParameters::InitFrame(Camera *cam) 
     286void GPUProgramParameters::InitFrame(Camera *cam, DirectionalLight *light) 
    269287{ 
    270288        static PerfTimer mytimer; 
     
    272290 
    273291        cam->GetModelViewMatrix(sCurrentViewMatrix); 
    274         sCurrentViewVector = cam->GetDirection(); 
     292        sCurrentViewDir = cam->GetDirection(); 
     293 
     294        Matrix4x4 vo; 
     295        // transform to view space 
     296        cam->GetViewOrientationMatrix(vo); 
     297        //sCurrentLightDir = /*vo **/ -light->GetDirection(); 
     298        sCurrentLightDir = vo * -light->GetDirection(); 
    275299} 
    276300 
     
    318342 
    319343 
    320 /*void ShaderProgram::Release() 
     344void ShaderProgram::Release() 
    321345{ 
    322346        CGParameterArray::const_iterator it, it_end = mTextureParams.end(); 
     
    325349        for (it = mTextureParams.begin(); it != it_end; ++ it) 
    326350                cgGLDisableTextureParameter(*it); 
    327 }*/ 
     351} 
    328352 
    329353 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h

    r3045 r3046  
    7070        */ 
    7171        void SetTimerParam(int idx); 
    72         /** This parameter is connected to the current view matrix that is updated 
    73                 once per frame. 
     72        /** This parameter is connected to the current view matrix 
     73                that is updated once per frame. 
    7474        */ 
    7575        void SetViewMatrixParam(int idx); 
    76         /** This parameter is connected to the current view vector that is updated 
    77                 once per frame. 
    78         */ 
    79         void SetViewVectorParam(int idx); 
     76        /** This parameter is connected to the current view direction  
     77                that is updated once per frame. 
     78        */ 
     79        void SetViewDirParam(int idx); 
     80        /** This parameter is connected to the current light direction  
     81                that is updated once per frame. 
     82        */ 
     83        void SetLightDirParam(int idx); 
    8084 
    8185         
     
    103107        /** Function should be called once per frame to update frame related parameters. 
    104108        */ 
    105         static void InitFrame(Camera *cam); 
     109        static void InitFrame(Camera *cam, DirectionalLight *light); 
    106110 
    107111 
     
    164168 
    165169        int mTimerParam; 
    166         int mViewVectorParam; 
     170        int mViewDirParam; 
     171        int mLightDirParam; 
    167172        int mViewMatrixParam; 
    168173 
     
    222227        void SetArray2f(const std::string &name, float *vals, int numElements); 
    223228        void SetArray3f(const std::string &name, float *vals, int numElements); 
    224         /// Sets a matrix parameter 
     229        /** Sets a matrix parameter 
     230        */ 
    225231        void SetMatrix(const std::string &name, const Matrix4x4 &mat); 
    226232        /** Sets the texture parameter. 
    227233        */ 
    228234        void SetTexture(const std::string &name, unsigned int tex); 
    229  
    230235        /** Binds the program. 
    231236        */ 
     
    234239        */ 
    235240        inline bool IsValid() const { return mProgram != NULL; } 
    236         /** Enable / disable a texture parameter. 
    237         */ 
    238         //void EnableTexture(const std::string &name); 
    239         //void DisableTexture(const std::string &name); 
    240  
     241        /** Releases all texture resources. 
     242        */ 
     243        void Release(); 
    241244 
    242245 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r3045 r3046  
    714714         
    715715 
    716         const bool useToneMapping = ((renderMethod == RENDER_DEPTH_PASS_DEFERRED) || (renderMethod == RENDER_DEFERRED)) && useHDR; 
     716        const bool useToneMapping =  
     717                ((renderMethod == RENDER_DEPTH_PASS_DEFERRED) ||  
     718                 (renderMethod == RENDER_DEFERRED)) && useHDR; 
     719 
    717720 
    718721        Vector3 sunAmbient; 
     
    883886 
    884887        // set GPU related parameters 
    885         GPUProgramParameters::InitFrame(camera); 
     888        GPUProgramParameters::InitFrame(camera, light); 
    886889 
    887890 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg

    r3045 r3046  
    9090 
    9191 
    92 pixel frag(fragin IN,  uniform float4x4 viewMatrix) 
     92pixel frag(fragin IN, uniform float4x4 viewMatrix) 
    9393{ 
    9494        pixel pix; 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/treeanimation.cg

    r3045 r3046  
    2424        //float4 color1: COLOR1;   
    2525        float4 eyePos: TEXCOORD1; // eye position 
    26         float3 normal: TEXCOORD2; 
     26        float4 normal: TEXCOORD2; 
    2727}; 
    2828 
     
    4040{ 
    4141        vtxout OUT; 
    42  
    43 /* 
    44 // Transform vertex position into homogenous clip-space. 
    45 OUT.HPosition = mul(ModelViewProj, IN.Position); 
    46 // Transform normal from model-space to view-space. 
    47 float3 normalVec = normalize(mul(ModelViewIT, 
    48 IN.Normal).xyz); 
    49 // Store normalized light vector. 
    50 float3 lightVec = normalize(LightVec.xyz); 
    51 // Calculate half angle vector. 
    52 float3 eyeVec = float3(0.0, 0.0, 1.0); 
    53 float3 halfVec = normalize(lightVec + eyeVec); 
    54 // Calculate diffuse component. 
    55 float diffuse = dot(normalVec, lightVec); 
    56 // Calculate specular component. 
    57 float specular = dot(normalVec, halfVec); 
    58 // Use the lit function to compute lighting vector from 
    59 // diffuse and specular values. 
    60 float4 lighting = lit(diffuse, specular, 32); 
    61 // Blue diffuse material 
    62 float3 diffuseMaterial = float3(0.0, 0.0, 1.0); 
    63 // White specular material 
    64 float3 specularMaterial = float3(1.0, 1.0, 1.0); 
    65 // Combine diffuse and specular contributions and 
    66 // output final vertex color. 
    67 OUT.Color.rgb = lighting.y * diffuseMaterial + 
    68 lighting.z * specularMaterial; 
    69 OUT.Color.a = 1.0; 
    70 return OUT; 
    71 } 
    72 */ 
    7342        OUT.texCoord = IN.texCoord; 
    7443                 
     
    8150        OUT.position += float4(factor * windDir, 0); 
    8251 
    83         OUT.normal = mul(glstate.matrix.invtrans.modelview[0], IN.normal); 
     52        OUT.normal = normalize(mul(glstate.matrix.invtrans.modelview[0], IN.normal)); 
     53        //const float3 l = normalize(mul(glstate.matrix.modelview[0], float4(lightDir, 0))).xyz; 
     54        const float3 l = normalize(lightDir); 
    8455 
    85         Out.color = IN.color * max(0, dot(OUT.normal, lightDir)); 
    86         //OUT.color1 = IN.color1; 
     56        const float dif = max(.0f, dot(OUT.normal.xyz, l)); 
    8757 
     58        //OUT.color.xyz = IN.color.xyz * max(0, dot(OUT.normal.xyz, normalize(lightDir))); 
     59        OUT.color = glstate.material.ambient + glstate.material.front.diffuse * dif; 
     60        OUT.color.w = IN.color.w; 
    8861        return OUT; 
    8962} 
    9063 
    91 /** vertex shader which conducts an simple tree animation 
    92         that bends the tree depending quadratically on the height 
     64/** vertex shader which provides an simple tree animation 
     65        that bends the tree depending quadratically on the height using vertex displacement. 
    9366        This version of the shader is used for deferred shading and thus only  
    9467        displaces the vertices and outputs the color, put does not do any shading. 
     
    10477 
    10578        OUT.color = IN.color; 
    106         //OUT.color1 = IN.color1; 
    107  
    10879        OUT.texCoord = IN.texCoord; 
    10980                 
Note: See TracChangeset for help on using the changeset viewer.