Sign in to follow this  
BloodLust666

GLSL Diffuse lighting doesn't work

Recommended Posts

BloodLust666    104
I'm working with GLSL and for some reason my diffuse calculation keeps coming out to 0s... frag shader, everything is in eye space
uniform vec3 lightPosition; // a directional light
uniform vec4 lightAmbient;

// I'm getting this data from a texture
vec4 color;
vec3 normal;
vec3 vertex; // this is the interpolated vertex

vec3 E 		= normalize(- vertex.xyz);
vec3 L          = normalize(lightPosition);
vec4 Idiff      = color * max(dot(normal,L),0.0);
vec4 Iamb       = lightAmbient * color;

gl_FragColor =  Iamb + Idiff;



but for some reason my scene always shows ONLY the Iamb color only. I even tried substituting (normal,L) to (normal,vec3(0.0, 1.0, 0.0)) just to see what would happen and that seems to work, but that only gives the light direction in relation to the eye orientation. Every frame, I update lightPosition by multiplying the camera's viewMatrix by the light's position then sending THAT vector as the position in eye space. I know this works because on the CPU side, I have my light set at (0,1,0) and when I point the camera straight up, the new value is (0,0,-1) which is the light in eye space, so that's right. I has to be something in the shader [Edited by - BloodLust666 on April 28, 2010 12:41:45 AM]

Share this post


Link to post
Share on other sites
BloodLust666    104
ok, the diffuse is working now, turns out I had a misspelling when I was updating that uniform... BUT now it seems as though the diffuse is very exaggerated. When I look up, nothing is lit (except ambiance), but when I look down everything is lit, including walls that weren't lit when I looked up. Am I doing this correclty? I'm "pretty" sure everything is in eye space...

Backing up a bit, I'm doing deferred shading, on my first pass I have

Vertex shader

varying vec2 texCoord;
varying vec3 normal;
varying vec4 position;

void main (void)
{
texCoord = gl_MultiTexCoord0.xy;
normal = gl_NormalMatrix * gl_Normal;
position = gl_ModelViewMatrix * gl_Vertex;
gl_Position = ftransform();
}




Fragment Shader

varying vec2 texCoord;
varying vec3 normal;
varying vec4 position;

uniform sampler2D texture;

void main (void)
{
gl_FragData[0] = vec4(texture2D(texture, texCoord).rgb, 1.0);
gl_FragData[1] = vec4(normal,1.0);
gl_FragData[2] = vec4(position.xyz,gl_FrontMaterial.shininess);
}



As you can see at this step, the color and normals are in eye space.

Then in my second pass, I'm pass in each light and render as a quad

Vertex shader

varying vec2 texCoord;

void main (void)
{
texCoord = gl_MultiTexCoord0.xy;
gl_Position = ftransform();
}



Fragment shader

varying vec2 texCoord;

// geometry
uniform sampler2D textureDiffuse;
uniform sampler2D textureNormal;
uniform sampler2D texturePosition;

// light
uniform int lightType;
uniform vec3 lightPosition;
uniform vec4 lightAmbient;
uniform vec4 lightDiffuse;
uniform vec4 lightSpecular;

void main (void)
{
vec2 flipped = vec2(texCoord.x, 1.0 - texCoord.y);// render texture renders these upside down. This is a fix

vec4 Idiff;
vec4 Iamb;
vec4 Ispec = vec4(0.0, 0.0, 0.0, 0.0);

vec4 color = texture2D(textureDiffuse, flipped );
vec3 normal = normalize(texture2D( textureNormal, flipped ).xyz);
vec4 position = texture2D(texturePosition, flipped );
float shiny = position.a;
position.a = 1.0;

vec3 E = normalize(- position.xyz);
vec3 L;
vec3 H;
float NdotHV;

Iamb = lightAmbient * color;

if ( lightType == 1 ) // directional
{
L = normalize(lightPosition);
H = normalize(E - L);
NdotHV = max(dot(normal, H),0.0);
Idiff = color * max(dot(normal,L),0.0);
//Ispec = lightSpecular * color * pow(NdotHV,shiny);
}
else if ( lightType == 2 ) // point light (not done yet.. help?)
{
}

gl_FragColor = Idiff + Iamb + Ispec;
}

Share this post


Link to post
Share on other sites
BloodLust666    104
I'm pretty sure it's in eye space because before I pass lightPosition to the shader, I get the camera's ModelView Matrix and multiply it by the light's world position. If that's eye space, then yes it's in eye space as well.


lightPosition = light->GetPosition();
lightPosition.Rotate(m_camera.GetViewMatrix());
m_deferredPass2Shader.UpdateGlobalUniformData("lightPosition", cWUniformData(sWVector3(lightPosition)));

Share this post


Link to post
Share on other sites
Lord_Evil    680
If I'm not wrong, you're calculating a direction in this line:

H = normalize(E - L);

However, if L (lightPosition) is a direction this doesn't make sense.

Edit2: I guess your normals are normalized, aren't they?

Edit3: What's your light direction? Could it point towards the camera? In that case your objects would be lit from the back and you wouldn't see that light.

Edit: forget the following, never mind

I'd say it should just be

H = L;

i.e. the next line should be

NdotHV = max(dot(normal, L),0.0);


[Edited by - Lord_Evil on April 28, 2010 2:20:28 PM]

Share this post


Link to post
Share on other sites
BloodLust666    104
The specular reflection isn't the problem. My diffuse lighting isn't working correctly, but I imagine once I fix the diffuse problem, the specular issue should be fixed too.

Edit: to clearify. L is my light direction, H is the half vector between the eye and the light direction

Edit2: yes all my normals are normalized

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this