Changeset 2944
- Timestamp:
- 09/15/08 00:07:01 (16 years ago)
- Location:
- GTP/trunk/App/Demos/Vis/FriendlyCulling/src
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp
r2929 r2944 1223 1223 { 1224 1224 Polyhedron *oldPolyhedron = new Polyhedron(polyhedron); 1225 1226 if (!oldPolyhedron->Valid()) cerr << "******************* not valid!! ************* " << endl;1227 1228 1225 Polyhedron *newPolyhedron = NULL; 1229 1226 … … 1235 1232 { 1236 1233 DEL_PTR(newPolyhedron); 1237 cerr << "polyhedron not valid or NULL!" << endl; 1238 1234 //cerr << "polyhedron not valid or NULL!" << endl; 1239 1235 return NULL; 1240 1236 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp
r2942 r2944 57 57 58 58 //mBaseOrientation = Matrix4x4(right, up, -dir); 59 mBaseOrientation = MyLookAt(Vector3::ZERO(), dir, up);//Matrix4x4(right, up, -dir);59 mBaseOrientation = LookAt(Vector3::ZERO(), dir, up);//Matrix4x4(right, up, -dir); 60 60 mViewOrientation = mBaseOrientation; 61 61 } … … 108 108 109 109 frustum = Frustum(matProjectionView); 110 110 111 111 112 //////////// -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
r2932 r2944 22 22 static CGprogram sCgAntiAliasingProgram = NULL; 23 23 static CGprogram sCgDeferredShadowProgram = NULL; 24 static CGprogram sCgShadowDebugProgram = NULL;25 24 26 25 static CGparameter sColorsTexCombineParam; … … 31 30 static CGparameter sNormalsTexDeferredParam; 32 31 33 static CGparameter sShadowMapDebugParam;34 static CGparameter sShadowMatrixDebugParam;35 static CGparameter sColorsTexDebugParam;36 37 38 32 static CGprogram sCgCombinedSsaoProgram = NULL; 39 33 static CGprogram sCgCombinedIllumProgram = NULL; 40 34 41 /////////////////////////////////////// 735 /////////////////////////////////////// 42 36 43 37 … … 95 89 static CGparameter sSampleWidthParam; 96 90 91 static CGparameter sNoiseTexShadowParam; 92 static CGparameter sSamplesShadowParam; 93 94 97 95 //#define USE_3D_SSAO 98 96 … … 102 100 // ssao random spherical samples 103 101 #ifdef USE_3D_SSAO 104 105 102 static Sample2 samples3[NUM_SAMPLES]; 106 107 103 #else 108 109 104 static Sample2 samples2[NUM_SAMPLES]; 110 111 105 #endif 106 Sample2 pcfSamples[16]; 112 107 113 108 static int colorBufferIdx = 0; … … 281 276 sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions"); 282 277 sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors"); 283 sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals"); 278 sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals"); 284 279 } 285 280 else … … 316 311 317 312 sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex"); 318 319 313 sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples"); 320 314 } … … 338 332 sColorsTexGiParam = cgGetNamedParameter(sCgGiProgram, "colors"); 339 333 sNormalsTexGiParam = cgGetNamedParameter(sCgGiProgram, "normals"); 340 sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture");341 334 342 335 sOldModelViewProjMatrixGiParam = cgGetNamedParameter(sCgGiProgram, "oldModelViewProj"); … … 344 337 sTemporalCoherenceGiParam = cgGetNamedParameter(sCgGiProgram, "temporalCoherence"); 345 338 339 sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture"); 346 340 sSamplesGiParam = cgGetNamedParameter(sCgGiProgram, "samples"); 347 341 … … 430 424 sSampleWidthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "sampleWidth"); 431 425 sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix"); 426 427 sNoiseTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "noiseTexture"); 428 sSamplesShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "samples"); 429 430 PoissonDiscSampleGenerator2 poisson(16, 1.0f); 431 poisson.Generate((float *)pcfSamples); 432 433 cgGLSetParameterArray2f(sSamplesShadowParam, 0, 16, (const float *)pcfSamples); 432 434 } 433 435 else 434 436 cerr << "deferred program failed to load" << endl; 435 436 sCgShadowDebugProgram =437 cgCreateProgramFromFile(context,438 CG_SOURCE,439 "src/shaders/deferred.cg",440 RenderState::sCgFragmentProfile,441 "main_shadow_debug",442 NULL);443 444 if (sCgShadowDebugProgram != NULL)445 {446 cgGLLoadProgram(sCgShadowDebugProgram);447 448 // we need size of texture for scaling449 sShadowMapDebugParam = cgGetNamedParameter(sCgShadowDebugProgram, "shadowMap");450 sShadowMatrixDebugParam = cgGetNamedParameter(sCgShadowDebugProgram, "shadowMatrix");451 sColorsTexDebugParam = cgGetNamedParameter(sCgShadowDebugProgram, "colorTex");452 }453 else454 cerr << "shadow debug program failed to load" << endl;455 437 456 438 PrintGLerror("init"); … … 494 476 glLoadIdentity(); 495 477 496 if (shadowMap && (mShadingMethod == GI)) 497 { 498 ShadowDebug(fbo, shadowMap); 499 } 478 if (shadowMap) 479 FirstPassShadow(fbo, shadowMap); 500 480 else 501 { 502 if (shadowMap) 503 FirstPassShadow(fbo, shadowMap); 504 else 505 FirstPass(fbo); 506 507 switch (mShadingMethod) 508 { 509 case SSAO: 510 ComputeSsao(fbo, tempCohFactor, oldProjViewMatrix, projViewMatrix); 511 CombineSsao(fbo); 512 break; 513 case GI: 514 ComputeGlobIllum(fbo, tempCohFactor, oldProjViewMatrix); 515 CombineIllum(fbo); 516 break; 517 default: // DEFAULT 518 // do nothing: standard deferred shading 519 break; 520 } 521 522 AntiAliasing(fbo); 523 } 481 FirstPass(fbo); 482 483 switch (mShadingMethod) 484 { 485 case SSAO: 486 ComputeSsao(fbo, tempCohFactor, oldProjViewMatrix, projViewMatrix); 487 CombineSsao(fbo); 488 break; 489 case GI: 490 ComputeGlobIllum(fbo, tempCohFactor, oldProjViewMatrix); 491 CombineIllum(fbo); 492 break; 493 default: // DEFAULT 494 // do nothing: standard deferred shading 495 break; 496 } 497 498 AntiAliasing(fbo); 499 524 500 glEnable(GL_LIGHTING); 525 501 glDisable(GL_TEXTURE_2D); … … 1010 986 1011 987 cgGLSetParameter1f(sMaxDepthShadowParam, mScaleFactor); 1012 cgGLSetParameter1f(sSampleWidthParam, 1.0f / shadowMap->GetSize()); 988 //cgGLSetParameter1f(sSampleWidthParam, 10.0f / shadowMap->GetSize()); 989 cgGLSetParameter1f(sSampleWidthParam, 2.5f / shadowMap->GetSize()); 1013 990 1014 991 cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x); 992 993 cgGLSetTextureParameter(sNoiseTexShadowParam, noiseTex); 994 cgGLEnableTextureParameter(sNoiseTexShadowParam); 995 1015 996 1016 997 glColor3f(1.0f, 1.0f, 1.0f); … … 1032 1013 cgGLDisableTextureParameter(sShadowMapParam); 1033 1014 1015 cgGLDisableTextureParameter(sNoiseTexShadowParam); 1016 1034 1017 FrameBufferObject::Release(); 1035 1018 1036 1019 PrintGLerror("deferred shading + shadows"); 1037 }1038 1039 1040 void DeferredRenderer::ShadowDebug(FrameBufferObject *fbo, ShadowMap *shadowMap)1041 {1042 GLuint colorsTex = shadowMap->GetShadowColorTexture();1043 GLuint shadowTex = shadowMap->GetDepthTexture();1044 1045 1046 Matrix4x4 shadowMatrix;1047 shadowMap->GetTextureMatrix(shadowMatrix);1048 1049 //glMatrixMode(GL_PROJECTION);glLoadIdentity();1050 //glViewport(0, 0, shadowMap->GetSize(), shadowMap->GetSize());1051 1052 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);1053 1054 cgGLBindProgram(sCgShadowDebugProgram);1055 1056 cgGLSetTextureParameter(sShadowMapDebugParam, shadowTex);1057 cgGLSetTextureParameter(sColorsTexDebugParam, colorsTex);1058 1059 cgGLEnableTextureParameter(sShadowMapDebugParam);1060 1061 cgGLSetMatrixParameterfc(sShadowMatrixDebugParam, (const float *)shadowMatrix.x);1062 1063 glColor3f(1.0f, 1.0f, 1.0f);1064 1065 glBegin(GL_QUADS);1066 1067 float offs2 = 0.5f;1068 1069 glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);1070 glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);1071 glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);1072 glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);1073 1074 glEnd();1075 1076 cgGLDisableTextureParameter(sColorsTexDebugParam);1077 cgGLDisableTextureParameter(sShadowMapDebugParam);1078 1079 PrintGLerror("shadows debug");1080 1020 } 1081 1021 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
r2930 r2944 77 77 78 78 void AntiAliasing(FrameBufferObject *fbo); 79 80 79 /** Helper method that computes the view vectors in the corners of the current view frustum. 81 80 */ 82 81 void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br); 83 82 84 85 void ShadowDebug(FrameBufferObject *fbo, ShadowMap *shadowMap);86 83 87 84 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp
r2942 r2944 570 570 ret.z = v.x * M.x[0][2] + v.y * M.x[1][2] + v.z * M.x[2][2] + M.x[3][2]; 571 571 denom = v.x * M.x[0][3] + v.y * M.x[1][3] + v.z * M.x[2][3] + M.x[3][3]; 572 //denom = M.x[0][3] + M.x[1][3] + M.x[2][3] + M.x[3][3];573 572 574 573 if (denom != 1.0) … … 576 575 return ret; 577 576 } 577 578 579 Vector3 Matrix4x4::Transform(float &w, const Vector3 &v, float h) const 580 { 581 Vector3 ret; 582 float denom; 583 584 ret.x = v.x * x[0][0] + v.y * x[1][0] + v.z * x[2][0] + h * x[3][0]; 585 ret.y = v.x * x[0][1] + v.y * x[1][1] + v.z * x[2][1] + h * x[3][1]; 586 ret.z = v.x * x[0][2] + v.y * x[1][2] + v.z * x[2][2] + h * x[3][2]; 587 w = v.x * x[0][3] + v.y * x[1][3] + v.z * x[2][3] + h * x[3][3]; 588 589 return ret; 590 } 591 578 592 579 593 // Apply the rotation portion of a matrix to a vector. … … 754 768 nUp = Normalize(CrossProd(nRight, nDir)); 755 769 756 Matrix4x4 m(nRight, nUp, -nDir);757 758 m.x[3][0] = -DotProd(nRight, pos);759 m.x[3][1] = -DotProd(nUp, pos);760 m.x[3][2] = DotProd(nDir, pos);761 762 return m;763 }764 765 766 Matrix4x4 MyLookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up)767 {768 const Vector3 nDir = Normalize(dir);769 Vector3 nUp = Normalize(up);770 771 Vector3 nRight = Normalize(CrossProd(nDir, nUp));772 nUp = Normalize(CrossProd(nRight, nDir));773 774 770 Matrix4x4 m(nRight.x, nRight.y, nRight.z, 0, 775 771 nUp.x, nUp.y, nUp.z, 0, -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.h
r2942 r2944 52 52 53 53 float Det3x3() const; 54 55 /** multiply with homogen vector form, where the 4 coordinat is in h, 56 the resulting homogen coordinate returned in w 57 */ 58 Vector3 Transform(float &w, const Vector3 &v, float h) const; 54 59 55 60 … … 160 165 Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far); 161 166 Matrix4x4 LookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up); 162 Matrix4x4 MyLookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up);163 167 164 168 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Plane3.cpp
r2913 r2944 9 9 { 10 10 Vector3 v1 = a - b, v2 = c - b; 11 mNormal = Normalize(CrossProd(v2, v1)); 11 12 mNormal = Normalize(CrossProd(v2, v1)); 12 13 mD = -DotProd(b, mNormal); 13 14 } … … 17 18 mNormal(normal) 18 19 { 20 //mNormal = Normalize(normal) 19 21 mD = -DotProd(normal, point); 20 22 } … … 29 31 sy(a.mNormal.y, b.mNormal.y, c.mNormal.y), 30 32 sz(a.mNormal.z, b.mNormal.z, c.mNormal.z), 31 sd(a.mD, b.mD,c.mD);33 sd(a.mD, b.mD, c.mD); 32 34 33 35 Matrix4x4 md(a.mNormal, b.mNormal, c.mNormal), mx, my, mz; … … 42 44 return false; 43 45 44 result.SetValue(mx.Det3x3()/det, 45 my.Det3x3()/det, 46 mz.Det3x3()/det); 46 result.SetValue(mx.Det3x3() / det, my.Det3x3() / det, mz.Det3x3() / det); 47 47 48 48 return true; … … 107 107 108 108 109 void Plane3::Transform(const Matrix4x4 &m) 110 { 111 float w; 112 113 const Matrix4x4 invTranspose = Transpose(Invert(m)); 114 115 const Vector3 n = invTranspose.Transform(w, mNormal, -mD); 116 117 const float mag = Magnitude(n); 118 119 mNormal = n / mag; 120 // our d is -d of homogen equation 121 mD = -w / mag; 122 123 // find point on plane 124 /*const Vector3 pt = mNormal * -mD; 125 126 // transform point 127 const Vector3 newPt = m * pt; 128 // transform normal 129 const Vector3 newNormal = TransformNormal(m, newPt); 130 131 const float mag = Magnitude(newNormal); 132 133 return Plane3(newNormal / mag, newPt / mag);*/ 109 134 } 135 136 137 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Plane3.h
r2911 r2944 8 8 { 9 9 10 /** Class describing a 3D Plane.10 /** Class describing a plane in 3D space. 11 11 */ 12 12 class Plane3 … … 17 17 18 18 Plane3(const Vector3 &a, const Vector3 &b, const Vector3 &c); 19 19 /** Note: we assume that the normal vector is already normalized. 20 */ 20 21 Plane3(const Vector3 &normal, const Vector3 &point); 21 22 … … 66 67 return *(&mNormal.x + inx); 67 68 } 69 /** Transforms the plane with the matrix m. 70 */ 71 void Transform(const Matrix4x4 &m); 72 68 73 69 74 /////////// … … 82 87 83 88 89 //protected: 90 84 91 /////////////// 85 92 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.cpp
r2943 r2944 71 71 if (!newPolyhedron) 72 72 { 73 cerr << "polyhedron not valid or NULL!" << endl;73 //cerr << "polyhedron not valid or NULL!" << endl; 74 74 return NULL; 75 75 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
r2943 r2944 187 187 188 188 189 // z0 is the point that lies on the parallel plane to the near plane through e (A) 190 //and on the near plane of the C frustum (the plane z = bZmax) and on the line x = e.x 191 Vector3 ShadowMap::GetLightSpaceZ0(const Matrix4x4 &lightSpace, 192 const Vector3 &e, 193 const float maxZ, 194 const Vector3 &eyeDir) const 195 { 196 // to calculate the parallel plane to the near plane through e we 197 // calculate the plane A with the three points 198 Plane3 planeA(e, eyeDir); 199 200 planeA.Transform(lightSpace); 201 202 // get the parameters of A from the plane equation n dot d = 0 203 const float d = planeA.mD; 204 const Vector3 n = planeA.mNormal; 205 206 // transform to light space 207 const Vector3 e_ls = lightSpace * e; 208 209 Vector3 z0; 210 211 z0.x = e_ls.x; 212 z0.y = (d - n.z * maxZ - n.x * e_ls.x) / n.y; 213 z0.z = maxZ; 214 215 return z0; 216 //return V3(e_ls.x(),(d-n.z()*b_lsZmax-n.x()*e_ls.x())/n.y(),b_lsZmax); 217 } 218 219 220 float ShadowMap::ComputeNOpt(const Matrix4x4 &lightSpace, 221 const AxisAlignedBox3 &extremalPoints, 222 const VertexArray &body) const 223 { 224 const Vector3 nearPt = GetNearCameraPointE(body); 225 const Vector3 eyeDir = mCamera->GetDirection(); 226 227 Matrix4x4 eyeView; 228 mCamera->GetModelViewMatrix(eyeView); 229 230 const Matrix4x4 invLightSpace = Invert(lightSpace); 231 232 const Vector3 z0_ls = GetLightSpaceZ0(lightSpace, nearPt, extremalPoints.Max().z, eyeDir); 233 const Vector3 z1_ls = Vector3(z0_ls.x, z0_ls.y, extremalPoints.Min().z); 234 235 // transform back to world space 236 const Vector3 z0_ws = invLightSpace * z0_ls; 237 const Vector3 z1_ws = invLightSpace * z1_ls; 238 239 // transform to eye space 240 const Vector3 z0_es = eyeView * z0_ws; 241 const Vector3 z1_es = eyeView * z1_ws; 242 243 const float z0 = z0_es.z; 244 const float z1 = z1_es.z; 245 246 cout << "z0 ls: " << z0_ls << " z1 ls: " << z1_ls << endl; 247 cout << "z0: " << z0_es << " z1: " << z1_es << endl; 248 249 const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]); 250 251 const float n = d / (sqrt(z1 / z0) - 1.0f); 252 253 return n; 254 } 255 256 189 257 float ShadowMap::ComputeN(const AxisAlignedBox3 &extremalPoints) const 190 258 { 191 const float n = mCamera->GetNear();259 const float nearPlane = mCamera->GetNear(); 192 260 193 261 const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]); … … 196 264 const float sinGamma = sin(fabs(acos(dotProd))); 197 265 198 return (n + sqrt(n * (n + d * sinGamma))) / sinGamma; 266 // test for values close to zero 267 if (sinGamma < 1e-6f) return 1e6f; 268 269 return (nearPlane + sqrt(nearPlane * (nearPlane + d * sinGamma))) / sinGamma; 199 270 } 200 271 … … 205 276 ) 206 277 { 207 //return IdentityMatrix();208 209 278 AxisAlignedBox3 bounds_ls = GetExtremalPoints(lightSpace, body); 210 279 … … 213 282 //-- first find the free parameter values n, and P (the projection center), and the projection depth 214 283 215 const float n = ComputeN(bounds_ls); 216 217 //cout << "n: " << n << endl; 284 //const float n = ComputeN(bounds_ls); 285 const float n = ComputeNOpt(lightSpace, extremalPoints, body); 286 287 cout << "n: " << n << endl; 288 289 if (n >= 1e6f) // light direction nearly parallel to view => switch to uniform 290 return IdentityMatrix(); 218 291 219 292 const Vector3 nearPt = GetNearCameraPointE(body); … … 358 431 359 432 VertexArray frustumPoints; 360 //Vector3 lightDir = Vector3(0, 0, -1);361 433 IncludeLightVolume(*polyhedron, frustumPoints, mLight->GetDirection(), mSceneBox); 362 434 … … 392 464 //look(from position, into the direction of the projected direction, with unchanged up-vector) 393 465 //const Matrix4x4 frame = MyLookAt2(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y()); 394 const Matrix4x4 frame = MyLookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());466 const Matrix4x4 frame = LookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y()); 395 467 396 468 lightProj *= frame; … … 600 672 601 673 Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir); 602 Matrix4x4 lightView = MyLookAt(mShadowCam->GetPosition(), dir, upVec);674 Matrix4x4 lightView = LookAt(mShadowCam->GetPosition(), dir, upVec); 603 675 604 676 mShadowCam->mViewOrientation = lightView; … … 606 678 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 607 679 608 glPolygonOffset( 1.0f, 2000.0f);680 glPolygonOffset(5.0f, 100.0f); 609 681 glEnable(GL_POLYGON_OFFSET_FILL); 610 682 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
r2943 r2944 83 83 float ComputeN(const AxisAlignedBox3 &extremalPoints) const; 84 84 85 float ComputeNOpt(const Matrix4x4 &lightSpace, 86 const AxisAlignedBox3 &extremalPoints, 87 const VertexArray &body) const; 88 85 89 Vector3 GetNearCameraPointE(const VertexArray &pts) const; 86 90 … … 91 95 */ 92 96 void _Render(RenderTraverser *renderer); 97 98 Vector3 GetLightSpaceZ0(const Matrix4x4 &lightSpace, 99 const Vector3 &e, 100 const float maxZ, 101 const Vector3 &eyeDir) const; 102 103 93 104 94 105 ////////////// -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
r2943 r2944 450 450 sceneQuery = new SceneQuery(bvh->GetBox(), traverser); 451 451 452 //Vector3 lightDir = Normalize(Vector3(0 , 1, -1));452 //Vector3 lightDir = Normalize(Vector3(0.2, -0.8, -1)); 453 453 Vector3 lightDir = Normalize(-Vector3(0.8f, -1.0f, 0.7f)); 454 454 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
r2943 r2944 13 13 }; 14 14 15 #define NUM_SAMPLES 16 16 17 18 float2 myreflect(float2 pt, float2 n) 19 { 20 // distance to plane 21 float d = dot(n, pt); 22 // reflect around plane 23 float2 rpt = pt - d * 2.0f * n; 24 25 return rpt; 26 } 15 27 16 28 … … 73 85 } 74 86 87 88 float CalcShadowTerm(fragment IN, 89 uniform sampler2D shadowMap, 90 uniform float w, 91 uniform float2 lightSpacePos, 92 float depth, 93 uniform float2 samples[NUM_SAMPLES], 94 uniform sampler2D noiseTexture 95 ) 96 { 97 float s; 98 99 float total_d = 0.0; 100 //float total_d = tex2D(shadowMap, lightSpacePos).x; 101 102 for (int i = 0; i < NUM_SAMPLES; ++ i) 103 { 104 const float2 offset = samples[i]; 105 106 #if 1 107 //////////////////// 108 //-- add random noise: reflect around random normal vector (warning: slow!) 109 110 float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy; 111 const float2 offsetTransformed = myreflect(offset, mynoise); 112 #else 113 const float2 offsetTransformed = offset; 114 #endif 115 // weight with projected coordinate to reach similar kernel size for near and far 116 float2 texcoord = lightSpacePos + offsetTransformed * w; 117 118 float shadowDepth = tex2D(shadowMap, texcoord).x; 119 120 total_d += step(shadowDepth, depth); 121 //total_d += length(offsetTransformed); 122 } 123 124 total_d /= (float)NUM_SAMPLES; 125 126 return total_d; 127 } 128 129 130 #if 0 75 131 /** The mrt shader for standard rendering 76 132 */ … … 114 170 115 171 shadowDepth[1] = tex2D(shadowMap, lightSpacePos.xy + float2( w, w)).x; 116 shadowDepth[2] = tex2D(shadowMap, lightSpacePos.xy -float2( w, -w)).x;117 shadowDepth[3] = tex2D(shadowMap, lightSpacePos.xy -float2(-w, -w)).x;118 shadowDepth[4] = tex2D(shadowMap, lightSpacePos.xy -float2(-w, w)).x;119 120 shadowDepth[5] = tex2D(shadowMap, lightSpacePos.xy -float2( w, 0)).x;121 shadowDepth[6] = tex2D(shadowMap, lightSpacePos.xy -float2( 0, w)).x;122 shadowDepth[7] = tex2D(shadowMap, lightSpacePos.xy -float2(-w, 0)).x;123 shadowDepth[8] = tex2D(shadowMap, lightSpacePos.xy -float2( 0, -w)).x;172 shadowDepth[2] = tex2D(shadowMap, lightSpacePos.xy + float2( w, -w)).x; 173 shadowDepth[3] = tex2D(shadowMap, lightSpacePos.xy + float2(-w, -w)).x; 174 shadowDepth[4] = tex2D(shadowMap, lightSpacePos.xy + float2(-w, w)).x; 175 176 shadowDepth[5] = tex2D(shadowMap, lightSpacePos.xy + float2( w, 0)).x; 177 shadowDepth[6] = tex2D(shadowMap, lightSpacePos.xy + float2( 0, w)).x; 178 shadowDepth[7] = tex2D(shadowMap, lightSpacePos.xy + float2(-w, 0)).x; 179 shadowDepth[8] = tex2D(shadowMap, lightSpacePos.xy + float2( 0, -w)).x; 124 180 125 181 … … 156 212 return OUT; 157 213 } 158 159 160 /** The mrt shader for standard rendering 161 */ 162 pixel main_shadow_debug(fragment IN, 163 uniform sampler2D shadowMap, 164 uniform sampler2D colorTex, 165 uniform float4x4 shadowMatrix) 214 #else 215 216 pixel main_shadow(fragment IN, 217 uniform sampler2D colors, 218 uniform sampler2D positions, 219 uniform sampler2D normals, 220 uniform sampler2D shadowMap, 221 uniform float4x4 shadowMatrix, 222 uniform float maxDepth, 223 uniform float sampleWidth, 224 uniform sampler2D noiseTexture, 225 uniform float2 samples[NUM_SAMPLES] 226 ) 166 227 { 167 228 pixel OUT; 168 #if 0 169 // visualize depth 170 OUT.color = tex2Dlod(colorTex, float4(IN.texCoord.xy, 0, 0)); 171 #else 172 float4 col; 173 col = tex2Dlod(shadowMap, float4(IN.texCoord.xy, 0, 0)); 174 OUT.color = float4(col.z / col.w, 0, 0, 1); 229 230 float4 norm = tex2D(normals, IN.texCoord.xy); 231 float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0)); 232 float4 position = tex2D(positions, IN.texCoord.xy); 233 234 235 // an ambient color term 236 float amb = norm.w; 237 238 float3 normal = normalize(norm.xyz); 239 float4 col = shade(IN, color, position, normal, amb); 240 241 position *= maxDepth; 242 position.w = 1.0f; 243 244 float4 lightSpacePos = mul(shadowMatrix, position); 245 lightSpacePos /= lightSpacePos.w; 246 247 OUT.color = col; 248 249 float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture); 250 251 if (amb < 0.9f) // hack: prevent shadowing the sky 252 { 253 // base lighting 254 const float baseLighting = 0.3f; 255 OUT.color *= baseLighting + (1.0f - baseLighting) * (1.0f - shadowTerm); 256 } 257 258 // also write out depth component 259 OUT.color.w = color.w; 260 261 return OUT; 262 } 263 175 264 #endif 176 return OUT;177 }
Note: See TracChangeset
for help on using the changeset viewer.