0

ハイトマップ法線を計算するための次のコードがあります

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");
}

コードが三角形の3つの頂点を取得し、その法線を計算してから、それらを追加して正規化し、平均化された法線を取得することを理解しています。しかし、他の3つの可能性をどのように得ることができるかわかりません。私は次のようなことをしています:

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");
}

しかし、私が望んでいた結果が得られるとは思いません。通常の平均化の概念は理解できますが、コードがわかりません。

4

1 に答える 1

1

こんにちは Yzwboy ここでは、「滑らかな」法線 (隣接する三角形に基づいて平均化) を作成しようとする 1 つの方法を示します。

「平滑化された」法線を計算するには、頂点に隣接する三角形の法線全体で平均化された法線を各頂点に割り当てる必要があります。

問題の頂点に隣接する 2 つのエッジ間の角度に基づいて加重平均を計算します (外積は簡単な計算です)。

擬似コード:

Vec3F faceNormal(int face_id, int vertex_id) // assumes C-->B-->A is clockwise
{
    Vec3f A = triangleMesh.face[face_id].vertex[vertex_id];       // A
    Vec3f B = triangleMesh.face[face_id].vertex[(vertex_id+1)%3]; // B
    Vec3f C = triangleMesh.face[face_id].vertex[(vertex_id+2)%3]; // C
    Vec3f BA = B-A;
    Vec3f CA = C-A;
    Vec3f Normal = BA.cross(CA);

    float sin_alpha = length(Normal) / (BA.len() * CA.len() );  // depending on your implementation of Vec3f it could be .magnitude() or .length() instead of .len()
    return (Normal.normalize() * asin(sin_alpha);)
}

次に、頂点ごとに法線を変化させます。

void computeNormals() {
    for (vertex v in triangleMesh)
    {
        Vec3f Normal (0,0,0);
        for (int i = 0;i < TriangleCount;i++)
            if (triangleMesh.face[i].contains(v) )
            {
                int vertID = vertexPositionInTriangle(i,v); //Can be 0,1 or 2.  Use an enum to make A = 0, B=1, C=2 if that is easier to read:)
                Normal = Normal + faceNormal(i,vertID);
            }
        addNormalToVertexV(Normal.normalize(),v); // this is a function to set the normal for a vertex, the vertex class must have a member for normal though and the arguments for the function are Vec3f, Vec3f
    }
}

各三角形の面積を計算して重み付けとして使用することもできますが、ほとんどの場合、角度を使用するのが最も効果的です。

Vec3f 仕様と組み込み関数に一致する名前を使用して作業を節約しようとしましたが、疑似コードを機能させるにはコーディングを行う必要があります (ここでは GL テスト環境にアクセスできません)。

お役に立てれば :)

于 2013-11-11T14:30:52.927 に答える