[SOLVED]Deferred Shading Problem

Started by Rekenaar, June 15, 2012, 18:52:32

Previous topic - Next topic

Rekenaar

Edit:
Kinda forgot about this thread, but I managed to figure it out :D
I can't remember exactly what the problem was, but I think it was my lighting calculations.

Also, if anyone wants a tutorial on how to get this all setup, just PM me and I'll post it.
Anyway, here is the new code (I removed self-illumination, specular reflections, attenuation and the lighting mask from this)

Per-model shader
[Vertex_Shader]
varying vec3 viewSpaceNormal, viewSpacePosition;

void main()
{
	gl_TexCoord[0] = gl_MultiTexCoord0;
	viewSpaceNormal = gl_NormalMatrix * gl_Normal;
	viewSpacePosition = gl_ModelViewMatrix * gl_Vertex;
	gl_Position = ftransform();
}
[Pixel_Shader]
varying vec3 viewSpaceNormal, viewSpacePosition;

uniform sampler2D diffuseTexture;

void main()
{
	vec3 diffuse = gl_FrontMaterial.diffuse * texture2D(diffuseTexture, gl_TexCoord[0]).rgb;
	vec3 normal = viewSpaceNormal.xyz;
	vec3 position = viewSpacePosition.xyz;
	
	gl_FragData[0] = vec4(diffuse, 1.0);
	gl_FragData[1] = vec4(normal, 1.0);
	gl_FragData[2] = vec4(position, 1.0);
}

And the deferred shader
[Vertex_Shader]
void main()
{
	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_Position = ftransform();
}
[Pixel_Shader]
uniform sampler2D screenDiffuse;
uniform sampler2D screenNormal;
uniform sampler2D screenPosition;

uniform vec3 globalAmbient;

uniform int lightCount;

void main()
{
    vec3 diffuse = texture2D(screenDiffuse, gl_TexCoord[0]).rgb;
	vec3 lighting = globalAmbient;
	
    vec3 normal = texture2D(screenNormal, gl_TexCoord[0]).rgb;
	vec3 position = texture2D(screenPosition, gl_TexCoord[0]).rgb;

	vec3 lightDirection;

	for(int i = 0; i < lightCount; i++) {
		lightDirection = gl_LightSource[i].position - position;
		
		lighting += gl_LightSource[i].diffuse * max(dot(normalize(lightDirection), normalize(normal)), 0.0);
	}
	gl_FragColor = vec4(diffuse * lighting, 1.0);
}

--
Daniel

Fool Running

You might try a more OpenGL-specific forum. I'm not sure many of us have done deferred shading (I know I haven't :-\).
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D