- Timestamp:
- 09/25/08 11:05:46 (16 years ago)
- 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 110 110 static CGparameter sColorsTexToneParam; 111 111 static CGparameter sIntensityTexDownSampleParam; 112 112 113 113 114 … … 419 420 cgGLLoadProgram(sCgAntiAliasingProgram); 420 421 421 sImageKeyParam = cgGetNamedParameter(sCgAntiAliasingProgram, "imageKey");422 sMiddleGreyParam = cgGetNamedParameter(sCgAntiAliasingProgram, "middleGrey");423 sWhiteLumParam = cgGetNamedParameter(sCgAntiAliasingProgram, "whiteLum");424 425 422 sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors"); 426 423 sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals"); … … 465 462 cerr << "deferred program failed to load" << endl; 466 463 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 467 505 PrintGLerror("init"); 468 506 } … … 499 537 glLoadIdentity(); 500 538 539 colorBufferIdx = 0; 540 501 541 const float offs = 0.5f; 502 542 glOrtho(-offs, offs, -offs, offs, 0, 1); … … 510 550 else 511 551 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); 512 559 513 560 switch (mShadingMethod) … … 575 622 576 623 // read the second buffer, write to the first buffer 577 //mNewFbo->Bind();578 624 mFbo->Bind(); 579 625 glDrawBuffers(1, mrt + mFboIndex); … … 685 731 ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx); 686 732 687 // we assume that we have a floating point rgba texture688 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 709 733 GLuint colorsTex = colorBuffer->GetTexture(); 710 734 GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); … … 720 744 cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex); 721 745 cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam); 722 723 cgGLSetParameter1f(sImageKeyParam, imageKey);724 cgGLSetParameter1f(sWhiteLumParam, whiteLum);725 cgGLSetParameter1f(sMiddleGreyParam, middleGrey);726 746 727 747 … … 752 772 void DeferredRenderer::FirstPass(FrameBufferObject *fbo, DirectionalLight *light) 753 773 { 754 GLuint colorsTex = fbo->GetColorBuffer( 0)->GetTexture();774 GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture(); 755 775 GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 756 776 GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); … … 758 778 fbo->Bind(); 759 779 760 colorBufferIdx = 3 ;780 colorBufferIdx = 3 - colorBufferIdx; 761 781 glDrawBuffers(1, mrt + colorBufferIdx); 762 782 … … 912 932 void DeferredRenderer::CombineIllum(FrameBufferObject *fbo) 913 933 { 914 GLuint colorsTex = fbo->GetColorBuffer( 3)->GetTexture();934 GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture(); 915 935 916 936 GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture(); … … 921 941 922 942 // overwrite old color texture 923 colorBufferIdx = 0; 943 colorBufferIdx = 3 - colorBufferIdx; 944 924 945 glDrawBuffers(1, mrt + colorBufferIdx); 925 946 … … 967 988 void DeferredRenderer::CombineSsao(FrameBufferObject *fbo) 968 989 { 969 GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture(); 990 fbo->Bind(); 991 992 GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture(); 970 993 GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture(); 971 994 972 fbo->Bind();973 974 995 // overwrite old color texture 975 colorBufferIdx = 0;996 colorBufferIdx = 3 - colorBufferIdx; 976 997 glDrawBuffers(1, mrt + colorBufferIdx); 977 998 … … 1017 1038 ShadowMap *shadowMap) 1018 1039 { 1019 GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 1040 fbo->Bind(); 1041 1042 GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture(); 1020 1043 1021 1044 GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); … … 1026 1049 shadowMap->GetTextureMatrix(shadowMatrix); 1027 1050 1028 fbo->Bind(); 1029 1030 colorBufferIdx = 3; 1051 colorBufferIdx = 3 - colorBufferIdx; 1031 1052 glDrawBuffers(1, mrt + colorBufferIdx); 1032 1053 … … 1049 1070 1050 1071 cgGLSetParameter1f(sMaxDepthShadowParam, mScaleFactor); 1072 1051 1073 //cgGLSetParameter1f(sSampleWidthParam, 10.0f / shadowMap->GetSize()); 1052 1074 cgGLSetParameter1f(sSampleWidthParam, 2.0f / shadowMap->GetSize()); 1053 //cgGLSetParameter1f(sSampleWidthParam, 2.0f / 2048);1075 1054 1076 1055 1077 cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x); … … 1108 1130 1109 1131 1110 void DeferredRenderer::ComputeToneParameters(FrameBufferObject *fbo) 1132 void DeferredRenderer::ComputeToneParameters(FrameBufferObject *fbo, 1133 DirectionalLight *light, 1134 float &imageKey, 1135 float &whiteLum, 1136 float &middleGrey) 1111 1137 { 1112 1138 ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx); 1113 1139 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 1114 1165 GLuint colorsTex = colorBuffer->GetTexture(); 1115 1166 1167 fbo->Bind(); 1168 1169 colorBufferIdx = 3 - colorBufferIdx; 1170 glDrawBuffers(1, mrt + colorBufferIdx); 1171 1116 1172 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1117 1173 … … 1142 1198 cgGLDisableTextureParameter(sColorsTexToneParam); 1143 1199 1200 FrameBufferObject::Release(); 1201 1144 1202 1145 1203 //////////// … … 1151 1209 for (int i = 0; i < 32; ++ i) 1152 1210 { 1153 DownSample(fbo);1211 //DownSample(fbo); 1154 1212 } 1155 1213 … … 1160 1218 void DeferredRenderer::DownSample(FrameBufferObject *fbo) 1161 1219 { 1162 GLuint intensityTex ;1220 GLuint intensityTex=0; 1163 1221 1164 1222 cgGLEnableProfile(RenderState::sCgFragmentProfile); … … 1170 1228 1171 1229 1172 void DeferredRenderer::ToneMap(FrameBufferObject *fbo, DirectionalLight *light) 1230 void DeferredRenderer::ToneMap(FrameBufferObject *fbo, 1231 DirectionalLight *light, 1232 float imageKey, 1233 float whiteLum, 1234 float middleGrey) 1173 1235 { 1174 1236 ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx); 1175 1237 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 1197 1243 1198 1244 GLuint colorsTex = colorBuffer->GetTexture(); … … 1228 1274 1229 1275 cgGLDisableTextureParameter(sColorsTexToneParam); 1230 1231 PrintGLerror("antialiasing"); 1276 FrameBufferObject::Release(); 1277 1278 PrintGLerror("ToneMap"); 1232 1279 } 1233 1280 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
r2972 r2973 75 75 void FirstPassShadow(FrameBufferObject *fbo, DirectionalLight *light, ShadowMap *shadowMap); 76 76 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); 79 80 80 81 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg
r2970 r2973 21 21 22 22 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 gray29 const float lum = middleGrey * pixLum / imageKey;30 31 // map to range and calc burnout32 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 49 23 float4 main(v2p IN, 50 24 uniform sampler2D colors, 51 uniform sampler2D normals, 52 uniform float imageKey, 53 uniform float whiteLum, 54 uniform float middleGrey 25 uniform sampler2D normals 55 26 ): COLOR 56 27 { … … 121 92 //return (s0 + s1 + s2 + s3) * 0.25f; 122 93 123 //return float4(w);124 col.xyz = ToneMap(imageKey, whiteLum, middleGrey, col.xyz);125 126 94 return col; 127 95 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg
r2972 r2973 7 7 float4 pos: WPOS; 8 8 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 9 14 }; 10 15 11 16 12 13 float3 ToneMap(float imageKey, float whiteLum, float middleGrey, float3 color) 17 struct pixel 14 18 { 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 }; 29 21 30 22 … … 37 29 for(int i = 0; i < 16; ++ i) 38 30 { 39 average += tex2D(colors, IN.texCoord + float2(downSampleOffs[i].x, downSampleOffs[i].y));31 average += tex2D(colors, IN.texCoord.xy + downSampleOffs[i]); 40 32 } 41 33 … … 47 39 48 40 float4 GreyScaleDownSample(fragment IN, 49 uniform sampler2D colors, 50 float2 offs[16]): COLOR 41 uniform sampler2D colors): COLOR 51 42 { 52 43 … … 55 46 float maximum = -1e20; 56 47 57 float4 color = 0.0f;58 59 48 // the rgb weights 60 49 const float3 w = float3(0.299f, 0.587f, 0.114f); 61 50 //float3 w = float3(0.2125f, 0.7154f, 0.0721f); 62 51 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 63 60 for (int i = 0; i < 4; ++ i) 64 61 { 65 co lor = tex2D(colors, IN.texcoord + float2(offs[i].x, offs[i].y));62 const float intensity = dot(cols[i].rgb, w); 66 63 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)); 71 66 } 72 67 73 68 average = exp(average); 74 69 70 return tex2D(colors, IN.texCoord.xy); 71 75 72 // Output the luminance to the render target 76 73 return float4(average, maximum, 0.0f, 1.0f); 77 74 } 75 78 76 79 80 77 pixel 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.