Changeset 2868


Ignore:
Timestamp:
08/26/08 16:27:11 (16 years ago)
Author:
mattausch
Message:

changed ssao to multipass algorithm

Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj

    r2864 r2868  
    795795                                > 
    796796                        </File> 
     797                        <File 
     798                                RelativePath=".\src\shaders\ssao.cg" 
     799                                > 
     800                        </File> 
     801                        <File 
     802                                RelativePath=".\src\shaders\temporal.cg" 
     803                                > 
     804                        </File> 
    797805                </Filter> 
    798806                <File 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env

    r2867 r2868  
    1111camPosition=483.398f 242.364f 186.078f 
    1212camDirection=1 0 0 
    13 useFullScreen=1 
     13useFullScreen=0 
    1414useLODs=1 
    1515#modelPath=data/city/model/ 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp

    r2867 r2868  
    5555void DeferredShader::Init(CGcontext context) 
    5656{ 
    57          
    5857        sCgDeferredProgram =  
    5958                cgCreateProgramFromFile(context,  
     
    112111        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
    113112 
    114         if (1) 
    115         { 
    116                 glEnable(GL_TEXTURE_2D); 
    117                 // generate mip map levels for position texture 
    118                 glBindTexture(GL_TEXTURE_2D, colorsTex); 
    119                 glGenerateMipmapEXT(GL_TEXTURE_2D); 
    120         } 
    121  
    122         // read the second buffer, write to the first buffer 
    123113        mFbo->Bind(); 
    124114 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp

    r2867 r2868  
    1919 
    2020static CGprogram sCgSsaoProgram = NULL; 
     21static CGprogram sCgDeferredProgram2 = NULL; 
     22static CGprogram sCgAntiAliasingProgram = NULL; 
     23 
     24static CGparameter sColorsTexDeferredParam; 
     25static CGparameter sPositionsTexDeferredParam; 
     26static CGparameter sNormalsTexDeferredParam; 
    2127 
    2228static CGparameter sColorsTexParam; 
    2329static CGparameter sPositionsTexParam; 
    2430static CGparameter sNormalsTexParam; 
     31 
     32 
    2533static CGparameter sOldModelViewProjMatrixParam; 
    2634static CGparameter sMaxDepthParam; 
     
    3038static CGparameter sNoiseMultiplierParam; 
    3139static CGparameter sExpFactorParam; 
    32 static CGprogram sCgAntiAliasingProgram; 
    3340 
    3441static CGparameter sColorsTexAntiAliasingParam; 
     
    8188        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    8289         
     90        mFbo3 = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
     91        // the diffuse color buffer 
     92        mFbo3->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
     93         
    8394 
    8495        // create noise texture for ssao 
     
    101112void SsaoShader::Init(CGcontext context) 
    102113{        
    103         /////////////// 
    104  
    105         sCgSsaoProgram =  
     114        sCgDeferredProgram2 =  
    106115                cgCreateProgramFromFile(context,  
    107116                                                                CG_SOURCE, 
    108117                                                                "src/shaders/deferred.cg",  
    109118                                                                RenderState::sCgFragmentProfile, 
    110                                                                 "main_ssao", 
     119                                                                "main2", 
     120                                                                NULL); 
     121 
     122        if (sCgDeferredProgram2 != NULL) 
     123        { 
     124                cgGLLoadProgram(sCgDeferredProgram2); 
     125 
     126                // we need size of texture for scaling 
     127                sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "positions");   
     128                sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "colors");   
     129                sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "normals");  
     130        } 
     131        else 
     132                cerr << "deferred program failed to load" << endl; 
     133 
     134 
     135        /////////////// 
     136 
     137        sCgSsaoProgram =  
     138                cgCreateProgramFromFile(context,  
     139                                                                CG_SOURCE, 
     140                                                                "src/shaders/ssao.cg",  
     141                                                                RenderState::sCgFragmentProfile, 
     142                                                                "main", 
    111143                                                                NULL); 
    112144 
     
    121153                sNoiseTexParam = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture"); 
    122154                sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier"); 
     155                 
    123156                sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj"); 
    124157                sMaxDepthParam = cgGetNamedParameter(sCgSsaoProgram, "maxDepth"); 
     
    162195                                                float expFactor) 
    163196{ 
    164         cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x); 
    165  
    166         glPushAttrib(GL_VIEWPORT_BIT); 
    167         glViewport(0, 0, mWidth, mHeight); 
    168  
    169         glDrawBuffers(1, mymrt); 
    170  
    171         cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    172  
    173         glDisable(GL_ALPHA_TEST); 
    174         glDisable(GL_TEXTURE_2D); 
    175         glDisable(GL_LIGHTING); 
    176  
    177         glMatrixMode(GL_PROJECTION); 
    178         glPushMatrix(); 
    179         glLoadIdentity(); 
    180  
    181         glMatrixMode(GL_MODELVIEW); 
    182         glPushMatrix(); 
    183         glLoadIdentity(); 
    184  
    185         const float offs = 0.5f; 
    186         glOrtho(-offs, offs, -offs, offs, 0, 1); 
    187  
     197         
    188198        // switch roles of old and new fbo 
    189199        // the algorihm uses two input fbos, where the one 
     
    192202        swap(mNewFbo, mOldFbo);  
    193203 
     204        cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x); 
     205 
     206        glPushAttrib(GL_VIEWPORT_BIT); 
     207        glViewport(0, 0, mWidth, mHeight); 
     208 
     209        FrameBufferObject::Release(); 
     210 
     211        glDrawBuffers(1, mymrt); 
     212 
     213        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
     214 
     215        glDisable(GL_ALPHA_TEST); 
     216        glDisable(GL_TEXTURE_2D); 
     217        glDisable(GL_LIGHTING); 
     218 
     219        glMatrixMode(GL_PROJECTION); 
     220        glPushMatrix(); 
     221        glLoadIdentity(); 
     222 
     223        glMatrixMode(GL_MODELVIEW); 
     224        glPushMatrix(); 
     225        glLoadIdentity(); 
     226 
     227        const float offs = 0.5f; 
     228        glOrtho(-offs, offs, -offs, offs, 0, 1); 
     229 
     230        FirstPass(fbo); 
    194231        ComputeSsao(fbo, expFactor); 
    195         // the second pass just renders the combined solution 
    196         //DisplayTexture(); 
    197232        AntiAliasing(fbo); 
    198233 
     
    214249void SsaoShader::ComputeSsao(FrameBufferObject *fbo, float expFactor) 
    215250{ 
    216         GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     251        GLuint colorsTex = mFbo3->GetColorBuffer(0)->GetTexture(); 
    217252        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
    218253        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
     
    228263        // read the second buffer, write to the first buffer 
    229264        mNewFbo->Bind(); 
     265        glDrawBuffers(1, mymrt); 
     266 
    230267        GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture(); 
    231268 
    232         glDrawBuffers(1, mymrt); 
    233  
    234269        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    235270 
     271        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    236272        cgGLBindProgram(sCgSsaoProgram); 
    237273 
     
    385421 
    386422        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    387  
    388423        cgGLBindProgram(sCgAntiAliasingProgram); 
    389  
     424         
    390425        cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex); 
    391426        cgGLEnableTextureParameter(sColorsTexAntiAliasingParam); 
     
    393428        cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex); 
    394429        cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam); 
    395          
     430 
    396431        glColor3f(1.0f, 1.0f, 1.0f); 
    397432 
     
    418453 
    419454 
     455void SsaoShader::FirstPass(FrameBufferObject *fbo) 
     456{ 
     457        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     458        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
     459        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
     460 
     461        mFbo3->Bind(); 
     462        //mNewFbo->Bind(); 
     463 
     464        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     465         
     466        glDrawBuffers(1, mymrt); 
     467 
     468        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
     469 
     470        cgGLBindProgram(sCgDeferredProgram2); 
     471 
     472        cgGLSetTextureParameter(sColorsTexDeferredParam, colorsTex); 
     473        cgGLEnableTextureParameter(sColorsTexDeferredParam); 
     474 
     475        cgGLSetTextureParameter(sPositionsTexDeferredParam, positionsTex); 
     476        cgGLEnableTextureParameter(sPositionsTexDeferredParam); 
     477 
     478        cgGLSetTextureParameter(sNormalsTexDeferredParam, normalsTex); 
     479        cgGLEnableTextureParameter(sNormalsTexDeferredParam); 
     480         
     481        glColor3f(1.0f, 1.0f, 1.0f); 
     482 
     483        const float offs = 0.5f; 
     484 
     485        glBegin(GL_QUADS); 
     486 
     487        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f); 
     488        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f); 
     489        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f); 
     490        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f); 
     491 
     492        glEnd(); 
     493 
     494        cgGLDisableTextureParameter(sColorsTexDeferredParam); 
     495        cgGLDisableTextureParameter(sPositionsTexDeferredParam); 
     496        cgGLDisableTextureParameter(sNormalsTexDeferredParam); 
     497 
     498        cgGLDisableProfile(RenderState::sCgFragmentProfile); 
     499 
     500        FrameBufferObject::Release(); 
     501 
     502        PrintGLerror("deferred shading"); 
     503} 
     504 
    420505} // namespace 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h

    r2865 r2868  
    5050        void ComputeSsao(FrameBufferObject *fbo, float expFactor); 
    5151 
    52         //void DisplayTexture(); 
     52        void FirstPass(FrameBufferObject *fbo); 
     53 
    5354        void AntiAliasing(FrameBufferObject *fbo); 
    5455 
     
    6970        FrameBufferObject *mOldFbo; 
    7071        FrameBufferObject *mNewFbo; 
     72        FrameBufferObject *mFbo3; 
    7173}; 
    7274 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r2867 r2868  
    425425        InitCg(); 
    426426 
     427        DeferredShader::Init(sCgContext); 
    427428        SsaoShader::Init(sCgContext); 
    428         DeferredShader::Init(sCgContext); 
    429  
     429 
     430        deferredShader = new DeferredShader(texWidth, texHeight); 
    430431        ssaoShader = new SsaoShader(texWidth, texHeight, camera, myfar / 10.0f); 
    431         deferredShader = new DeferredShader(texWidth, texHeight); 
    432432 
    433433        // initialize the render traverser 
     
    539539        // the diffuse color buffer 
    540540        fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false); 
     541 
    541542        // the positions buffer 
    542543        fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_MIPMAP_LINEAR, true); 
    543544        //fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, true); 
     545         
    544546        // the normals buffer 
    545547        //fbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg

    r2867 r2868  
    1111 
    1212// the barrier for detecting a discontinuity 
    13 uniform float4 e_barrier = float4(5e-3, 5e-3, 0, 0); // x = normal, y = depth 
     13uniform float4 e_barrier = float4(5e-4, 5e-4, 0, 0); // x = normal, y = depth 
    1414// the weights for normal / depth discontinuity 
    1515uniform float4 e_weights = float4(1.0f, 1.0f, 1.0f, 1.0f); // x = normal, y = depth 
     
    3232        nd.w = dot(nc, float3(tex2D(normals, IN.lb.xy))); 
    3333 
    34         nd -= e_barrier.x; 
    35         nd = step(0.0f, nd); 
     34        nd.x -= e_barrier.x; 
     35        nd = step((float4)0.0f, nd); 
    3636 
    3737        float ne = saturate(dot(nd, e_weights.x)); 
     
    8383 
    8484        return (s0 + s1 + s2 + s3) / 4.0f; 
     85        //return float4(w); 
    8586} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg

    r2867 r2868  
    1 //////////////////// 
    2 // Screen Spaced Ambient Occlusion shader 
    3 // mainly based on shader of Alexander Kusternig 
    4  
    5 //#define NUM_SAMPLES 8 
    6 #define NUM_SAMPLES 16 
    7  
    8 // rule of thumb: approx 1 / NUM_SAMPLES 
    9 #define SAMPLE_INTENSITY 0.15 
    10 //#define SAMPLE_INTENSITY 0.125f 
    11  
    12 #define AREA_SIZE 9e-1f 
    13 //#define VIEW_CORRECTION_SCALE 0.3f 
    14 #define VIEW_CORRECTION_SCALE 0.5f 
    15 #define DISTANCE_SCALE 1e-6f 
    16  
    171struct fragment 
    182{ 
     
    3014 
    3115 
    32 float2 reflect(float2 pt, float2 n) 
    33 { 
    34   // distance to plane 
    35   float d = dot(n, pt); 
    36   // reflect around plane 
    37   float2 rpt = pt - d * 2.0f * n; 
    38  
    39   return rpt; 
    40 } 
    41  
    42  
    43 float2 rotate(float2 pt, float2 n) 
    44 { 
    45         float2 ptTransformed; 
    46         ptTransformed.x = n.r * pt.x - n.g * pt.y; 
    47         ptTransformed.y = n.g * pt.x + n.r * pt.y; 
    48  
    49         return ptTransformed; 
    50 } 
    51  
    52  
    53 /** The ssao shader returning the an intensity value between 0 and 1 
    54 */ 
    55 float ssao(fragment IN, 
    56                    uniform sampler2D positions, 
    57                    uniform sampler2D noiseTexture, 
    58                    uniform float2 samples[NUM_SAMPLES], 
    59                    uniform float3 currentNormal, 
    60                    uniform float3 currentViewDir, 
    61                    uniform float noiseMultiplier, 
    62                    uniform float4 centerPosition 
    63                    ) 
    64 { 
    65         // the w coordinate from the persp. projection 
    66         float w = centerPosition.w; 
    67  
    68         // Check in a circular area around the current position. 
    69         // Shoot vectors to the positions there, and check the angle to these positions. 
    70         // Summing up these angles gives an estimation of the occlusion at the current position. 
    71  
    72         float total_ao = 0.0; 
    73  
    74         const float areaSize = 5e-1f; 
    75  
    76         for (int i = 0; i < NUM_SAMPLES; i ++)  
    77         { 
    78                 float2 offset = samples[i]; 
    79  
    80                 //sample noisetex; r stores costheta, g stores sintheta 
    81                 //float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f; 
    82                 float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy; 
    83  
    84                 // rotation 
    85                 //float2 offsetTransformed = offset; 
    86                 //float2 offsetTransformed = rotate(offset, mynoise); 
    87                 float2 offsetTransformed = reflect(offset, mynoise); 
    88  
    89                 // weight with projected coordinate to reach similar kernel size for near and far 
    90                 float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; 
    91  
    92                 // sample downsampled texture in order to speed up texture accesses 
    93                 float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz; 
    94                 //float3 sample_position = tex2D(positions, texcoord).xyz; 
    95  
    96                 float3 vector_to_sample = sample_position - centerPosition.xyz; 
    97                 float length_to_sample = length(vector_to_sample); 
    98                 float3 direction_to_sample = vector_to_sample / length_to_sample; 
    99  
    100                 // Angle between current normal and direction to sample controls AO intensity. 
    101                 float cos_angle = dot(direction_to_sample, currentNormal); 
    102                 cos_angle = max(cos_angle, 0.0f); 
    103  
    104                 // distance between current position and sample position controls AO intensity. 
    105                 float distance_intensity =  
    106                         (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample); 
    107  
    108                 // if surface normal perpenticular to view dir, some samples probably count less  
    109                 // => compensate for this 
    110                 float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); 
    111  
    112                 total_ao += cos_angle * distance_intensity * view_correction; 
    113         } 
    114  
    115         return (1.0f - total_ao); 
    116         //return dot(currentViewDir, currentNormal); 
    117 } 
    118  
    119  
    120 /** Computes ambient occlusion + diffuse reflections 
    121 */ 
    122 float4 globIllum(fragment IN, 
    123                                  uniform sampler2D colors, 
    124                                  uniform sampler2D positions, 
    125                                  uniform sampler2D noiseTexture, 
    126                                  uniform float2 samples[NUM_SAMPLES], 
    127                                  uniform float3 currentNormal, 
    128                                  uniform float3 currentViewDir, 
    129                                  uniform float noiseMultiplier, 
    130                                  uniform float4 centerPosition 
    131                                  ) 
    132 { 
    133         // the w coordinate from the persp. projection 
    134         float w = centerPosition.w; 
    135  
    136         // Check in a circular area around the current position. 
    137         // Shoot vectors to the positions there, and check the angle to these positions. 
    138         // Summing up these angles gives an estimation of the occlusion at the current position. 
    139  
    140         float total_ao = 0.0; 
    141         float3 total_color = float3(0.0f); 
    142  
    143         const float areaSize = 5e-1f; 
    144  
    145         for (int i = 0; i < NUM_SAMPLES; i ++)  
    146         { 
    147                 float2 offset = samples[i]; 
    148  
    149                 //sample noisetex; r stores costheta, g stores sintheta 
    150                 float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f; 
    151  
    152                 // rotation 
    153                 float2 offsetTransformed = rotate(offset, mynoise); 
    154  
    155                 // weight with projected coordinate to reach similar kernel size for near and far 
    156                 float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; 
    157  
    158                 float3 sample_position = tex2D(positions, texcoord).xyz; 
    159                 float3 sample_color = tex2D(colors, texcoord).xyz; 
    160  
    161                 float3 vector_to_sample = sample_position - centerPosition.xyz; 
    162                 float length_to_sample = length(vector_to_sample); 
    163                 float3 direction_to_sample = vector_to_sample / length_to_sample; 
    164  
    165                 // Angle between current normal and direction to sample controls AO intensity. 
    166                 float cos_angle = dot(direction_to_sample, currentNormal); 
    167                 cos_angle = max(cos_angle, 0.0f); 
    168  
    169                 // distance between current position and sample position controls AO intensity. 
    170                 float distance_intensity =  
    171                         (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample); 
    172  
    173                 // if normal perpenticular to view dir, only half of the samples count 
    174                 float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); 
    175  
    176                 total_ao += cos_angle * distance_intensity * view_correction; 
    177                 total_color += cos_angle * distance_intensity * view_correction * sample_color * 0.3f; 
    178         } 
    179  
    180         return float4(total_color, 1.0f - total_ao); 
    181 } 
    182  
    18316 
    18417/** function for standard deferred shading 
    18518*/ 
    18619float4 shade(fragment IN,  
    187                          uniform sampler2D colors, 
    188                          uniform sampler2D positions, 
     20                         uniform float4 color, 
     21                         uniform float4 position, 
    18922                         uniform float3 normal, 
    19023                         uniform float amb) 
     
    19326        float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f); 
    19427 
    195         float4 color = tex2D(colors, IN.texCoord.xy); 
    196  
    197         float4 position = tex2D(positions, IN.texCoord.xy); 
    198  
    199         float4 ambient = 0.3f; 
     28        // global ambient 
     29        const float4 ambient = 0.3f; 
    20030 
    20131        // float3 L = normalize(lightPosition - position); 
     
    21242 
    21343 
    214 /** The mrt shader for screen space ambient occlusion 
     44/** The mrt shader for standard rendering 
    21545*/ 
    216 pixel main_ssao(fragment IN,  
    217                                 uniform sampler2D colors, 
    218                                 uniform sampler2D positions, 
    219                                 uniform sampler2D normals, 
    220                                 uniform sampler2D noiseTexture, 
    221                                 uniform float2 samples[NUM_SAMPLES], 
    222                                 uniform float noiseMultiplier, 
    223                                 uniform sampler2D oldTex, 
    224                                 const uniform float4x4 oldModelViewProj, 
    225                                 uniform float maxDepth, 
    226                                 uniform float expFactor 
    227                                 ) 
     46pixel main2(fragment IN,  
     47                   uniform sampler2D colors, 
     48                   uniform sampler2D positions, 
     49                   uniform sampler2D normals 
     50                   ) 
    22851{ 
    22952        pixel OUT; 
    23053 
    231         float4 normal = tex2D(normals, IN.texCoord.xy); 
    232          
    233         // the ambient term 
    234         float amb = normal.w; 
     54        float4 norm = tex2D(normals, IN.texCoord.xy); 
     55        float4 color = tex2D(colors, IN.texCoord.xy); 
     56        float4 position = tex2D(positions, IN.texCoord.xy); 
     57 
     58        // an ambient color term 
     59        float amb = norm.w; 
    23560 
    23661        // expand normal 
    237         normal = normalize(normal);// * 2.0f - 1.0f); 
    238         /// the current view direction 
    239         float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f)); 
     62        float3 normal = normalize(norm.xyz);// * 2.0f - float4(1.0f)); 
    24063 
    241         // the current world position 
    242         float4 centerPosition = tex2D(positions, IN.texCoord.xy); 
     64        float4 col = shade(IN, color, position, normal, amb); 
    24365         
    244         float4 col = shade(IN, colors, positions, normal.xyz, amb); 
    245  
    246         float ao = ssao(IN, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition); 
    247         float4 attenuated_color = ao * col; 
    248         //float4 attenuated_color = ao; 
    249  
    250         //float4 new_col = globIllum(IN, colors, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition);  
    251         //float4 attenuated_color = ao * col + new_col; 
    252          
    253         const float x = expFactor; 
    254  
    255         float4 dummy = centerPosition * maxDepth; 
    256         dummy.w = 1.0f; 
    257  
    258         float4 oldPos = mul(oldModelViewProj, dummy); 
    259  
    260         float newDepth = oldPos.z / oldPos.w; 
    261   
    262         float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f; 
    263         float4 col1 = tex2D(oldTex, tex); 
    264  
    265         float oldDepth = col1.w; 
    266         float depthDif = 1.0f - newDepth / oldDepth; 
    267  
    268         if ((tex.x >= 0.0f) && (tex.x < 1.0f) &&  
    269                 (tex.y >= 0.0f) && (tex.y < 1.0f) &&  
    270                 (abs(depthDif)  < 1e-4f)) 
    271         { 
    272                 OUT.color = attenuated_color * expFactor + col1 * float4(1.0f - expFactor); 
    273         } 
    274         else 
    275         { 
    276                 OUT.color = attenuated_color; 
    277         } 
    278  
    279         //OUT.color.xyz = viewDir; 
    280         //OUT.color = attenuated_color; 
    281          
    282         OUT.color.w = tex2D(colors, IN.texCoord.xy).w; 
     66        //OUT.color = float4(1.0f); 
     67        OUT.color = col; 
     68        OUT.color.w = color.w; 
    28369 
    28470        return OUT; 
    28571} 
    286  
    28772 
    28873/** The mrt shader for standard rendering 
     
    29681        pixel OUT; 
    29782 
    298         float4 normal = tex2D(normals, IN.texCoord.xy); 
     83        float4 norm = tex2D(normals, IN.texCoord.xy); 
     84        float4 color = tex2D(colors, IN.texCoord.xy); 
     85        float4 position = tex2D(positions, IN.texCoord.xy); 
    29986 
    30087        // an ambient color term 
    301         float amb = normal.w; 
     88        float amb = norm.w; 
    30289 
    30390        // expand normal 
    304         normal = normalize(normal);// * 2.0f - float4(1.0f)); 
     91        float3 normal = normalize(norm.xyz);// * 2.0f - float4(1.0f)); 
    30592 
    306         float4 col = shade(IN, colors, positions, normal.xyz, amb); 
     93        float4 col = shade(IN, color, position, normal.xyz, amb); 
    30794         
     95        //OUT.color = float4(1.0f); 
    30896        OUT.color = col; 
    30997 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg

    r2809 r2868  
     1//////////////////// 
     2// Screen Spaced Ambient Occlusion shader 
     3// based on shader of Alexander Kusternig 
     4 
     5//#define NUM_SAMPLES 8 
     6#define NUM_SAMPLES 16 
     7 
     8// rule of thumb: approx 1 / NUM_SAMPLES 
     9#define SAMPLE_INTENSITY 0.15 
     10//#define SAMPLE_INTENSITY 0.125f 
     11 
     12#define AREA_SIZE 9e-1f 
     13//#define VIEW_CORRECTION_SCALE 0.3f 
     14#define VIEW_CORRECTION_SCALE 0.5f 
     15#define DISTANCE_SCALE 1e-6f 
     16 
     17 
    118struct fragment 
    219{ 
    3     float4 pos: WPOS; // normalized screen position 
    4     float4 view: COLOR; 
    5     float4 texcoord: TEXCOORD0;       
    6     float4 lindepth: TEXCOORD1; 
     20        // normalized screen position 
     21        float4 pos: WPOS; 
     22        float4 texCoord: TEXCOORD0;  
     23        float3 view: COLOR0; 
    724}; 
    825 
     
    1027struct pixel 
    1128{ 
    12     float4 col: COLOR0; 
     29        float4 color: COLOR0; 
    1330}; 
    1431 
    1532 
    16 float3 reflect(float3 pt, float3 n) 
     33float2 reflect(float2 pt, float2 n) 
    1734{ 
    1835  // distance to plane 
    1936  float d = dot(n, pt); 
    2037  // reflect around plane 
    21   float3 rpt = pt - d * 2.0f * n; 
    22    
    23   //return pt; 
     38  float2 rpt = pt - d * 2.0f * n; 
     39 
    2440  return rpt; 
    2541} 
    2642 
    2743 
     44float2 rotate(float2 pt, float2 n) 
     45{ 
     46        float2 ptTransformed; 
     47        ptTransformed.x = n.r * pt.x - n.g * pt.y; 
     48        ptTransformed.y = n.g * pt.x + n.r * pt.y; 
     49 
     50        return ptTransformed; 
     51} 
     52 
     53 
     54/** The ssao shader returning the an intensity value between 0 and 1 
     55*/ 
     56float ssao(fragment IN, 
     57                   uniform sampler2D positions, 
     58                   uniform sampler2D noiseTexture, 
     59                   uniform float2 samples[NUM_SAMPLES], 
     60                   uniform float3 currentNormal, 
     61                   uniform float3 currentViewDir, 
     62                   uniform float noiseMultiplier, 
     63                   uniform float4 centerPosition 
     64                   ) 
     65{ 
     66        // the w coordinate from the persp. projection 
     67        float w = centerPosition.w; 
     68 
     69        // Check in a circular area around the current position. 
     70        // Shoot vectors to the positions there, and check the angle to these positions. 
     71        // Summing up these angles gives an estimation of the occlusion at the current position. 
     72 
     73        float total_ao = 0.0; 
     74 
     75        const float areaSize = 5e-1f; 
     76 
     77        for (int i = 0; i < NUM_SAMPLES; i ++)  
     78        { 
     79                float2 offset = samples[i]; 
     80 
     81                //sample noisetex; r stores costheta, g stores sintheta 
     82                //float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f; 
     83                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy; 
     84 
     85                // rotation 
     86                //float2 offsetTransformed = offset; 
     87                //float2 offsetTransformed = rotate(offset, mynoise); 
     88                float2 offsetTransformed = reflect(offset, mynoise); 
     89 
     90                // weight with projected coordinate to reach similar kernel size for near and far 
     91                float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; 
     92 
     93                // sample downsampled texture in order to speed up texture accesses 
     94                float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz; 
     95                //float3 sample_position = tex2D(positions, texcoord).xyz; 
     96 
     97                float3 vector_to_sample = sample_position - centerPosition.xyz; 
     98                float length_to_sample = length(vector_to_sample); 
     99                float3 direction_to_sample = vector_to_sample / length_to_sample; 
     100 
     101                // Angle between current normal and direction to sample controls AO intensity. 
     102                float cos_angle = dot(direction_to_sample, currentNormal); 
     103                cos_angle = max(cos_angle, 0.0f); 
     104 
     105                // distance between current position and sample position controls AO intensity. 
     106                float distance_intensity =  
     107                        (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample); 
     108 
     109                // if surface normal perpenticular to view dir, some samples probably count less  
     110                // => compensate for this 
     111                float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); 
     112 
     113                total_ao += cos_angle * distance_intensity * view_correction; 
     114        } 
     115 
     116        return (1.0f - total_ao); 
     117        //return dot(currentViewDir, currentNormal); 
     118} 
     119 
     120 
     121/** Computes ambient occlusion + diffuse reflections 
     122*/ 
     123float4 globIllum(fragment IN, 
     124                                 uniform sampler2D colors, 
     125                                 uniform sampler2D positions, 
     126                                 uniform sampler2D noiseTexture, 
     127                                 uniform float2 samples[NUM_SAMPLES], 
     128                                 uniform float3 currentNormal, 
     129                                 uniform float3 currentViewDir, 
     130                                 uniform float noiseMultiplier, 
     131                                 uniform float4 centerPosition 
     132                                 ) 
     133{ 
     134        // the w coordinate from the persp. projection 
     135        float w = centerPosition.w; 
     136 
     137        // Check in a circular area around the current position. 
     138        // Shoot vectors to the positions there, and check the angle to these positions. 
     139        // Summing up these angles gives an estimation of the occlusion at the current position. 
     140 
     141        float total_ao = 0.0; 
     142        float3 total_color = float3(0.0f); 
     143 
     144        const float areaSize = 5e-1f; 
     145 
     146        for (int i = 0; i < NUM_SAMPLES; i ++)  
     147        { 
     148                float2 offset = samples[i]; 
     149 
     150                //sample noisetex; r stores costheta, g stores sintheta 
     151                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f; 
     152 
     153                // rotation 
     154                float2 offsetTransformed = rotate(offset, mynoise); 
     155 
     156                // weight with projected coordinate to reach similar kernel size for near and far 
     157                float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; 
     158 
     159                float3 sample_position = tex2D(positions, texcoord).xyz; 
     160                float3 sample_color = tex2D(colors, texcoord).xyz; 
     161 
     162                float3 vector_to_sample = sample_position - centerPosition.xyz; 
     163                float length_to_sample = length(vector_to_sample); 
     164                float3 direction_to_sample = vector_to_sample / length_to_sample; 
     165 
     166                // Angle between current normal and direction to sample controls AO intensity. 
     167                float cos_angle = dot(direction_to_sample, currentNormal); 
     168                cos_angle = max(cos_angle, 0.0f); 
     169 
     170                // distance between current position and sample position controls AO intensity. 
     171                float distance_intensity =  
     172                        (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample); 
     173 
     174                // if normal perpenticular to view dir, only half of the samples count 
     175                float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); 
     176 
     177                total_ao += cos_angle * distance_intensity * view_correction; 
     178                total_color += cos_angle * distance_intensity * view_correction * sample_color * 0.3f; 
     179        } 
     180 
     181        return float4(total_color, 1.0f - total_ao); 
     182} 
     183 
     184 
     185/** The mrt shader for screen space ambient occlusion 
     186*/ 
    28187pixel main(fragment IN,  
    29            uniform sampler2D lindepth, 
    30            uniform sampler2D scene, 
    31            uniform sampler2D normal, 
    32            uniform float invTexSize, 
    33            uniform float radius, 
    34            uniform float3 eyevec, 
    35            uniform float3 samples[32]) 
    36 { 
    37     pixel OUT; 
     188                   uniform sampler2D colors, 
     189                   uniform sampler2D positions, 
     190                   uniform sampler2D normals, 
     191                   uniform sampler2D noiseTexture, 
     192                   uniform float2 samples[NUM_SAMPLES], 
     193                   uniform float noiseMultiplier, 
     194                   uniform sampler2D oldTex, 
     195                   const uniform float4x4 oldModelViewProj, 
     196                   uniform float maxDepth, 
     197                   uniform float expFactor 
     198                   ) 
     199{ 
     200        pixel OUT; 
     201 
     202        float4 normal = tex2D(normals, IN.texCoord.xy); 
     203         
     204        // the ambient term 
     205        float amb = normal.w; 
     206 
     207        // expand normal 
     208        normal = normalize(normal);// * 2.0f - 1.0f); 
     209        /// the current view direction 
     210        float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f)); 
     211 
     212        // the current world position 
     213        float4 centerPosition = tex2D(positions, IN.texCoord.xy); 
     214         
     215        float4 col = tex2D(colors, IN.texCoord.xy); 
     216        float currentDepth = col.w; 
     217 
     218        float ao = ssao(IN, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition); 
     219        float4 attenuated_color = ao * col; 
     220        //float4 attenuated_color = ao; 
     221 
     222        //float4 new_col = globIllum(IN, colors, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition);  
     223        //float4 attenuated_color = ao * col + new_col; 
     224         
     225        const float x = expFactor; 
     226 
     227        float4 dummy = centerPosition * maxDepth; 
     228        dummy.w = 1.0f; 
     229 
     230        float4 oldPos = mul(oldModelViewProj, dummy); 
     231 
     232        float newDepth = oldPos.z / oldPos.w; 
    38233  
    39     // eye space z 
    40     float eyez = tex2D(lindepth, IN.texcoord.xy).x; 
    41  
    42     float3 viewvec = IN.view.xyz * 2.0f - float3(1.0f); 
    43     viewvec /= viewvec.z; 
    44  
    45     //return float4(viewvec.xyz, 1.0f); 
    46     // eye point 
    47     //float3 eyept = eyez * viewvec; 
    48     float3 eyept = float3(IN.texcoord.xy, eyez); 
    49  
    50     float4 pl = tex2D(normal, IN.pos.xy * invTexSize); 
    51     pl = pl * 2.0 - float4(1.0); 
    52  
    53     float occlusion = 0.0; 
    54  
    55     // gather occlusion from surrounding samples 
    56     for (int i = 0; i < 32; i ++) 
    57     { 
    58         // generate new sample point 
    59         //float3 samplepos = eyept + radius * samples[i]; 
    60         // create some dithering by reflecting the sample point alond some random plane 
    61         float3 samplepos = eyept + radius * reflect(samples[i], pl.xyz); 
    62  
    63         // project to texture space q: why the scaling? 
    64         float2 sampletex = (samplepos.xy / samplepos.z);// * float2(0.75, 1.0); 
    65          
    66         //float2 sampletexn = sampletex; 
    67         // normalize to [0 .. 1], move eye point to center 
    68         //float2 sampletexn = sampletex * 0.5f + float2(0.5f); 
    69         float2 sampletexn = sampletex + float2(0.5f); 
    70  
    71         // look up depth at sample point 
    72         float depth = tex2D(lindepth, sampletexn).x; 
    73         // compare: is point occluded? 
    74         //float zdist = 50.0f * max(-samplepos.z + depth, 0.0f); 
    75         float zdist = 50.0f * max(samplepos.z - depth, 0.0f); 
    76         // occlusion factor shrinks quadratic with distance to occluder 
    77         occlusion += 1.0 / (1.0 + zdist * zdist); 
    78         //occlusion += 1.0 / (1.0 + zdist); 
    79    } 
    80  
    81    // normalize 
    82    occlusion /= 32.0f; 
    83    
    84    OUT.col = 1.0f - occlusion; 
    85    //   OUT.col = tex2D(scene, IN.texcoord.xy) * 0.5 + (1.0f - occlusion); 
    86  
    87    return OUT; 
    88 } 
     234        float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f; 
     235        float4 col1 = tex2D(oldTex, tex); 
     236 
     237        float oldDepth = col1.w; 
     238        float depthDif = 1.0f - newDepth / oldDepth; 
     239 
     240        if ((tex.x >= 0.0f) && (tex.x < 1.0f) &&  
     241                (tex.y >= 0.0f) && (tex.y < 1.0f) &&  
     242                (abs(depthDif)  < 1e-4f)) 
     243        { 
     244                OUT.color = attenuated_color * expFactor + col1 * float4(1.0f - expFactor); 
     245        } 
     246        else 
     247        { 
     248                OUT.color = attenuated_color; 
     249        } 
     250 
     251        //OUT.color.xyz = viewDir; 
     252        //OUT.color = attenuated_color; 
     253         
     254        OUT.color.w = currentDepth; 
     255 
     256        return OUT; 
     257} 
Note: See TracChangeset for help on using the changeset viewer.