基于高度图的地形上的奇怪正常生成

时间:2014-04-30 14:24:33

标签: c++ opengl terrain normals heightmap

我正在使用libnoise和OpenGL进行地形生成。我似乎做了一个有点复杂的正常生成算法:

list = glGenLists(1);
glNewList(list, GL_COMPILE);


glPushAttrib(GL_ENABLE_BIT);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDisable(GL_TEXTURE_2D);

glPushMatrix();

int h = 256;
int w = 256;
float h1 = 0.0f;
float h2 = 0.0f;
float h3 = 0.0f;

float div = 7.0f;

float lPos[] = { 128, image.GetHeight() + 15.0f, 128, 1.0f };

for (int x = 1; x < h; x++)
{
    glColor3ub(139, 69, 19);
    glLightfv(GL_LIGHT0, GL_POSITION, lPos);
    glBegin(GL_TRIANGLE_STRIP);
    for (int z = 1; z < w; z++)
    {
        h1 = image.GetValue(x, z).red / 7.0f;
        h2 = image.GetValue(x + 1, z).red / 7.0f;


        Vector3 t1, t2, t3;
        Vector3 v1, v2, v3, v4, v5, v6;


        t1 = Vector3(x, h1, z);


        t2 = Vector3(x - 1, image.GetValue(x - 1, z).red / div, z);
        t3 = Vector3(x, image.GetValue(x, z - 1).red / div, z - 1);
        v1 = Vector3::Normalize(Vector3::Cross(t3 - t1, t2 - t1));

        t2 = Vector3(x + 1, image.GetValue(x + 1, z).red / div, z);
        t3 = Vector3(x, image.GetValue(x, z + 1).red / div, z + 1);
        v2 = Vector3::Normalize(Vector3::Cross(t3 - t1, t2 - t1));

        t2 = Vector3(x + 1, image.GetValue(x + 1, z).red / div, z);
        t3 = Vector3(x + 1, image.GetValue(x + 1, z - 1).red / div, z - 1);
        v3 = Vector3::Normalize(Vector3::Cross(t3 - t1, t2 - t1));

        t2 = Vector3(x + 1, image.GetValue(x + 1, z - 1).red / div, - 1);
        t3 = Vector3(x , image.GetValue(x, z - 1).red / div, z - 1);
        v4 = Vector3::Normalize(Vector3::Cross(t3 - t1, t2 - t1));

        t2 = Vector3(x - 1, image.GetValue(x - 1, z + 1).red / div, z + 1);
        t3 = Vector3(x - 1, image.GetValue(x - 1, z).red / div, z);
        v5 = Vector3::Normalize(Vector3::Cross(t3 - t1, t2 - t1));

        t2 = Vector3(x - 1, image.GetValue(x - 1, z + 1).red / div, z + 1);
        t3 = Vector3(x, image.GetValue(x, z + 1).red / div, z + 1);
        v6 = Vector3::Normalize(Vector3::Cross(t3 - t1, t2 - t1));


        Vector3 normal1 = Vector3::Normalize((v1 + v2 + v3 + v4 + v5 + v6) / 6);

        glNormal3f(normal1.X, normal1.Y, normal1.Z);
        glVertex3f(x, h1, z);


        t1 = Vector3(x + 1, h2, z);


        t2 = Vector3(x + 1 - 1, image.GetValue(x + 1 - 1, z).red / div, z);
        t3 = Vector3(x + 1, image.GetValue(x + 1, z - 1).red / div, z - 1);
        v1 = Vector3::Normalize(Vector3::Cross(t3 - t1, t2 - t1));

        t2 = Vector3(x + 1 + 1, image.GetValue(x + 1 + 1, z).red / div, z);
        t3 = Vector3(x + 1, image.GetValue(x + 1, z + 1).red / div, z + 1);
        v2 = Vector3::Normalize(Vector3::Cross(t3 - t1, t2 - t1));

        t2 = Vector3(x + 1 + 1, image.GetValue(x + 1 + 1, z).red / div, z);
        t3 = Vector3(x + 1 + 1, image.GetValue(x + 1 + 1, z - 1).red / div, z - 1);
        v3 = Vector3::Normalize(Vector3::Cross(t3 - t1, t2 - t1));

        t2 = Vector3(x + 1 + 1, image.GetValue(x + 1 + 1, z - 1).red / div, -1);
        t3 = Vector3(x + 1, image.GetValue(x + 1, z - 1).red / div, z - 1);
        v4 = Vector3::Normalize(Vector3::Cross(t3 - t1, t2 - t1));

        t2 = Vector3(x + 1 - 1, image.GetValue(x + 1 - 1, z + 1).red / div, z + 1);
        t3 = Vector3(x + 1 - 1, image.GetValue(x + 1 - 1, z).red / div, z);
        v5 = Vector3::Normalize(Vector3::Cross(t3 - t1, t2 - t1));

        t2 = Vector3(x + 1 - 1, image.GetValue(x + 1 - 1, z + 1).red / div, z + 1);
        t3 = Vector3(x + 1, image.GetValue(x + 1, z + 1).red / div, z + 1);
        v6 = Vector3::Normalize(Vector3::Cross(t3 - t1, t2 - t1));


        normal1 = Vector3::Normalize((v1 + v2 + v3 + v4 + v5 + v6) / 6);

        glNormal3f(normal1.X, normal1.Y, normal1.Z);
        glVertex3f(x + 1, h2, z);
    }
    glEnd();
}

glPopMatrix();
glPopAttrib();
glEndList();

所以,你可以看到我正在生成法线,通过平均六个周围面的法线,从而实现平滑的阴影。问题在于,在某些部分(特别是地形的较低部分),比特仍然是黑色的,有着奇怪的阴影。 这是一张照片:

Terrain with glitched normals enter image description here

我的正常代如何运作:

注意!!!!我画了Y,我的意思是Z,对不起!

这是一张图片: TopView

绿色是第一个顶点。
红色是第二个顶点(x轴上的第一个+ 1)
黄色是相邻三角形的点。

X是外循环。
Z是内环。

因为我正在使用GL_TRIANGLE_STRIP,所以我只需要2个顶点来进行下一次迭代来构建一个三角形。

所以...构建每个三角形:
p1 =(x,图像高度,z)
p2 =(x + 1,图像高度,z)

并在下一次迭代(z ++)上

p3 =(x,图像高度,z + 1)
p4 =(x + 1,图像高度,z + 1)

...等

1 个答案:

答案 0 :(得分:0)

我没有仔细检查所有代码,但我注意到你在这一行的末尾附近错过了一个“z”:

t2 = Vector3(x + 1, image.GetValue(x + 1, z - 1).red / div, - 1);

计算v4(两次)。

另外,在这一行:

Vector3 normal1 = Vector3::Normalize((v1 + v2 + v3 + v4 + v5 + v6) / 6);

在标准化之前,您不需要除以6。事实上,在对它们求和之前,您可以尝试不对各个交叉产品进行标准化。这将使法线朝向中心更接近顶点的三角形加权。

相关问题