0

要在地形上行走物体,目前我保持相机和物体固定,并根据按下的键盘按钮移动地形(使用 glTranslatef)。我能够轻松地沿 x 和 z 轴移动,但是当我沿 y 轴移动时,我遇到了很多麻烦。这是代码的基本部分:

我制作了一个名为 Terrain 的类,它使用 bmp 文件来获取不同点的高度。基本代码是:

float Terrain::getHeight(int x, int z) {
    return hs[z][x];
}

Terrain* loadTerrain(const char* filename, float height) {
    Image* image = loadBMP(filename);
    Terrain* t = new Terrain(image->width, image->height);
    for (int y = 0; y < image->height; y++) {
        for (int x = 0; x < image->width; x++) {
            unsigned char color = (unsigned char) image->pixels[3
                    * (y * image->width + x)];
            float h = height * ((color / 255.0f) - 0.5f);
            t->setHeight(x, y, h);

        }
    }

    delete image;
    t->computeNormals();
    return t;
}

所以基本上我已经将它们全部存储在一个数组中,并且可以随时使用 x 和 z 获取 y 。这是我的drawTerrain代码,它采用三个浮点值,xx,yy和zz,并且基本上使用glTranslatef在xx和zz方向上转换地形(我还没有使用yy)。

void drawTerrain(float xx, float yy, float zz, float aangle) {
    float yyy;
    glPushMatrix();
    glTranslatef(0.0f, 0.0f, 0.0f);
    glRotatef(-aangle, 0.0f, 1.0f, 0.0f);
    glTranslatef(xx, yy, zz);

    GLfloat ambientColor[] = { 0.4f, 0.4f, 0.4f, 1.0f };
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

    GLfloat lightColor0[] = { 0.6f, 0.6f, 0.6f, 1.0f };
    GLfloat lightPos0[] = { -0.5f, 0.8f, 0.1f, 0.0f };
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, _textureId1);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    float scale = 50.0f / max(_terrain->width() - 1, _terrain->length() - 1);
    glScalef(scale, scale, scale);
    glTranslatef(-(float) (_terrain->width() - 1) / 2, 0.0f,
            -(float) (_terrain->length() - 1) / 2);

    glColor3f(1.0f, 1.0f, 1.0f);
    for (int z = 0; z < _terrain->length() - 1; z++) {
        //Makes OpenGL draw a triangle at every three consecutive vertices
        glBegin(GL_TRIANGLE_STRIP);
        for (int x = 0; x < _terrain->width(); x += 2) {
            /*if (x == 0 && z == 0)
             glTexCoord2f(0.0f, 0.0f);
             if (x == _terrain->width() - 1 && z == 0)
             glTexCoord2f(1.0f, 0.0f);
             if (x == 0 && z == _terrain->length() - 1)
             glTexCoord2f(0.0f, 1.0f);
             if (x == _terrain->width() - 1 && z == _terrain->length() - 1)
             glTexCoord2f(1.0f, 1.0f);*/
            Vec3f normal = _terrain->getNormal(x, z);
            glNormal3f(normal[0], normal[1], normal[2]);
            glTexCoord2f((float) x / _terrain->width(),
                    (float) z / _terrain->length());
            glVertex3f(x, _terrain->getHeight(x, z), z);
            normal = _terrain->getNormal(x, z + 1);
            glNormal3f(normal[0], normal[1], normal[2]);
            glTexCoord2f((float) x / _terrain->width(),
                    (float) (z + 1) / _terrain->length());
            glVertex3f(x, _terrain->getHeight(x, z + 1), z + 1);
            glNormal3f(normal[0], normal[1], normal[2]);
            glTexCoord2f((float) (x + 1) / _terrain->width(),
                    (float) z / _terrain->length());
            glVertex3f(x + 1, _terrain->getHeight(x + 1, z), z);
            glNormal3f(normal[0], normal[1], normal[2]);
            glTexCoord2f((float) (x + 1) / _terrain->width(),
                    (float) (z + 1) / _terrain->length());
            glVertex3f(x + 1, _terrain->getHeight(x + 1, z + 1), z + 1);

        }
        //_y = (float) _terrain->getHeight((int) (_x * max(_terrain->width() - 1, _terrain->length() - 1) / 50.0f), (int) (_z * max(_terrain->width() - 1, _terrain->length() - 1) / 50.0f));
        glEnd();
    }

所以要在 y 方向翻译,我尝试使用以下代码:

float xxx,yyy,zzz;
xxx = (float) (_terrain->width() - 1) / (2 * scale);
xxx -= xx / scale;
zzz = (float) (_terrain->length() - 1) / (2 * scale);
zzz -= zz / scale;
yyy = scale * _terrain->getHeight(xxx,zzz);
glTranslatef(0.0f,yyy,0.0f);

但是翻译仍然不能根据高度工作,并且 y 翻译看起来也不连续(因为我正在转换整数和浮点值)。任何人都可以修复此代码,或建议一些其他方法,以便我可以正确地在地形上行走物体吗?谢谢。

4

0 回答 0