Ignore:
Timestamp:
10/09/08 11:19:33 (16 years ago)
Author:
mattausch
Message:
 
Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling
Files:
1 added
13 edited

Legend:

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

    r3012 r3017  
    1212EndProject 
    1313Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "IrradianceMapping", "IrradianceMapping.vcproj", "{91680C49-A358-48AE-A02C-66CB884B7D7F}" 
     14        ProjectSection(ProjectDependencies) = postProject 
     15                {03661866-4093-4B02-B26A-028EA91AF023} = {03661866-4093-4B02-B26A-028EA91AF023} 
     16        EndProjectSection 
    1417EndProject 
    1518Global 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp

    r3016 r3017  
    5353 
    5454static CGparameter sDownSampleOffsetParam; 
    55  
     55static CGparameter sFilterOffsetsParam; 
     56static CGparameter sFilterWeightsParam; 
    5657 
    5758static CGprogram sCgDownSampleProgram; 
     
    6566 
    6667static CGparameter sColorsTexParam; 
    67 static CGparameter sPositionsTexParam; 
    6868static CGparameter sNormalsTexParam; 
    6969 
     
    175175} 
    176176 
    177  
     177static void ComputeSampleOffsets(float *sampleOffsets, int w, int h) 
     178{ 
     179        /* 
     180        const float xoffs = 0.5f / w; 
     181        const float yoffs = 0.5f / h; 
     182 
     183        sampleOffsets[0] =  xoffs; sampleOffsets[1] =  yoffs; 
     184        sampleOffsets[2] =  xoffs; sampleOffsets[3] = -yoffs; 
     185        sampleOffsets[4] = -xoffs; sampleOffsets[5] = -yoffs; 
     186        sampleOffsets[6] = -xoffs; sampleOffsets[7] =  yoffs; 
     187        */ 
     188        //const float xoffs = .5f / w; 
     189        //const float yoffs = .5f / h; 
     190         
     191        const float xoffs = 1.0f / w; 
     192        const float yoffs = 1.0f / h; 
     193 
     194        int idx  = 0; 
     195 
     196        for (int x = -1; x <= 1; ++ x) 
     197        { 
     198                for (int y = -1; y <= 1; ++ y) 
     199                { 
     200                        sampleOffsets[idx + 0] = x * xoffs; 
     201                        sampleOffsets[idx + 1] = y * yoffs; 
     202 
     203                        idx += 2; 
     204                } 
     205        } 
     206} 
    178207 
    179208/** Generate poisson disc distributed sample points on the unit disc 
     
    284313 
    285314        mFbo = new FrameBufferObject(w / 2, h / 2, FrameBufferObject::DEPTH_NONE); 
    286  
    287         mFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 
    288         mFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 
    289         mFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 
    290         mFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 
     315        //mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
     316 
     317        mFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 
     318        mFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 
     319        mFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 
     320        mFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 
    291321 
    292322        mDownSampleFbo = new FrameBufferObject(w / 2, h / 2, FrameBufferObject::DEPTH_NONE); 
     323 
    293324        mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 
    294         mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 
    295         mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 
     325        mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 
    296326} 
    297327 
     
    361391                cgGLLoadProgram(sCgSsaoProgram); 
    362392 
    363                 sPositionsTexParam = cgGetNamedParameter(sCgSsaoProgram, "positions");   
    364393                sColorsTexParam = cgGetNamedParameter(sCgSsaoProgram, "colors");   
    365394                sEyePosParam = cgGetNamedParameter(sCgSsaoProgram, "eyePos"); 
     
    452481                sColorsTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "colors");   
    453482                sSsaoTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "ssaoTex"); 
     483 
     484                sFilterOffsetsParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "filterOffs"); 
     485                sFilterWeightsParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "filterWeights"); 
    454486        } 
    455487        else 
     
    622654                DownSample(fbo, colorBufferIdx, mDownSampleFbo, 0); 
    623655                DownSample(fbo, 1, mDownSampleFbo, 1); 
    624                 DownSample(fbo, 2, mDownSampleFbo, 2); 
    625656 
    626657                ComputeSsao(fbo, tempCohFactor, oldProjViewMatrix, projViewMatrix); 
     
    690721        GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture(); 
    691722        GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture(); 
    692         GLuint positionsTex = fbo->GetColorBuffer(2)->GetTexture(); 
    693723#else 
    694724        GLuint colorsTex = mDownSampleFbo->GetColorBuffer(0)->GetTexture(); 
    695725        GLuint normalsTex = mDownSampleFbo->GetColorBuffer(1)->GetTexture(); 
    696         GLuint positionsTex = mDownSampleFbo->GetColorBuffer(2)->GetTexture(); 
    697726#endif 
    698727 
     
    708737         
    709738        cgGLBindProgram(sCgSsaoProgram); 
    710  
    711         cgGLSetTextureParameter(sPositionsTexParam, positionsTex); 
    712         cgGLEnableTextureParameter(sPositionsTexParam); 
    713739 
    714740        cgGLSetTextureParameter(sColorsTexParam, colorsTex); 
     
    764790        glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-offs,  offs, -0.5f); 
    765791 
     792        //cout<<bl << " " << br << endl; 
     793 
    766794        glEnd(); 
    767795 
    768796        cgGLDisableTextureParameter(sColorsTexParam); 
    769         cgGLDisableTextureParameter(sPositionsTexParam); 
    770797        cgGLDisableTextureParameter(sNormalsTexParam); 
    771798        cgGLDisableTextureParameter(sNoiseTexParam); 
     
    10401067 
    10411068 
     1069static float GaussianDistribution(float x, float y, float rho) 
     1070{ 
     1071    float g = 1.0f / sqrtf(2.0f * M_PI * rho * rho); 
     1072    g *= expf( -(x*x + y*y) / (2.0f * rho * rho)); 
     1073 
     1074    return g; 
     1075} 
     1076 
     1077 
    10421078void DeferredRenderer::CombineSsao(FrameBufferObject *fbo) 
    10431079{ 
     
    10541090 
    10551091        cgGLBindProgram(sCgCombinedSsaoProgram); 
     1092 
     1093        float filterOffsets[NUM_DOWNSAMPLES * 2]; 
     1094        float filterWeights[NUM_DOWNSAMPLES]; 
     1095 
     1096        PoissonDiscSampleGenerator2 poisson(NUM_DOWNSAMPLES, 1.0f); 
     1097        poisson.Generate((float *)filterOffsets); 
     1098 
     1099        const float xoffs = 2.0f / fbo->GetWidth(); 
     1100        const float yoffs = 2.0f / fbo->GetHeight(); 
     1101 
     1102        for (int i = 0; i < NUM_DOWNSAMPLES; ++ i) 
     1103        { 
     1104                float x = filterOffsets[2 * i + 0]; 
     1105                float y = filterOffsets[2 * i + 1]; 
     1106 
     1107                filterOffsets[2 * i + 0] *= xoffs; 
     1108                filterOffsets[2 * i + 1] *= yoffs; 
     1109 
     1110                filterWeights[i] = GaussianDistribution(x, y, 1.0f); 
     1111        } 
     1112 
     1113        //ComputeSampleOffsets(filterOffsets, fbo->GetWidth(), fbo->GetHeight()); 
     1114        cgGLSetParameterArray2f(sFilterOffsetsParam, 0, NUM_DOWNSAMPLES, (const float *)filterOffsets); 
     1115        cgGLSetParameterArray1f(sFilterWeightsParam, 0, NUM_DOWNSAMPLES, (const float *)filterWeights); 
    10561116 
    10571117        cgGLSetTextureParameter(sColorsTexCombinedSsaoParam, colorsTex); 
     
    11871247 
    11881248        const float minKey = 0.09f; 
    1189         const float maxKey = 0.5f; 
     1249        const float maxKey = 0.45f; 
    11901250 
    11911251        const float lightIntensity = DotProd(-light->GetDirection(), Vector3::UNIT_Z()); 
    11921252        middleGrey = lightIntensity * maxKey + (1.0f - lightIntensity) * minKey; 
    11931253 
    1194  
    1195 #if 1 
    11961254 
    11971255        ////////// 
     
    12261284        PrintGLerror("ToneMapParams"); 
    12271285 
    1228 #endif 
    12291286 
    12301287        /////////////////// 
     
    12651322 
    12661323 
    1267 static void ComputeDownsSampleOffsets(float *downSampleOffsets, int w, int h) 
    1268 { 
    1269         /* 
    1270         float downSampleOffsets[8]; 
    1271  
    1272         const float xoffs = 0.5f / w; 
    1273         const float yoffs = 0.5f / h; 
    1274  
    1275         downSampleOffsets[0] =  xoffs; downSampleOffsets[1] =  yoffs; 
    1276         downSampleOffsets[2] =  xoffs; downSampleOffsets[3] = -yoffs; 
    1277         downSampleOffsets[4] = -xoffs; downSampleOffsets[5] = -yoffs; 
    1278         downSampleOffsets[6] = -xoffs; downSampleOffsets[7] =  yoffs; 
    1279         */ 
    1280         //const float xoffs = .5f / w; 
    1281         //const float yoffs = .5f / h; 
    1282          
    1283         const float xoffs = 1.0f / w; 
    1284         const float yoffs = 1.0f / h; 
    1285  
    1286         int idx  = 0; 
    1287  
    1288         for (int x = -1; x <= 1; ++ x) 
    1289         { 
    1290                 for (int y = -1; y <= 1; ++ y) 
    1291                 { 
    1292                         downSampleOffsets[idx + 0] = x * xoffs; 
    1293                         downSampleOffsets[idx + 1] = y * yoffs; 
    1294  
    1295                         idx += 2; 
    1296                 } 
    1297         } 
    1298 } 
    1299  
    1300  
    13011324void DeferredRenderer::DownSample(FrameBufferObject *fbo, int bufferIdx, 
    13021325                                                                  FrameBufferObject *downSampleFbo,  
    13031326                                                                  int downSampleBufferIdx) 
    13041327{ 
    1305         float downSampleOffsets[NUM_DOWNSAMPLES * 2]; 
    1306         ComputeDownsSampleOffsets(downSampleOffsets, fbo->GetWidth(), fbo->GetHeight()); 
    1307  
    13081328        ColorBufferObject *colorBuffer = fbo->GetColorBuffer(bufferIdx); 
    13091329        GLuint colorsTex = colorBuffer->GetTexture(); 
     
    13161336        cgGLSetTextureParameter(sColorsTexDownSampleParam, colorsTex); 
    13171337        cgGLEnableTextureParameter(sColorsTexDownSampleParam); 
     1338 
     1339        float downSampleOffsets[NUM_DOWNSAMPLES * 2]; 
     1340        ComputeSampleOffsets(downSampleOffsets, fbo->GetWidth(), fbo->GetHeight()); 
    13181341 
    13191342        cgGLSetParameterArray2f(sDownSampleOffsetParam, 0, NUM_DOWNSAMPLES, (const float *)downSampleOffsets); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.cpp

    r3007 r3017  
    9393        { 
    9494        case RGB_UBYTE:  
    95                 mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGB8; 
    96                 components = GL_RGB; 
    97                 break; 
     95                mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGB8; components = GL_RGB; break; 
    9896        case RGBA_UBYTE: 
    9997                mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGBA8; break; 
     98        case RGB_FLOAT_32:  
     99                mGlFormat = GL_FLOAT; mInternalFormat = GL_RGB16F_ARB; components = GL_RGB;     break; 
    100100        case RGBA_FLOAT_16: 
    101101                mGlFormat = GL_FLOAT; mInternalFormat = GL_RGBA16F_ARB; break; 
    102         case RGB_FLOAT_32:  
    103                 mGlFormat = GL_FLOAT; mInternalFormat = GL_RGB32F_ARB; 
    104                 components = GL_RGB; 
    105                 break; 
     102        case RGB_FLOAT_16:  
     103                mGlFormat = GL_FLOAT; mInternalFormat = GL_RGB32F_ARB; components = GL_RGB;     break; 
    106104        case RGBA_FLOAT_32:  
    107105                mGlFormat = GL_FLOAT; mInternalFormat = GL_RGBA32F_ARB; break; 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.h

    r3007 r3017  
    1515public: 
    1616 
    17         enum FORMAT { RGB_UBYTE, RGBA_UBYTE, RGBA_FLOAT_16, RGB_FLOAT_32, RGBA_FLOAT_32}; 
     17        enum FORMAT { RGB_UBYTE, RGBA_UBYTE, RGB_FLOAT_16, RGBA_FLOAT_16, RGB_FLOAT_32, RGBA_FLOAT_32 }; 
     18 
    1819        enum WRAP_TYPE { WRAP_REPEAT, WRAP_CLAMP_TO_EDGE }; 
    1920        enum FILTER_TYPE { FILTER_NEAREST, FILTER_LINEAR }; 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp

    r2983 r3017  
    343343                diffuse = color * 3e-1f; 
    344344 
     345        // diffuse component should be more saturated (and less blueish) for high sun positions 
     346        diffuse.x *= 1.2f; 
     347        diffuse.z *= 0.8f; 
     348 
     349        // scale diffuse component in order to make sky look less bright in relation to 
     350        // the geometry in the evening 
    345351        diffuse *= (2.0f - 1.0f * DotProd(sunDir, Vector3::UNIT_Z())); 
    346352 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r3016 r3017  
    616616        fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, ColorBufferObject::FILTER_NEAREST); 
    617617        // the normals buffer 
    618         fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST); 
     618        fbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST); 
    619619        // the positions buffer 
    620620        //fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST); 
     
    947947        cgGLSetParameter3f(sTRParam, tr.x, tr.y, tr.z); 
    948948 
     949        //cout<<bl << " " << br << endl; 
    949950        cgGLSetParameter3f(sBLParamTex, bl.x, bl.y, bl.z); 
    950951        cgGLSetParameter3f(sBRParamTex, br.x, br.y, br.z); 
    951952        cgGLSetParameter3f(sTLParamTex, tl.x, tl.y, tl.z); 
    952953        cgGLSetParameter3f(sTRParamTex, tr.x, tr.y, tr.z); 
     954 
    953955 
    954956        // draw to 3 color buffers 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h

    r3016 r3017  
    1010 
    1111// for quadratic falloff 
    12 #define SAMPLE_INTENSITY 0.15f 
    13 //#define SAMPLE_INTENSITY 0.32f 
     12#define SAMPLE_INTENSITY 0.1f 
     13//#define SAMPLE_INTENSITY 0.2f 
    1414 
    15 #define AREA_SIZE 8e-1f 
     15#define SAMPLE_RADIUS 8e-1f 
    1616 
    1717//#define DISTANCE_SCALE 1e-1f 
    1818#define DISTANCE_SCALE 5e-2f 
    1919 
    20 #define ILLUM_INTENSITY 5e-2f 
     20#define ILLUM_INTENSITY 8e-2f 
    2121 
    22 #define VIEW_CORRECTION_SCALE 0.1f 
     22#define VIEW_CORRECTION_SCALE 1.0f 
    2323 
    2424 
     
    4040#define MAX_LOD_LEVEL 10 
    4141 
    42 #define MIN_DEPTH_DIFF 1e-3f 
     42#define MIN_DEPTH_DIFF 1e-6f 
    4343// burnout 
    4444#define WHITE_LUMINANCE 3e4f 
     
    4646/////////////////// 
    4747 
    48 #define NUM_DOWNSAMPLES 9 
     48#define NUM_DOWNSAMPLES 16 
    4949 
    5050 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg

    r3016 r3017  
    168168        // diffuse intensity 
    169169        const float angle = saturate(dot(normal, lightDir));  
    170         //float4 lightDiffuse = float4(1.0f, 1.0f, 0.9f, 1.0f); 
    171170        const float4 lightDiffuse = glstate.light[0].diffuse; 
    172         //float4(1.0f, 1.0f, 0.9f, 1.0f); 
    173  
     171         
    174172        float4 diffuse = lightDiffuse * angle; 
    175173 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg

    r3016 r3017  
    88struct fragment 
    99{ 
    10          // normalized screen position 
    11         float4 pos: WPOS; 
    1210        float2 texCoord: TEXCOORD0;  
    1311        float3 view: TEXCOORD1; 
     
    6866                                   uniform float3 tl, 
    6967                                   uniform float3 tr 
    70                                    //, uniform float3 viewDir 
     68                                   , uniform float3 viewDir 
    7169                                   ) 
    7270{ 
     
    9896#endif 
    9997                // weight with projected coordinate to reach similar kernel size for near and far 
    100                 float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; 
     98                float2 texcoord = IN.texCoord.xy + offsetTransformed * w; 
    10199 
    102100                //if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f)) ++ numSamples; 
     
    115113                const float3 sample_color = sample.xyz; 
    116114 
     115                // distance between current position and sample position controls AO intensity. 
     116 
    117117                float3 vector_to_sample = sample_position - centerPosition.xyz; 
    118118                const float length_to_sample = length(vector_to_sample); 
     
    124124 
    125125                const float denom = (DISTANCE_SCALE + length_to_sample * length_to_sample); 
    126                 // distance between current position and sample position controls AO intensity. 
     126 
    127127                const float ssao_intensity = SAMPLE_INTENSITY / denom; 
    128  
    129128                const float illum_intensity = ILLUM_INTENSITY / denom; 
    130129 
    131                 // if normal perpenticular to view dir, only half of the samples count 
    132 #if 0 
    133                 const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); 
    134                 total_color.w -= cos_angle * distance_intensity * view_correction; 
    135                 total_color.xyz += cos_angle * distance_intensity * view_correction * sample_color * ILLUM_INTENSITY; 
    136 #endif 
     130                // if normal perpenticular to view dir, only the samples approx count half 
     131#if 1 
     132                const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * dot(viewDir, currentNormal); 
     133         
     134                total_ao += cos_angle * ssao_intensity * view_correction; 
     135                total_color += cos_angle * illum_intensity * sample_color * view_correction; 
     136#else 
    137137                total_ao += cos_angle * ssao_intensity; 
    138138                total_color += cos_angle * illum_intensity * sample_color; 
     139#endif 
    139140        } 
    140141 
     
    170171        float3 normal = normalize(norm.xyz);     
    171172 
    172         // the w coordinate from the persp. projection 
    173         float w = norm.w; 
    174  
    175  
     173         
    176174        ///////////// 
    177175        //-- reconstruct position from the eye space depth 
     
    182180        const float3 eyeSpacePos = -viewDir * eyeDepth; 
    183181 
    184         float3 centerPosition = eyePos + eyeSpacePos; 
    185         //const float3 centerPosition = tex2D(positions, IN.texCoord.xy).xyz; 
    186  
     182        const float3 centerPosition = eyePos + eyeSpacePos; 
     183        const float4 realPos = float4(centerPosition, 1.0f); 
     184 
     185        // calculcate the current projected depth for next frame 
     186        float4 currentPos = mul(modelViewProj, realPos); 
     187        const float w = SAMPLE_RADIUS / currentPos.w; 
     188        currentPos /= currentPos.w; 
     189        const float currentDepth = currentPos.z * 1e-3f; 
    187190 
    188191        /////////// 
    189192        //-- compute color bleeding + ao 
    190193 
    191         GiStruct gi = globIllum(IN, colors, noiseTexture, samples, normal, eyeSpacePos, w, bl, br, tl, tr); 
     194        GiStruct gi = globIllum(IN, colors, noiseTexture, samples, normal, eyeSpacePos, w, bl, br, tl, tr, normalize(IN.view)); 
    192195         
    193196 
     
    195198        //-- compute temporally smoothing 
    196199 
    197         float4 realPos = float4(centerPosition, 1.0f); 
    198  
    199  
    200         // calculcate the current projected depth for next frame 
    201         float4 currentPos = mul(modelViewProj, realPos); 
    202         currentPos /= currentPos.w; 
    203         const float currentDepth = currentPos.z; 
    204          
    205  
    206         /////////// 
    207         //-- reprojection new frame into old one  
    208          
     200         
     201        // reprojection new frame into old one  
    209202        // calculate projected depth 
    210203        float4 projPos = mul(oldModelViewProj, realPos); 
     
    212205 
    213206        // the current depth projected into the old frame 
    214         const float projDepth = projPos.z; 
     207        const float projDepth = projPos.z * 1e-3f; 
    215208 
    216209        // fit from unit cube into 0 .. 1 
    217         float2 tex = (projPos.xy) * 0.5f + 0.5f; 
     210        float2 tex = projPos.xy * 0.5f + 0.5f; 
    218211 
    219212        // retrieve the sample from the last frame 
     
    221214        float4 oldIllum = tex2D(oldIllumTex, tex); 
    222215 
    223         const float oldDepth = oldSsao.w; 
    224         const float depthDif = 1.0f - projDepth / oldDepth; 
    225  
    226         float oldWeight = clamp(oldSsao.z, 0, temporalCoherence); 
     216        const float oldDepth = oldSsao.z; 
     217        //const float depthDif = 1.0f - projDepth / oldDepth; 
     218        const float depthDif = projDepth - oldDepth; 
     219 
     220        float oldWeight = clamp(oldSsao.y, 0, temporalCoherence); 
    227221 
    228222        float newWeight; 
    229223 
    230224        //const float oldNumSamples = oldSsao.y; 
    231         //const float oldAvgDepth = oldSsao.z; 
    232  
     225         
    233226        if (//(temporalCoherence > 0.0f) && 
    234227                (tex.x >= 0.0f) && (tex.x < 1.0f) &&  
     
    241234                newWeight = oldWeight + 1; 
    242235 
    243                 OUT.ssao_col.xy = (gi.ao + oldSsao.xy * oldWeight) / newWeight; 
     236                OUT.ssao_col.x = (gi.ao + oldSsao.x * oldWeight) / newWeight; 
    244237                OUT.illum_col.xyz  = (gi.illum + oldIllum.xyz * oldWeight) / newWeight; 
    245238        } 
     
    248241                newWeight = 0; 
    249242 
    250                 OUT.ssao_col.xy = gi.ao.xy; 
     243                OUT.ssao_col.x = gi.ao.x; 
    251244                OUT.illum_col.xyz = gi.illum; 
    252245        } 
    253246 
    254         OUT.ssao_col.z = newWeight; 
    255         OUT.ssao_col.w = currentDepth; 
     247        OUT.ssao_col.y = newWeight; 
     248        OUT.ssao_col.z = currentDepth; 
    256249 
    257250        return OUT; 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg

    r3016 r3017  
    1717  float4 worldPos: TEXCOORD1; // world position 
    1818  float3 normal: TEXCOORD2; 
    19   float4 mypos: TEXCOORD3; 
     19  float4 projPos: TEXCOORD3; 
    2020}; 
    2121 
     
    2828        float4 texCoord: TEXCOORD0;     
    2929 
    30         float4 projPos: WPOS; 
     30        float4 winPos: WPOS; 
    3131        float4 worldPos: TEXCOORD1; // world position 
    3232        float3 normal: TEXCOORD2; 
    33         float4 mypos: TEXCOORD3; 
     33        float4 projPos: TEXCOORD3; 
    3434}; 
    3535 
     
    3838{ 
    3939        float4 col: COLOR0; 
    40         float4 norm: COLOR1; 
     40        float3 norm: COLOR1; 
    4141        float3 pos: COLOR2; 
    4242}; 
     
    5959 
    6060        OUT.normal = IN.normal; 
    61         OUT.mypos = OUT.position; 
     61        OUT.projPos = OUT.position; 
    6262 
    6363        return OUT; 
     
    7575} 
    7676 
     77//#pragma position_invariant fragtex 
    7778 
    7879pixel fragtex(fragin IN,  
     
    9697        // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term) 
    9798        pix.col = (glstate.material.emission + glstate.material.diffuse) * texColor;  
    98         // save world space normal in third rt 
    99         pix.norm.xyz = IN.normal; 
    100         // store projection coordinates with positions (used for ssao) 
    101         pix.norm.w = IN.projPos.w; 
     99        // save world space normal in rt 
     100        pix.norm = IN.normal; 
    102101 
    103         const float4 projPos = IN.mypos / IN.mypos.w; 
    104          
    105102        // hack: squeeze some information about ambient into the texture 
    106103        //pix.col.w = glstate.material.emission.x; 
    107104 
    108105        // compute eye linear depth 
    109         const float2 screenCoord = projPos.xy * 0.5f + 0.5f; 
    110         const float magView = length(Interpol(screenCoord, bl, br, tl, tr)); 
    111         pix.col.w = length(eyePos - IN.worldPos.xyz) / magView; 
    112  
    113 #if 0 
    114         // save world position in second render target 
    115         pix.pos = IN.worldPos.xyz * maxDepth; 
    116 #endif 
     106        const float4 projPos = IN.projPos / IN.projPos.w; 
     107         
     108        float2 screenCoord = projPos.xy * 0.5f + 0.5f; 
     109         
     110        const float3 viewVec = Interpol(screenCoord, bl, br, tl, tr); 
     111        const float invMagView = 1.0f / length(viewVec); 
     112        // note: has to done in this order, otherwise strange precision problems! 
     113        pix.col.w = invMagView * length(eyePos - IN.worldPos.xyz); 
    117114 
    118115        return pix; 
     
    132129        pix.col = glstate.material.diffuse + glstate.material.emission; 
    133130 
    134         pix.norm.xyz = IN.normal; 
    135          
    136         // store projection coordinates with positions (used for ssao) 
    137         pix.norm.w = IN.mypos.w; 
    138  
    139         const float4 projPos = IN.mypos / IN.mypos.w; 
     131        pix.norm = IN.normal; 
    140132 
    141133        // hack: squeeze some information about the ambient term into the target 
     
    143135         
    144136        // compute eye linear depth and scale with lenght to avoid sqr root in pixel shader 
     137        const float4 projPos = IN.projPos / IN.projPos.w; 
     138         
    145139        float2 screenCoord = projPos.xy * 0.5f + 0.5f; 
    146140        const float magView = length(Interpol(screenCoord, bl, br, tl, tr)); 
    147         pix.col.w = length(eyePos - IN.worldPos.xyz)  / magView; 
    148  
    149 #if 0 
    150         pix.pos = IN.worldPos.xyz * maxDepth; 
    151 #endif 
     141        pix.col.w = length(eyePos - IN.worldPos.xyz) / magView; 
    152142 
    153143        return pix; 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg

    r3016 r3017  
    1111struct fragment 
    1212{ 
    13          // normalized screen position 
    14         float4 pos: WPOS; 
    1513        float2 texCoord: TEXCOORD0;  
    1614        float3 view: TEXCOORD1; 
     
    4644 
    4745// reconstruct world space position 
    48 inline float3 ReconstructSamplePosition( 
    49                                                                                 uniform sampler2D colors, 
    50                                                                                 float2 texcoord,  
    51                                                                                 float3 bl, float3 br, float3 tl, float3 tr) 
    52 { 
    53 #if USE_EYE_SPACE_DEPTH 
    54         float eyeSpaceDepth = tex2Dlod(colors, float4(texcoord, 0, 0)).w; 
    55         //float3 rotView = normalize(Interpol(texcoord, bl, br, tl, tr)); 
    56         float3 rotView = Interpol(texcoord, bl, br, tl, tr); 
    57          
    58         float3 samplePos = -rotView * eyeSpaceDepth; 
    59 #else 
    60         float3 samplePos = tex2Dlod(colors, float4(texcoord, 0, SSAO_MIPMAP_LEVEL)).xyz; 
    61 #endif 
     46inline float3 ReconstructSamplePos(uniform sampler2D colors, 
     47                                                                   float2 texcoord,  
     48                                                                   float3 bl, float3 br, float3 tl, float3 tr) 
     49{ 
     50        const float eyeSpaceDepth = tex2Dlod(colors, float4(texcoord, 0, 0)).w; 
     51        float3 viewVec = Interpol(texcoord, bl, br, tl, tr); 
     52        float3 samplePos = -viewVec * eyeSpaceDepth; 
     53 
    6254        return samplePos; 
    6355} 
     
    7668                   uniform float3 br, 
    7769                   uniform float3 tl, 
    78                    uniform float3 tr 
    79                    //, uniform float3 viewDir 
     70                   uniform float3 tr,  
     71                   uniform float3 viewDir 
    8072                   ) 
    8173{ 
     
    9688                // add random noise: reflect around random normal vector (warning: slow!) 
    9789 
    98                 float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy; 
     90                float2 mynoise = tex2D(noiseTexture, IN.texCoord).xy; 
    9991                const float2 offsetTransformed = myreflect(offset, mynoise); 
    10092#else 
     
    10294#endif 
    10395                // weight with projected coordinate to reach similar kernel size for near and far 
    104                 float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * scaleFactor; 
     96                float2 texcoord = IN.texCoord.xy + offsetTransformed * scaleFactor; 
    10597 
    10698                //if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f))++ numSamples; 
    10799 
    108                 float3 samplePos = ReconstructSamplePosition(colors, texcoord, bl, br, tl, tr); 
    109  
    110  
    111                 /////// 
    112                 //-- compute contribution of current sample taking into account direction and angle 
    113  
    114                 float3 dirSample = samplePos - centerPosition.xyz; 
     100                const float3 samplePos = ReconstructSamplePos(colors, texcoord, bl, br, tl, tr); 
     101 
     102 
     103                //////////////// 
     104                //-- compute contribution of sample using the direction and angle 
     105 
     106                float3 dirSample = samplePos - centerPosition; 
    115107                const float lengthSample = length(dirSample); 
    116108 
     
    118110 
    119111                // angle between current normal and direction to sample controls AO intensity. 
    120                 const float cos_angle = max(dot(nDirSample, currentNormal), 0.0f); 
     112                const float cosAngle = max(dot(nDirSample, currentNormal), 0.0f); 
    121113 
    122114                // the distance_scale offset is used to avoid singularity that occurs at global illumination when  
    123115                // the distance to a sample approaches zero 
    124                 const float intensity =  
    125                         //(SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + lengthSample * lengthSample); 
    126                         SAMPLE_INTENSITY / (DISTANCE_SCALE + lengthSample * lengthSample); 
    127  
    128 #if 0 
     116                const float intensity = SAMPLE_INTENSITY / (DISTANCE_SCALE + lengthSample * lengthSample); 
     117 
     118#if 1 
    129119                // if surface normal perpenticular to view dir, approx. half of the samples will not count 
    130120                // => compensate for this (on the other hand, projected sampling area could be larger!) 
    131                 const float viewCorrection = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); 
    132                 total_ao += cos_angle * intensity * viewCorrection; 
     121                const float viewCorrection = 1.0f + VIEW_CORRECTION_SCALE * dot(viewDir, currentNormal); 
     122                total_ao += cosAngle * intensity * viewCorrection; 
     123#else 
     124                total_ao += cosAngle * intensity; 
    133125#endif 
    134                 total_ao += cos_angle * intensity; 
    135126        } 
    136127 
     
    138129} 
    139130 
     131#pragma position_invariant main 
    140132 
    141133/** The mrt shader for screen space ambient occlusion 
     
    143135pixel main(fragment IN,  
    144136                   uniform sampler2D colors, 
    145                    uniform sampler2D positions, 
    146137                   uniform sampler2D normals, 
    147138                   uniform sampler2D noiseTexture, 
     
    161152 
    162153        float4 norm = tex2Dlod(normals, float4(IN.texCoord, 0 ,0)); 
    163         float3 normal = normalize(norm.xyz); 
    164  
    165         // the w coordinate from the persp. projection 
    166         float w = norm.w; 
    167  
    168  
    169 #if USE_EYE_SPACE_DEPTH 
     154        const float3 normal = normalize(norm.xyz); 
     155 
    170156        /// reconstruct position from the eye space depth 
    171157        float3 viewDir = IN.view; 
    172158        const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w; 
    173  
    174159        const float3 eyeSpacePos = -viewDir * eyeDepth; 
    175  
    176         float3 centerPosition; 
    177         centerPosition.xyz = eyePos + eyeSpacePos; 
    178  
    179         const float2 ao = ssao(IN, colors, noiseTexture, samples, normal, eyeSpacePos, w, bl, br, tl, tr); 
    180  
    181 #else 
    182  
    183         // the current world position 
    184         const float3 centerPosition = tex2Dlod(positions, float4(IN.texCoord, 0, 0)).xyz; 
    185  
    186         const float2 ao = ssao(IN, positions, noiseTexture, samples, normal, centerPosition, w, bl, br, tl, tr); 
    187  
    188 #endif 
     160        const float3 centerPosition = eyePos + eyeSpacePos; 
     161 
     162        float4 realPos = float4(centerPosition, 1.0f); 
     163 
     164 
     165        //////////////// 
     166        //-- calculcate the current projected depth for next frame 
     167         
     168        float4 currentPos = mul(modelViewProj, realPos); 
     169         
     170        const float w = SAMPLE_RADIUS / currentPos.w; 
     171        currentPos /= currentPos.w; 
     172         
     173        const float currentDepth = currentPos.z * 1e-3f; 
     174 
     175        const float2 ao = ssao(IN, colors, noiseTexture, samples, normal, eyeSpacePos, w, bl, br, tl, tr, normalize(viewDir)); 
     176 
    189177 
    190178        ///////////////// 
    191179        //-- compute temporally smoothing 
    192180 
    193         float4 realPos = float4(centerPosition, 1.0f); 
    194  
    195  
    196         // calculcate the current projected depth for next frame 
    197         float4 currentPos = mul(modelViewProj, realPos); 
    198         currentPos /= currentPos.w; 
    199         const float currentDepth = currentPos.z; 
    200          
    201  
    202         /////////// 
    203         //-- reprojection new frame into old one  
     181 
     182        // reproject new frame into old one  
    204183         
    205184        // calculate projected depth 
     
    208187 
    209188        // the current depth projected into the old frame 
    210         const float projDepth = projPos.z; 
     189        const float projDepth = projPos.z * 1e-3f; 
    211190 
    212191        // fit from unit cube into 0 .. 1 
    213         float2 tex = (projPos.xy) * 0.5f + 0.5f; 
     192        const float2 tex = projPos.xy * 0.5f + 0.5f; 
    214193 
    215194        // retrieve the sample from the last frame 
    216195        float4 oldCol = tex2D(oldTex, tex); 
    217196 
    218         const float oldDepth = oldCol.w; 
    219         const float depthDif = 1.0f - projDepth / oldDepth; 
     197        const float oldDepth = oldCol.z; 
     198        //const float depthDif = 1.0f - projDepth / oldDepth; 
     199        const float depthDif = projDepth - oldDepth; 
    220200 
    221201 
    222202        //const float oldNumSamples = oldCol.y; 
    223         const float oldWeight = clamp(oldCol.z, 0, temporalCoherence); 
     203        const float oldWeight = clamp(oldCol.y, 0, temporalCoherence); 
    224204 
    225205        float newWeight; 
     
    227207        // the number of valid samples in this frame 
    228208        //const float newNumSamples = ao.y; 
    229  
    230209 
    231210        if (//(temporalCoherence > 0) && 
     
    239218                // increase the weight for convergence 
    240219                newWeight = oldWeight + 1.0f; 
    241                 OUT.illum_col.xy = (ao.xy + oldCol.xy * oldWeight) / newWeight; 
     220                OUT.illum_col.x = (ao.x + oldCol.x * oldWeight) / newWeight; 
    242221                //if (!(oldNumSamples > ao.y - 1.5f)) newWeight = 0; 
    243222        } 
    244223        else 
    245224        {        
    246                 OUT.illum_col.xy = ao.xy; 
     225                OUT.illum_col.x = ao.x; 
    247226                newWeight = 0; 
    248227        } 
    249228 
    250         OUT.illum_col.z = newWeight; 
    251         OUT.illum_col.w = currentDepth; 
     229        //OUT.illum_col.y=depthDif; 
     230        OUT.illum_col.y = newWeight; 
     231        OUT.illum_col.z = currentDepth; 
    252232 
    253233        return OUT; 
     234} 
     235 
     236 
     237float Filter(float2 texCoord,  
     238                         uniform sampler2D ssaoTex, 
     239                         uniform float2 filterOffs[NUM_DOWNSAMPLES], 
     240                         uniform float filterWeights[NUM_DOWNSAMPLES] 
     241) 
     242{ 
     243        float average = .0f; 
     244        float w = .0f; 
     245 
     246        for (int i = 0; i < NUM_DOWNSAMPLES; ++ i) 
     247        { 
     248                average += filterWeights[i] * tex2Dlod(ssaoTex, float4(texCoord + filterOffs[i], 0, 0)).x; 
     249                w += filterWeights[i]; 
     250        } 
     251 
     252        average *= 1.0f / (float)w; 
     253 
     254        return average; 
    254255} 
    255256 
     
    257258pixel combine(fragment IN,  
    258259                          uniform sampler2D colors, 
    259                           uniform sampler2D ssaoTex) 
     260                          uniform sampler2D ssaoTex, 
     261                          uniform float2 filterOffs[NUM_DOWNSAMPLES], 
     262                          uniform float filterWeights[NUM_DOWNSAMPLES] 
     263                          ) 
    260264{ 
    261265        pixel OUT; 
    262266 
    263267        float4 col = tex2Dlod(colors, float4(IN.texCoord, 0, 0)); 
    264         float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0)); 
     268        float3 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0)); 
     269 
     270        if (ao.y < 2000.0f) 
     271                ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights); 
    265272 
    266273        OUT.illum_col = col * ao.x; 
    267         //OUT.illum_col = float4(ao.x,ao.x,ao.x, ao.w); 
     274        //OUT.illum_col.xyz = float3(ao.x,1-ao.y*1e-2f, 0); 
    268275        OUT.illum_col.w = col.w; 
    269276 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao3d.cg

    r3010 r3017  
    6464 
    6565                float3 mynoise = (float3)tex2D(noiseTexture, IN.texCoord.xy); 
    66                 const float3 offsetTransformed = myreflect(offset, mynoise) * AREA_SIZE; 
    67                 //const float3 offsetTransformed = offset * AREA_SIZE; 
     66                const float3 offsetTransformed = myreflect(offset, mynoise) * SAMPLE_RADIUS; 
     67                //const float3 offsetTransformed = offset * SAMPLE_RADIUS; 
    6868 
    6969                // compute position 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg

    r3016 r3017  
    4646    return average; 
    4747}*/ 
    48    
     48  
     49 
     50// let bilinear filtering do its work 
    4951float4 DownSample(frag IN, 
    5052                                  uniform sampler2D colors, 
Note: See TracChangeset for help on using the changeset viewer.