- Timestamp:
- 09/15/08 00:07:01 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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
Note: See TracChangeset
for help on using the changeset viewer.