如何计算法线矩阵? [英] How to calculate the normal matrix?

查看:37
本文介绍了如何计算法线矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的普通矩阵有问题.

vs.glsl

#version 440

in vec3 vPosition;
in vec3 vNormal;

out vec4 eyeCordFs;
out vec4 eyeNormalFs;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

void main()
{   
    mat4 modelView = view * model;
    mat4 normalMatrix = view * transpose(inverse(model));
    vec4 eyeNorm = normalize(normalMatrix * vec4(vNormal, 0.0));
    vec4 eyeCord= modelView * vec4(vPosition, 1.0);

    eyeCordFs = eyeCord;
    eyeNormalFs = eyeNorm;

    gl_Position = proj * modelView * vec4( vPosition,1.0);
}

fs.glsl

#version 440

in vec4 eyeCordFs;
in vec4 eyeNormalFs;

out vec3 outputColor;

uniform vec4 lightPos;

void main()
{
    vec4 s = normalize(lightPos - eyeCordFs) ;
    vec4 r = reflect(-s,eyeNormalFs);
    vec4 v = normalize(-eyeCordFs);
    float spec = max( dot(v,r),0.0 );
    float diff = max(dot(eyeNormalFs,s),0.0);

    vec3 diffColor = diff * vec3(1,0,0);
    vec3 specColor = pow(spec,3) * vec3(1,1,1);
    vec3 ambientColor = vec3(0.1,0.1,0.1);

    outputColor =  diffColor + 0.5 * specColor + ambientColor; 
}

这个输出看起来像

这对我来说似乎有点奇怪.但我知道我没有缩放任何东西,所以我想我可以使用 modelView 矩阵来转换我的法线.

Which seemed a bit strange to me. But I knew that I am not scaling anything so I thought I could use the modelView matrix to transform my normals.

所以我换了线

vec4 eyeNorm = normalize(normalMatrix * vec4(vNormal, 0.0));

vec4 eyeNorm = normalize(modelView * vec4(vNormal, 0.0));

现在输出看起来像这样

看起来是正确的.我是否以错误的方式计算了 normalMatrix?

which looks correct. Am I calculating my normalMatrix the wrong way?

推荐答案

普通矩阵是模型视图矩阵的转置逆矩阵.所以在 GLSL 中它会是

The normal matrix is the transpose inverse of the modelview matrix. So in GLSL it would be

mat4 normalMatrix = transpose(inverse(modelView));

但是您应该计算着色器中的法线矩阵.在着色器中这样做会浪费大量宝贵的 GPU 周期.矩阵求逆并不是一个廉价的操作,在着色器中执行它会迫使 GPU 一次又一次地为每个顶点执行计算.在 CPU 上预先计算并作为统一传递.

However you should NOT calculate the normal matrix in the shader. Doing that in the shader wastes a lot of precious GPU cycles. Matrix inversion is not a cheap operation to start with and doing it in the shader forces the GPU to perform the calculation for each and every vertex again and again. Precalculate it on the CPU and pass it as a uniform.

这篇关于如何计算法线矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆