以下是三角形网格的表面法线问题:
float3 calcNormal( float3 p0 , float3 p1 , float3 p2 ){
float3 V = { p1.x - p0.x , p1.y - p0.y , p1.z - p0.z };
float3 U = { p2.x - p0.x , p2.y - p0.y , p2.z - p0.z };
float3 normal = {V.y * U.z - U.y*V.z , V.x * U.z - U.x * V.z , V.x * U.y - U.x * V.y };
return normal;
}
三角形法线功能↑
glNormal3f(calcNormal( { i*size , heightMap[i][j]*h , j*size } , {(i+1)*size, heightMap[i+1][j]*h,j*size } , { i*size,heightMap[i][j+1]*h,j*size } ).x ,
calcNormal( { i*size , heightMap[i][j]*h , j*size } , {(i+1)*size, heightMap[i+1][j]*h,j*size } , { i*size,heightMap[i][j+1]*h,j*size } ).y,
calcNormal( { i*size , heightMap[i][j]*h , j*size } , {(i+1)*size, heightMap[i+1][j]*h,j*size } , { i*size,heightMap[i][j+1]*h,j*size } ).z);
使用上面的代码,我计算一个正方形中2个三角形的第一个三角形的法线,使用下面的代码,我计算出第二个三角形的法线(我想)。
glNormal3f(calcNormal( { (i+1)*size , heightMap[i+1][j]*h , j*size } , {(i)*size, heightMap[i][j+1]*h,(j+1)*size } , { (i+1)*size,heightMap[i+1][j+1]*h,(j+1)*size } ).x ,
calcNormal( { (i+1)*size , heightMap[i+1][j]*h , j*size } , {(i)*size, heightMap[i][j+1]*h,(j+1)*size } , { (i+1)*size,heightMap[i+1][j+1]*h,(j+1)*size } ).y,
calcNormal( { (i+1)*size , heightMap[i+1][j]*h , j*size } , {(i)*size, heightMap[i][j+1]*h,(j+1)*size } , { (i+1)*size,heightMap[i+1][j+1]*h,(j+1)*size } ).z);
在这种情况下,是否可以使用三角形法线而不是顶点法线,如果是这样,我该如何放置法线调用?
...
glBegin(GL_TRIANGLE_STRIP);
...
glNormal3f(/*calculated normal*/); //I know these are supposed to be vertex normals
glColor3f(0,color_map ,color_ugate-color_map );
glVertex3f(i*size , heightMap[i][j]*h , j*size);
glNormal3f(/*calculated normal*/);
glColor3f(0,color_map ,color_ugate-color_map );
glVertex3f((i+1)*size , heightMap[i+1][j]*h , j*size);
glNormal3f(/*calculated normal*/);
glColor3f(0,color_map ,color_ugate-color_map );
glVertex3f(i*size , heightMap[i][j+1]*h , (j+1)*size);
glNormal3f(/*calculated normal*/);
glColor3f(0,color_map ,color_ugate-color_map );
glVertex3f((i+1)*size , heightMap[i+1][j+1]*h , (j+1)*size);
...
glEnd();
...