我正在尝试计算每个顶点的法线。但我做错了什么。当我运行代码时,我看到了这个:
这是我的代码,请注意 vertex1 是当前顶点之前的顶点,而 vertex2 是当前顶点之后的顶点。
for (int j = 0; j < meshes[t].face[i].numOfPoints; j++)
{
if (normalSetChange)
{
vector3D vertex1, vertex2;
if ((j < meshes[t].face[i].numOfPoints - 1) && (j > 0))
{
vertex1 = vertexes[meshes[t].face[i].vertex[j + 1]] - vertexes[meshes[t].face[i].vertex[j]];
vertex2 = vertexes[meshes[t].face[i].vertex[j - 1]] - vertexes[meshes[t].face[i].vertex[j]];
}
else if (j < meshes[t].face[i].numOfPoints - 1)
{
vertex1 = vertexes[meshes[t].face[i].vertex[j + 1]] - vertexes[meshes[t].face[i].vertex[j]];
vertex2 = vertexes[meshes[t].face[i].vertex[meshes[t].face[i].numOfPoints - 1]] - vertexes[meshes[t].face[i].vertex[j]];
}
else if (j > 0)
{
vertex1 = vertexes[meshes[t].face[i].vertex[0]] - vertexes[meshes[t].face[i].vertex[j]];
vertex2 = vertexes[meshes[t].face[i].vertex[j - 1]] - vertexes[meshes[t].face[i].vertex[j]];
}
normalSet = vector3D(vertex1.y * vertex2.z - vertex1.z * vertex2.y,
vertex1.z * vertex2.x - vertex1.x * vertex2.z,
vertex1.x * vertex2.y - vertex1.y * vertex2.x);
normalLength = sqrt(normalSet.x * normalSet.x + normalSet.y * normalSet.y + normalSet.z * normalSet.z);
normalSet.x /= normalLength;
normalSet.y /= normalLength;
normalSet.z /= normalLength;
writePolygonLineVCN(PolygonLineVCN(vertexes[meshes[t].face[i].vertex[j]], vertexestexCoordinate[meshes[t].face[i].texCoordinate[j]], normalSet), newFile[workOnCPU]);
}
else
writePolygonLineVCN(PolygonLineVCN(vertexes[meshes[t].face[i].vertex[j]], vertexestexCoordinate[meshes[t].face[i].texCoordinate[j]], vertexesNormals[meshes[t].face[i].normal[j]]), newFile[workOnCPU]);
}