c++openglglslshadernormals

How to compute vertex normals for a triangle mesh in OpenGl?


To give background, I'm currently generating a surface of revolution with it's center of mass centered at (0,0,0) in the WCS. The surface of a revolution is y=x^2 where 0 <= x <= 1.

I have converted this surface of revolution into a virtual buffer object, and can render it successfully on the screen. However, I cannot seem to get Blinn-Phong shading to work on the object. I'm fairly sure that the problem lies in my normal calculation.

This is the stub that creates the object and calculates normals:

GLfloat vp[49 * 49 * 18];    // array of vertex points


int _i = 50;
int _j = 50;
float vertices[50][50][3];
for (int i = 0; i < _i; i++) {
    float fT = (float) i / (_i - 1);
    float fY = fT;
    float fZ = sqrt(fT);
    for (int j = 0; j < _j; j++) {
        float fS = 2 * M_PI * (float) j / (_j - 1);
        vertices[i][j][0] = fZ * cos(fS);
        vertices[i][j][1] = fY - 0.5; // offset by 0.5 to make center of mass the center
        vertices[i][j][2] = fZ * sin(fS);
    }
}
int curr = 0;
for (int i = 0; i < _i - 1; i++) {
    for (int j = 0; j < _j - 1; j++) {
        vp[curr++] = vertices[i][j][0];
        vp[curr++] = vertices[i][j][1];
        vp[curr++] = vertices[i][j][2];
        vp[curr++] = vertices[i+1][j][0];
        vp[curr++] = vertices[i+1][j][1];
        vp[curr++] = vertices[i+1][j][2];
        vp[curr++] = vertices[i][j+1][0];
        vp[curr++] = vertices[i][j+1][1];
        vp[curr++] = vertices[i][j+1][2];
        vp[curr++] = vertices[i+1][j][0];
        vp[curr++] = vertices[i+1][j][1];
        vp[curr++] = vertices[i+1][j][2];
        vp[curr++] = vertices[i+1][j+1][0];
        vp[curr++] = vertices[i+1][j+1][1];
        vp[curr++] = vertices[i+1][j+1][2];
        vp[curr++] = vertices[i][j+1][0];
        vp[curr++] = vertices[i][j+1][1];
        vp[curr++] = vertices[i][j+1][2];
    }
}

GLuint vao;
glGenVertexArrays (1, &vao);   // generating and binding is common pattern in OpenGL
glBindVertexArray (vao);       // basically setting up memory and associating it

GLuint points_vbo;
glGenBuffers(1, &points_vbo);
glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
glBufferData(GL_ARRAY_BUFFER, 49 * 49 * 18 * sizeof (GLfloat), vp, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);

GLfloat normals[49 * 49 * 18 / 3];
curr = 0;
for (int i = 0; i < 49 * 49 * 18; i += 9){
    int Ux = vp[i+3] - vp[i];
    int Uy = vp[i+4] - vp[i+1];
    int Uz = vp[i+5] - vp[i+2];
    int Vx = vp[i+6] - vp[i];
    int Vy = vp[i+7] - vp[i+1];
    int Vz = vp[i+8] - vp[i+2];

    normals[curr++] = Uy * Vz - Uz * Vy;
    normals[curr++] = Uz * Vx - Ux * Vz;
    normals[curr++] = Ux * Vy - Uy * Vx;
}

GLuint normals_vbo;
glGenBuffers(1, &normals_vbo);
glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
glBufferData(GL_ARRAY_BUFFER, 49 * 49 * 18 / 3 * sizeof(GLfloat), normals, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(1);

This is my vertex shader:

#version 410

layout (location = 0) in vec3 vtxPosition;
layout (location = 1) in vec3 normal;

uniform mat4 proj_mat, view_mat, model_mat;

out vec3 Normal;
out vec3 fpos;

void main () {
    gl_Position = proj_mat * view_mat * model_mat * vec4(vtxPosition, 1.0);
    fpos = vec3(model_mat * vec4(vtxPosition, 1.0));
    Normal = normal;
}

And lastly my fragment shader:

#version 410


// Define INPUTS from fragment shader
//uniform mat4 view_mat;
in vec3 Normal;
in vec3 fpos;

// These come from the VAO for texture coordinates.
in vec2 texture_coords;

// And from the uniform outputs for the textures setup in main.cpp.
uniform sampler2D texture00;
uniform sampler2D texture01;

out vec4 fragment_color; //RGBA color

const vec3 lightPos = vec3(0.0,0.0,5.0);
const vec3 diffColor = vec3(1.0,0.5,0.0);
const vec3 specColor = vec3(1.0,1.0,1.0);

void main () {
  vec3 normal = normalize(Normal);
  vec3 lightDir = normalize(lightPos - fpos);
  float lamb = max(dot(lightDir, normal), 0.0);
  float spec = 0.0;

  if (lamb > 0.0) {
    vec3 refDir = reflect(-lightDir, normal);
    vec3 viewDir = normalize(-fpos);

    float specAngle = max(dot(refDir, viewDir), 0.0);
    spec = pow(specAngle, 4.0);
  }

  fragment_color = vec4(lamb * diffColor + spec * specColor, 1.0);
}

This is the current rendering of the object: current shading


Solution

  • You have to specify 1 normal attribute for each vertex coordinate. A vertex coordinate and its attributes form a tuple.
    Furthermore you have to use the data type float rather than int, for computing the normal vectors:

    GLfloat normals[49 * 49 * 18];
    curr = 0;
    for (int i = 0; i < 49 * 49 * 18; i += 9){
        float Ux = vp[i+3] - vp[i];
        float Uy = vp[i+4] - vp[i+1];
        float Uz = vp[i+5] - vp[i+2];
        float Vx = vp[i+6] - vp[i];
        float Vy = vp[i+7] - vp[i+1];
        float Vz = vp[i+8] - vp[i+2];
    
        float nx = Uy * Vz - Uz * Vy;
        float ny = Uz * Vx - Ux * Vz;
        float nz = Ux * Vy - Uy * Vx;
    
        for (int j = 0; j < 3; ++j) {
            normals[curr++] = nx;
            normals[curr++] = ny;
            normals[curr++] = nz;
        }
    }
    
    glBufferData(GL_ARRAY_BUFFER, 49 * 49 * 18 * sizeof(GLfloat), normals, GL_STATIC_DRAW);
    

    I recommend to invert the normal vector of the back faces for a double sided light model:

    vec3 normal = normalize(Normal);
    vec3 viewDir = normalize(-fpos);
    if (dot(normal, viewDir) < 0.0)
        normal *= -1.0;
    

    Fragment shader:

    #version 410
    
    // Define INPUTS from fragment shader
    //uniform mat4 view_mat;
    in vec3 Normal;
    in vec3 fpos;
    
    // These come from the VAO for texture coordinates.
    in vec2 texture_coords;
    
    // And from the uniform outputs for the textures setup in main.cpp.
    uniform sampler2D texture00;
    uniform sampler2D texture01;
    
    out vec4 fragment_color; //RGBA color
    
    const vec3 lightPos = vec3(0.0,0.0,5.0);
    const vec3 diffColor = vec3(1.0,0.5,0.0);
    const vec3 specColor = vec3(1.0,1.0,1.0);
    
    void main () {
        vec3 normal = normalize(Normal);
        vec3 viewDir = normalize(-fpos);
        if (dot(normal, viewDir) < 0.0)
            normal *= -1.0;
      
        vec3 lightDir = normalize(lightPos - fpos);
        float lamb = max(dot(lightDir, normal), 0.0);
        float spec = 0.0;
    
        if (lamb > 0.0) {
            vec3 refDir = reflect(-lightDir, normal);
    
            float specAngle = max(dot(refDir, viewDir), 0.0);
            spec = pow(specAngle, 4.0);
        }
    
        fragment_color = vec4(lamb * diffColor + spec * specColor, 1.0);
    }