Jump to content
  • Advertisement
Sign in to follow this  
Irlan Robson

OpenGL OpenGL Lighting Issue

This topic is 2074 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

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

Share this post


Link to post
Share on other sites
Advertisement

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

Edited by Buckeye

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!