Changeset 2973


Ignore:
Timestamp:
09/25/08 11:05:46 (16 years ago)
Author:
mattausch
Message:
 
Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling/src
Files:
4 edited

Legend:

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

    r2972 r2973  
    110110static CGparameter sColorsTexToneParam; 
    111111static CGparameter sIntensityTexDownSampleParam; 
     112 
    112113 
    113114 
     
    419420                cgGLLoadProgram(sCgAntiAliasingProgram); 
    420421 
    421                 sImageKeyParam = cgGetNamedParameter(sCgAntiAliasingProgram, "imageKey"); 
    422                 sMiddleGreyParam = cgGetNamedParameter(sCgAntiAliasingProgram, "middleGrey"); 
    423                 sWhiteLumParam = cgGetNamedParameter(sCgAntiAliasingProgram, "whiteLum"); 
    424  
    425422                sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors");   
    426423                sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals"); 
     
    465462                cerr << "deferred program failed to load" << endl; 
    466463 
     464        sCgToneProgram =  
     465                cgCreateProgramFromFile(context,  
     466                                                                CG_SOURCE, 
     467                                                                "src/shaders/tonemap.cg",  
     468                                                                RenderState::sCgFragmentProfile, 
     469                                                                "ToneMap", 
     470                                                                NULL); 
     471 
     472        if (sCgToneProgram != NULL) 
     473        { 
     474                cgGLLoadProgram(sCgToneProgram); 
     475 
     476                sImageKeyParam = cgGetNamedParameter(sCgToneProgram, "imageKey"); 
     477                sMiddleGreyParam = cgGetNamedParameter(sCgToneProgram, "middleGrey"); 
     478                sWhiteLumParam = cgGetNamedParameter(sCgToneProgram, "whiteLum"); 
     479 
     480                sColorsTexToneParam = cgGetNamedParameter(sCgToneProgram, "colors");   
     481        } 
     482        else 
     483                cerr << "tone program failed to load" << endl; 
     484 
     485        sCgInitialIntensityProgram =  
     486                cgCreateProgramFromFile(context,  
     487                                                                CG_SOURCE, 
     488                                                                "src/shaders/tonemap.cg",  
     489                                                                RenderState::sCgFragmentProfile, 
     490                                                                "GreyScaleDownSample", 
     491                                                                NULL); 
     492 
     493        if (sCgInitialIntensityProgram != NULL) 
     494        { 
     495                cgGLLoadProgram(sCgInitialIntensityProgram); 
     496 
     497                // we need size of texture for scaling 
     498                sColorsTexInitialParam = cgGetNamedParameter(sCgInitialIntensityProgram, "colors");   
     499        } 
     500        else 
     501                cerr << "intensity program failed to load" << endl; 
     502 
     503 
     504 
    467505        PrintGLerror("init"); 
    468506} 
     
    499537        glLoadIdentity(); 
    500538 
     539        colorBufferIdx = 0; 
     540 
    501541        const float offs = 0.5f; 
    502542        glOrtho(-offs, offs, -offs, offs, 0, 1); 
     
    510550        else 
    511551                FirstPass(fbo, light); 
     552 
     553 
     554        float imageKey, whiteLum, middleGrey; 
     555 
     556        ComputeToneParameters(fbo, light, imageKey, whiteLum, middleGrey); 
     557 
     558        ToneMap(fbo, light, imageKey, whiteLum, middleGrey); 
    512559 
    513560        switch (mShadingMethod) 
     
    575622 
    576623        // read the second buffer, write to the first buffer 
    577         //mNewFbo->Bind(); 
    578624        mFbo->Bind(); 
    579625        glDrawBuffers(1, mrt + mFboIndex); 
     
    685731        ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx); 
    686732 
    687         // we assume that we have a floating point rgba texture 
    688         float *pixels = (float *)colorBuffer->ReadTexture(); 
    689  
    690         const int w = colorBuffer->GetHeight(); 
    691         const int h = colorBuffer->GetWidth(); 
    692  
    693         const float imageKey = ToneMapper().CalcImageKey(pixels, w, h); 
    694         const float whiteLum = 1.0f * ToneMapper().CalcMaxLuminance(pixels, w, h); 
    695  
    696         //const float minKey = 0.18f; 
    697         //const float maxKey = 0.72f; 
    698  
    699         //const float minKey = 0.0045f; 
    700         const float minKey = 0.09f; 
    701         const float maxKey = 0.5f; 
    702  
    703         const float lightIntensity = DotProd(-light->GetDirection(), Vector3::UNIT_Z()); 
    704         const float middleGrey = lightIntensity * maxKey + (1.0f - lightIntensity) * minKey; 
    705  
    706         //cout << "middlegrey: " << middleGrey << endl; 
    707         delete [] pixels; 
    708  
    709733        GLuint colorsTex = colorBuffer->GetTexture(); 
    710734        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
     
    720744        cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex); 
    721745        cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam); 
    722  
    723         cgGLSetParameter1f(sImageKeyParam, imageKey); 
    724         cgGLSetParameter1f(sWhiteLumParam, whiteLum); 
    725         cgGLSetParameter1f(sMiddleGreyParam, middleGrey); 
    726746 
    727747 
     
    752772void DeferredRenderer::FirstPass(FrameBufferObject *fbo, DirectionalLight *light) 
    753773{ 
    754         GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     774        GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture(); 
    755775        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
    756776        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
     
    758778        fbo->Bind(); 
    759779 
    760         colorBufferIdx = 3; 
     780        colorBufferIdx = 3 - colorBufferIdx; 
    761781        glDrawBuffers(1, mrt + colorBufferIdx); 
    762782 
     
    912932void DeferredRenderer::CombineIllum(FrameBufferObject *fbo) 
    913933{ 
    914         GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture(); 
     934        GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture(); 
    915935 
    916936        GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture(); 
     
    921941 
    922942        // overwrite old color texture 
    923         colorBufferIdx = 0; 
     943        colorBufferIdx = 3 - colorBufferIdx; 
     944 
    924945        glDrawBuffers(1, mrt + colorBufferIdx); 
    925946 
     
    967988void DeferredRenderer::CombineSsao(FrameBufferObject *fbo) 
    968989{ 
    969         GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture(); 
     990        fbo->Bind(); 
     991 
     992        GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture(); 
    970993        GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture(); 
    971994 
    972         fbo->Bind(); 
    973  
    974995        // overwrite old color texture 
    975         colorBufferIdx = 0; 
     996        colorBufferIdx = 3 - colorBufferIdx; 
    976997        glDrawBuffers(1, mrt + colorBufferIdx); 
    977998 
     
    10171038                                                                           ShadowMap *shadowMap) 
    10181039{ 
    1019         GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     1040        fbo->Bind(); 
     1041 
     1042        GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture(); 
    10201043 
    10211044        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
     
    10261049        shadowMap->GetTextureMatrix(shadowMatrix); 
    10271050 
    1028         fbo->Bind(); 
    1029  
    1030         colorBufferIdx = 3; 
     1051        colorBufferIdx = 3 - colorBufferIdx; 
    10311052        glDrawBuffers(1, mrt + colorBufferIdx); 
    10321053 
     
    10491070 
    10501071        cgGLSetParameter1f(sMaxDepthShadowParam, mScaleFactor); 
     1072 
    10511073        //cgGLSetParameter1f(sSampleWidthParam, 10.0f / shadowMap->GetSize()); 
    10521074        cgGLSetParameter1f(sSampleWidthParam, 2.0f / shadowMap->GetSize()); 
    1053         //cgGLSetParameter1f(sSampleWidthParam, 2.0f / 2048); 
     1075         
    10541076          
    10551077        cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x); 
     
    11081130 
    11091131 
    1110 void DeferredRenderer::ComputeToneParameters(FrameBufferObject *fbo) 
     1132void DeferredRenderer::ComputeToneParameters(FrameBufferObject *fbo,  
     1133                                                                                         DirectionalLight *light, 
     1134                                                                                         float &imageKey,  
     1135                                                                                         float &whiteLum, 
     1136                                                                                         float &middleGrey) 
    11111137{ 
    11121138        ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx); 
    11131139 
     1140        // we assume that we have a floating point rgba texture 
     1141        float *pixels = (float *)colorBuffer->ReadTexture(); 
     1142         
     1143        const int w = colorBuffer->GetHeight(); 
     1144        const int h = colorBuffer->GetWidth(); 
     1145 
     1146        imageKey = ToneMapper().CalcImageKey(pixels, w, h); 
     1147        whiteLum = 1.0f * ToneMapper().CalcMaxLuminance(pixels, w, h); 
     1148 
     1149        //const float minKey = 0.18f; 
     1150        //const float maxKey = 0.72f; 
     1151 
     1152        //const float minKey = 0.0045f; 
     1153        const float minKey = 0.09f; 
     1154        const float maxKey = 0.5f; 
     1155 
     1156        const float lightIntensity = DotProd(-light->GetDirection(), Vector3::UNIT_Z()); 
     1157        middleGrey = lightIntensity * maxKey + (1.0f - lightIntensity) * minKey; 
     1158 
     1159        //cout << "middlegrey: " << middleGrey << endl; 
     1160        delete [] pixels; 
     1161 
     1162 
     1163        ///////////// 
     1164 
    11141165        GLuint colorsTex = colorBuffer->GetTexture(); 
    1115          
     1166 
     1167        fbo->Bind(); 
     1168 
     1169        colorBufferIdx = 3 - colorBufferIdx; 
     1170        glDrawBuffers(1, mrt + colorBufferIdx); 
     1171 
    11161172        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    11171173 
     
    11421198        cgGLDisableTextureParameter(sColorsTexToneParam); 
    11431199 
     1200        FrameBufferObject::Release(); 
     1201 
    11441202         
    11451203        //////////// 
     
    11511209        for (int i = 0; i < 32; ++ i) 
    11521210        { 
    1153                 DownSample(fbo); 
     1211                //DownSample(fbo); 
    11541212        } 
    11551213 
     
    11601218void DeferredRenderer::DownSample(FrameBufferObject *fbo) 
    11611219{ 
    1162         GLuint intensityTex; 
     1220        GLuint intensityTex=0; 
    11631221         
    11641222        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
     
    11701228 
    11711229 
    1172 void DeferredRenderer::ToneMap(FrameBufferObject *fbo, DirectionalLight *light) 
     1230void DeferredRenderer::ToneMap(FrameBufferObject *fbo, 
     1231                                                           DirectionalLight *light, 
     1232                                                           float imageKey,  
     1233                                                           float whiteLum, 
     1234                                                           float middleGrey) 
    11731235{ 
    11741236        ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx); 
    11751237 
    1176         // we assume that we have a floating point rgba texture 
    1177         float *pixels = (float *)colorBuffer->ReadTexture(); 
    1178  
    1179         const int w = colorBuffer->GetHeight(); 
    1180         const int h = colorBuffer->GetWidth(); 
    1181  
    1182         const float imageKey = ToneMapper().CalcImageKey(pixels, w, h); 
    1183         const float whiteLum = 1.0f * ToneMapper().CalcMaxLuminance(pixels, w, h); 
    1184  
    1185         //const float minKey = 0.18f; 
    1186         //const float maxKey = 0.72f; 
    1187  
    1188         //const float minKey = 0.0045f; 
    1189         const float minKey = 0.09f; 
    1190         const float maxKey = 0.5f; 
    1191  
    1192         const float lightIntensity = DotProd(-light->GetDirection(), Vector3::UNIT_Z()); 
    1193         const float middleGrey = lightIntensity * maxKey + (1.0f - lightIntensity) * minKey; 
    1194  
    1195         //cout << "middlegrey: " << middleGrey << endl; 
    1196         delete [] pixels; 
     1238        fbo->Bind(); 
     1239 
     1240        colorBufferIdx = 3 - colorBufferIdx; 
     1241        glDrawBuffers(1, mrt + colorBufferIdx); 
     1242 
    11971243 
    11981244        GLuint colorsTex = colorBuffer->GetTexture(); 
     
    12281274 
    12291275        cgGLDisableTextureParameter(sColorsTexToneParam); 
    1230  
    1231         PrintGLerror("antialiasing"); 
     1276        FrameBufferObject::Release(); 
     1277 
     1278        PrintGLerror("ToneMap"); 
    12321279} 
    12331280 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h

    r2972 r2973  
    7575        void FirstPassShadow(FrameBufferObject *fbo, DirectionalLight *light, ShadowMap *shadowMap); 
    7676 
    77         void ComputeToneParameters(FrameBufferObject *fbo); 
    78         void ToneMap(FrameBufferObject *fbo, DirectionalLight *light); 
     77        void ComputeToneParameters(FrameBufferObject *fbo, DirectionalLight *light, float &imageKey, float &whiteLum, float &middleGrey); 
     78 
     79        void ToneMap(FrameBufferObject *fbo, DirectionalLight *light, float imageKey, float whiteLum, float middleGrey); 
    7980 
    8081 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg

    r2970 r2973  
    2121 
    2222 
    23 float3 ToneMap(float imageKey, float whiteLum, float middleGrey, float3 color) 
    24 { 
    25         float3 outCol; 
    26         const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z; 
    27          
    28         // adjust to middle gray 
    29         const float lum = middleGrey * pixLum / imageKey; 
    30           
    31         // map to range and calc burnout 
    32         const float burnedLum = lum * (1.0f + lum / whiteLum * whiteLum) / (1.0f + lum); 
    33          
    34         outCol = color * burnedLum / pixLum; 
    35  
    36         /* 
    37         outCol = color; 
    38  
    39         outCol.rgb *= MIDDLE_GRAY /(MIDDLE_GRAY + 0.001f); 
    40     outCol.rgb *= (1.0f + color.rgb / whiteLum); 
    41         outCol.rgb /= (1.0f + color.rgb); 
    42 */ 
    43         //vColor.rgb += 0.6f * vBloom; 
    44          
    45         return outCol; 
    46 } 
    47  
    48  
    4923float4 main(v2p IN,  
    5024                        uniform sampler2D colors, 
    51                         uniform sampler2D normals, 
    52                         uniform float imageKey, 
    53                         uniform float whiteLum, 
    54                         uniform float middleGrey 
     25                        uniform sampler2D normals 
    5526                        ): COLOR 
    5627{ 
     
    12192        //return (s0 + s1 + s2 + s3) * 0.25f; 
    12293 
    123         //return float4(w); 
    124         col.xyz = ToneMap(imageKey, whiteLum, middleGrey, col.xyz); 
    125  
    12694        return col; 
    12795} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg

    r2972 r2973  
    77        float4 pos: WPOS; 
    88        float4 texCoord: TEXCOORD0;  
     9 
     10        float2 lt: TEXCOORD1; // left top 
     11        float2 rb: TEXCOORD2; // right bottom 
     12        float2 rt: TEXCOORD3; // right top 
     13        float2 lb: TEXCOORD4; // left bottom 
    914}; 
    1015 
    1116 
    12  
    13 float3 ToneMap(float imageKey, float whiteLum, float middleGrey, float3 color) 
     17struct pixel 
    1418{ 
    15         float3 outCol; 
    16         const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z; 
    17          
    18         // adjust to middle gray 
    19         const float lum = middleGrey * pixLum / imageKey; 
    20           
    21         // map to range and calc burnout 
    22         const float burnedLum = lum * (1.0f + lum / whiteLum * whiteLum) / (1.0f + lum); 
    23          
    24         outCol = color * burnedLum / pixLum; 
    25  
    26         return outCol; 
    27 } 
    28  
     19        float4 col: COLOR0; 
     20}; 
    2921 
    3022 
     
    3729    for(int i = 0; i < 16; ++ i) 
    3830    { 
    39         average += tex2D(colors, IN.texCoord + float2(downSampleOffs[i].x, downSampleOffs[i].y) ); 
     31        average += tex2D(colors, IN.texCoord.xy + downSampleOffs[i]); 
    4032    } 
    4133         
     
    4739 
    4840float4 GreyScaleDownSample(fragment IN, 
    49                                                    uniform sampler2D colors, 
    50                                                    float2 offs[16]): COLOR 
     41                                                   uniform sampler2D colors): COLOR 
    5142{ 
    5243 
     
    5546        float maximum = -1e20; 
    5647 
    57         float4 color = 0.0f; 
    58          
    5948        // the rgb weights 
    6049        const float3 w = float3(0.299f, 0.587f, 0.114f); 
    6150        //float3 w = float3(0.2125f, 0.7154f, 0.0721f); 
    6251 
     52        float4 cols[4]; 
     53 
     54        cols[0] = tex2D(colors, IN.texCoord.xy + IN.lt.xy); 
     55        cols[1] = tex2D(colors, IN.texCoord.xy + IN.lb.xy); 
     56        cols[2] = tex2D(colors, IN.texCoord.xy + IN.rt.xy); 
     57        cols[3] = tex2D(colors, IN.texCoord.xy + IN.rb.xy); 
     58 
     59 
    6360        for (int i = 0; i < 4; ++ i) 
    6461        { 
    65                 color = tex2D(colors, IN.texcoord + float2(offs[i].x, offs[i].y)); 
     62                const float intensity = dot(cols[i].rgb, w); 
    6663 
    67                 const float intensity = dot(color.rgb, W); 
    68  
    69                 maximum = max(maximum, intentsity); 
    70                 average += (0.25f * log(1e-5f + GreyValue)); 
     64                maximum = max(maximum, intensity); 
     65                average += (0.25f * log(1e-5f + intensity)); 
    7166        } 
    7267 
    7368        average = exp(average); 
    7469 
     70        return tex2D(colors, IN.texCoord.xy); 
     71 
    7572        // Output the luminance to the render target 
    7673        return float4(average, maximum, 0.0f, 1.0f); 
    7774} 
     75     
    7876 
    79      
    80      
     77pixel ToneMap(fragment IN, 
     78                          uniform sampler2D colors, 
     79                          uniform float imageKey,  
     80                          uniform float whiteLum,  
     81                          uniform float middleGrey) 
     82{ 
     83        pixel OUT; 
     84        float4 color = tex2D(colors, IN.texCoord.xy); 
     85 
     86        const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z; 
     87         
     88        // adjust to middle gray 
     89        const float lum = middleGrey * pixLum / imageKey; 
     90          
     91        // map to range and calc burnout 
     92        const float scaleLum = lum * (1.0f + lum / whiteLum * whiteLum) / (1.0f + lum); 
     93 
     94        OUT.col = color * scaleLum / pixLum; 
     95        OUT.col.w = color.w; 
     96 
     97        return OUT; 
     98} 
Note: See TracChangeset for help on using the changeset viewer.