[2891] | 1 | #include "ShadowMapping.h"
|
---|
| 2 | #include "FrameBufferObject.h"
|
---|
| 3 | #include "RenderState.h"
|
---|
| 4 | #include "RenderTraverser.h"
|
---|
| 5 | #include "Light.h"
|
---|
[2911] | 6 | #include "Polygon3.h"
|
---|
| 7 | #include "Polyhedron.h"
|
---|
[3038] | 8 | #include "ResourceManager.h"
|
---|
[2911] | 9 |
|
---|
[2891] | 10 | #include <IL/il.h>
|
---|
| 11 | #include <assert.h>
|
---|
| 12 |
|
---|
[2911] | 13 |
|
---|
[2891] | 14 | using namespace std;
|
---|
| 15 |
|
---|
| 16 |
|
---|
| 17 | namespace CHCDemoEngine
|
---|
| 18 | {
|
---|
| 19 |
|
---|
[2911] | 20 | static Polyhedron *polyhedron = NULL;
|
---|
[2929] | 21 | static Polyhedron *lightPoly = NULL;
|
---|
[2911] | 22 |
|
---|
| 23 |
|
---|
[2891] | 24 | static void PrintGLerror(char *msg)
|
---|
| 25 | {
|
---|
| 26 | GLenum errCode;
|
---|
| 27 | const GLubyte *errStr;
|
---|
| 28 |
|
---|
| 29 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
| 30 | {
|
---|
| 31 | errStr = gluErrorString(errCode);
|
---|
| 32 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 |
|
---|
[2936] | 37 | static Polyhedron *CreatePolyhedron(const Matrix4x4 &lightMatrix,
|
---|
| 38 | const AxisAlignedBox3 &sceneBox)
|
---|
[2931] | 39 | {
|
---|
| 40 | Frustum frustum(lightMatrix);
|
---|
[2891] | 41 |
|
---|
[2931] | 42 | vector<Plane3> clipPlanes;
|
---|
[2891] | 43 |
|
---|
[2931] | 44 | for (int i = 0; i < 6; ++ i)
|
---|
| 45 | {
|
---|
| 46 | ////////////
|
---|
| 47 | //-- normalize the coefficients
|
---|
[2891] | 48 |
|
---|
[2931] | 49 | // the clipping planes look outward the frustum,
|
---|
| 50 | // so distances > 0 mean that a point is outside
|
---|
| 51 | const float invLength = -1.0f / Magnitude(frustum.mClipPlanes[i].mNormal);
|
---|
| 52 |
|
---|
| 53 | frustum.mClipPlanes[i].mD *= invLength;
|
---|
| 54 | frustum.mClipPlanes[i].mNormal *= invLength;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[2939] | 57 | // first create near plane because of precision issues
|
---|
| 58 | clipPlanes.push_back(frustum.mClipPlanes[4]);
|
---|
| 59 |
|
---|
[2931] | 60 | clipPlanes.push_back(frustum.mClipPlanes[0]);
|
---|
| 61 | clipPlanes.push_back(frustum.mClipPlanes[1]);
|
---|
| 62 | clipPlanes.push_back(frustum.mClipPlanes[2]);
|
---|
| 63 | clipPlanes.push_back(frustum.mClipPlanes[3]);
|
---|
[2936] | 64 | clipPlanes.push_back(frustum.mClipPlanes[5]);
|
---|
| 65 |
|
---|
[2931] | 66 | return Polyhedron::CreatePolyhedron(clipPlanes, sceneBox);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 |
|
---|
[2891] | 70 | static void GrabDepthBuffer(float *data, GLuint depthTexture)
|
---|
| 71 | {
|
---|
| 72 | glEnable(GL_TEXTURE_2D);
|
---|
| 73 | glBindTexture(GL_TEXTURE_2D, depthTexture);
|
---|
| 74 |
|
---|
| 75 | glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_FLOAT, data);
|
---|
| 76 |
|
---|
| 77 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
| 78 | glDisable(GL_TEXTURE_2D);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 |
|
---|
| 82 | static void ExportDepthBuffer(float *data, int size)
|
---|
| 83 | {
|
---|
| 84 | ilInit();
|
---|
| 85 | assert(ilGetError() == IL_NO_ERROR);
|
---|
| 86 |
|
---|
| 87 | ILstring filename = ILstring("shadow.tga");
|
---|
| 88 | ilRegisterType(IL_FLOAT);
|
---|
| 89 |
|
---|
| 90 | const int depth = 1;
|
---|
| 91 | const int bpp = 1;
|
---|
| 92 |
|
---|
| 93 | if (!ilTexImage(size, size, depth, bpp, IL_LUMINANCE, IL_FLOAT, data))
|
---|
| 94 | {
|
---|
| 95 | cerr << "IL error " << ilGetError() << endl;
|
---|
| 96 |
|
---|
| 97 | ilShutDown();
|
---|
| 98 | assert(ilGetError() == IL_NO_ERROR);
|
---|
| 99 |
|
---|
| 100 | return;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[3219] | 103 | ilEnable(IL_FILE_OVERWRITE);
|
---|
[2891] | 104 | if (!ilSaveImage(filename))
|
---|
| 105 | {
|
---|
| 106 | cerr << "TGA write error " << ilGetError() << endl;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | ilShutDown();
|
---|
| 110 | assert(ilGetError() == IL_NO_ERROR);
|
---|
| 111 |
|
---|
| 112 | cout << "exported depth buffer" << endl;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 |
|
---|
| 116 |
|
---|
[2924] | 117 | static AxisAlignedBox3 GetExtremalPoints(const Matrix4x4 &m,
|
---|
| 118 | const VertexArray &pts)
|
---|
| 119 | {
|
---|
| 120 | AxisAlignedBox3 extremalPoints;
|
---|
| 121 | extremalPoints.Initialize();
|
---|
| 122 |
|
---|
| 123 | VertexArray::const_iterator it, it_end = pts.end();
|
---|
| 124 |
|
---|
| 125 | for (it = pts.begin(); it != it_end; ++ it)
|
---|
| 126 | {
|
---|
| 127 | Vector3 pt = *it;
|
---|
| 128 | pt = m * pt;
|
---|
| 129 |
|
---|
| 130 | extremalPoints.Include(pt);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | return extremalPoints;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
[3062] | 137 | ShadowMap::ShadowMap(DirectionalLight *light,
|
---|
| 138 | int size,
|
---|
[3011] | 139 | const AxisAlignedBox3 &sceneBox,
|
---|
[3062] | 140 | PerspectiveCamera *cam):
|
---|
[2897] | 141 | mSceneBox(sceneBox), mSize(size), mCamera(cam), mLight(light)
|
---|
[2891] | 142 | {
|
---|
| 143 | mFbo = new FrameBufferObject(size, size, FrameBufferObject::DEPTH_32, true);
|
---|
[2965] | 144 |
|
---|
[3067] | 145 | // need a color buffer to keep driver happy
|
---|
[3011] | 146 | mFbo->AddColorBuffer(ColorBufferObject::RGB_UBYTE,
|
---|
[2965] | 147 | ColorBufferObject::WRAP_CLAMP_TO_EDGE,
|
---|
| 148 | ColorBufferObject::FILTER_NEAREST);
|
---|
[2891] | 149 |
|
---|
[3062] | 150 | mShadowCam = new PerspectiveCamera(1);
|
---|
[2891] | 151 | }
|
---|
| 152 |
|
---|
| 153 |
|
---|
[2894] | 154 | ShadowMap::~ShadowMap()
|
---|
[2891] | 155 | {
|
---|
| 156 | DEL_PTR(mFbo);
|
---|
[2897] | 157 | DEL_PTR(mShadowCam);
|
---|
[3019] | 158 |
|
---|
| 159 | DEL_PTR(lightPoly);
|
---|
| 160 | DEL_PTR(polyhedron);
|
---|
[2891] | 161 | }
|
---|
| 162 |
|
---|
| 163 |
|
---|
[2963] | 164 | static void DrawPolyhedron(Polyhedron *poly, const Vector3 &color)
|
---|
[2891] | 165 | {
|
---|
[2929] | 166 | if (!poly) return;
|
---|
[2911] | 167 |
|
---|
[2929] | 168 | for (size_t i = 0; i < poly->NumPolygons(); ++ i)
|
---|
[2911] | 169 | {
|
---|
[2929] | 170 | glColor3f(color.x, color.y, color.z);
|
---|
[2911] | 171 |
|
---|
| 172 | glBegin(GL_LINE_LOOP);
|
---|
| 173 |
|
---|
[2929] | 174 | Polygon3 *p = poly->GetPolygons()[i];
|
---|
[2911] | 175 |
|
---|
[2929] | 176 | for (size_t j = 0; j < p->mVertices.size(); ++ j)
|
---|
[2911] | 177 | {
|
---|
[2929] | 178 | Vector3 v = p->mVertices[j];
|
---|
[2911] | 179 | glVertex3d(v.x, v.y, v.z);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | glEnd();
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 |
|
---|
[2963] | 187 | void ShadowMap::VisualizeFrustra()
|
---|
[2911] | 188 | {
|
---|
[2963] | 189 | DrawPolyhedron(lightPoly, Vector3(1, 0, 1));
|
---|
| 190 | DrawPolyhedron(polyhedron, Vector3(0, 1, 0));
|
---|
[2913] | 191 | }
|
---|
[2911] | 192 |
|
---|
[2913] | 193 |
|
---|
[2944] | 194 | // z0 is the point that lies on the parallel plane to the near plane through e (A)
|
---|
| 195 | //and on the near plane of the C frustum (the plane z = bZmax) and on the line x = e.x
|
---|
| 196 | Vector3 ShadowMap::GetLightSpaceZ0(const Matrix4x4 &lightSpace,
|
---|
| 197 | const Vector3 &e,
|
---|
| 198 | const float maxZ,
|
---|
| 199 | const Vector3 &eyeDir) const
|
---|
| 200 | {
|
---|
| 201 | // to calculate the parallel plane to the near plane through e we
|
---|
| 202 | // calculate the plane A with the three points
|
---|
| 203 | Plane3 planeA(e, eyeDir);
|
---|
| 204 |
|
---|
| 205 | planeA.Transform(lightSpace);
|
---|
| 206 |
|
---|
| 207 | // get the parameters of A from the plane equation n dot d = 0
|
---|
| 208 | const float d = planeA.mD;
|
---|
| 209 | const Vector3 n = planeA.mNormal;
|
---|
| 210 |
|
---|
| 211 | // transform to light space
|
---|
| 212 | const Vector3 e_ls = lightSpace * e;
|
---|
| 213 |
|
---|
| 214 | Vector3 z0;
|
---|
| 215 |
|
---|
| 216 | z0.x = e_ls.x;
|
---|
| 217 | z0.y = (d - n.z * maxZ - n.x * e_ls.x) / n.y;
|
---|
| 218 | z0.z = maxZ;
|
---|
| 219 |
|
---|
| 220 | return z0;
|
---|
| 221 | //return V3(e_ls.x(),(d-n.z()*b_lsZmax-n.x()*e_ls.x())/n.y(),b_lsZmax);
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 |
|
---|
| 225 | float ShadowMap::ComputeNOpt(const Matrix4x4 &lightSpace,
|
---|
| 226 | const AxisAlignedBox3 &extremalPoints,
|
---|
| 227 | const VertexArray &body) const
|
---|
| 228 | {
|
---|
| 229 | const Vector3 nearPt = GetNearCameraPointE(body);
|
---|
| 230 | const Vector3 eyeDir = mCamera->GetDirection();
|
---|
| 231 |
|
---|
| 232 | Matrix4x4 eyeView;
|
---|
| 233 | mCamera->GetModelViewMatrix(eyeView);
|
---|
| 234 |
|
---|
| 235 | const Matrix4x4 invLightSpace = Invert(lightSpace);
|
---|
| 236 |
|
---|
| 237 | const Vector3 z0_ls = GetLightSpaceZ0(lightSpace, nearPt, extremalPoints.Max().z, eyeDir);
|
---|
| 238 | const Vector3 z1_ls = Vector3(z0_ls.x, z0_ls.y, extremalPoints.Min().z);
|
---|
| 239 |
|
---|
| 240 | // transform back to world space
|
---|
| 241 | const Vector3 z0_ws = invLightSpace * z0_ls;
|
---|
| 242 | const Vector3 z1_ws = invLightSpace * z1_ls;
|
---|
| 243 |
|
---|
| 244 | // transform to eye space
|
---|
| 245 | const Vector3 z0_es = eyeView * z0_ws;
|
---|
| 246 | const Vector3 z1_es = eyeView * z1_ws;
|
---|
| 247 |
|
---|
| 248 | const float z0 = z0_es.z;
|
---|
| 249 | const float z1 = z1_es.z;
|
---|
| 250 |
|
---|
| 251 | cout << "z0 ls: " << z0_ls << " z1 ls: " << z1_ls << endl;
|
---|
| 252 | cout << "z0: " << z0_es << " z1: " << z1_es << endl;
|
---|
| 253 |
|
---|
| 254 | const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]);
|
---|
| 255 |
|
---|
| 256 | const float n = d / (sqrt(z1 / z0) - 1.0f);
|
---|
| 257 |
|
---|
| 258 | return n;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 |
|
---|
[2920] | 262 | float ShadowMap::ComputeN(const AxisAlignedBox3 &extremalPoints) const
|
---|
[2913] | 263 | {
|
---|
[2944] | 264 | const float nearPlane = mCamera->GetNear();
|
---|
[2916] | 265 |
|
---|
[2920] | 266 | const float d = fabs(extremalPoints.Max()[2] - extremalPoints.Min()[2]);
|
---|
| 267 |
|
---|
[2916] | 268 | const float dotProd = DotProd(mCamera->GetDirection(), mShadowCam->GetDirection());
|
---|
| 269 | const float sinGamma = sin(fabs(acos(dotProd)));
|
---|
| 270 |
|
---|
[2944] | 271 | // test for values close to zero
|
---|
| 272 | if (sinGamma < 1e-6f) return 1e6f;
|
---|
| 273 |
|
---|
[2954] | 274 | const float scale = 2.0f;
|
---|
| 275 | return scale * (nearPlane + sqrt(nearPlane * (nearPlane + d * sinGamma))) / sinGamma;
|
---|
[2916] | 276 | }
|
---|
| 277 |
|
---|
| 278 |
|
---|
[2924] | 279 | Matrix4x4 ShadowMap::CalcLispSMTransform(const Matrix4x4 &lightSpace,
|
---|
[2920] | 280 | const AxisAlignedBox3 &extremalPoints,
|
---|
[2924] | 281 | const VertexArray &body
|
---|
[2920] | 282 | )
|
---|
[2917] | 283 | {
|
---|
[2928] | 284 | AxisAlignedBox3 bounds_ls = GetExtremalPoints(lightSpace, body);
|
---|
| 285 |
|
---|
[2913] | 286 | ///////////////
|
---|
[2915] | 287 | //-- We apply the lispsm algorithm in order to calculate an optimal light projection matrix
|
---|
[2924] | 288 | //-- first find the free parameter values n, and P (the projection center), and the projection depth
|
---|
| 289 |
|
---|
[2945] | 290 | const float n = ComputeN(bounds_ls);
|
---|
| 291 | //const float n = ComputeNOpt(lightSpace, extremalPoints, body); cout << "n: " << n << endl;
|
---|
[2924] | 292 |
|
---|
[2944] | 293 | if (n >= 1e6f) // light direction nearly parallel to view => switch to uniform
|
---|
| 294 | return IdentityMatrix();
|
---|
| 295 |
|
---|
[2924] | 296 | const Vector3 nearPt = GetNearCameraPointE(body);
|
---|
[2933] | 297 |
|
---|
[2915] | 298 | //get the coordinates of the near camera point in light space
|
---|
[2924] | 299 | const Vector3 lsNear = lightSpace * nearPt;
|
---|
[2915] | 300 |
|
---|
[2939] | 301 | // the start point has the x and y coordinate of e, the z coord of the near plane of the light volume
|
---|
[2924] | 302 | const Vector3 startPt = Vector3(lsNear.x, lsNear.y, bounds_ls.Max().z);
|
---|
[2943] | 303 |
|
---|
[2915] | 304 | // the new projection center
|
---|
[2936] | 305 | const Vector3 projCenter = startPt + Vector3::UNIT_Z() * n;
|
---|
[2915] | 306 |
|
---|
| 307 | //construct a translation that moves to the projection center
|
---|
| 308 | const Matrix4x4 projectionCenter = TranslationMatrix(-projCenter);
|
---|
| 309 |
|
---|
[2916] | 310 | // light space y size
|
---|
[2933] | 311 | const float d = fabs(bounds_ls.Max()[2] - bounds_ls.Min()[2]);
|
---|
[2922] | 312 |
|
---|
[2924] | 313 | const float dy = fabs(bounds_ls.Max()[1] - bounds_ls.Min()[1]);
|
---|
| 314 | const float dx = fabs(bounds_ls.Max()[0] - bounds_ls.Min()[0]);
|
---|
[2922] | 315 |
|
---|
[2924] | 316 |
|
---|
[2915] | 317 |
|
---|
[2928] | 318 | //////////
|
---|
[2924] | 319 | //-- now apply these values to construct the perspective lispsm matrix
|
---|
| 320 |
|
---|
| 321 | Matrix4x4 matLispSM;
|
---|
| 322 |
|
---|
[2939] | 323 | matLispSM = GetFrustum(-1.0, 1.0, -1.0, 1.0, n, n + d);
|
---|
[2924] | 324 |
|
---|
| 325 | // translate to the projection center
|
---|
[2920] | 326 | matLispSM = projectionCenter * matLispSM;
|
---|
[2915] | 327 |
|
---|
| 328 | // transform into OpenGL right handed system
|
---|
[2920] | 329 | Matrix4x4 refl = ScaleMatrix(1.0f, 1.0f, -1.0f);
|
---|
| 330 | matLispSM *= refl;
|
---|
[2924] | 331 |
|
---|
[2920] | 332 | return matLispSM;
|
---|
[2915] | 333 | }
|
---|
| 334 |
|
---|
[2925] | 335 | #if 0
|
---|
[3063] | 336 |
|
---|
[2925] | 337 | Vector3 ShadowMap::GetNearCameraPointE(const VertexArray &pts) const
|
---|
| 338 | {
|
---|
| 339 | float maxDist = -1e25f;
|
---|
| 340 | Vector3 nearest = Vector3::ZERO();
|
---|
[2915] | 341 |
|
---|
[2925] | 342 | Matrix4x4 eyeView;
|
---|
| 343 | mCamera->GetModelViewMatrix(eyeView);
|
---|
| 344 |
|
---|
| 345 | VertexArray newPts;
|
---|
| 346 | polyhedron->CollectVertices(newPts);
|
---|
| 347 |
|
---|
| 348 | //the LVS volume is always in front of the camera
|
---|
| 349 | VertexArray::const_iterator it, it_end = pts.end();
|
---|
| 350 |
|
---|
| 351 | for (it = pts.begin(); it != it_end; ++ it)
|
---|
| 352 | {
|
---|
| 353 | Vector3 pt = *it;
|
---|
| 354 | Vector3 ptE = eyeView * pt;
|
---|
[2943] | 355 |
|
---|
[2925] | 356 | if (ptE.z > 0) cerr <<"should not happen " << ptE.z << endl;
|
---|
| 357 | else
|
---|
| 358 | if (ptE.z > maxDist)
|
---|
| 359 | {
|
---|
| 360 | cout << " d " << ptE.z;
|
---|
| 361 |
|
---|
| 362 | maxDist = ptE.z;
|
---|
| 363 | nearest = pt;
|
---|
| 364 | }
|
---|
| 365 | }
|
---|
| 366 |
|
---|
[2933] | 367 | // return Invert(eyeView) * nearest;
|
---|
[2925] | 368 | return nearest;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | #else
|
---|
| 372 |
|
---|
[2920] | 373 | Vector3 ShadowMap::GetNearCameraPointE(const VertexArray &pts) const
|
---|
[2919] | 374 | {
|
---|
[2925] | 375 | VertexArray newPts;
|
---|
| 376 | polyhedron->CollectVertices(newPts);
|
---|
| 377 |
|
---|
[2922] | 378 | Vector3 nearest = Vector3::ZERO();
|
---|
| 379 | float minDist = 1e25f;
|
---|
[2920] | 380 |
|
---|
[2924] | 381 | const Vector3 camPos = mCamera->GetPosition();
|
---|
[2920] | 382 |
|
---|
[2925] | 383 | VertexArray::const_iterator it, it_end = newPts.end();
|
---|
[2920] | 384 |
|
---|
[2925] | 385 | for (it = newPts.begin(); it != it_end; ++ it)
|
---|
[2920] | 386 | {
|
---|
| 387 | Vector3 pt = *it;
|
---|
| 388 |
|
---|
| 389 | const float dist = SqrDistance(pt, camPos);
|
---|
| 390 |
|
---|
| 391 | if (dist < minDist)
|
---|
| 392 | {
|
---|
| 393 | minDist = dist;
|
---|
| 394 | nearest = pt;
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | return nearest;
|
---|
| 399 | }
|
---|
| 400 |
|
---|
[2925] | 401 | #endif
|
---|
[2920] | 402 |
|
---|
[2933] | 403 | Vector3 ShadowMap::GetProjViewDir(const Matrix4x4 &lightSpace,
|
---|
| 404 | const VertexArray &pts) const
|
---|
[2920] | 405 | {
|
---|
[2919] | 406 | //get the point in the LVS volume that is nearest to the camera
|
---|
[2920] | 407 | const Vector3 e = GetNearCameraPointE(pts);
|
---|
| 408 |
|
---|
[2919] | 409 | //construct edge to transform into light-space
|
---|
[2920] | 410 | const Vector3 b = e + mCamera->GetDirection();
|
---|
[2919] | 411 | //transform to light-space
|
---|
[2920] | 412 | const Vector3 e_lp = lightSpace * e;
|
---|
| 413 | const Vector3 b_lp = lightSpace * b;
|
---|
| 414 |
|
---|
| 415 | Vector3 projDir(b_lp - e_lp);
|
---|
| 416 |
|
---|
[2919] | 417 | //project the view direction into the shadow map plane
|
---|
[2932] | 418 | projDir.y = .0f;
|
---|
[2920] | 419 |
|
---|
| 420 | return Normalize(projDir);
|
---|
[2919] | 421 | }
|
---|
| 422 |
|
---|
| 423 |
|
---|
[2915] | 424 | bool ShadowMap::CalcLightProjection(Matrix4x4 &lightProj)
|
---|
| 425 | {
|
---|
| 426 | ///////////////////
|
---|
| 427 | //-- First step: calc frustum clipped by scene box
|
---|
| 428 |
|
---|
[2922] | 429 | DEL_PTR(polyhedron);
|
---|
[2915] | 430 | polyhedron = CalcClippedFrustum(mSceneBox);
|
---|
| 431 |
|
---|
| 432 | if (!polyhedron) return false; // something is wrong
|
---|
| 433 |
|
---|
| 434 | // include the part of the light volume that "sees" the frustum
|
---|
| 435 | // we only require frustum vertices
|
---|
| 436 |
|
---|
| 437 | VertexArray frustumPoints;
|
---|
[2939] | 438 | IncludeLightVolume(*polyhedron, frustumPoints, mLight->GetDirection(), mSceneBox);
|
---|
[2915] | 439 |
|
---|
| 440 |
|
---|
| 441 | ///////////////
|
---|
| 442 | //-- transform points from world view to light view and calculate extremal points
|
---|
| 443 |
|
---|
| 444 | Matrix4x4 lightView;
|
---|
| 445 | mShadowCam->GetModelViewMatrix(lightView);
|
---|
| 446 |
|
---|
[2920] | 447 | const AxisAlignedBox3 extremalPoints = GetExtremalPoints(lightView, frustumPoints);
|
---|
[2913] | 448 |
|
---|
[2920] | 449 | // we use directional lights, so the projection can be set to identity
|
---|
| 450 | lightProj = IdentityMatrix();
|
---|
[2913] | 451 |
|
---|
[2922] | 452 | // switch coordinate system to that used in the lispsm algorithm for calculations
|
---|
| 453 | Matrix4x4 transform2LispSM = ZeroMatrix();
|
---|
| 454 |
|
---|
| 455 | transform2LispSM.x[0][0] = 1.0f;
|
---|
[2934] | 456 | transform2LispSM.x[1][2] = -1.0f; // y => -z
|
---|
[2924] | 457 | transform2LispSM.x[2][1] = 1.0f; // z => y
|
---|
[2922] | 458 | transform2LispSM.x[3][3] = 1.0f;
|
---|
| 459 |
|
---|
[2936] | 460 |
|
---|
[2922] | 461 | //switch to the lightspace used in the article
|
---|
[2936] | 462 | lightProj *= transform2LispSM;
|
---|
[2922] | 463 |
|
---|
[2924] | 464 | const Vector3 projViewDir = GetProjViewDir(lightView * lightProj, frustumPoints);
|
---|
[2916] | 465 |
|
---|
[2952] | 466 | //do DirectionalLight Space Perspective shadow mapping
|
---|
[2922] | 467 | //rotate the lightspace so that the projected light view always points upwards
|
---|
| 468 | //calculate a frame matrix that uses the projViewDir[lightspace] as up vector
|
---|
[2917] | 469 | //look(from position, into the direction of the projected direction, with unchanged up-vector)
|
---|
[2938] | 470 | //const Matrix4x4 frame = MyLookAt2(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());
|
---|
[2944] | 471 | const Matrix4x4 frame = LookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());
|
---|
[2917] | 472 |
|
---|
[2936] | 473 | lightProj *= frame;
|
---|
[2924] | 474 |
|
---|
[2917] | 475 | const Matrix4x4 matLispSM =
|
---|
[2920] | 476 | CalcLispSMTransform(lightView * lightProj, extremalPoints, frustumPoints);
|
---|
[2917] | 477 |
|
---|
[2936] | 478 | lightProj *= matLispSM;
|
---|
[2917] | 479 |
|
---|
[2922] | 480 | // change back to GL coordinate system
|
---|
| 481 | Matrix4x4 transformToGL = ZeroMatrix();
|
---|
| 482 |
|
---|
[2940] | 483 | transformToGL.x[0][0] = 1.0f;
|
---|
| 484 | transformToGL.x[1][2] = 1.0f; // z => y
|
---|
[2934] | 485 | transformToGL.x[2][1] = -1.0f; // y => -z
|
---|
[2940] | 486 | transformToGL.x[3][3] = 1.0f;
|
---|
[2922] | 487 |
|
---|
[2936] | 488 | lightProj *= transformToGL;
|
---|
[2922] | 489 |
|
---|
[2920] | 490 | AxisAlignedBox3 lightPts = GetExtremalPoints(lightView * lightProj, frustumPoints);
|
---|
| 491 |
|
---|
[2917] | 492 | // focus projection matrix on the extremal points => scale to unit cube
|
---|
[2922] | 493 | Matrix4x4 scaleTranslate = GetFittingProjectionMatrix(lightPts);
|
---|
[2917] | 494 |
|
---|
[2939] | 495 | lightProj = lightProj * scaleTranslate;
|
---|
| 496 |
|
---|
| 497 | Matrix4x4 mymat = lightView * lightProj;
|
---|
| 498 |
|
---|
| 499 | AxisAlignedBox3 lightPtsNew = GetExtremalPoints(mymat, frustumPoints);
|
---|
| 500 |
|
---|
[2920] | 501 | // we have to flip the signs in order to tranform to opengl right handed system
|
---|
| 502 | Matrix4x4 refl = ScaleMatrix(1, 1, -1);
|
---|
| 503 | lightProj *= refl;
|
---|
[2916] | 504 |
|
---|
[2913] | 505 | return true;
|
---|
[2911] | 506 | }
|
---|
| 507 |
|
---|
| 508 |
|
---|
[2913] | 509 | Polyhedron *ShadowMap::CalcClippedFrustum(const AxisAlignedBox3 &box) const
|
---|
| 510 | {
|
---|
[2931] | 511 | Polyhedron *p = mCamera->ComputeFrustum();
|
---|
| 512 |
|
---|
| 513 | Polyhedron *clippedPolyhedron = box.CalcIntersection(*p);
|
---|
| 514 | DEL_PTR(p);
|
---|
[2913] | 515 |
|
---|
[2931] | 516 | return clippedPolyhedron;
|
---|
| 517 | }
|
---|
[2913] | 518 |
|
---|
| 519 |
|
---|
[2931] | 520 | //calculates the up vector for the light coordinate frame
|
---|
| 521 | static Vector3 CalcUpVec(const Vector3 viewDir, const Vector3 lightDir)
|
---|
| 522 | {
|
---|
| 523 | //we do what gluLookAt does...
|
---|
| 524 | //left is the normalized vector perpendicular to lightDir and viewDir
|
---|
| 525 | //this means left is the normalvector of the yz-plane from the paper
|
---|
| 526 | Vector3 left = CrossProd(lightDir, viewDir);
|
---|
[2913] | 527 |
|
---|
[2931] | 528 | //we now can calculate the rotated(in the yz-plane) viewDir vector
|
---|
| 529 | //and use it as up vector in further transformations
|
---|
| 530 | Vector3 up = CrossProd(left, lightDir);
|
---|
[2913] | 531 |
|
---|
[2931] | 532 | return Normalize(up);
|
---|
[2913] | 533 | }
|
---|
| 534 |
|
---|
| 535 |
|
---|
[2932] | 536 | void ShadowMap::GetTextureMatrix(Matrix4x4 &m) const
|
---|
| 537 | {
|
---|
| 538 | m = mTextureMatrix;
|
---|
| 539 | }
|
---|
| 540 |
|
---|
| 541 |
|
---|
| 542 | unsigned int ShadowMap::GetDepthTexture() const
|
---|
| 543 | {
|
---|
| 544 | return mFbo->GetDepthTex();
|
---|
| 545 | }
|
---|
| 546 |
|
---|
[3063] | 547 |
|
---|
[2932] | 548 | unsigned int ShadowMap::GetShadowColorTexture() const
|
---|
| 549 | {
|
---|
| 550 | return mFbo->GetColorBuffer(0)->GetTexture();
|
---|
| 551 |
|
---|
| 552 | }
|
---|
| 553 |
|
---|
| 554 |
|
---|
| 555 | void ShadowMap::IncludeLightVolume(const Polyhedron &polyhedron,
|
---|
| 556 | VertexArray &frustumPoints,
|
---|
| 557 | const Vector3 lightDir,
|
---|
| 558 | const AxisAlignedBox3 &sceneBox
|
---|
| 559 | )
|
---|
| 560 | {
|
---|
| 561 | // we don't need closed form anymore => just store vertices
|
---|
| 562 | VertexArray vertices;
|
---|
| 563 | polyhedron.CollectVertices(vertices);
|
---|
| 564 |
|
---|
[3063] | 565 | // we 'look' at each point and intect rays with the scene bounding box
|
---|
[2932] | 566 | VertexArray::const_iterator it, it_end = vertices.end();
|
---|
| 567 |
|
---|
| 568 | for (it = vertices.begin(); it != it_end; ++ it)
|
---|
| 569 | {
|
---|
| 570 | Vector3 v = *it;
|
---|
| 571 |
|
---|
| 572 | frustumPoints.push_back(v);
|
---|
[2939] | 573 | // hack: start at point which is guaranteed to be outside of box
|
---|
[2932] | 574 | v -= Magnitude(mSceneBox.Diagonal()) * lightDir;
|
---|
| 575 |
|
---|
| 576 | SimpleRay ray(v, lightDir);
|
---|
| 577 |
|
---|
| 578 | float tNear, tFar;
|
---|
| 579 |
|
---|
| 580 | if (sceneBox.Intersects(ray, tNear, tFar))
|
---|
| 581 | {
|
---|
| 582 | Vector3 newpt = ray.Extrap(tNear);
|
---|
| 583 | frustumPoints.push_back(newpt);
|
---|
| 584 | }
|
---|
| 585 | }
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 |
|
---|
[3063] | 589 | void ShadowMap::ComputeShadowMap(RenderTraverser *renderer,
|
---|
| 590 | const Matrix4x4 &projView)
|
---|
[2911] | 591 | {
|
---|
[2891] | 592 | mFbo->Bind();
|
---|
[2892] | 593 |
|
---|
[2891] | 594 | glDrawBuffers(1, mrt);
|
---|
| 595 |
|
---|
| 596 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
| 597 | glViewport(0, 0, mSize, mSize);
|
---|
| 598 |
|
---|
[3068] | 599 | // turn off colors + lighting (should be handled by the render state)
|
---|
[2891] | 600 | glShadeModel(GL_FLAT);
|
---|
| 601 |
|
---|
| 602 |
|
---|
[2942] | 603 | /////////////
|
---|
| 604 | //-- render scene into shadow map
|
---|
[2894] | 605 |
|
---|
[2942] | 606 | _Render(renderer);
|
---|
[2911] | 607 |
|
---|
[2891] | 608 | glPopAttrib();
|
---|
[2943] | 609 | glShadeModel(GL_SMOOTH);
|
---|
[2928] | 610 |
|
---|
[2913] | 611 | #if 0
|
---|
[2911] | 612 | float *data = new float[mSize * mSize];
|
---|
[2891] | 613 |
|
---|
| 614 | GrabDepthBuffer(data, mFbo->GetDepthTex());
|
---|
| 615 | ExportDepthBuffer(data, mSize);
|
---|
| 616 |
|
---|
[2892] | 617 | delete [] data;
|
---|
[2911] | 618 |
|
---|
[2892] | 619 | PrintGLerror("shadow map");
|
---|
[2913] | 620 | #endif
|
---|
[2942] | 621 |
|
---|
[3063] | 622 |
|
---|
| 623 | //////////////
|
---|
| 624 | //-- compute texture matrix
|
---|
| 625 |
|
---|
| 626 | static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f,
|
---|
| 627 | 0.0f, 0.5f, 0.0f, 0.5f,
|
---|
| 628 | 0.0f, 0.0f, 0.5f, 0.5f,
|
---|
| 629 | 0.0f, 0.0f, 0.0f, 1.0f);
|
---|
| 630 |
|
---|
| 631 | mTextureMatrix = mLightProjView * biasMatrix;
|
---|
| 632 |
|
---|
[2891] | 633 | FrameBufferObject::Release();
|
---|
| 634 | }
|
---|
| 635 |
|
---|
| 636 |
|
---|
[3063] | 637 | void ShadowMap::RenderShadowView(RenderTraverser *renderer,
|
---|
| 638 | const Matrix4x4 &projView)
|
---|
[2931] | 639 | {
|
---|
[2942] | 640 | glEnable(GL_LIGHTING);
|
---|
[2943] | 641 |
|
---|
[2942] | 642 | _Render(renderer);
|
---|
| 643 |
|
---|
[2943] | 644 | /*glDisable(GL_LIGHTING);
|
---|
| 645 | glDisable(GL_DEPTH_TEST);
|
---|
| 646 |
|
---|
| 647 | Polyhedron *hpoly = CreatePolyhedron(projView, mSceneBox);
|
---|
| 648 | DrawPoly(hpoly, Vector3(1, 1, 1));
|
---|
| 649 | DEL_PTR(hpoly);
|
---|
| 650 |
|
---|
| 651 | glEnable(GL_LIGHTING);
|
---|
| 652 | glEnable(GL_DEPTH_TEST);*/
|
---|
| 653 |
|
---|
[2942] | 654 | glDisable(GL_POLYGON_OFFSET_FILL);
|
---|
| 655 | }
|
---|
| 656 |
|
---|
| 657 |
|
---|
| 658 | void ShadowMap::_Render(RenderTraverser *renderer)
|
---|
| 659 | {
|
---|
[2939] | 660 | const Vector3 dir = mLight->GetDirection();
|
---|
[2931] | 661 |
|
---|
| 662 | mShadowCam->SetDirection(dir);
|
---|
| 663 |
|
---|
| 664 | // set position so that we can see the whole scene
|
---|
| 665 | Vector3 pos = mSceneBox.Center();
|
---|
| 666 | pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.5f);
|
---|
| 667 |
|
---|
[2932] | 668 | mShadowCam->SetPosition(mCamera->GetPosition());
|
---|
| 669 |
|
---|
[3063] | 670 | const Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir);
|
---|
| 671 | const Matrix4x4 lightView = LookAt(mShadowCam->GetPosition(), dir, upVec);
|
---|
[2932] | 672 |
|
---|
| 673 | mShadowCam->mViewOrientation = lightView;
|
---|
| 674 |
|
---|
[2931] | 675 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 676 |
|
---|
[2944] | 677 | glPolygonOffset(5.0f, 100.0f);
|
---|
[2931] | 678 | glEnable(GL_POLYGON_OFFSET_FILL);
|
---|
[2932] | 679 |
|
---|
| 680 | Matrix4x4 lightProj;
|
---|
[2931] | 681 | CalcLightProjection(lightProj);
|
---|
| 682 |
|
---|
| 683 | mLightProjView = lightView * lightProj;
|
---|
[2932] | 684 |
|
---|
[2931] | 685 | DEL_PTR(lightPoly);
|
---|
| 686 | lightPoly = CreatePolyhedron(mLightProjView, mSceneBox);
|
---|
| 687 |
|
---|
[3063] | 688 | glMatrixMode(GL_PROJECTION);
|
---|
| 689 | glPushMatrix();
|
---|
| 690 |
|
---|
[2931] | 691 | glMatrixMode(GL_MODELVIEW);
|
---|
| 692 | glPushMatrix();
|
---|
[2951] | 693 |
|
---|
[3063] | 694 | // set projection matrix manually
|
---|
| 695 | mShadowCam->mProjection = lightProj;
|
---|
| 696 |
|
---|
| 697 | // load gl view projection
|
---|
| 698 | mShadowCam->SetupViewProjection();
|
---|
[2931] | 699 |
|
---|
[2942] | 700 |
|
---|
[2931] | 701 | /////////////
|
---|
| 702 | //-- render scene into shadow map
|
---|
| 703 |
|
---|
| 704 | renderer->RenderScene();
|
---|
| 705 |
|
---|
[2943] | 706 |
|
---|
[2931] | 707 | glMatrixMode(GL_PROJECTION);
|
---|
| 708 | glPopMatrix();
|
---|
| 709 |
|
---|
[2951] | 710 | glMatrixMode(GL_MODELVIEW);
|
---|
| 711 | glPopMatrix();
|
---|
| 712 |
|
---|
[2942] | 713 | glDisable(GL_POLYGON_OFFSET_FILL);
|
---|
[2931] | 714 | }
|
---|
[3062] | 715 |
|
---|
| 716 |
|
---|
[2891] | 717 | } // namespace
|
---|