定向光视图/投影矩阵

时间:2015-02-23 14:00:27

标签: opengl graphics shadow-mapping directional-light

我在为定向灯实现阴影贴图时遇到了困难。我不知道如何正确设置矩阵...我希望阴影从左上角出现(由太阳投射)。我所拥有的只是以下代码来生成世界空间中的平截头体角落:

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))
}

如何设置太阳的视图矩阵?

0 个答案:

没有答案