GLSL正常矩阵无法正常工作

时间:2014-11-21 00:58:54

标签: opengl matrix glsl normals

直截了当:当我使用模型矩阵变换顶点的法线时,我得到了正确的光线效果,但是当我使用普通矩阵时,我得到了错误的结果(即旋转的立方体在错误的面上被点亮)。

以下是我如何计算普通矩阵(使用jbullet):

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

这是我的GLSL: 顶点着色器:

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

片段着色器:

#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 个答案:

答案 0 :(得分:0)

我找到了! OpenGL使用列主矩阵,而jbullet使用行主矩阵。这就是错误的结果。