OpenGL Lighting Issue

Started by
10 comments, last by Irlan Robson 10 years, 2 months ago

In the above shader, perspectiveMatrix wasn't suppose to be at the location 0 ?

Nope. There is a reason why explicit layout qualifiers (ie, the layout = something you put before each attribute) exist, the OpenGL driver will give you the location it sees fit, it doesn't matters where you declare your uniform in the shader. It may not even give you a location at all if it sees the uniform isn't actually being used in the shader. Thats why you query the uniform location, because until that call you simply don't know where your uniforms are in the shader program.

In this case with attributes you can either set a default location (like you're doing), so you don't have to query the driver for the location, or leave the location unspecified, and query the location the driver gave it.

With uniforms you can't specify the shader location (at least not in OpenGL 3.3), so you have to query it for each shader program.

Ok.

I've just figure out the problem. The issue is that I was giving the view position to the light position. When viewing my cube, the normal that I was viewing was pointing to Z+ and the camera was pointing to Z-. It's normal to have a intensive solig light color toward my face when I'm the light and the normal is the mirror.

I'm going to post the sources here. Was modified to per fragment lighting (lighting computations inside the fragment shader).


#version 330


layout (location = 0) in vec3 position;
layout (location = 1) in vec2 tc;
layout (location = 2) in vec3 normal;


out VS_OUT
{
vec3 N;
vec3 L;
vec3 V;
} vs_out;


uniform mat4 perspectiveMatrix;
uniform mat4 viewMatrix;
uniform mat4 worldMatrix;


uniform vec3 lightPosition;


void main() 
{
  mat3 worldView = mat3(viewMatrix) * mat3(worldMatrix);
  vec3 P = worldView * position;
  
  vs_out.N = worldView * normal;
  vs_out.L = lightPosition - P; 
  vs_out.V = -P;
  
  gl_Position = (perspectiveMatrix * viewMatrix * worldMatrix) * vec4(position, 1.0f);
}

#version 330


in VS_OUT
{
vec3 N;
vec3 L;
vec3 V;
} fs_in;


out vec4 color;


uniform vec3 emission;
uniform vec3 ambient;
uniform vec3 diffuse;
uniform vec3 specular;
uniform float shininess;


void main(void)
{
    vec3 N = normalize(fs_in.N);
vec3 L = normalize(fs_in.L);
vec3 V = normalize(fs_in.V);


vec3 R = reflect(-L, N);


vec3 a = ambient;
vec3 d = diffuse * max(dot(N, L), 0.0f);
vec3 s = specular * pow(max(dot(R, V), 0.0f), shininess);


color = vec4(a + d + s, 1.0f);
}


Advertisement

In the above shader, perspectiveMatrix wasn't suppose to be at the location 0 ?

Nope. There is a reason why explicit layout qualifiers (ie, the layout = something you put before each attribute) exist, the OpenGL driver will give you the location it sees fit, it doesn't matters where you declare your uniform in the shader. It may not even give you a location at all if it sees the uniform isn't actually being used in the shader. Thats why you query the uniform location, because until that call you simply don't know where your uniforms are in the shader program.

In this case with attributes you can either set a default location (like you're doing), so you don't have to query the driver for the location, or leave the location unspecified, and query the location the driver gave it.

With uniforms you can't specify the shader location (at least not in OpenGL 3.3), so you have to query it for each shader program.

Ok.

I've just figure out the problem. The issue is that I was giving the view position to the light position. When viewing my cube, the normal that I was viewing was pointing to Z+ and the camera was pointing to Z-. It's normal to have a intensive solig light color toward my face when I'm the light and the normal is the mirror.

I'm going to post the sources here. Was modified to per fragment lighting (lighting computations inside the fragment shader).


#version 330


layout (location = 0) in vec3 position;
layout (location = 1) in vec2 tc;
layout (location = 2) in vec3 normal;


out VS_OUT
{
vec3 N;
vec3 L;
vec3 V;
} vs_out;


uniform mat4 perspectiveMatrix;
uniform mat4 viewMatrix;
uniform mat4 worldMatrix;


uniform vec3 lightPosition;


void main() 
{
  mat3 worldView = mat3(viewMatrix) * mat3(worldMatrix);
  vec3 P = worldView * position;
  
  vs_out.N = worldView * normal;
  vs_out.L = lightPosition - P; 
  vs_out.V = -P;
  
  gl_Position = (perspectiveMatrix * viewMatrix * worldMatrix) * vec4(position, 1.0f);
}

#version 330


in VS_OUT
{
vec3 N;
vec3 L;
vec3 V;
} fs_in;


out vec4 color;


uniform vec3 emission;
uniform vec3 ambient;
uniform vec3 diffuse;
uniform vec3 specular;
uniform float shininess;


void main(void)
{
    vec3 N = normalize(fs_in.N);
vec3 L = normalize(fs_in.L);
vec3 V = normalize(fs_in.V);


vec3 R = reflect(-L, N);


vec3 a = ambient;
vec3 d = diffuse * max(dot(N, L), 0.0f);
vec3 s = specular * pow(max(dot(R, V), 0.0f), shininess);


color = vec4(a + d + s, 1.0f);
}


This topic is closed to new replies.

Advertisement