Changeset 2920
- Timestamp:
- 09/09/08 11:25:53 (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/Matrix4x4.cpp
r2917 r2920 680 680 681 681 //output is initialized with the same result as glFrustum 682 Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far) 682 Matrix4x4 GetFrustum(float left, float right, 683 float bottom, float top, 684 float near, float far) 683 685 { 684 686 Matrix4x4 m; -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
r2919 r2920 166 166 167 167 168 float ShadowMap::ComputeN() const 169 { 170 const float n = mShadowCam->GetNear(); 171 const float f = mShadowCam->GetFar(); 172 173 const float d = fabs(f - n); 168 float ShadowMap::ComputeN(const AxisAlignedBox3 &extremalPoints) const 169 { 170 const float n = mCamera->GetNear(); 171 172 const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]); 174 173 175 174 const float dotProd = DotProd(mCamera->GetDirection(), mShadowCam->GetDirection()); … … 180 179 181 180 182 Matrix4x4 ShadowMap::CalcLispSMTransform(const Matrix4x4 &lightProjView, const AxisAlignedBox3 &extremalPoints) 183 { 184 Matrix4x4 lispMtx; 181 Matrix4x4 ShadowMap::CalcLispSMTransform(const Matrix4x4 &lightProjView, 182 const AxisAlignedBox3 &extremalPoints, 183 const VertexArray &pts 184 ) 185 { 186 Matrix4x4 matLispSM; 185 187 186 188 /////////////// 187 189 //-- We apply the lispsm algorithm in order to calculate an optimal light projection matrix 188 190 189 const float n = 1 e20f;//ComputeN();191 const float n = 10;//ComputeN(extremalPoints); 190 192 191 193 cout << "n: " << n << endl; 192 194 193 const Vector3 nearPt = mShadowCam->GetNear() * mShadowCam->GetDirection() + mShadowCam->GetPosition();195 const Vector3 nearPt = GetNearCameraPointE(pts); 194 196 195 197 //get the coordinates of the near camera point in light space … … 197 199 198 200 //c start has the x and y coordinate of e, the z coord of B.min() 199 const Vector3 startPt = Vector3 (lsNear.x, lsNear.y, extremalPoints.Min().y);201 const Vector3 startPt = Vector3::ZERO();//Vector3(lsNear.x, lsNear.y, extremalPoints.Max().z); 200 202 201 203 // the new projection center 202 const Vector3 unit_y = Vector3(0, 1, 0); 203 Vector3 projCenter = startPt - unit_y * n; 204 Vector3 projCenter = startPt + Vector3::UNIT_Z() * n; 204 205 205 206 //construct a translation that moves to the projection center … … 208 209 // light space y size 209 210 const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]); 210 211 lispMtx = GetFrustum(-1.0, 1.0, -1.0, 1.0, n, n + d); 212 213 cout << "lispsm\n" << lispMtx << endl; 214 215 lispMtx *= projectionCenter; 211 cout << "d: " << d << endl; 212 matLispSM = GetFrustum(-1.0, 1.0, -1.0, 1.0, n, n + d); 213 214 cout << "lispsm\n" << matLispSM << endl; 215 216 matLispSM = projectionCenter * matLispSM; 217 218 cout << "center\n" << projectionCenter << endl; 219 cout << "new\n" << matLispSM << endl; 216 220 217 221 // transform into OpenGL right handed system 218 Matrix4x4 scale = ScaleMatrix(1.0f, 1.0f, -1.0f); 219 220 lispMtx *= scale; 221 222 return IdentityMatrix(); 223 } 224 225 226 Vector3 ShadowMap::GetProjViewDir(const Matrix4x4 &lightSpace) const 222 Matrix4x4 refl = ScaleMatrix(1.0f, 1.0f, -1.0f); 223 matLispSM *= refl; 224 //matLispSM = IdentityMatrix(); 225 226 return matLispSM; 227 } 228 229 230 Vector3 ShadowMap::GetNearCameraPointE(const VertexArray &pts) const 231 { 232 Vector3 nearest; 233 float minDist = 1e20f; 234 235 Vector3 camPos = mCamera->GetPosition(); 236 237 /*Matrix4x4 inverseCamView; 238 mCamera->GetModelViewMatrix(invervseCamView); 239 inverseCamView.Invert();*/ 240 241 VertexArray::const_iterator it, it_end = pts.end(); 242 243 for (it = pts.begin(); it != it_end; ++ it) 244 { 245 Vector3 pt = *it; 246 247 const float dist = SqrDistance(pt, camPos); 248 249 if (dist < minDist) 250 { 251 minDist = dist; 252 nearest = pt; 253 } 254 } 255 256 return nearest; 257 } 258 259 260 Vector3 ShadowMap::GetProjViewDir(const Matrix4x4 &lightSpace, const VertexArray &pts) const 227 261 { 228 262 //get the point in the LVS volume that is nearest to the camera 229 const V3 e = getNearCameraPointE(fs); 263 const Vector3 e = GetNearCameraPointE(pts); 264 230 265 //construct edge to transform into light-space 231 const V 3 b = e+fs.getEyeDir();266 const Vector3 b = e + mCamera->GetDirection(); 232 267 //transform to light-space 233 const V3 e_lp = lightSpace.mulHomogenPoint(e); 234 const V3 b_lp = lightSpace.mulHomogenPoint(b); 235 V3 projDir(b_lp-e_lp); 268 const Vector3 e_lp = lightSpace * e; 269 const Vector3 b_lp = lightSpace * b; 270 271 Vector3 projDir(b_lp - e_lp); 272 236 273 //project the view direction into the shadow map plane 237 projDir.y() = 0.0; 238 return projDir; 274 projDir.y = 0.0; 275 //projDir.z = 0.0; 276 277 return Normalize(projDir); 278 } 279 280 281 static AxisAlignedBox3 GetExtremalPoints(const Matrix4x4 &lightView, 282 const VertexArray &pts) 283 { 284 AxisAlignedBox3 extremalPoints; 285 extremalPoints.Initialize(); 286 287 VertexArray::const_iterator it, it_end = pts.end(); 288 289 for (it = pts.begin(); it != it_end; ++ it) 290 { 291 Vector3 pt = *it; 292 pt = lightView * pt; 293 294 extremalPoints.Include(pt); 295 } 296 297 return extremalPoints; 239 298 } 240 299 … … 262 321 //-- transform points from world view to light view and calculate extremal points 263 322 264 AxisAlignedBox3 extremalPoints;265 extremalPoints.Initialize();266 267 323 Matrix4x4 lightView; 268 324 mShadowCam->GetModelViewMatrix(lightView); 269 325 270 VertexArray::const_iterator it, it_end = frustumPoints.end(); 271 272 for (it = frustumPoints.begin(); it != it_end; ++ it) 273 { 274 Vector3 pt = *it; 275 pt = lightView * pt; 276 277 extremalPoints.Include(pt); 278 } 279 280 lightProj = IdentityMatrix(); // we use directional lights 281 282 Matrix4x4 shadowView; 283 mShadowCam->GetModelViewMatrix(shadowView); 284 285 Vector3 projViewDir = 326 const AxisAlignedBox3 extremalPoints = GetExtremalPoints(lightView, frustumPoints); 327 328 // we use directional lights, so the projection can be set to identity 329 lightProj = IdentityMatrix(); 330 331 const Vector3 projViewDir = GetProjViewDir(lightProj, frustumPoints); 332 333 cout << "projViewDir: " << projViewDir << endl; 334 286 335 //do Light Space Perspective shadow mapping 287 336 //rotate the lightspace so that the proj light view always points upwards 288 337 //calculate a frame matrix that uses the projViewDir[light-space] as up vector 289 338 //look(from position, into the direction of the projected direction, with unchanged up-vector) 290 Matrix4x4 frame = LookAt(Vector3 (0), projViewDir, Vector3(0, 0, 1));291 lightProj *= frame;339 Matrix4x4 frame = LookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y()); 340 //lightProj = frame * lightProj; 292 341 293 342 const Matrix4x4 matLispSM = 294 CalcLispSMTransform(shadowView * lightProj, extremalPoints); 295 296 lightProj *= matLispSM; 343 CalcLispSMTransform(lightView * lightProj, extremalPoints, frustumPoints); 344 345 lightProj = matLispSM * lightProj; 346 347 AxisAlignedBox3 lightPts = GetExtremalPoints(lightView * lightProj, frustumPoints); 348 349 //cout << "max: " << lightPts.Max() << endl; 350 //cout << "min: " << lightPts.Min() << endl; 297 351 298 352 // focus projection matrix on the extremal points => scale to unit cube 299 lightProj *= GetFittingProjectionMatrix(extremalPoints); 300 301 // we have to flip the signs in order tp opengl style projection matrix 302 Matrix4x4 scale = ScaleMatrix(-1, -1, -1); 303 lightProj *= scale; 353 lightProj *= GetFittingProjectionMatrix(lightPts); 354 //lightProj *= GetFittingProjectionMatrix(extremalPoints); 355 356 cout << "max: " << lightProj * lightPts.Max() << endl; 357 cout << "min: " << lightProj * lightPts.Min() << endl; 358 359 // we have to flip the signs in order to tranform to opengl right handed system 360 Matrix4x4 refl = ScaleMatrix(1, 1, -1); 361 lightProj *= refl; 304 362 305 363 return true; -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
r2917 r2920 65 65 66 66 bool CalcLightProjection(Matrix4x4 &lightProj); 67 Matrix4x4 CalcLispSMTransform(const Matrix4x4 &lightProjView, const AxisAlignedBox3 &bounds); 67 68 Matrix4x4 CalcLispSMTransform(const Matrix4x4 &lightProjView, 69 const AxisAlignedBox3 &bounds, 70 const VertexArray &pts); 68 71 69 72 void IncludeLightVolume(const Polyhedron &polyhedron, … … 72 75 const AxisAlignedBox3 &sceneBox); 73 76 74 float ComputeN() const; 77 float ComputeN(const AxisAlignedBox3 &extremalPoints) const; 78 79 Vector3 GetNearCameraPointE(const VertexArray &pts) const; 80 81 Vector3 GetProjViewDir(const Matrix4x4 &lightSpace, const VertexArray &pts) const; 82 83 75 84 76 85 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Vector3.h
r2916 r2920 81 81 } 82 82 83 83 84 /** Returns the axis, where the std::vector has the largest value. 84 85 */ … … 142 143 Vector3& operator/= (float A); 143 144 145 static inline Vector3 UNIT_X() { return Vector3(1, 0, 0); } 146 static inline Vector3 UNIT_Y() { return Vector3(0, 1, 0); } 147 static inline Vector3 UNIT_Z() { return Vector3(0, 0, 1); } 148 static inline Vector3 ZERO() { return Vector3(0, 0, 0); } 149 144 150 145 151 //////////
Note: See TracChangeset
for help on using the changeset viewer.