Jump to content
  • Advertisement
Sign in to follow this  
darrenc182

OpenGL Deferred Shading Problem

This topic is 3757 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 am implementing deferred shading using OpenGL and GLSlang. I'm able to create my G-Buffer and I can render the lighting to a full screen quad, but what I'm having trouble with is that my light moves whenever my camera moves and I don't know how to fix the problem. I have been wondering if I need to send the light and camera/eye in eye space as oppose to world space which is what I am sending it to my final shader as? I can post some source if anyone needs to see it to get a better idea of what I'm talking about. Thank you in advance for any and all help.

Share this post


Link to post
Share on other sites
Advertisement
OpenGL doesn't have any "world space". If you use glLight for the light position, the position will be transformed by the current modelview matrix, so that the position you get from gl_LightSource[...].position is in eye space.

If you're doing something else, I think you might have to describe what you do a bit better.

Share this post


Link to post
Share on other sites
Hey Lord Crc. I'm passing in my light position, colour and size as uniforms. I'm also passing my camera as a uniform. Do you want to see any of my shader source or my app source code?

Share this post


Link to post
Share on other sites
Quote:
Original post by Lord Crc
OpenGL doesn't have any "world space". If you use glLight for the light position, the position will be transformed by the current modelview matrix, so that the position you get from gl_LightSource[...].position is in eye space.

If you're doing something else, I think you might have to describe what you do a bit better.


How can you not have world space?

Share this post


Link to post
Share on other sites
Here are my vertex and fragment shaders for creating the G Buffer. I believe it is working fine, but I could be wrong.

[vertex shader]
varying vec3 tangent;
varying vec3 bitangent;
varying vec3 normal;
varying vec4 colour;
varying vec4 pos4;
varying vec3 sem;

uniform vec3 light;

void main(void)
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
pos4 = gl_ModelViewMatrix * gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;
tangent = gl_NormalMatrix * gl_MultiTexCoord1.xyz;
binormal = gl_NormalMatrix * gl_MultiTexCoord2.xyz;
normal = gl_NormalMatrix * gl_MultiTexCoord3.xyz;
colour = gl_Color;
sem = vec3(1.0); //gl_MultiTexCoord0.xyz;
}

[fragment shader]

varying vec3 normal;
varying vec4 colour;
varying vec3 L;
varying vec4 pos4;
varying vec3 sem;

void main(void)
{

// Construct the texture-space matrix and rotate the normal into eye space
vec3 bumpNormal = texture2D(normalMap, gl_TexCoord[0].xy).rgb;

bumpNormal = bumpNormal.x * tangent + bumpNormal.y * binormal + bumpNormal.z * normal;

bumpNormal = faceforward( bumpNormal, normalize(pos4.xyz), bumpNormal);

vec4 diffuse_buffer = vec4(colour.rgb*texture2D( baseTex, gl_TexCoord[0].xy ).rgb, sem.z);

gl_FragData[0] = vec4(pos4.xyz, sem.x);
gl_FragData[1] = vec4(bumpNormal * 0.5 + 0.5, sem.y);
gl_FragData[2] = diffuse_buffer; //Diffuse Colour Buffer
gl_FragData[3] = vec4(tangent.xyz, 1.0);

}





And here are my lighting pass shaders and I think the problem is in the fragment shader or the eye and or light data that I'm passing to it.

[vertex shader]
void main(void)
{
gl_Position = ftransform();

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord1;
gl_TexCoord[2] = gl_MultiTexCoord2;
}

[fragment shader]
uniform sampler2D Tex0;
uniform sampler2D Tex1;
uniform sampler2D Tex2;
uniform sampler2D Tex3;
uniform float Rl;
uniform vec4 Cl;
uniform float ambient;
uniform vec3 Light, Eye;

float myclamp(float x, float xmin, float xmax)
{
return (min(max(x, xmin), xmax));
}

void main(void)
{
vec4 colorMap, pos4, norm4, T, tan4, viewTanSpace, lightTanSpace, vVector, lVector;
vec3 bitan;

pos4 = texture2D(Tex0, gl_TexCoord[0].xy).rgba; // G Buffer 0, Position
norm4 = texture2D(Tex1, gl_TexCoord[0].xy).rgba; // G Buffer 1, Normal
T = texture2D(Tex2, gl_TexCoord[0].xy).rgba; // G Buffer 2, Diffuse

lVector = vec4(Light - pos4.xyz, 1.0);
vVector = -pos4;

// Distance to light
float Dl = length(lVector);

// Attenuation
float a = myclamp(1.0 - (Dl*Dl)/(Rl*Rl), 0.0, 1.0);

// Normalized vector to light
vec4 L = normalize(lVector);

// Normalized vector to eye
vec4 V = normalize(vVector);

// Gloss factor
float g = norm4.w;

// Reflection vector for specular
vec4 R = 2.0 * dot(L, norm4) * norm4 - L;

// Self-shadowing term
float s = myclamp(L.z * 8.0, 0.0, 1.0);

// Diffuse colour = L.N * light colour * attenuation * self-shadowing
vec4 Diffuse = dot(L, norm4) * Cl * a * s;

float diff = myclamp(dot(norm4, L), 0.0, ambient);

// Specular colour = (R.V)^g * attenuation * self-shadowing
float Specular = myclamp(pow(myclamp(dot(R, V), 0.0, norm4.w), 16.0), 0.0, 1.0) * norm4.w * a * s;

// Output final colour
gl_FragColor = (Diffuse+diff) * T + vec4(Specular, Specular, Specular, 1.0);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Matt AufderheideHow can you not have world space?


OpenGL does not have any world space. Whether I, you and everyone else have one is entirely up to us :)

Share this post


Link to post
Share on other sites
From your shader code, the position stored in the G-buffer is in view space. From what I can gather, you pass the light and camera positions in world space. Easiest is to pass the light positions in view space (in view space the camera position and direction is given).

Share this post


Link to post
Share on other sites
Quote:
Original post by Lord Crc
From your shader code, the position stored in the G-buffer is in view space. From what I can gather, you pass the light and camera positions in world space. Easiest is to pass the light positions in view space (in view space the camera position and direction is given).


How do I pass the light positions to view space? Do I just multiple the light position by the currect modelview matrix after calling the glLookAt() function?

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!