三角形的法线的c++实现:(输入三角形的三个顶点坐标,输出法线)
int calTriNormal( double ver1[3], double ver2[3], double ver3[3] , double normal[3] )
{
double temp1[3], temp2[3];
double length = 0.0;
temp1[0] = ver1[0] - ver2[0];
temp1[1] = ver1[1] - ver2[1];
temp1[2] = ver1[2] -ver2[2];
temp2[0] = ver2[0] - ver3[0];
temp2[1] = ver2[1] - ver3[1];
temp2[2] = ver2[2] - ver3[2];
//计算法线
normal[0] = temp1[1] * temp2[2 ] - temp1[2] * temp2[1] ;
normal[1] = temp1[0] * temp2[2] - temp1[2] * temp2[0] ;
normal[2] = temp1[0] * temp2[1] - temp1[1] * temp2[0] ;
//法线单位化
length = sqrt(normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2] );
if(length == 0.0f)
length = 1.0f;
normal[0] /= length ;
normal[1] /= length ;
normal[2] /= length ;
return 0;
}