如何在OpenGL中计算三角形网格的顶点法线?

Posted

技术标签:

【中文标题】如何在OpenGL中计算三角形网格的顶点法线?【英文标题】:How to compute vertex normals for a triangle mesh in OpenGl? 【发布时间】:2020-04-16 01:48:41 【问题描述】:

为了提供背景,我目前正在生成一个旋转曲面,它的质心在 WCS 中以 (0,0,0) 为中心。旋转的表面是 y=x^2,其中 0

我已经将这个旋转表面转换成一个虚拟缓冲对象,并且可以成功地在屏幕上渲染它。但是,我似乎无法让 Blinn-Phong 着色在对象上工作。我很确定问题出在我的正常计算上。

这是创建对象并计算法线的存根:

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);

这是我的顶点着色器:

#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;

最后是我的片段着色器:

#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);

这是对象的当前渲染:

【问题讨论】:

【参考方案1】:

您必须为每个顶点坐标指定 1 个法线属性。一个顶点坐标和它的属性组成一个元组。 此外,您必须使用数据类型float 而不是int 来计算法线向量:

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);

我建议为双面光模型反转背面的法线向量:

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

片段着色器:

#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);

【讨论】:

以上是关于如何在OpenGL中计算三角形网格的顶点法线?的主要内容,如果未能解决你的问题,请参考以下文章

OpenGL 计算三角形法线 C++

通用三角形网格的简单纹理映射

计算 GL_TRIANGLE 的顶点法线

为 OpenGL Gouraud 着色提供带有三角形条和扇形的法线

OpenGL ES——导入.stl格式的3D模型

将 SDF 计算为三角形网格的最有效方法