Search code examples
openglgraphicsrenderingscalemesh

glscalef effect on normal vectors


So I have a .3ds mesh that I imported into my project and it's quite large so I wanted to scale it down in size using glscalef. However the rendering algorithm I use in my shader makes use of normal vector values and so after scaling my rendering algorithm no longer works exactly as it should. So how do remedy this? Is there a glscalef for normals as well?


Solution

  • Normal vectors are transformed by the transposed inverse of the modelview matrix. However a second constraint is, that normals be of unit length and scaling the modelview changes that. So in your shader, you should apply a normalization step

    #version 330
    
    uniform mat4x4 MV;
    uniform mat4x4 P;
    uniform mat4x4 N; // = transpose(inv(MV));
    
    in vec3 vertex_pos;    // vertes position attribute
    in vec3 vertex_normal; // vertex normal attribute
    
    out vec3 trf_normal;
    
    void main()
    {
        trf_normal = normalize( (N * vec4(vertex_normal, 1)).xyz );
        gl_Position = P * MV * vertex_pos;
    }
    

    Note that "normalization" is the process of turning a vector into colinear vector of its own with unit length, and has nothing to do with the concept of surface normals.