It’s still swimming :(, I guess I’m missing something?
To get the inverse view matrix I can just use the camera transform matrix correct?
“1. The Camera Transformation Matrix: The transformation that places the camera in the correct position and orientation in world space (this is the transformation that you would apply to a 3D model of the camera if you wanted to represent it in the scene).
2. The View Matrix: This matrix will transform vertices from world-space to view-space. This matrix is the inverse of the camera’s transformation matrix described above.” - http://www.3dgep.com/understanding-the-view-matrix/
The inverse projection matrix I’m getting from openGL after applying the camera’s projection and then inverting…
I’m calculating the light matrix like so(up is defaulted to +y):
Vector3 zaxis = (look.subtract(eye)).normalize();
Vector3 xaxis = (up.crossProduct(zaxis)).normalize();
Vector3 yaxis = zaxis.crossProduct(xaxis);
m_mat[0][0] = xaxis.m_x; m_mat[0][1] = xaxis.m_y; m_mat[0][2] = xaxis.m_z; m_mat[0][3] = -xaxis.dotProduct(eye);
m_mat[1][0] = yaxis.m_x; m_mat[1][1] = yaxis.m_y; m_mat[1][2] = yaxis.m_z; m_mat[1][3] = -yaxis.dotProduct(eye);
m_mat[2][0] = zaxis.m_x; m_mat[2][1] = zaxis.m_y; m_mat[2][2] = zaxis.m_z; m_mat[2][3] = -zaxis.dotProduct(eye);
m_mat[3][0] = 0.0f; m_mat[3][1] = 0.0f; m_mat[3][2] = 0.0f; m_mat[3][3] = 1.0f;
and then to find the bounds I should just be able to do the following for each point:
points[0] = new Vector3( 1.0f, 1.0f, 1.0f);
points[0] = light_matrix.multiply((inv_view.multiply((inv_proj.multiply(points[0])))));
- After finding the centerXYZ I am snapping like so (max_extent is a float member value, also I think this is where I screwed up):
float extent = Math.max(max_xyz.m_x - min_xyz.m_x, max_xyz.m_y - min_xyz.m_y) * 0.5f;
if(extent > max_extent) max_extent = extent;
float multiple = 2.0f * max_extent / SHADOW_MAP_SIZE;
center_xyz.m_x = Math.round(center_xyz.m_x / multiple) * multiple;
center_xyz.m_y = Math.round(center_xyz.m_y / multiple) * multiple;
center_xyz.m_z = Math.round(center_xyz.m_z / multiple) * multiple;
- and finally my light projection matrix:
gl.glViewport(0, 0, SHADOW_MAP_SIZE, SHADOW_MAP_SIZE);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(center_xyz.m_x - max_extent,
center_xyz.m_x + max_extent,
center_xyz.m_y - max_extent,
center_xyz.m_y + max_extent,
-max_xyz.m_z,
-min_xyz.m_z);
This also makes the shadow map area very small between 3 and 6 units wide as the max_extent it usually just larger than 1.59.