GLSL normal matrix not working properly

950 views Asked by At

Straight to the point: when I use my model matrix to transform the vertices' normals, I get the right light results, but when I use the normal matrix I get the wrong results (i.e. rotated cubes are lit on the wrong faces).

Here's how I calculate the normal matrix (using jbullet):

modelMat.transpose();
modelMat.invert();

Here's my GLSL: Vertex shader:

#version 330

uniform mat4 ProjectionMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ModelMatrix;
uniform mat4 NormalMatrix;

layout(location = 0) in vec4 inPosition;
layout(location = 1) in vec4 inColor;
layout(location = 2) in vec3 inNormal;

out vec4 vColor;
out vec3 vNormal;
out vec3 vWorldPos;

void main() {
    gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * inPosition;
    vColor = inColor;
    vNormal = normalize((NormalMatrix * vec4(inNormal, 0.0))).xyz;
    vWorldPos = (ModelMatrix * inPosition).xyz;}

Fragment shader:

#version 330

uniform float AmbientIntensity; // Color
uniform vec3 DirectionalLight; // Normal
uniform vec3 DirectionalLightColor; // Color

uniform vec3 EyeWorldPos;
uniform float SpecularIntensity;
uniform float SpecularPower;

in vec4 vColor;
in vec3 vNormal;
in vec3 vWorldPos;

out vec4 pixel;

void main() {
    vec4 ambientColor = vec4(DirectionalLightColor * AmbientIntensity, 1.0);
    float diffuseFactor = dot(normalize(vNormal), -DirectionalLight);

    vec4 specularColor = vec4(0.0, 0.0, 0.0, 0.0);

    if (diffuseFactor > 0.0) {
        vec3 vertexToEye = normalize(EyeWorldPos - vWorldPos);
        vec3 lightReflect = normalize(reflect(DirectionalLight, vNormal));
        float specularFactor = pow(dot(vertexToEye, lightReflect), SpecularPower);

        if (specularFactor > 0)
            specularColor = vec4(DirectionalLightColor, 1.0f) * SpecularIntensity * specularFactor;
    }
    else
        diffuseFactor = max(0.0, diffuseFactor);


    vec4 diffuseColor = vec4(DirectionalLightColor * diffuseFactor, 1.0);

    pixel = vColor * (ambientColor + diffuseColor + specularColor);}
1

There are 1 answers

1
Benoît Dubreuil On BEST ANSWER

I found it! OpenGL uses column major matrices while jbullet uses row major matrices. That's what was giving wrong results.