我的问题是关于 OpenGL 和 Normals,我了解它们背后的数学原理,并且取得了一些成功。
我在下面附加的函数接受一个交错的顶点数组,并计算每 4 个顶点的法线。这些代表具有相同方向的QUADS。据我了解,这 4 个顶点应该共享相同的法线。只要他们面对同样的方式。
我遇到的问题是我的 QUADS 使用对角渐变进行渲染,就像这样:灯光效果- 除了阴影在中间,灯光在角落。
我以一致的方式绘制我的 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];
}
}
}