GLSL Normals not transforming propertly
        Posted  
        
            by 
                instancedName
            
        on Game Development
        
        See other posts from Game Development
        
            or by instancedName
        
        
        
        Published on 2014-06-07T18:41:16Z
        Indexed on 
            2014/06/07
            21:38 UTC
        
        
        Read the original article
        Hit count: 344
        
I've been stuck on this problem for two days. I've read many articles about transforming normals, but I'm just totaly stuck. I understand choping off W component for "turning off" translation, and doing inverse/traspose transformation for non-uniform scaling problem, but my bug seems to be from a different source.
So, I've imported a simple ball into OpenGL. Only transformation that I'm applying is rotation over time. But when my ball rotates, the illuminated part of the ball moves around just as it would if direction light direction was changing.
I just can't figure out what is the problem. Can anyone help me with this?
Here's the GLSL code:
Vertex Shader:
#version 440 core
uniform mat4 World, View, Projection;
layout(location = 0) in vec3 VertexPosition;
layout(location = 1) in vec3 VertexColor;
layout(location = 2) in vec3 VertexNormal;
out vec4 Color;
out vec3 Normal;
void main()
{
    Color = vec4(VertexColor, 1.0);
    vec4 n = World * vec4(VertexNormal, 0.0f);
    Normal = n.xyz;
    gl_Position = Projection * View * World * vec4(VertexPosition, 1.0);
}
Fragment Shader:
#version 440 core
uniform vec3 LightDirection = vec3(0.0, 0.0, -1.0);
uniform vec3 LightColor = vec3(1f);
in vec4 Color;
in vec3 Normal;
out vec4 FragColor;
void main()
{
    diffuse = max(0.0, dot(normalize(-LightDirection), normalize(Normal)));
    vec4 scatteredLight = vec4(LightColor * diffuse, 1.0f);
    FragColor = min(Color * scatteredLight, vec4(1.0));
}
        © Game Development or respective owner