- Timestamp:
- 09/08/08 10:14:49 (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
r2913 r2915 676 676 677 677 m.x[0][0] = -2.0f / (box.Max()[0] - box.Min()[0]); 678 m.x[0][1] = .0f; 679 m.x[0][2] = .0f; 680 m.x[0][3] = .0f; 681 678 682 m.x[1][0] = .0f; 683 m.x[1][1] = -2.0f / (box.Max()[1] - box.Min()[1]); 684 m.x[1][2] = .0f; 685 m.x[1][3] = .0f; 686 679 687 m.x[2][0] = .0f; 688 m.x[2][1] = .0f; 689 m.x[2][2] = -2.0f / (box.Max()[2] - box.Min()[2]); 690 m.x[2][3] = .0f; 691 680 692 m.x[3][0] = (box.Max()[0] + box.Min()[0]) / (box.Max()[0] - box.Min()[0]); 681 693 m.x[3][1] = (box.Max()[1] + box.Min()[1]) / (box.Max()[1] - box.Min()[1]); 694 m.x[3][2] = (box.Max()[2] + box.Min()[2]) / (box.Max()[2] - box.Min()[2]); 695 m.x[3][3] = 1.0f; 696 697 return m; 698 } 699 700 701 //output is initialized with the same result as glFrustum 702 Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far) 703 { 704 Matrix4x4 m; 705 706 const float xDif = 1.0f / (right - left); 707 const float yDif = 1.0f / (top - bottom); 708 const float zDif = 1.0f / (near - far); 709 710 m.x[0][0] = 2.0f * near * xDif; 682 711 m.x[0][1] = .0f; 683 m.x[1][1] = -2.0f / (box.Max()[1] - box.Min()[1]);684 m.x[2][1] = .0f;685 m.x[3][1] = (box.Max()[1] + box.Min()[1]) / (box.Max()[1] - box.Min()[1]);686 687 712 m.x[0][2] = .0f; 713 m.x[0][3] = .0f; 714 715 m.x[1][0] = .0f; 716 m.x[1][1] = 2.0f * near * yDif; 688 717 m.x[1][2] = .0f; 689 m.x[2][2] = -2.0f / (box.Max()[2] - box.Min()[2]);690 m.x[3][2] = (box.Max()[2] + box.Min()[2]) / (box.Max()[2] - box.Min()[2]);691 692 m.x[0][3] = .0f;693 718 m.x[1][3] = .0f; 694 m.x[2][3] = .0f; 695 m.x[3][3] = 1.0f; 696 697 return m; 698 } 699 700 701 } 719 720 m.x[2][0] = (right + left) * xDif; 721 m.x[2][1] = (top + bottom)* yDif; 722 m.x[2][2] = (far + near) * zDif; 723 m.x[2][3] = -1.0f; 724 725 m.x[3][0] = .0f; 726 m.x[3][1] = .0f; 727 m.x[3][2] = 2.0f * near * far * zDif; 728 m.x[3][3] = .0f; 729 } 730 731 732 733 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.h
r2913 r2915 111 111 friend Matrix4x4 GetFittingProjectionMatrix(const AxisAlignedBox3 &box); 112 112 113 //output is initialized with the same result as glFrustum 114 friend Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far); 115 113 116 // Overloaded output operator. 114 117 friend std::ostream& operator<< (std::ostream &s, const Matrix4x4 &M); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
r2914 r2915 144 144 VertexArray::const_iterator it, it_end = vertices.end(); 145 145 146 //cout << "=================" << endl;147 148 146 for (it = vertices.begin(); it != it_end; ++ it) 149 147 { … … 155 153 v -= Magnitude(mSceneBox.Diagonal()) * lightDir; 156 154 157 //cout << "pt: " << v << endl;158 155 SimpleRay ray(v, lightDir); 159 156 … … 163 160 { 164 161 Vector3 newpt = ray.Extrap(tNear); 165 Vector3 newpt2 = ray.Extrap(tFar); 166 167 frustumPoints.push_back(newpt); 168 169 //if (newpt.z < 220) cout << "ipt: " << newpt << " " << *it << endl; 162 frustumPoints.push_back(newpt); 170 163 } 171 164 } … … 173 166 174 167 175 bool ShadowMap::CalcLightProjection(Matrix4x4 &lightProj) 176 { 177 DEL_PTR(polyhedron); 168 bool ShadowMap::CalcLightProjectionLispSM(Matrix4x4 &lightProj) 169 { 170 Matrix4x4 lispMtx = IdentityMatrix(); 171 172 const float dotProd = DotProd(mCamera->GetDirection(), mShadowCam->GetDirection()); 173 const float sinGamma = sqrt(1.0f - dotProd * dotProd); 178 174 179 175 … … 181 177 //-- First step: calc frustum clipped by scene box 182 178 179 DEL_PTR(polyhedron); 183 180 polyhedron = CalcClippedFrustum(mSceneBox); 184 181 … … 200 197 Matrix4x4 lightView; 201 198 mShadowCam->GetModelViewMatrix(lightView); 202 203 //Matrix4x4 inverseCamView;204 //mCamera->GetModelViewMatrix(inverseCamView);205 //Invert(inverseCamView);206 207 //Matrix4x4 m = inverseCamView * lightView;208 199 209 200 VertexArray::const_iterator it, it_end = frustumPoints.end(); … … 212 203 { 213 204 Vector3 pt = *it; 214 215 //pt = inverseCamView * pt;216 205 pt = lightView * pt; 217 //v = m * v;218 206 219 207 extremalPoints.Include(pt); 220 //cout << "pt: " << pt << endl; 208 } 209 210 211 /////////////// 212 //-- We apply the lispsm algorithm in order to calculate an optimal light projection matrix 213 214 // use lispsm formulas 215 const float fac = 1.0f / sinGamma; 216 217 const float z_n = fac * mCamera->GetNear(); 218 219 // light space y size 220 const float d = fabs(extremalPoints.Max()[1] - extremalPoints.Min()[1]); 221 222 const float z_f = z_n + d * sinGamma; 223 224 // this is the famous n parameter 225 const float n = (z_n + sqrt(z_f * z_n)) /sinGamma; 226 const float f = n + d; 227 228 const Vector3 nearPt = mShadowCam->GetNear() * mShadowCam->GetDirection() + mShadowCam->GetPosition(); 229 230 //get the coordinates of the near camera point in light space 231 const Vector3 lsNear = lightView * nearPt; 232 233 //c start has the x and y coordinate of e, the z coord of B.min() 234 const Vector3 startPt = Vector3(lsNear.x, lsNear.y, extremalPoints.Max().z); 235 236 // the new projection center 237 Vector3 projCenter = startPt - Vector3(0, 0, 1) * n; 238 239 //construct a translation that moves to the projection center 240 const Matrix4x4 projectionCenter = TranslationMatrix(-projCenter); 241 242 lightProj = IdentityMatrix(); 243 244 //one possibility for a simple perspective transformation matrix 245 //with the two parameters n(near) and f(far) in y direction 246 247 // a = (f+n)/(f-n); b = -2*f*n/(f-n); 248 // [ 1 0 0 0] 249 // [ 0 a 0 b] 250 // [ 0 0 1 0] 251 // [ 0 1 0 0] 252 253 lightProj.x[1][1] = (f + n) / (f - n); 254 lightProj.x[1][3] = 1.0f; 255 256 lightProj.x[3][1] = -2.0f * f * n / (f - n); 257 lightProj.x[3][3] = 0.0f; 258 259 cout << "here4\n" << lightProj << endl; 260 261 // transform into OpenGL right handed system 262 Matrix4x4 scale = ScaleMatrix(1.0f, 1.0f, -1.0f); 263 264 lightProj = projectionCenter * scale * lightProj; 265 266 Vector3 pmax = extremalPoints.Max(); 267 Vector3 pmin = extremalPoints.Min(); 268 269 cout << "min: " << lightProj * pmin << endl; 270 cout << "max: " << lightProj * pmax << endl; 271 272 273 return true; 274 } 275 276 277 bool ShadowMap::CalcLightProjection(Matrix4x4 &lightProj) 278 { 279 DEL_PTR(polyhedron); 280 281 282 /////////////////// 283 //-- First step: calc frustum clipped by scene box 284 285 polyhedron = CalcClippedFrustum(mSceneBox); 286 287 if (!polyhedron) return false; // something is wrong 288 289 // include the part of the light volume that "sees" the frustum 290 // we only require frustum vertices 291 292 VertexArray frustumPoints; 293 IncludeLightVolume(*polyhedron, frustumPoints, mShadowCam->GetDirection(), mSceneBox); 294 295 296 /////////////// 297 //-- transform points from world view to light view and calculate extremal points 298 299 AxisAlignedBox3 extremalPoints; 300 extremalPoints.Initialize(); 301 302 Matrix4x4 lightView; 303 mShadowCam->GetModelViewMatrix(lightView); 304 305 VertexArray::const_iterator it, it_end = frustumPoints.end(); 306 307 for (it = frustumPoints.begin(); it != it_end; ++ it) 308 { 309 Vector3 pt = *it; 310 pt = lightView * pt; 311 312 extremalPoints.Include(pt); 221 313 } 222 314 223 315 // focus projection matrix on the extremal points 224 316 lightProj = GetFittingProjectionMatrix(extremalPoints); 225 226 /*Vector3 pmax = extremalPoints.Max();227 Vector3 pmin = extremalPoints.Min();228 229 cout << "min: " << lightProj * pmin << endl;230 cout << "max: " << lightProj * pmax << endl;231 232 for (it = frustumPoints.begin(); it != it_end; ++ it)233 {234 Vector3 pt = *it;235 236 pt = lightView * pt;237 pt = lightProj * pt;238 239 cout << "pt: " << pt << endl;240 }*/241 317 242 318 return true; … … 283 359 284 360 Polyhedron *p = new Polyhedron(polygons); 285 //return p;286 287 361 Polyhedron *clippedPolyhedron = box.CalcIntersection(*p); 288 362 … … 348 422 mShadowCam->GetModelViewMatrix(lightView); 349 423 350 CalcLightProjection(lightProj); 424 //CalcLightProjection(lightProj); 425 CalcLightProjectionLispSM(lightProj); 351 426 352 427 glMatrixMode(GL_PROJECTION); … … 354 429 glLoadMatrixf((float *)lightProj.x); 355 430 356 //cout << "new:\n" << lightProj << endl;431 cout << "new:\n" << lightProj << endl; 357 432 358 433 glMatrixMode(GL_MODELVIEW); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
r2913 r2915 63 63 */ 64 64 Polyhedron *CalcClippedFrustum(const AxisAlignedBox3 &box) const; 65 66 65 66 bool CalcLightProjectionLispSM(Matrix4x4 &lightProj); 67 67 bool CalcLightProjection(Matrix4x4 &lightProj); 68 68
Note: See TracChangeset
for help on using the changeset viewer.