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