How to calculate the normal matrix?

前端 未结 2 1339
梦毁少年i
梦毁少年i 2021-01-30 07:32

I have some trouble with my normal matrix.

vs.glsl

#version 440

in vec3 vPosition;
in vec3 vNormal;

out vec4 eyeCordFs;
out vec4 eyeNormalFs;

uniform          


        
相关标签:
2条回答
  • 2021-01-30 08:10

    It is also a default uniform in GLSL. You can just declare it as such

    uniform mat3 normalMatrix;
    

    See here.

    0 讨论(0)
  • 2021-01-30 08:17

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

    mat4 normalMatrix = transpose(inverse(modelView));
    

    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.

    0 讨论(0)
提交回复
热议问题