Jump to content

  • Log In with Google      Sign In   
  • Create Account


OpenGL Lighting Issue


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Irlan R.   Members   -  Reputation: 1405

Like
0Likes
Like

Posted 17 February 2014 - 10:37 AM

The issue is: the more distant the light is more the object is illuminated. Seems to be the normals or shaders.
Here is the shader's sources:
#version 330


layout(location = 0) in vec3 in_position;
layout(location = 1) in vec2 in_texCoord;
layout(location = 2) in vec3 in_normal;


out vec2 out_texCoord;
out vec4 out_color;


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

uniform vec3 viewPosition;
uniform vec3 globalAmbient;
uniform vec3 lightPosition;
uniform vec3 lightColor;

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

void main() 
{
  mat3 worldMatrix3 = mat3(worldMatrix);
  vec3 position = worldMatrix3 * in_position;
  vec3 surfaceNormal = normalize(transpose(inverse(worldMatrix3)) * in_normal);
  
  vec3 a = ambient * globalAmbient;
  vec3 d = diffuse * lightColor;
  vec3 s = specular * lightColor;
  
  vec3 lightDirection = normalize(lightPosition - position); 
  float cosIncidence = clamp(dot(surfaceNormal, lightDirection), 0.0f, 1.0f);
  
  d *= cosIncidence;

  if (cosIncidence > 0.0f)
  {
    vec3 viewDirection = normalize(viewPosition - position);
    vec3 halfDirection = normalize(lightDirection + viewDirection);

    float phongTherm = clamp(dot(surfaceNormal, halfDirection), 0.0f, 1.0f);
    phongTherm = pow(phongTherm, shininess);

    s *= phongTherm;
  }
  
  gl_Position = (perspectiveMatrix * viewMatrix * worldMatrix) * vec4(in_position, 1.0f);
  out_texCoord = in_texCoord;
  out_color = vec4(emission + a + d + s, 1.0f);
}
 
#version 330


in vec2 out_texCoord;
in vec4 out_color;


out vec4 ex_color;


void main()
{
    ex_color = out_color;
}

Edited by irlanrobson, 17 February 2014 - 11:33 AM.


Sponsor:

#2 Buckeye   Crossbones+   -  Reputation: 4084

Like
0Likes
Like

Posted 17 February 2014 - 10:51 AM

Wild guess, (I don't know what happens internally) but have you tried transpose( inverse( worldMat ) )?


Edited by Buckeye, 17 February 2014 - 10:51 AM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.


#3 Irlan R.   Members   -  Reputation: 1405

Like
0Likes
Like

Posted 17 February 2014 - 11:31 AM

Wild guess, (I don't know what happens internally) but have you tried transpose( inverse( worldMat ) )?

Yeah, nothing changes. I've already check the OpenGL internal code and everything is fine.

Just one thing that I've noticed, is that the

 

getUniformLocation() is returning the wrong location of the uniforms.

 

viewMatrixLocation 9
globalAmbientLocation 3
perspectiveMatrixLocation 6
viewPositionLocation 10
lightColorLocation 4
lightPositionLocation 5
 
In the above shader, perspectiveMatrix wasn't suppose to be at the location 0 ?
 
I've check all the attributes bindings, vbo, ibo bindings, shader states changes and is they are all syncronised.
I'm loading a OBJ Model. I've  already check each v, vt and vn and they all filled in a beauty sequence in my array.
I've marked the option in Blender to set -Z to the front direction (wich OpenGL uses).
 
Now something that I'm scared is the way I'm constructing the indices.
Since the OBJ Model doesn't give them all, I'm just using the indices of the vertices. Here is this part of the code:
 
 
std::vector<unsigned int> indices;
for (size_t i = 0; i < vertices.size(); ++i) 
{
indices.push_back(i);
}

And drawing them with GL_TRIANGLES.

 

I'm using GLM for the matrices.

 

 

The Camera's Update method:

 

perspectiveMatrix = glm::perspective(xFov, aspectRatio, zNear, zFar);
const glm::vec3 viewUp = glm::vec3(0.0f, 1.0f, 0.0f);
viewMatrix = glm::lookAt(viewPosition, viewTarget, viewUp);

Edited by irlanrobson, 17 February 2014 - 11:44 AM.


#4 DTR666   Members   -  Reputation: 182

Like
0Likes
Like

Posted 17 February 2014 - 11:41 AM

Hi,

 

I'm not sure there's any normalisation about the order of uniforms.

Anyway you could use something like that to force the location (GL 4.3 I think):

layout(location = 2) uniform mat4 WorldMatrix;

 

Otherwise you have to use glGetUniformLocation(), to retrieve the location of each uniforms.

 

See OpenGL wiki for the details:

http://www.opengl.org/wiki/Uniform_(GLSL)

 

And maybe you could use a graphic debugger to identify your issue ? AMD GPU Perfstudio, NVidia nSight, Intel GPA,...

It saves my life a lot ;)


Edited by DTR666, 17 February 2014 - 11:57 AM.


#5 Buckeye   Crossbones+   -  Reputation: 4084

Like
0Likes
Like

Posted 17 February 2014 - 11:46 AM

If you keep editing your original post without noting the changes (which is bad form), it's a bit difficult to help you. Better to post revised code snippets.


Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.


#6 Irlan R.   Members   -  Reputation: 1405

Like
0Likes
Like

Posted 17 February 2014 - 11:49 AM

Hi,

 

Maybe you could use a graphic debugger to identify your issue ? AMD GPU Perfstudio, NVidia nSight, Intel GPA,...

It saves my life a lot ;)

Ok.



#7 Irlan R.   Members   -  Reputation: 1405

Like
1Likes
Like

Posted 17 February 2014 - 11:51 AM

If you keep editing your original post without noting the changes (which is bad form), it's a bit difficult to help you. Better to post revised code snippets.

Ok. Edit no more.



#8 Buckeye   Crossbones+   -  Reputation: 4084

Like
0Likes
Like

Posted 17 February 2014 - 12:27 PM


vec3 lightDirection = normalize(lightPosition - position); // point light??

float cosIncidence = clamp(dot(surfaceNormal, lightDirection), 0.0f, 1.0f);
d *= cosIncidence;

Are you simulating a point light or directional light? If it's a point light, you don't have attenuation (which would be more common).

 

With that point light and no attenuation, depending on the normal, dot(normal, lightdirection) may increase with distance. If the lightposition is very close to the vertex, but away from the position normal, diffuse will decrease.


Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.


#9 Irlan R.   Members   -  Reputation: 1405

Like
0Likes
Like

Posted 17 February 2014 - 12:53 PM

Point Light. 

I've just figure out that the normals are correct (even with the cull back mode, it shows the mesh with the "wrong reflection", but is not culled at all).

I'm reading a OpenGL Superbible copy that I have here, and seems that my calculations are kind of... incorrect. If something changes, I will post here to help others.



#10 TheChubu   Crossbones+   -  Reputation: 3971

Like
0Likes
Like

Posted 17 February 2014 - 12:58 PM

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.


"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


#11 Irlan R.   Members   -  Reputation: 1405

Like
0Likes
Like

Posted 17 February 2014 - 05:34 PM

 

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);
}




#12 Irlan R.   Members   -  Reputation: 1405

Like
0Likes
Like

Posted 17 February 2014 - 05:43 PM

 

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);
}







Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS