Jump to content
  • Advertisement
Sign in to follow this  
Nairou

Trying to get deferred shading to look right

This topic is 2593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying to piece together how deferred shading works, and have been working on a prototype project to see if I can get it to work.

Today I completed both shader passes (geometry and lighting), and it appears to somewhat work, but the end result is far from correct. However, as I am still trying to learn how this works, I don't yet understand what I did wrong. I'm hoping someone can help identify the problem.

Here is the result I'm currently getting:

example2.jpg

I'm currently testing with a single light, positioned at the camera. It almost looks correct, but the lighting around the edge of the circle is weird, and it doesn't have a convincing spherical appearance.



This is the normal map I'm using (mapped onto a cube):

normal.jpg



And here are the FBO textures I'm rendering the geometry to (diffuse, normal, position), as rendered by gDEbugger:

example1.jpg


The normal and position textures are GL_FLOAT, and the numeric values stored appear correct as far as I can tell from within gDEbugger.


Here are my shaders:


Geometry vertex shader:


#version 150

uniform mat4 worldproj;
uniform mat4 world;
in vec4 position;
in vec3 normal;
in vec3 tangent;
in vec3 bitangent;
in vec2 texture;

out vec3 v2f_normal;
out vec3 v2f_tangent;
out vec3 v2f_bitangent;
out vec2 v2f_texture;
out vec3 v2f_position;

void main()
{
gl_Position = worldproj * position;
mat3 worldrot = mat3(world);
v2f_position = worldrot * position.xyz;
v2f_normal = worldrot * normal;
v2f_tangent = worldrot * tangent;
v2f_bitangent = worldrot * bitangent;
v2f_texture = texture;
}


Geometry fragment shader:


#version 150

in vec3 v2f_normal;
in vec3 v2f_tangent;
in vec3 v2f_bitangent;
in vec2 v2f_texture;
in vec3 v2f_position;
out vec4 outColor;
out vec4 outNormal;
out vec4 outPosition;
uniform sampler2D texture0;
uniform sampler2D texture1;

void main()
{
outColor = texture2D( texture0, v2f_texture );
vec3 normal = vec3( texture2D( texture1, v2f_texture ) ) * 2 - 1;
mat3 t;
t[0] = v2f_tangent;
t[1] = v2f_bitangent;
t[2] = v2f_normal;
normal = normalize( normal * t );
outNormal = vec4(normal, 1.0);
outPosition = vec4(v2f_position, 1.0);
}


Lighting vertex shader:


#version 150

in vec4 position;
in vec2 texture;

out vec2 v2f_texture;

void main()
{
gl_Position = position;
v2f_texture = texture;
}


Lighting fragment shader:


#version 150

in vec2 v2f_texture;
out vec4 outputcolor;
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform sampler2D texture2;

vec4 lightDirection = vec4( 0, 0, -100, 1 );

void main()
{
vec4 diffuse = texture2D( texture0, v2f_texture );
vec4 normal = texture2D( texture1, v2f_texture );
vec4 position = texture2D( texture2, v2f_texture );

vec4 light = normalize( lightDirection - position );
float brightness = clamp( dot( light, normal ), 0, 1 );

//outputcolor = vec4(brightness,brightness,brightness,1);
outputcolor = diffuse * brightness;
}


I'm hoping I just messed up a calculation somewhere. It is hard to find good examples of deferred shaders, so I'm having to do my best guess from what little I understand of the math involved. Any input would be great!

Share this post


Link to post
Share on other sites
Advertisement
Thanks! I went back and tried some different normal maps, and all of them did appear correct. I guess it is just the sphere that was a little too extreme to see properly.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!