为了获得更平滑的地形表面外观,解决方案是将单个非法线关联到三角形,而是将法线关联到三角形的三个顶点中的每一个。
所以,当我尝试实现它时,我发现了一些问题,比如模糊
http://i.stack.imgur.com/6oPcW.png
这是一个明确的计划,我正在尝试做的事情:http: //i.stack.imgur.com/7T04y.png
我正在使用这段代码:
for (int i=0;i<nb_pt_z;i++)
{
for (int j=0;j<nb_pt_x;j++)
{
Vector N(0.0, 0.0, 0.0);
if (i != 0)
{
if (j != 0)
{
Vector v1(points_terrain[((i-1)*nb_pt_x)+j].x-points_terrain[(i*nb_pt_x)+j].x,
points_terrain[((i-1)*nb_pt_x)+j].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
points_terrain[((i-1)*nb_pt_x)+j].z-points_terrain[(i*nb_pt_x)+j].z);
Vector v2(points_terrain[(i*nb_pt_x)+(j-1)].x-points_terrain[(i*nb_pt_x)+j].x,
points_terrain[(i*nb_pt_x)+(j-1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
points_terrain[(i*nb_pt_x)+(j-1)].z-points_terrain[(i*nb_pt_x)+j].z);
N += v1^v2;
}
if (j != nb_pt_z-1)
{
Vector v1(points_terrain[((i-1)*nb_pt_x)+(j+1)].x-points_terrain[(i*nb_pt_x)+j].x,
points_terrain[((i-1)*nb_pt_x)+(j+1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
points_terrain[((i-1)*nb_pt_x)+(j+1)].z-points_terrain[(i*nb_pt_x)+j].z);
Vector v2(points_terrain[((i-1)*nb_pt_x)+j].x-points_terrain[(i*nb_pt_x)+j].x,
points_terrain[((i-1)*nb_pt_x)+j].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
points_terrain[((i-1)*nb_pt_x)+j].z-points_terrain[(i*nb_pt_x)+j].z);
N += v1^v2;
Vector v3(points_terrain[(i*nb_pt_x)+(j+1)].x-points_terrain[(i*nb_pt_x)+j].x,
points_terrain[(i*nb_pt_x)+(j+1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
points_terrain[(i*nb_pt_x)+(j+1)].z-points_terrain[(i*nb_pt_x)+j].z);
Vector v4(points_terrain[((i-1)*nb_pt_x)+(j+1)].x-points_terrain[(i*nb_pt_x)+j].x,
points_terrain[((i-1)*nb_pt_x)+(j+1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
points_terrain[((i-1)*nb_pt_x)+(j+1)].z-points_terrain[(i*nb_pt_x)+j].z);
N+= v3^v4;
}
}
if (i != nb_pt_x-1)
{
if (j != 0)
{
Vector v1(points_terrain[(i*nb_pt_x)+(j-1)].x-points_terrain[(i*nb_pt_x)+j].x,
points_terrain[(i*nb_pt_x)+(j-1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
points_terrain[(i*nb_pt_x)+(j-1)].z-points_terrain[(i*nb_pt_x)+j].z);
Vector v2(points_terrain[((i+1)*nb_pt_x)+(j-1)].x-points_terrain[(i*nb_pt_x)+j].x,
points_terrain[((i+1)*nb_pt_x)+(j-1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
points_terrain[((i+1)*nb_pt_x)+(j-1)].z-points_terrain[(i*nb_pt_x)+j].z);
N+= v1^v2;
Vector v3(points_terrain[((i+1)*nb_pt_x)+(j-1)].x-points_terrain[(i*nb_pt_x)+j].x,
points_terrain[((i+1)*nb_pt_x)+(j-1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
points_terrain[((i+1)*nb_pt_x)+(j-1)].z-points_terrain[(i*nb_pt_x)+j].z);
Vector v4(points_terrain[((i+1)*nb_pt_x)+j].x-points_terrain[(i*nb_pt_x)+j].x,
points_terrain[((i+1)*nb_pt_x)+j].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
points_terrain[((i+1)*nb_pt_x)+j].z-points_terrain[(i*nb_pt_x)+j].z);
N+= v3^v4;
}
if (j != nb_pt_z-1)
{
Vector v3(points_terrain[((i+1)*nb_pt_x)+j].x-points_terrain[(i*nb_pt_x)+j].x,
points_terrain[((i+1)*nb_pt_x)+j].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
points_terrain[((i+1)*nb_pt_x)+j].z-points_terrain[(i*nb_pt_x)+j].z);
Vector v4(points_terrain[(i*nb_pt_x)+(j+1)].x-points_terrain[(i*nb_pt_x)+j].x,
points_terrain[(i*nb_pt_x)+(j+1)].hauteur-points_terrain[(i*nb_pt_x)+j].hauteur,
points_terrain[(i*nb_pt_x)+(j+1)].z-points_terrain[(i*nb_pt_x)+j].z);
N+= v3^v4;
}
}
N.normalize();
points_terrain[(i*nb_pt_x)+j].nx = N.x;
points_terrain[(i*nb_pt_x)+j].ny = N.y;
points_terrain[(i*nb_pt_x)+j].nz = N.z;
}
}
typedef struct
{
GLfloat s, t;
GLfloat nx, ny, nz;
GLfloat x, hauteur,z;
} Point_terrain; /* Me, I have a pointer of that already initialized and well fill *
我正在使用顶点数组来绘制这些点。
num = 0;
for( z=0; z<nb_pt_z-1; z++ )
{
for( x=0; x<nb_pt_x-1; x++ )
{
int ind=z*nb_pt_x+x;
points_indice[num++] =ind;
points_indice[num++] = ind+nb_pt_x;
points_indice[num++] = ind+nb_pt_x+1;
points_indice[num++] = ind;
points_indice[num++] = ind+nb_pt_x+1;
points_indice[num++] = ind+1;
}
}
和 ..
glInterleavedArrays( GL_T2F_N3F_V3F, 0, points_terrain );
glDrawElements( GL_TRIANGLES, nbIndice,
GL_UNSIGNED_INT, points_indice );
那么,有人知道问题出在哪里吗?