Changeset 3213


Ignore:
Timestamp:
12/08/08 03:01:45 (15 years ago)
Author:
mattausch
Message:

lense flare starting to work

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

Legend:

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

    r3102 r3213  
    134134        mat.x[3][0] = -DotProd(GetRightVector(), mPosition); 
    135135        mat.x[3][1] = -DotProd(GetUpVector(), mPosition); 
    136         mat.x[3][2] = DotProd(GetDirection(), mPosition); 
     136        mat.x[3][2] =  DotProd(GetDirection(), mPosition); 
    137137} 
    138138 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp

    r3212 r3213  
    1010#include "Light.h" 
    1111#include "ShaderManager.h" 
     12#include "Texture.h" 
     13 
    1214#include <math.h> 
    1315 
     
    6163static ShaderProgram *sCgScaleDepthProgram = NULL; 
    6264static ShaderProgram *sCgPrepareSsaoProgram = NULL; 
     65static ShaderProgram *sCgLenseFlareProgram = NULL; 
    6366 
    6467 
     
    7679static float ssaoFilterWeights[NUM_SSAO_FILTER_SAMPLES]; 
    7780 
    78 static GLuint sBurstTex; 
    79 static GLuint sHaloTex[4]; 
     81static Texture *sHaloTex[4]; 
    8082 
    8183int DeferredRenderer::colorBufferIdx = 0; 
    8284 
    8385 
    84 static void PrepareLenseFlare() 
    85 { 
    86         sBurstTex = new Texture("burst.jpg"); 
    87  
    88         Texture(const std::string &filename); 
    89  
    90         glEnable(GL_TEXTURE_2D); 
    91         glGenTextures(1, &sBurstTex); 
    92         glBindTexture(GL_TEXTURE_2D, sBurstTex); 
    93                  
    94         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    95         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    96         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    97         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    98  
    99         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, randomNormals); 
    100  
    101         glBindTexture(GL_TEXTURE_2D, 0); 
    102         glDisable(GL_TEXTURE_2D); 
    103  
    104         delete [] randomNormals; 
    105  
    106         cout << "prepared lense flare textures" << endl; 
    107  
    108         PrintGLerror("prepare lense flare"); 
    109 } 
    11086 
    11187 
     
    322298 
    323299 
     300static void PrepareLenseFlare() 
     301{ 
     302        string textures[] = {"lens1.jpg", "lens2.jpg", "lens3.jpg", "lens4.jpg"}; 
     303 
     304        for (int i = 0; i < 4; ++ i) 
     305        { 
     306                sHaloTex[i] = new Texture(model_path + textures[i]); 
     307 
     308                sHaloTex[i]->SetBoundaryModeS(Texture::CLAMP); 
     309                sHaloTex[i]->SetBoundaryModeT(Texture::CLAMP); 
     310 
     311                sHaloTex[i]->Create(); 
     312        } 
     313 
     314        cout << "prepared lense flare textures" << endl; 
     315 
     316        PrintGLerror("prepare lense flare"); 
     317} 
     318 
     319 
    324320DeferredRenderer::DeferredRenderer(int w, int h, PerspectiveCamera *cam): 
    325321mWidth(w), mHeight(h),  
     
    383379 
    384380        mEyePos = mOldEyePos = Vector3::ZERO(); 
     381 
     382        PrepareLenseFlare(); 
    385383 
    386384        InitCg(); 
     
    420418        sCgLogLumProgram = sm->CreateFragmentProgram("tonemap", "CalcAvgLogLum", "avgLogLum"); 
    421419        sCgPrepareSsaoProgram = sm->CreateFragmentProgram("deferred", "PrepareSsao", "PrepareSsao"); 
     420        sCgLenseFlareProgram = sm->CreateFragmentProgram("lenseFlare", "LenseFlare", "LenseFlare"); 
    422421 
    423422 
     
    498497 
    499498        //////////////// 
     499 
     500         
     501        string lenseFlareParams[] =  
     502                {"colorsTex", "flareTex1", "flareTex2", "flareTex3", "flareTex4",  
     503                 "vectorToLight", "distanceToLight"}; 
     504 
     505        sCgLenseFlareProgram->AddParameters(lenseFlareParams, 0, 7); 
     506 
     507 
     508        //////////////// 
     509        //-- prepare filters for ssao 
     510 
    500511 
    501512        const float filterWidth = 1.0f; 
     
    560571        } 
    561572 
    562         // multisampling is difficult / costly with deferred shading 
    563         // at least do some edge blurring  
     573        // q: use antialiasing before or after ssao? 
    564574        //if (useAntiAliasing) AntiAliasing(fbo, light); 
    565575 
     
    587597        } 
    588598 
     599 
     600        LenseFlare(fbo, light); 
     601 
    589602        // multisampling is difficult / costly with deferred shading 
    590603        // at least do some edge blurring  
     
    12101223        /////////////////// 
    12111224 
    1212  
     1225        // use view orientation as we assume that the eye point is always in the center 
     1226        // of our coordinate system, this way we have the highest precision near the eye point 
    12131227        mCamera->GetViewOrientationMatrix(matViewing); 
    12141228        mCamera->GetProjectionMatrix(matProjection); 
     
    12571271 
    12581272 
    1259 void DeferredRenderer::LenseFlare(FrameBufferObject *fbo) 
    1260 { 
     1273void DeferredRenderer::LenseFlare(FrameBufferObject *fbo, 
     1274                                                                  DirectionalLight *light 
     1275                                                                  ) 
     1276{ 
     1277        // the sun is a large distance in the reverse light direction 
     1278        // => extrapolate light pos 
     1279         
     1280        const Vector3 lightPos = light->GetDirection() * -1e3f; 
     1281 
     1282        Vector3 projLightPos = mProjViewMatrix * lightPos * 0.5f + Vector3(0.5f); 
     1283        cout << "lightPos " << projLightPos << endl; 
     1284 
     1285        // check if light is visible in the image 
     1286        if ((projLightPos.x < -0.2f) || (projLightPos.y < -0.2f) || 
     1287                (projLightPos.x >= 1.2f) || (projLightPos.y >= 1.2f)) 
     1288                return; 
     1289 
     1290        // vector to light from screen center in texture space 
     1291        Vector3 vectorToLight = projLightPos - Vector3(0.5f); 
     1292        vectorToLight.z = .0f; 
     1293 
     1294        const float distanceToLight = Magnitude(vectorToLight); 
     1295        vectorToLight /= distanceToLight; 
     1296 
     1297        cout << "dist " << distanceToLight << " v " << vectorToLight << endl; 
     1298 
     1299 
    12611300        ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx); 
    1262  
     1301        GLuint colorsTex = colorBuffer->GetTexture(); 
     1302         
    12631303        FlipFbos(fbo); 
    12641304 
    1265         sCgLenseFlareProgram->SetTexture(0, colorsTex); 
    1266         sCgLenseFlareProgram->SetTexture(1, sBurstTex); 
    1267         sCgLenseFlareProgram->SetTexture(2, sHaloTex[0]); 
    1268         sCgLenseFlareProgram->SetTexture(3, sHaloTex[1]); 
    1269         sCgLenseFlareProgram->SetValue1f(4, sHaloTex[2]); 
    1270         sCgLenseFlareProgram->SetValue1f(5, sHaloTex[3]); 
     1305        int i = 0; 
     1306 
     1307        sCgLenseFlareProgram->SetTexture(i ++, colorsTex); 
     1308        sCgLenseFlareProgram->SetTexture(i ++, sHaloTex[0]->GetId()); 
     1309        sCgLenseFlareProgram->SetTexture(i ++, sHaloTex[1]->GetId()); 
     1310        sCgLenseFlareProgram->SetTexture(i ++, sHaloTex[2]->GetId()); 
     1311        sCgLenseFlareProgram->SetTexture(i ++, sHaloTex[3]->GetId()); 
     1312        sCgLenseFlareProgram->SetValue2f(i ++, vectorToLight.x, vectorToLight.y); 
     1313        sCgLenseFlareProgram->SetValue1f(i ++, distanceToLight); 
    12711314 
    12721315        DrawQuad(sCgLenseFlareProgram); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h

    r3212 r3213  
    119119        void SortSamples(); 
    120120 
     121        void LenseFlare(FrameBufferObject *fbo, DirectionalLight *light); 
     122  
    121123 
    122124        //////////// 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/irradiance.cpp

    r3207 r3213  
    11551155 
    11561156                ShadowMap *sm = showShadowMap ? shadowMap : NULL; 
     1157 
    11571158                deferredShader->Render(fbo, ssaoTempCohFactor, light, useHDR, useAntiAliasing, sm); 
    11581159        } 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/common.h

    r3170 r3213  
     1/************************************/ 
     2/*     Common shader functions      */ 
     3/************************************/ 
     4 
     5/** Interpolate bilinearly between 2 vectors 
     6*/ 
    17inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr) 
    28{ 
     
    915 
    1016 
    11 // reconstruct world space position 
     17/** reconstruct world space position 
     18*/ 
    1219inline float3 ReconstructSamplePos(uniform sampler2D tex, 
    1320                                                                   float2 texcoord,  
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/lenseFlare.cg

    r3212 r3213  
    66}; 
    77 
    8 float4 main(fragment IN,  
    9                         uniform sampler2D colors, 
    10                         uniform sampler2D , 
    11                         ): COLOR 
     8 
     9 
     10float4 LenseFlare(fragment IN,  
     11                                  uniform sampler2D colorsTex, 
     12                                  uniform sampler2D flareTex1, 
     13                                  uniform sampler2D flareTex2, 
     14                                  uniform sampler2D flareTex3, 
     15                                  uniform sampler2D flareTex4, 
     16                                  uniform float2 vectorToLight, // vector to current light position 
     17                                  uniform float distanceToLight // distance to current light position 
     18                                  ): COLOR 
    1219{ 
    1320        // center color 
    14         const float4 centerColor = tex2Dlod(colors, float4(IN.texCoords, 0, 0)); 
    15         // center depth 
    16         const float4 centerDepth = float4(centerColor.w); 
     21        const float4 color = tex2Dlod(colorsTex, float4(IN.texCoords, 0, 0)); 
    1722         
    18         const float2 lt = IN.texCoords + offsets[0]; 
    19         const float2 rb = IN.texCoords + offsets[1]; 
    20         const float2 rt = IN.texCoords + offsets[2]; 
    21         const float2 lb = IN.texCoords + offsets[3]; 
     23        const float scale = 0.5f; 
     24        const float center = float2(.5f, .5f); 
     25        const float2 newPos = vectorToLight * distanceToLight; 
    2226 
     27        const float2 newTexCoords = (IN.texCoords - center) / scale + center - newPos; 
    2328 
    24         return col; 
     29        const float4 flare1 = tex2Dlod(flareTex1, float4(newTexCoords, 0, 0)); 
     30 
     31        float4 result; 
     32 
     33        result.w = color.w; 
     34 
     35        //const float factor = 0.9f; 
     36        //result.xyz = lerp(color.xyz, flare1.xyz, factor); 
     37        //result.xyz = color.xyz * (1.0f - flare1.w) + flare1.xyz * flare1.w; 
     38        result.xyz = color.xyz + flare1.xyz; 
     39 
     40        return result; 
    2541} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg

    r3212 r3213  
    258258                        float3 viewDir, 
    259259                        float newWeight, 
    260                         float sampleIntensity 
     260                        float sampleIntensity, 
     261                        bool isMovingObject 
    261262                        ) 
    262263{ 
     
    312313 
    313314                ++ numSamples; 
    314                  
     315 
    315316                // check if the samples have been valid in the last frame 
    316                 // only mark sample as invalid if taking it into account can have influence the ssao: 
    317                 // hence we also check if the sample or the same sample in the previous frame 
    318                 // had any chance to be near the current sample 
     317                // only mark sample as invalid if in the last / current frame 
     318                // they possibly have any influence on the ao 
    319319                const float changeFactor = sampleColor.y; 
    320320                const float pixelValid = sampleColor.x; 
    321321 
    322                 // hack: cedistance measure can fail in some cases => choose something different 
     322                // we check if the sample could have been near enough to the current pixel  
     323                // to have any influence in the current or last frame 
    323324                const float tooFarAway = step(0.5f, lengthToSample - changeFactor); 
    324325                validSamples = max(validSamples, (1.0f - tooFarAway) * pixelValid); 
    325                 //validSamples += sampleColor.x; 
    326  
    327                 //if ((validSamples < 1.0f) && (newWeight > 200) && (numSamples >= 8)) break; 
    328                 //if ((validSamples < 1.0f) && (numSamples >= 8)) break; 
    329         } 
    330  
     326 
     327#ifdef USE_GTX 
     328                // we can bail out early and use a minimal #samples) 
     329                // if some conditions are met as long as the hardware supports it 
     330                if (numSamples >= 8) 
     331                { 
     332                        // if the pixel belongs to a static object and all the samples stay valid in the current frame 
     333                        if (!isMovingObject && (validSamples < 1.0f)) break; 
     334                        // if the pixel belongs to a dynamic object but the #accumulated samples for this pixel is sufficiently high  
     335                        // (=> there was no discontinuity recently) 
     336                        else if (isMovingObject && (newWeight > NUM_SAMPLES * 5)) break; 
     337                } 
     338#endif 
     339 
     340        } 
     341 
     342        // scale ao contribution 
    331343        total_ao /= numSamples; 
    332344 
    333         return float3(max(0.0f, 1.0f - total_ao), validSamples, numSamples); 
     345        return float3(total_ao, validSamples, numSamples); 
    334346} 
    335347 
     
    382394        float scaleFactor = kernelRadius * invw; 
    383395 
     396        const float sqrMoveSpeed = SqrLen(diffVec); 
     397        const bool isMovingObject = (sqrMoveSpeed > DYNAMIC_OBJECTS_THRESHOLD); 
     398 
    384399         
    385400        ///////////////// 
     
    403418        if (eyeSpaceDepth < 1e10f) 
    404419        { 
    405                 ao = ssao(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir), oldWeight, sampleIntensity); 
     420                ao = ssao(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir), oldWeight, sampleIntensity, isMovingObject); 
    406421                //ao = ssao2(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir), normals, sampleIntensity); 
    407422        } 
     
    411426        } 
    412427 
    413         const float squaredLen = SqrLen(diffVec); 
    414  
    415         // check if we have to reset pixel because one of the sample points was invalid 
    416         if (squaredLen < DYNAMIC_OBJECTS_THRESHOLD) 
    417         { 
    418                 if (ao.y > 4.0f)  
    419                         oldWeight = 0;  
    420                 else if (ao.y > 1.0f) 
    421                         oldWeight = min(oldWeight, 4.0f * NUM_SAMPLES); 
    422         } 
    423  
     428         
     429        /////////// 
     430        //-- check if we have to reset pixel because one of the sample points was invalid 
     431        //-- only do this if the current pixel does not belong to a moving object 
     432 
     433        // the weight equals the number of sampled shot in this pass 
    424434        const float newWeight = ao.z; 
     435 
     436        const float completelyResetThres = 4.0f; 
     437        const float partlyResetThres = 1.0f; 
     438         
     439        if (!isMovingObject) 
     440        { 
     441                if (ao.y > completelyResetThres)  
     442                        oldWeight = .0f;  
     443                else if (ao.y > partlyResetThres) 
     444                        oldWeight = min(oldWeight, 4.0f * newWeight); 
     445        } 
     446 
     447        // the new weight for the next frame 
    425448        const float combinedWeight = clamp(newWeight + oldWeight, .0f, temporalCoherence); 
    426449 
    427          
    428         // blend between old and new samples (and avoid division by zero) 
     450        ////////// 
     451        //-- blend ao between old and new samples (and avoid division by zero) 
    429452        OUT.illum_col.x = (ao.x * newWeight + oldSsao * oldWeight) / max(1e-6f, newWeight + oldWeight); 
     453 
    430454        OUT.illum_col.z = SqrLen(diffVec); 
    431455        OUT.illum_col.y = combinedWeight; 
Note: See TracChangeset for help on using the changeset viewer.