Search code examples
openglnormals

glNormalPointer define an array of vertex or faces normals?


I use glNormalPointer() to load values of normals in my code. The problem is that I could not understand which normals should I load per vertex or per face? Interestingly, but I try both variants and could not see any differences.

Could You clarify which normals should I pass to shader?

I use normals in the following vertex shader:

#version 120

void main() {
    vec3 normal, lightDir;
    vec4 diffuse, ambient, globalAmbient;
    float NdotL;

    normal = gl_NormalMatrix * gl_Normal;
    gl_Position = gl_ModelViewProjectionMatrix *  gl_Vertex;
    // Calculate color
    lightDir = normalize(vec3(gl_LightSource[0].position));
    NdotL = max(abs(dot(normal, lightDir)), 0.0);
    diffuse = gl_Color * gl_LightSource[0].diffuse;
    ambient = gl_Color * gl_LightSource[0].ambient;
    globalAmbient = gl_LightModel.ambient * gl_Color;
    gl_FrontColor =  NdotL * diffuse + globalAmbient + ambient;
 }

Solution

  • All vertex attribute arrays, whether built-in or user-defined, provide data for each vertex, with a 1:1 ratio between vertex shader invocations and elements fetched from the array. OpenGL has no concept of "face normals". You can use flat interpolation and knowledge of the provoking vertex to create the effect of face normals, but your input data will still have to have a normal for each vertex.