0

I'm having difficulties implementing shadow mapping for a directional light. I don't know how to setup the matrixes correctly... I want the shadows to appear from the upper top left (as cast by the sun). All I've got is the following code to generate the frustum corners in world space:

private def frustumBox(viewMatrix: Matrix4f, projectionMatrix: Matrix4f): BoundingBox = {
  val corners = new Array[Vector4f](8)
  corners(0) = new Vector4f(-1, -1, -1, 1)
  corners(1) = new Vector4f(-1,  1, -1, 1)
  corners(2) = new Vector4f( 1,  1, -1, 1)
  corners(3) = new Vector4f( 1, -1, -1, 1)
  corners(4) = new Vector4f(-1, -1,  1, 1)
  corners(5) = new Vector4f(-1,  1,  1, 1)
  corners(6) = new Vector4f( 1,  1,  1, 1)
  corners(7) = new Vector4f( 1, -1,  1, 1)

  val inverseVpMatrix = Matrix4f.mul(projectionMatrix, viewMatrix, null)
  inverseVpMatrix.invert

  for (corner <- corners) {
     Matrix4f.transform(inverseVpMatrix, corner, corner)
     val w = 1.0f / corner.w
     corner.x *= w
     corner.y *= w
     corner.z *= w
  }

  val min = new Vector3f(Float.MaxValue, Float.MaxValue, Float.MaxValue)
  val max = new Vector3f(Float.MinValue, Float.MinValue, Float.MinValue)

  for (corner <- corners) {
     if (corner.x < min.x)
        min.x = corner.x
     if (corner.y < min.y)
        min.y = corner.y
     if (corner.z < min.z)
        min.z = corner.z

     if (corner.x > max.x)
        max.x = corner.x
     if (corner.y > max.y)
        max.y = corner.y
     if (corner.z > max.z)
        max.z = corner.z
  }

  new BoundingBox(Vector3(min.x, min.y, min.z), Vector3(max.x, max.y, max.z))
}

How do I setup the view matrix for the sun?

OoS
  • 1
  • For directional light, a orthographic projection matrix would make most sense. – derhass Feb 23 '15 at 20:28
  • I've already solved this issue by using a fixed orthographic projection (I have a fixed camera rotation) and transforming the two points by the view matrix (without translation) and then translating the view matrix. – OoS Feb 24 '15 at 21:25

0 Answers0