I am writing a 3ds file parser in c++. Now I am working on normals, and I found this piece of code somewhere on web:
我正在用c ++编写一个3ds文件解析器。现在我正在研究法线,我在网上找到了这段代码:
void calculateNormals(vert a, vert b, vert c,GLfloat *nx,GLfloat *ny, GLfloat *nz){
GLfloat Qx, Qy, Qz, Px, Py, Pz;
GLfloat v1[3] = {a.x,a.y,a.z};
GLfloat v2[3] = {b.x,b.y,b.z};
GLfloat v3[3] = {c.x,c.y,c.z};
Qx = v2[0]-v1[0];
Qy = v2[1]-v1[1];
Qz = v2[2]-v1[2];
Px = v3[0]-v1[0];
Py = v3[1]-v1[1];
Pz = v3[2]-v1[2];
*nx = Py*Qz - Pz*Qy;
*ny = Pz*Qx - Px*Qz;
*nz = Px*Qy - Py*Qx;
}
I understand almost everything, except 3 last lines. I just... can't figure out how and why it works.
我理解几乎所有东西,除了最后3行。我只是...无法弄清楚它是如何工作的原因。
Can someone explain how it's calculated?
有人能解释一下它的计算方法吗?
2 个解决方案
#1
As Cyber said in comments the solution is calculating the cross product. The math is described here.
正如Cyber在评论中所说,解决方案是计算交叉产品。数学在这里描述。
The code is turning the triangle points into the 2 vectors ( from point 1 to 2 and from point 1 to 3) then it is taking them and calculating the cross product over them.
代码将三角形点转换为2个向量(从第1点到第2点,从第1点到第3点),然后它们将它们计算出来并计算它们之间的叉积。
#2
It is a cross product. Note that when a vector (I,j,k) is multiplied (cross product) in (i0,j0,k0), the result is (jk0-kj0,ki0-ik0,ij0-ji0) usually normals should be normalized too if the vectors are not unit length.
这是一个交叉产品。注意,当向量(I,j,k)在(i0,j0,k0)中乘以(叉积)时,结果是(jk0-kj0,ki0-ik0,ij0-ji0),如果通常法线也应该归一化,如果向量不是单位长度。
#1
As Cyber said in comments the solution is calculating the cross product. The math is described here.
正如Cyber在评论中所说,解决方案是计算交叉产品。数学在这里描述。
The code is turning the triangle points into the 2 vectors ( from point 1 to 2 and from point 1 to 3) then it is taking them and calculating the cross product over them.
代码将三角形点转换为2个向量(从第1点到第2点,从第1点到第3点),然后它们将它们计算出来并计算它们之间的叉积。
#2
It is a cross product. Note that when a vector (I,j,k) is multiplied (cross product) in (i0,j0,k0), the result is (jk0-kj0,ki0-ik0,ij0-ji0) usually normals should be normalized too if the vectors are not unit length.
这是一个交叉产品。注意,当向量(I,j,k)在(i0,j0,k0)中乘以(叉积)时,结果是(jk0-kj0,ki0-ik0,ij0-ji0),如果通常法线也应该归一化,如果向量不是单位长度。