问题描述
给定一个凸多面体,其顶点(x,y,z)指定了多面体的面.
Given a convex polyhedron with defined vertices (x, y, z) that specifies the faces of the polyhedron.
如何计算多面体每个面的表面法线?
我需要表面法线来计算顶点法线以执行 Gouraud 着色.我能找到的关于如何做到这一点的唯一线索是 Newell 的方法,但我如何确保法线是向外的法线而不是向内的?感谢您的帮助.
I need the surface normal in order to compute the vertex normal to perform Gouraud shading. The only clue I could find about how to do this is Newell's method, but how do I make sure the normals are outward normals and not inward? Thanks for any help.
推荐答案
计算人脸法线
您必须计算跨越包含给定面的平面的两个向量的叉积.它为您提供了该面的(非单位)法向量.您必须对其进行标准化,然后就完成了.
You have to compute the cross product of two vectors spanning the plane that contains the given face. It gives you the (non-unit) normal vector of that face. You have to normalize it and you are done.
如果x0
、x1
、x2
是三角面的顶点,则法线可计算为
If x0
, x1
, x2
are the vertices of a triangular face, then the normal can be computed as
vector3 get_normal(vector3 x0, vector3 x1, vector3 x2)
{
vector3 v0 = x0 - x2;
vector3 v1 = x1 - x2;
vector3 n = cross(v0, v1);
return normalize(n);
}
请注意,叉积遵循右手法则:
右手定则表明向量的交叉方向产品是通过放置u和v尾对尾来确定的,将右手展平,向u的方向伸展,然后然后将手指向 v 夹角的方向弯曲与你.然后拇指指向cross(u, v)的方向.
调整三角形的方向
要确保所有法线都指向多面体的内部/外部,三角形的方向必须一致,这意味着所有顶点必须遵循逆时针 (CCW) 或顺时针 (CW) 顺序.这在计算机图形学中也称为缠绕.
To make sure that all your normals are pointing inside/outside of the polyhedron, the triangles must be uniformly oriented, which means that all the vertices must follow a counter-clockwise (CCW) or clockwise (CW) order. This is also called winding in computer graphics.
您可以通过计算下面矩阵的行列式来检查三角形的方向,其中 x3
是第四个点,即您在测试期间的观察点.
You can check the orientation of your triangles by computing the determinant of the matrix below where x3
is a fourth point, your view point during the test.
| x0.x x0.y x0.z 1 |
| x1.x x1.y x1.z 1 |
| x2.x x2.y x2.z 1 |
| x3.x x3.y x3.z 1 |
- 行列式 > 0:
x3
位于由 CCW 点{ x0, x1, x2 定义的平面的
+
一侧} - 行列式
x3
在由CCW点{ x0, x1, x2 }
-一侧> - determinant = 0:
x3
与{ x0, x1, x2 }
共面 - determinant > 0:
x3
is on the+
side of the plane defined by CCW points{ x0, x1, x2 }
- determinant < 0:
x3
is on the-
side of the plane defined by CCW points{ x0, x1, x2 }
- determinant = 0:
x3
is coplanar with{ x0, x1, x2 }
旋转顶点的顺序(通过向左或向右移动所有顶点)不会改变方向.所以 { x0, x1, x2 }
与 { x2, x0, x1 }
和 { x1, x2, x0 }
具有相同的方向.
Rotating the order of the vertices (by shifting all of them left or right) doesn't change the orientation. So { x0, x1, x2 }
has the same orientation as { x2, x0, x1 }
and { x1, x2, x0 }
.
然而,如果你交换两个连续元素的顺序,你也会交换到相反的方向.这意味着 { x0, x1, x2 }
与 { x1, x0, x2 }
的方向相反.
However if you swap the order of two consecutive elements, you also swap to the opposite orientation. It means that { x0, x1, x2 }
has the opposite orientation as { x1, x0, x2 }
.
使用此信息,您可以轻松地确定三角形的方向:使用谓词矩阵测试每个三角形.如果测试失败,只需交换任意两个连续顶点元素的顺序即可解决问题.
Using this information you can easily orient your triangles: test each triangle using the predicate matrix. If the test fails, simply swap the order of any two consecutive vertex elements and problem solved.
这篇关于计算面法线和缠绕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!