Deferred rendering shadow mapping shader

Hello,

I’ve followed some tutorials on deferred rendering and searched a lot, but I don’t get what’s going wrong here. In the tutorials it works this way, and when I try it, it doesn’t.

I’m getting the following error:


Unable to create fragment shader:
Fragment shader failed to compile with the following errors:
WARNING: 0:20: warning(#402) Implicit truncation of vector from size 4 to size 1. 
ERROR: 0:21: error(#247) Function return is not matching type
WARNING: 0:39: warning(#402) Implicit truncation of vector from size 4 to size 3. 
WARNING: 0:42: warning(#402) Implicit truncation of vector from size 4 to size 3. 
ERROR: error(#273) 1 compilation errors.  No code generated

The fragment shader I’m using with this test:


uniform sampler2D tDiffuse; 
uniform sampler2D tPosition;
uniform sampler2D tNormals;
uniform sampler2D tShadowMap;
uniform vec3 cameraPosition;
uniform mat4 worldToLightViewMatrix;
uniform mat4 lightViewToProjectionMatrix;
uniform mat4 worldToCameraViewMatrix;

float readShadowMap(vec3 eyeDir)
{
	mat4 cameraViewToWorldMatrix = inverse(worldToCameraViewMatrix);
	mat4 cameraViewToProjectedLightSpace = lightViewToProjectionMatrix * worldToLightViewMatrix * cameraViewToWorldMatrix;
	vec4 projectedEyeDir = cameraViewToProjectedLightSpace * vec4(eyeDir,1);
	projectedEyeDir = projectedEyeDir/projectedEyeDir.w;

	vec2 textureCoordinates = projectedEyeDir.xy * vec2(0.5,0.5) + vec2(0.5,0.5);

	const float bias = 0.0001;
	float depthValue = texture2D( tShadowMap, textureCoordinates ) - bias;
	return projectedEyeDir.z * 0.5 + 0.5 < depthValue;
}

void main( void )
{
	// Read the data from the textures
	vec4 image = texture2D( tDiffuse, gl_TexCoord[0].xy );
	vec4 position = texture2D( tPosition, gl_TexCoord[0].xy );
	vec4 normal = texture2D( tNormals, gl_TexCoord[0].xy );
	
	mat4 lightViewToWolrdMatrix = inverse(worldToLightViewMatrix);
	vec3 light = lightViewToWolrdMatrix[3].xyz;
	vec3 lightDir = light - position.xyz;
	
	normal = normalize(normal);
	lightDir = normalize(lightDir);
	
	vec3 eyeDir = position.xyz - cameraPosition;
	vec3 reflectedEyeVector = normalize(reflect(eyeDir, normal));
	
	float shadow = readShadowMap(eyeDir);
	float diffuseLight = max(dot(normal,lightDir),0) * shadow;
	float ambientLight = 0.1;

	gl_FragColor = (diffuseLight + ambientLight ) * image + pow(max(dot(lightDir,reflectedEyeVector),0.0), 100) * 1.5 * shadow;
}

The solution or a push in the right direction would be welcome. :wink:


ERROR: 0:21: error(#247) Function return is not matching type

Have you tried to return a float instead of a boolean ?


return projectedEyeDir.z * 0.5 + 0.5 < depthValue ? 1.0 : 0.0;

I haven’t tested it but it seems to be something like that.
Other warnings are few cast problems that you should solve too.

Must “normal” be a vec4? not a vec3? This would cause an error at 39 and 42 (as in the log)…

Thank you for your response, the errors are gone. Now I have to fix the most important thing: the shadows aren’t appearing. :wink:

Excuse me if I am assuming a bit too much, but it sounds like you don’t have too much experience with shaders. I suggest that you take a step back and learn the basics a little better, maybe do some regular shadow mapping shaders or a few simple post processing effects to get a hang of the language.
Again I could be completely mistaken, if that’s the case excuse me.

Other than that: Oh goody deferred rendering :smiley:

You are right, I don’t have much experience with shaders. I am working with OpenGL and shaders for about 3 weeks now. :smiley:

In that case deferred rendering is most likely a bit over your head :slight_smile:
Go ahead and check out davedes tutorials here: http://www.java-gaming.org/index.php?topic=28048.0
As well as shadertoy.com. Most of the shaders on there right now are pretty useless, however they are nice looking and give you an idea of just how powerful shaders can be. The site is nice for just testing out a quick shader if you want to. Just do some research you can find just about anything you want for shaders online. Good luck!