- Timestamp:
- 12/08/08 03:01:45 (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/Camera.cpp
r3102 r3213 134 134 mat.x[3][0] = -DotProd(GetRightVector(), mPosition); 135 135 mat.x[3][1] = -DotProd(GetUpVector(), mPosition); 136 mat.x[3][2] = DotProd(GetDirection(), mPosition);136 mat.x[3][2] = DotProd(GetDirection(), mPosition); 137 137 } 138 138 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
r3212 r3213 10 10 #include "Light.h" 11 11 #include "ShaderManager.h" 12 #include "Texture.h" 13 12 14 #include <math.h> 13 15 … … 61 63 static ShaderProgram *sCgScaleDepthProgram = NULL; 62 64 static ShaderProgram *sCgPrepareSsaoProgram = NULL; 65 static ShaderProgram *sCgLenseFlareProgram = NULL; 63 66 64 67 … … 76 79 static float ssaoFilterWeights[NUM_SSAO_FILTER_SAMPLES]; 77 80 78 static GLuint sBurstTex; 79 static GLuint sHaloTex[4]; 81 static Texture *sHaloTex[4]; 80 82 81 83 int DeferredRenderer::colorBufferIdx = 0; 82 84 83 85 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 }110 86 111 87 … … 322 298 323 299 300 static 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 324 320 DeferredRenderer::DeferredRenderer(int w, int h, PerspectiveCamera *cam): 325 321 mWidth(w), mHeight(h), … … 383 379 384 380 mEyePos = mOldEyePos = Vector3::ZERO(); 381 382 PrepareLenseFlare(); 385 383 386 384 InitCg(); … … 420 418 sCgLogLumProgram = sm->CreateFragmentProgram("tonemap", "CalcAvgLogLum", "avgLogLum"); 421 419 sCgPrepareSsaoProgram = sm->CreateFragmentProgram("deferred", "PrepareSsao", "PrepareSsao"); 420 sCgLenseFlareProgram = sm->CreateFragmentProgram("lenseFlare", "LenseFlare", "LenseFlare"); 422 421 423 422 … … 498 497 499 498 //////////////// 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 500 511 501 512 const float filterWidth = 1.0f; … … 560 571 } 561 572 562 // multisampling is difficult / costly with deferred shading 563 // at least do some edge blurring 573 // q: use antialiasing before or after ssao? 564 574 //if (useAntiAliasing) AntiAliasing(fbo, light); 565 575 … … 587 597 } 588 598 599 600 LenseFlare(fbo, light); 601 589 602 // multisampling is difficult / costly with deferred shading 590 603 // at least do some edge blurring … … 1210 1223 /////////////////// 1211 1224 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 1213 1227 mCamera->GetViewOrientationMatrix(matViewing); 1214 1228 mCamera->GetProjectionMatrix(matProjection); … … 1257 1271 1258 1272 1259 void DeferredRenderer::LenseFlare(FrameBufferObject *fbo) 1260 { 1273 void 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 1261 1300 ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx); 1262 1301 GLuint colorsTex = colorBuffer->GetTexture(); 1302 1263 1303 FlipFbos(fbo); 1264 1304 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); 1271 1314 1272 1315 DrawQuad(sCgLenseFlareProgram); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
r3212 r3213 119 119 void SortSamples(); 120 120 121 void LenseFlare(FrameBufferObject *fbo, DirectionalLight *light); 122 121 123 122 124 //////////// -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/irradiance.cpp
r3207 r3213 1155 1155 1156 1156 ShadowMap *sm = showShadowMap ? shadowMap : NULL; 1157 1157 1158 deferredShader->Render(fbo, ssaoTempCohFactor, light, useHDR, useAntiAliasing, sm); 1158 1159 } -
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 */ 1 7 inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr) 2 8 { … … 9 15 10 16 11 // reconstruct world space position 17 /** reconstruct world space position 18 */ 12 19 inline float3 ReconstructSamplePos(uniform sampler2D tex, 13 20 float2 texcoord, -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/lenseFlare.cg
r3212 r3213 6 6 }; 7 7 8 float4 main(fragment IN, 9 uniform sampler2D colors, 10 uniform sampler2D , 11 ): COLOR 8 9 10 float4 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 12 19 { 13 20 // 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)); 17 22 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; 22 26 27 const float2 newTexCoords = (IN.texCoords - center) / scale + center - newPos; 23 28 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; 25 41 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
r3212 r3213 258 258 float3 viewDir, 259 259 float newWeight, 260 float sampleIntensity 260 float sampleIntensity, 261 bool isMovingObject 261 262 ) 262 263 { … … 312 313 313 314 ++ numSamples; 314 315 315 316 // 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 319 319 const float changeFactor = sampleColor.y; 320 320 const float pixelValid = sampleColor.x; 321 321 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 323 324 const float tooFarAway = step(0.5f, lengthToSample - changeFactor); 324 325 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 331 343 total_ao /= numSamples; 332 344 333 return float3( max(0.0f, 1.0f - total_ao), validSamples, numSamples);345 return float3(total_ao, validSamples, numSamples); 334 346 } 335 347 … … 382 394 float scaleFactor = kernelRadius * invw; 383 395 396 const float sqrMoveSpeed = SqrLen(diffVec); 397 const bool isMovingObject = (sqrMoveSpeed > DYNAMIC_OBJECTS_THRESHOLD); 398 384 399 385 400 ///////////////// … … 403 418 if (eyeSpaceDepth < 1e10f) 404 419 { 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); 406 421 //ao = ssao2(IN, colors, noiseTex, samples, normal, eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir), normals, sampleIntensity); 407 422 } … … 411 426 } 412 427 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 424 434 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 425 448 const float combinedWeight = clamp(newWeight + oldWeight, .0f, temporalCoherence); 426 449 427 428 // blendbetween old and new samples (and avoid division by zero)450 ////////// 451 //-- blend ao between old and new samples (and avoid division by zero) 429 452 OUT.illum_col.x = (ao.x * newWeight + oldSsao * oldWeight) / max(1e-6f, newWeight + oldWeight); 453 430 454 OUT.illum_col.z = SqrLen(diffVec); 431 455 OUT.illum_col.y = combinedWeight;
Note: See TracChangeset
for help on using the changeset viewer.