Texture Matrix for Cube Maps

Hey guys,
I tried to implement Shadow Mapping in my OpenGL Game. For directional light it worked perfectly, but now I need point lights. I have created 6 depth maps and combined them into a cube map. For directional light I needed a texture matrix for the shader. I calculated the texture matrix like that: Bias * LightProjection * LightView * CameraViewInvert. Now my question: Do I need a texture matrix for cube maps (point lights) too? And if yes, how I can calculate it? Because the point light has 6 light view and projection matrices.

Thanks!

Sure you need. First, you definitely have to transform your vectors using the CameraViewInvert and your LightView matrix (choose the negative Z face’s LightView matrix for the purpose), the tricky part will be applying the projection and bias. I cannot recall by hearth how to do it exactly, but if you do the math you should figure it out.

Btw, you shouldn’t use fixed function texturing, it’s 2013, use shaders :slight_smile:

Yes, I have shaders to do the light. But how I can use shaders instead of make the texture matrix?

If you use the same projection for all faces of the cube map (likely) you can get away with only 2 texture matrices: one for LightProjection and one for LightView*… – with just a little glue math in between to grab the correct depth to compare against for that cube map face. Here’s the trick:


vec4 position_ls = light_view_matrix * camera_view_matrix_inv * position_cs;
vec4 abs_position = abs(position_ls);
float fs_z = -max(abs_position.x, max(abs_position.y, abs_position.z));
vec4 clip = light_projection_matrix * vec4(0.0, 0.0, fs_z, 1.0);
float depth = (clip.z / clip.w) * 0.5 + 0.5;
vec4 result = shadowCube(shadow, vec4(position_ls.xyz, depth));

You can find this code in a complete working test program on this in this OpenGL.org forum thread post by deadc0de.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.