三角形法线与顶点法线

Triangle normal vs vertex normal

我可以通过 v0v1v2 三个顶点位置并使用叉积:

问题是 3D 网格数据结构需要每个顶点的法线。我的意思是,它需要 n1n2n3。我不知道计算它们的正确方法是什么。

尝试过

我尝试对 n1n2n3 使用相同的 N 值,但我不确定这是否是正确的方法:

n1 = n2 = n3 = N

像这样实现平滑顶点法线:

std::vector<Vec3d> points = ...
std::vector<Vec3i> facets = ...

// Count how many faces/triangles a vertex is shared by
std::vector<int> counters;
counters.resize(points.size());

// Compute normals
norms.clear();
norms.resize(points.size());
for (Vec3i f : facets) {
    int i0 = f.x();
    int i1 = f.y();
    int i2 = f.z();
    Vec3d pos0 = points.at(i0);
    Vec3d pos1 = points.at(i1);
    Vec3d pos2 = points.at(i2);
    Vec3d N = triangleNormal(pos0, pos1, pos2);
    
    // Must be normalized
    // 
    N.normalize();

    norms[i0] += N;
    norms[i1] += N;
    norms[i2] += N;

    counters[i0]++;
    counters[i1]++;
    counters[i2]++;
}

for (int i = 0; i < static_cast<int>(norms.size()); ++i) {
    if (counters[i] > 0)
        norms[i] /= counters[i];
    else
        norms[i].normalize();
}