[Solved] Shadow mapping

I’m having issues with my shadow map, here’s the result:

the top image is of the shadow produced(from the camera’s perspective), the bottom (red) is the color render from the light’s perspective(the depth texture is as expected)

Here’s the code, I think it has something to do with the matrix transforms, but I’m not sure.

main loop:

		camera.updateProjection(g_gl, g_glu, window_width, window_height);
		renderFromLightPosToTexture();
		camera.updateModelView(g_gl, true);
		renderShadow();
private void renderFromLightPosToTexture()
	{
		g_gl.glMatrixMode(GL2.GL_MODELVIEW);
		g_gl.glLoadIdentity();
		
		g_gl.glRotatef(light_dir.m_x, 1.0f, 0.0f, 0.0f);
		g_gl.glRotatef(light_dir.m_y, 0.0f, 1.0f, 0.0f);
		Vector3 translate = engine.m_light.getTransform().getTranslate();
		g_gl.glTranslatef(-translate.m_x, -translate.m_y, -translate.m_z);
		
		float[] matrix_array = new float[16];
		g_gl.glGetFloatv(GL2.GL_PROJECTION_MATRIX, matrix_array, 0);
		Matrix4 proj_matrix = new Matrix4();
		proj_matrix.setMatrix(matrix_array);

	    matrix_array = new float[16];
	    g_gl.glGetFloatv(GL2.GL_MODELVIEW_MATRIX, matrix_array, 0);
		Matrix4 modelview_matrix = new Matrix4();
		modelview_matrix.setMatrix(matrix_array);
		
		Matrix4 bias_matrix = new Matrix4().initBiasMatrix();
		bias_matrix.transpose();//make column major
		shadow_matrix = bias_matrix.multiply(proj_matrix.multiply(modelview_matrix));
		
		depth_fbo.bindFrameBuffer(g_gl);
		mesh.draw(g_gl, true, false, false, false, false, 0, "basic_anim", engine.m_light.getTransform().getTranslate(), camera.getPosition(), shadow_matrix);
		depth_fbo.unBindFrameBuffer(g_gl);
	}
	private void renderShadow()
	{
		shadow_fbo.bindFrameBuffer(g_gl);
		mesh.draw(g_gl, true, false, false, false, false, depth_fbo.getDepthTextureHandle(), "shadow", engine.m_light.getTransform().getTranslate(), camera.getPosition(), shadow_matrix);
		shadow_fbo.unBindFrameBuffer(g_gl);
	}

vertex shader:

#version 130
in vec3 vert_pos;

uniform mat4 modelview_matrix;
uniform mat4 projection_matrix;
uniform mat4 light_model_view_proj_matrix;

out vec4 light_vertex_pos;

void main(void)
{   
   gl_Position = projection_matrix * modelview_matrix * vec4(vert_pos.xyz, 1.0);
   
   light_vertex_pos = light_model_view_proj_matrix * vec4(vert_pos, 1.0);
}

frag shader:

#version 130
in vec4 light_vertex_pos;

uniform sampler2D shadow_map;

out vec4 out_color;

void main(void)
{
   vec4 shadow_coord;
   shadow_coord.x = light_vertex_pos.x / light_vertex_pos.w;
   shadow_coord.y = light_vertex_pos.y / light_vertex_pos.w;
   shadow_coord.z = light_vertex_pos.z / light_vertex_pos.w;
   shadow_coord.w = light_vertex_pos.w / light_vertex_pos.w;
   
   vec4 depth_val = texture2D(shadow_map, shadow_coord.xy);
   
   float shadow_val = 1.0;
   if(depth_val.z < shadow_coord.z)
      shadow_val = 0.5;
   
   out_color = vec4(shadow_val, shadow_val, shadow_val, 1.0);
}

Well, it looks like I have it right.

That’s roughly what I’d expect, given the resolution of the depth map. My hunch as to the reason it doesn’t work in my above post is because I’m scaling the model down? Here it’s at it’s normal size, roughly 100X bigger (even though it looks smaller).

Now the question I have is why can’t I scale the model down. Basically all I should have to do is this in my vertex shader is change this:

light_vertex_pos = light_model_view_proj_matrix * vec4(vert_pos, 1.0);

to this:

light_vertex_pos = light_model_view_proj_matrix * modelview_matrix * vec4(vert_pos, 1.0);

Correct?

The thing is when I do that it renders the entire model white, no shadow =(

OK got it,

It shouldn’t be this:


light_vertex_pos = light_model_view_proj_matrix * modelview_matrix * vec4(vert_pos, 1.0);

It should be:


light_vertex_pos = light_model_view_proj_matrix * model_to_world_matrix * vec4(vert_pos, 1.0);

duh…