这是我的Setup()函数:
void Setup() // TOUCH IT !!
{
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
//Parameter handling
glShadeModel (GL_SMOOTH);
//glEnable(GL_NORMALIZE);
glDepthFunc(GL_LEQUAL); //renders a fragment if its z value is less or equal of the stored value
glClearDepth(1);
//Set up light source
GLfloat light_position[] = { 20.0, 0.0, 0.0, 0.0 };
GLfloat ambientLight[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0 };
glLightfv( GL_LIGHT0, GL_POSITION, light_position);
glLightfv( GL_LIGHT0, GL_AMBIENT, ambientLight );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseLight );
glEnable(GL_LIGHT0);
// polygon rendering mode
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glColorMaterial( GL_FRONT, GL_EMISSION );
// material identities
float specReflection[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float ambReflection[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float diffReflection[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection);
glMaterialfv(GL_FRONT, GL_AMBIENT, ambReflection);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffReflection);
glMateriali(GL_FRONT,GL_SHININESS,20);
//// about texture
//glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
// Black background
glClearColor(0.0f,0.0f,0.0f,1.0f);
}
这是我的CalcNormal()
函数,正在计算法线。 v1和v2是三角形的两个向量:
Point CalcNormal(Point v1, Point v2)
{
Point normal;
normal.x = v1.y*v2.z - v1.z*v2.y;
normal.y = -v1.x*v2.z + v2.x*v1.z;
normal.z = v1.x*v2.y - v2.x*v1.y;
float dist1 = sqrt( pow(v1.x,2) + pow(v1.y,2) + pow(v1.z,2));
float dist2 = sqrt( pow(v2.x,2) + pow(v2.y,2) + pow(v2.z,2));
float dist = dist1*dist2;
normal.x = normal.x/dist;
normal.y = normal.y/dist;
normal.z = normal.z/dist;
return normal;
}
这是我发送给CalcNormal()的向量:
Point n;
Point a1, a2;
a1.x=triangles.at(i).p2.x-triangles.at(i).p1.x; a1.y=triangles.at(i).p2.y-triangles.at(i).p1.y; a1.z=triangles.at(i).p2.z-triangles.at(i).p1.z;
a2.x=triangles.at(i).p3.x-triangles.at(i).p1.x; a2.y=triangles.at(i).p3.y-triangles.at(i).p1.y; a2.z=triangles.at(i).p3.z-triangles.at(i).p1.z;
n = CalcNormal(a1, a2);
结果是这样的:
答案 0 :(得分:1)
要使平滑着色有效,您还需要平滑法线 - 每个顶点。到目前为止您发布的代码仅显示了每个三角形的正常计算。在这种情况下,结果仍然是平坦的。
获得平滑法线的最简单方法是,在计算每个三角形的法线后,平均每个顶点的相邻三角形的所有法线。更复杂的方案使用基于三角形区域的一些函数的加权平均。尝试保留硬边缘也有一些启发式方法,但是由于启发式方法总是这样,所以总会有一些极端情况。理想情况下,在创建模型并将其存储在文件中时会创建法线。