Jump to content

  • Log In with Google      Sign In   
  • Create Account

GLSL lighting problem (normals)


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
5 replies to this topic

#1 assainator   Members   -  Reputation: 681

Like
0Likes
Like

Posted 21 November 2011 - 01:03 PM

Hello,

Last week I've run into a problem but can't find what's causing the problem.

The problem is as follows. I have a sphere and a light source. When the sphere isn't rotated, the lighting looks fine:
Posted Image

But as soon as I try to rotate it, the lighting get's all wrong. The picture below shows the sphere when it's rotated 90 degrees around the y-axis.
Posted Image


My first thought is that for some reason the normals aren't rotated at all or not rotated correctly.

I'm using GLSL and OpenGL 2.

I think shaders sources are useful:
vertex shader
// The diffuse value
varying float diffuse_value;

// The normalized light position
uniform vec3 NormalizedLightDirection;

void main()
{
    // Calculate the actual normal
    vec3 vertex_normal = normalize(gl_Normal * gl_NormalMatrix);

    // Calculate the diffuse value
    diffuse_value = max(dot(vertex_normal, NormalizedLightDirection), 0.0);

    // Set vertex position
    gl_Position = gl_ModelViewProjectionMatrix * (gl_Vertex);

    gl_FrontColor = gl_Color;
}


fragment shader:
varying float diffuse_value;

uniform vec4 DiffuseColor;

void main()
{
// Set the final color by multiplying the diffuse color with the diffuse value and then adding the ambient lighting
    gl_FragColor = vec4(1.0) * diffuse_value;
}


I appreciate any advice anybody can give me.
"What? It disintegrated. By definition, it cannot be fixed." - Gru - Dispicable me

"Dude, the world is only limited by your imagination" - Me


Sponsor:

#2 SaTANO   Members   -  Reputation: 407

Like
1Likes
Like

Posted 21 November 2011 - 02:25 PM

Correct this
vec3 vertex_normal = normalize(gl_NormalMatrix * gl_Normal );
OpenGL use column major matrices (same as your gl_Position calculation)

#3 Burnt_Fyr   Members   -  Reputation: 1243

Like
0Likes
Like

Posted 21 November 2011 - 02:54 PM

Hello,

Last week I've run into a problem but can't find what's causing the problem.

The problem is as follows. I have a sphere and a light source. When the sphere isn't rotated, the lighting looks fine:
Posted Image

But as soon as I try to rotate it, the lighting get's all wrong. The picture below shows the sphere when it's rotated 90 degrees around the y-axis.
Posted Image


My first thought is that for some reason the normals aren't rotated at all or not rotated correctly.

I'm using GLSL and OpenGL 2.

I think shaders sources are useful:
vertex shader

// The diffuse value
varying float diffuse_value;

// The normalized light position
uniform vec3 NormalizedLightDirection;

void main()
{
    // Calculate the actual normal
    vec3 vertex_normal = normalize(gl_Normal * gl_NormalMatrix);

    // Calculate the diffuse value
    diffuse_value = max(dot(vertex_normal, NormalizedLightDirection), 0.0);

    // Set vertex position
    gl_Position = gl_ModelViewProjectionMatrix * (gl_Vertex);

    gl_FrontColor = gl_Color;
}


fragment shader:
varying float diffuse_value;

uniform vec4 DiffuseColor;

void main()
{
// Set the final color by multiplying the diffuse color with the diffuse value and then adding the ambient lighting
    gl_FragColor = vec4(1.0) * diffuse_value;
}


I appreciate any advice anybody can give me.


Your Normalized light direction should be in the same space as the normal, and is not the direction the light is facing, but the direction from point to light. assuming that the gl_Normal matrix is the matrix translating from object space to world space, and that Normalized Light Direction is the vector that a directional light is pointing, use the inverse.

diffuse_value = max(dot(vertex_normal, -NormalizedLightDirection), 0.0);


if that doesn't work, check your gl_NormalMatrix to be sure that it is indeed what it should be. (identity for an object with no transforms, or a rotation matrix if you do have some)

#4 dpadam450   Members   -  Reputation: 928

Like
0Likes
Like

Posted 21 November 2011 - 04:48 PM

// Calculate the actual normal vec3 vertex_normal = normalize( gl_NormalMatrix* gl_Normal);
NormalizedLightDirection = normalize( gl_NormalMatrix* NormalizedLightDirection); // Calculate the diffuse value diffuse_value = max(dot(vertex_normal, NormalizedLightDirection), 0.0);

#5 assainator   Members   -  Reputation: 681

Like
0Likes
Like

Posted 22 November 2011 - 01:09 AM

Correct this

vec3 vertex_normal = normalize(gl_NormalMatrix * gl_Normal );
OpenGL use column major matrices (same as your gl_Position calculation)


That did the trick!

Thanks a lot for all the replies.
"What? It disintegrated. By definition, it cannot be fixed." - Gru - Dispicable me

"Dude, the world is only limited by your imagination" - Me


#6 Burnt_Fyr   Members   -  Reputation: 1243

Like
0Likes
Like

Posted 22 November 2011 - 08:55 AM

Correct this

vec3 vertex_normal = normalize(gl_NormalMatrix * gl_Normal );
OpenGL use column major matrices (same as your gl_Position calculation)


!FACEPALM




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