- Timestamp:
- 09/23/08 15:41:14 (16 years ago)
- Location:
- GTP/trunk/App/Demos/Vis/FriendlyCulling/src
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
r2965 r2966 9 9 #include "ShadowMapping.h" 10 10 #include "Light.h" 11 #include "ToneMapper.h" 11 12 12 13 … … 96 97 static CGparameter sLightDirShadowParam; 97 98 99 static CGparameter sImageKeyParam; 100 static CGparameter sImageKeyShadowParam; 98 101 99 102 //#define USE_3D_SSAO … … 108 111 static Sample2 samples2[NUM_SAMPLES]; 109 112 #endif 110 Sample2 pcfSamples[16]; 113 114 // number of pcf tabs 115 Sample2 pcfSamples[NUM_PCF_TABS]; 111 116 112 117 static int colorBufferIdx = 0; … … 281 286 sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals"); 282 287 sLightDirParam = cgGetNamedParameter(sCgDeferredProgram, "lightDir"); 288 289 sImageKeyParam = cgGetNamedParameter(sCgDeferredProgram, "imageKey"); 283 290 } 284 291 else … … 433 440 sLightDirShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "lightDir"); 434 441 435 PoissonDiscSampleGenerator2 poisson(16, 1.0f); 442 sImageKeyShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "imageKey"); 443 444 PoissonDiscSampleGenerator2 poisson(NUM_PCF_TABS, 1.0f); 436 445 poisson.Generate((float *)pcfSamples); 437 446 438 cgGLSetParameterArray2f(sSamplesShadowParam, 0, 16, (const float *)pcfSamples);447 cgGLSetParameterArray2f(sSamplesShadowParam, 0, NUM_PCF_TABS, (const float *)pcfSamples); 439 448 } 440 449 else … … 699 708 void DeferredRenderer::FirstPass(FrameBufferObject *fbo, DirectionalLight *light) 700 709 { 710 const float imageKey = ToneMapper().CalcImageKey(fbo->GetColorBuffer(0)); 711 701 712 GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 702 713 GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); … … 725 736 Vector3 lightDir = -light->GetDirection(); 726 737 cgGLSetParameter3f(sLightDirParam, lightDir.x, lightDir.y, lightDir.z); 738 739 cgGLSetParameter1f(sImageKeyParam, imageKey); 727 740 728 741 glColor3f(1.0f, 1.0f, 1.0f); … … 965 978 ShadowMap *shadowMap) 966 979 { 980 const float imageKey = ToneMapper().CalcImageKey(fbo->GetColorBuffer(0)); 981 967 982 GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 968 983 … … 1009 1024 cgGLSetParameter3f(sLightDirShadowParam, lightDir.x, lightDir.y, lightDir.z); 1010 1025 1026 // the average log luminance for tone mapping 1027 cgGLSetParameter1f(sImageKeyShadowParam, imageKey); 1011 1028 1012 1029 glColor3f(1.0f, 1.0f, 1.0f); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.h
r2965 r2966 41 41 ~ColorBufferObject(); 42 42 43 unsigned int GetTexture() const { return mTexId; }43 inline unsigned int GetTexture() const { return mTexId; } 44 44 45 inline int GetWidth() { return mWidth; } 46 47 inline int GetHeight() { return mHeight; } 45 48 /** Returns texture data. 46 49 */ 47 50 void *ReadTexture() const; 48 51 52 49 53 50 54 protected: -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ToneMapper.cpp
r2965 r2966 6 6 { 7 7 8 ToneMapper::ToneMapper(): mImageKey(.0f), mPreviousImageKey(.0f), mCurrentImageKey(.0f) 8 9 ToneMapper::ToneMapper() 9 10 { 10 11 } 11 12 12 13 13 bool ToneMapper::CalcLuminance(ColorBufferObject *colorBuffer) 14 float ToneMapper::CalcImageKey(ColorBufferObject *colorBuffer) const 14 15 { 15 int i = 0, j = 0; 16 float Rfloat, Gfloat, Bfloat; 16 float totalLum = 0; 17 17 18 int pixRGBE; 19 20 float pixLum = 0; 21 float totalLum = 0; 22 float pixNum = 0; 23 18 // we assume that we have a floating point rgba texture 24 19 float *pixels = (float *)colorBuffer->ReadTexture(); 25 20 26 /* 27 V_RETURN( pd3dDevice->GetRenderTargetData( pSourceRenderTarget, pPixelData ) ); 28 29 D3DSURFACE_DESC descSrc; 30 pPixelData->GetDesc( &descSrc ); 31 32 number_of_pixels = (float)(descSrc.Height * descSrc.Width); 21 const int w = colorBuffer->GetHeight(); 22 const int h = colorBuffer->GetWidth(); 33 23 34 // Lock the source texture for reading35 D3DLOCKED_RECT rcSrc; 36 V_RETURN( pPixelData->LockRect( &rcSrc, NULL, D3DLOCK_READONLY ) ); 37 38 // Location of texture data39 D3DXFLOAT16* pSourcePixels = (D3DXFLOAT16*)rcSrc.pBits;24 // Get full scene luminance 25 for (int i = 0; i < h; ++ i) 26 { 27 for (int j = 0; j < w; ++ j) 28 { 29 const int idx = i * w + j; 40 30 41 int localwidth = descSrc.Width; 42 int localheight = descSrc.Height; 43 g_start = g_myTimer.GetTime(); 44 45 // Get full scene luminance 46 for( i = 0; i < localheight; i++ ) 47 { 48 for( j = 0; j < localwidth; j++ ) 49 { 50 RGBEpixel = i * (localwidth) + j; 51 Rfloat =(float)pSourcePixels[4*RGBEpixel + 0]/255; 52 Gfloat =(float)pSourcePixels[4*RGBEpixel + 1]/255; 53 Bfloat =(float)pSourcePixels[4*RGBEpixel + 2]/255; 31 float r = pixels[4 * idx + 0]; 32 float g = pixels[4 * idx + 1]; 33 float b = pixels[4 * idx + 2]; 54 34 55 PixelLuminance = 0.2125f * Rfloat + 0.7154f * Gfloat + 0.0721f * Bfloat;35 float pixLum = 0.2125f * r + 0.7154f * g + 0.0721f * b; 56 36 57 TotalLuminance += (PixelLuminance + 0.001f);37 totalLum += log(pixLum + 1e-3f); 58 38 } 59 39 } 60 40 61 g_stop = g_myTimer.GetTime();41 const float key = exp(totalLum) / (float)(w * h); 62 42 63 g_dLumCalcTime = (double)g_stop - (double)g_start;43 static float previousKey = key; 64 44 65 pPixelData->UnlockRect(); 45 //float delta = imageKey - mPreviousImageKey; 46 // Constaint change for temporal coherence 47 //const float maxChange = 5-e2f; 48 //Clamp(delta, -maxChange, maxChange); 49 50 const float factor = 0.5f; 51 52 const float imageKey = key * factor + previousKey * (1.0f - factor); 53 previousKey = imageKey; 66 54 55 delete [] pixels; 67 56 68 g_fCurrentImageKey = (1/number_of_pixels) * TotalLuminance; 69 70 if(g_firstrun) 71 { 72 g_fPreviousImageKey = g_fCurrentImageKey; 73 g_firstrun = !g_firstrun; 74 } 75 76 float diff = (g_fCurrentImageKey - g_fPreviousImageKey); 77 78 // Constrain the luminance change to be more realistic 79 if (diff > 0.05f) 80 { 81 g_fImageKey = g_fPreviousImageKey + 0.05f; 82 g_fPreviousImageKey = g_fImageKey; 83 } 84 else if (diff < -0.05f) 85 { 86 g_fImageKey = g_fPreviousImageKey - 0.05f; 87 g_fPreviousImageKey = g_fImageKey; 88 } 89 else 90 { 91 g_fImageKey = g_fCurrentImageKey; 92 g_fPreviousImageKey = g_fImageKey; 93 } 94 95 return S_OK; 96 */ 97 return true; 57 return imageKey; 98 58 } 99 59 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ToneMapper.h
r2965 r2966 16 16 17 17 ToneMapper(); 18 /** Caculates scene luminance.18 /** Caculates average log lumincance. 19 19 */ 20 bool CalcLuminance(ColorBufferObject *colorBuffer); 21 22 23 protected: 24 25 float mImageKey; 26 float mPreviousImageKey; 27 float mCurrentImageKey; 20 float CalcImageKey(ColorBufferObject *colorBuffer) const; 28 21 }; 29 22 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.h
r2961 r2966 293 293 294 294 295 Real Random(Real max );296 int Random(int max );295 Real Random(Real maxValue); 296 int Random(int maxValue); 297 297 void Randomize(); 298 298 void Randomize(unsigned int seed); … … 310 310 } 311 311 312 void InitTiming(); 313 long GetTime(); 314 long GetRealTime(); 315 Real TimeDiff(long t1, long t2); 316 char *TimeString(); 317 312 inline Real Clamp(float x, float minVal, float maxVal) 313 { 314 if (x < minVal) return minVal; 315 if (x > maxVal) return maxVal; 316 317 return x; 318 } 318 319 319 320 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
r2964 r2966 1 1 #ifndef __SHADERENV_H 2 2 #define __SHADERENV_H 3 4 5 //////////// 6 //-- ssao + gi parameters 3 7 4 8 #define NUM_SAMPLES 8 5 9 //#define NUM_SAMPLES 16 6 10 7 8 11 // for linear falloff 9 12 //#define SAMPLE_INTENSITY 500.0f 10 //#define SAMPLE_INTENSITY 40.0f11 13 12 14 // for quadratic falloff 13 15 #define SAMPLE_INTENSITY 0.32f 14 16 //#define SAMPLE_INTENSITY 0.85f 15 //#define SAMPLE_INTENSITY 5.0f16 17 17 18 //#define AREA_SIZE 15e-1f 18 //#define AREA_SIZE 25e-1f19 19 #define AREA_SIZE 8e-1f 20 20 … … 24 24 //#define DISTANCE_SCALE 1e-4f 25 25 #define DISTANCE_SCALE 1e-6f 26 //#define DISTANCE_SCALE 1e-8f27 26 28 27 #define ILLUM_INTENSITY 5e-1f; … … 32 31 #define GI_MIPMAP_LEVEL 2 33 32 33 34 ///////// 35 //-- tone mapping 36 37 //#define MIDDLE_GRAY 0.36f 38 #define MIDDLE_GRAY 0.18f 39 40 41 ///////// 42 //-- shadowing 43 44 #define NUM_PCF_TABS 16 45 46 47 34 48 #endif // __SHADERENV_H -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
r2965 r2966 1 #include "../shaderenv.h" 2 3 1 4 struct fragment 2 5 { … … 13 16 }; 14 17 15 #define NUM_SAMPLES 16 18 19 20 float3 ToneMap(const float imageKey, float3 color) 21 { 22 float3 outCol; 23 const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z; 24 25 // adjust to middle gray 26 const float lum = MIDDLE_GRAY * pixLum / imageKey; 27 28 // map to range 29 const float scaledLum = lum / (1.0f + lum); 30 31 /*outCol *= MIDDLE_GRAY / (vLum + 1e-3f); 32 outCol *= (1.0f + vColor / LUM_WHITE); 33 outCol /= (1.0f + vColor);*/ 34 35 outCol = color / scaledLum; 36 //vColor.rgb += 0.6f * vBloom; 37 38 return outCol; 39 } 16 40 17 41 … … 40 64 float3 light2 = normalize(lightDir2.xyz); 41 65 float diffuseLight2 = saturate(dot(normal, light2)); 42 */66 */ 43 67 // diffuse intensity 44 68 const float angle = saturate(dot(normal, lightDir)); … … 62 86 uniform sampler2D positions, 63 87 uniform sampler2D normals, 64 uniform float3 lightDir 88 uniform float3 lightDir, 89 uniform float imageKey 65 90 ) 66 91 { … … 90 115 uniform float2 lightSpacePos, 91 116 uniform float depth, 92 uniform float2 samples[NUM_ SAMPLES],117 uniform float2 samples[NUM_PCF_TABS], 93 118 uniform sampler2D noiseTexture 94 119 ) … … 99 124 float total_d = 0.0; 100 125 101 for (int i = 0; i < NUM_ SAMPLES; ++ i)126 for (int i = 0; i < NUM_PCF_TABS; ++ i) 102 127 { 103 128 const float2 offset = samples[i]; … … 135 160 uniform float sampleWidth, 136 161 uniform sampler2D noiseTexture, 137 uniform float2 samples[NUM_SAMPLES], 138 uniform float3 lightDir 162 uniform float2 samples[NUM_PCF_TABS], 163 uniform float3 lightDir, 164 uniform float imageKey 139 165 ) 140 166 { … … 181 207 OUT.color = (emmisive < 0.95) ? (ambient + diffuse) * color : color; 182 208 209 OUT.color.xyz = ToneMap(imageKey, OUT.color.xyz); 210 183 211 // also write out depth component 184 212 OUT.color.w = color.w; … … 187 215 } 188 216 189 190 /*float4 FinalPass( SkyboxVS_Output Input ) : COLOR191 {192 float4 vColor = tex2D( s0, Input.Tex );193 float3 vBloom = tex2D( s1, Input.Tex );194 195 float vLum = g_fImageKey;196 vColor.rgb = vColor;197 198 // Tone mapping199 vColor.rgb *= g_fMiddleGray /(vLum + 0.001f);200 vColor.rgb *= (1.0f + vColor/LUM_WHITE);201 vColor.rgb /= (1.0f + vColor);202 203 vColor.rgb += 0.6f * vBloom;204 vColor.a = 1.0f;205 206 return vColor;207 }*/
Note: See TracChangeset
for help on using the changeset viewer.