【发布时间】:2013-11-11 11:50:33
【问题描述】:
我有以下代码用于计算高度图法线
void CalcMapNormals(HeightMap * map, Vec3f normals[])
{
int dst, i, j, right, bottom;
Vec3f p0, p1, p2;
Vec3f n0;
/* Avoid writing map->rows|cols - 1 all the time */
right = map->cols - 1;
bottom = map->rows - 1;
dst = 0;
for (i = 0; i < map->rows; i++) {
for (j = 0; j < map->cols; j++) {
Vec3Set(normals[dst], 0, 0, 0);
/* Vertex can have 2, 3, or 4 neighbours horizontally and vertically */
if (i < bottom && j < right) {
/* Right and below */
GetHeightPoint(map, i, j, p0);
GetHeightPoint(map, i + 1, j, p1);
GetHeightPoint(map, i + 1, j + 1, p2);
CalcTriNormal(n0, p0, p1, p2);
VecAdd(normals[dst], normals[dst], n0);
}
/* TODO: the other three possibilities */
VecNormalize(normals[dst]);
dst += 1;
}
}
/* Sanity check */
if (dst != map->rows * map->cols)
Fail("Internal error in CalcMapNormals: normals count mismatch");
}
我知道代码获取三角形的三个顶点,计算其法线,然后将它们相加并将它们归一化以获得平均法线。但我不知道您如何获得其他三种可能性,我一直在做以下事情:
void CalcMapNormals(HeightMap * map, Vec3f normals[])
{
int dst, i, j, right, bottom;
Vec3f p0, p1, p2;
Vec3f n0;
Vec3f p3, p4, p5;
Vec3f n1;
Vec3f p6, p7, p8;
Vec3f n2;
Vec3f p9, p10, p11;
Vec3f n3;
/* Avoid writing map->rows|cols - 1 all the time */
right = map->cols - 1;
bottom = map->rows - 1;
dst = 0;
for (i = 0; i < map->rows; i++) {
for (j = 0; j < map->cols; j++) {
Vec3Set(normals[dst], 0, 0, 0);
/* Vertex can have 2, 3, or 4 neighbours horizontally and vertically */
if (i < bottom && j < right) {
/* Right and below */
GetHeightPoint(map, i, j, p0);
GetHeightPoint(map, i + 1, j, p1);
GetHeightPoint(map, i + 1, j + 1, p2);
CalcTriNormal(n0, p0, p1, p2);
VecAdd(normals[dst], normals[dst], n0);
}
if ( i > bottom && j > 0)
{
GetHeightPoint(map, i, j, p3);
GetHeightPoint(map, i + 1, j, p4);
GetHeightPoint(map, i, j -1, p5);
CalcTriNormal(n1, p3, p4, p5);
VecAdd(normals[dst], normals[dst], n1);
}
if ( i > 0 && j > 0)
{
GetHeightPoint(map, i, j, p6);
GetHeightPoint(map, i, j - 1, p7);
GetHeightPoint(map, i - 1, j, p8);
CalcTriNormal(n2, p6, p7, p8);
VecAdd(normals[dst], normals[dst], n2);
}
if ( i > bottom && j < right)
{
GetHeightPoint(map, i, j, p9);
GetHeightPoint(map, i-1, j, p10);
GetHeightPoint(map, i, j+1, p11);
CalcTriNormal(n3, p9, p10, p11);
VecAdd(normals[dst], normals[dst], n3);
}
/* TODO: the other three possibilities */
VecNormalize(normals[dst]);
dst += 1;
}
}
/* Sanity check */
if (dst != map->rows * map->cols)
Fail("Internal error in CalcMapNormals: normals count mismatch");
}
但我不认为它给了我想要的结果,我得到了正常平均的概念,但无法弄清楚代码。
【问题讨论】:
-
那么您是在寻找每个顶点的法线还是每个三角形面的法线?只是为了澄清我的意思,所以我可以简洁地回答:) 我问的原因是因为听起来你正在尝试获取三角形所有 3 个顶点的法线,然后对它们进行平均,这与 a 的法线相同三角脸。
-
@GMasucci 谢谢,我试图获得每个顶点的平均法线,基本上我试图让所有相邻的三角形共享一个顶点(最多 6 个三角形可以共享一个顶点,我使用 4 ) 并获得平均正常值,如果我没有清楚地解释这一点,请见谅。
标签: c++ opengl normals heightmap