我的问题是关于OpenGL和法线,我理解他们背后的数学,并且我取得了一些成功。OpenGL计算法线(四边形)
我在下面附加的函数接受交错的顶点数组,并计算每4个顶点的法线。这些代表QUADS具有相同的方向。根据我的理解,这4个顶点应该共享相同的Normal。只要他们面对同样的道路。
我遇到的问题是我的QUADS渲染时有一个对角线渐变,非常像这样:Light Effect - 除了阴影在中间,角落中的灯光。
我以一致的方式画出我的QUADS。 TopLeft,TopRight,BottomRight,BottomLeft和我用来计算法线的顶点是TopRight - TopLeft和BottomRight - TopLeft。
希望有人可以看到我犯了一个错误的东西,但我已经在这几个小时没有占上风。
为了记录我呈现了一个立方体和一个茶壶旁边的我的对象来检查我的照明功能,所以我相当确定没有关于光位置的问题。
void CalculateNormals(point8 toCalc[], int toCalcLength)
{
GLfloat N[3], U[3], V[3];//N will be our final calculated normal, U and V will be the subjects of cross-product
float length;
for (int i = 0; i < toCalcLength; i+=4) //Starting with every first corner QUAD vertice
{
U[0] = toCalc[i+1][5] - toCalc[i][5]; U[1] = toCalc[i+1][6] - toCalc[i][6]; U[2] = toCalc[i+1][7] - toCalc[i][7]; //Calculate Ux Uy Uz
V[0] = toCalc[i+3][5] - toCalc[i][5]; V[1] = toCalc[i+3][6] - toCalc[i][6]; V[2] = toCalc[i+3][7] - toCalc[i][7]; //Calculate Vx Vy Vz
N[0] = (U[1]*V[2]) - (U[2] * V[1]);
N[1] = (U[2]*V[0]) - (U[0] * V[2]);
N[2] = (U[0]*V[1]) - (U[1] * V[0]);
//Calculate length for normalising
length = (float)sqrt((pow(N[0],2)) + (pow(N[1],2)) + (pow(N[2],2)));
for (int a = 0; a < 3; a++)
{
N[a]/=length;
}
for (int j = 0; i < 4; i++)
{
//Apply normals to QUAD vertices (3,4,5 index position of normals in interleaved array)
toCalc[i+j][3] = N[0]; toCalc[i+j][4] = N[1]; toCalc[i+j][5] = N[2];
}
}
}
这是C++;你有没有考虑过使用数学库,比如[GLM](http://glm.g-truc.net/)? – 2012-01-17 16:24:20
不幸的是,这是一个任务,因此我们不鼓励使用库。 – LBHoward 2012-01-17 16:38:58