高度图地形碰撞OpenGL

时间:2017-02-14 15:51:37

标签: c++ collision terrain heightmap

所以我根据高度图渲染了一个地形。它基于我遵循的本教程 - http://www.mbsoftworks.sk/index.php?page=tutorials&series=1&tutorial=8

现在我想让我的相机能够“走路”#34;在地形上,我不确定如何实际做到这一点。我已经检查了很多其他的例子,并得到了一些重心坐标的提示。但我不确定如何正确实施它。

float Terrain::getY(int x, int z)
{

    //return
}

void Terrain::createTerrain()
{
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    // Setup heightmap

    glGenVertexArrays(1, &uiVAOHeightmap); // Create one VAO
    glGenBuffers(1, &uiVBOHeightmapData); // One VBO for data
    glGenBuffers(1, &uiVBOIndices); // And finally one VBO for indices

    glBindVertexArray(uiVAOHeightmap);
    glBindBuffer(GL_ARRAY_BUFFER, uiVBOHeightmapData);

    float fHeights[HM_SIZE_X*HM_SIZE_Y] =
    {
        10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f,
        5.0f, 5.0f,5.0f, 10.0f, 10.0f, 10.0f,
        5.0f, 5.0f, 5.0f, 10.0f, 10.0f, 10.0f,
        5.0f, 5.0f, 10.0f, 10.0f, 10.0f, 10.0f,
        10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f,
        10.0f, 5.0f, 5.0f, 5.0f, 5.0f, 10.0f,
    };

    float fSizeX = 100.0f, fSizeZ = 100.0f;

    for (int i = 0; i<HM_SIZE_X*HM_SIZE_Y;i++)
    {
        for (int j = 0; j < HM_SIZE_X*HM_SIZE_Y; j++)
        {
            float column = float(i%HM_SIZE_X), row = float(i / HM_SIZE_X);
            vHeightmapData[i] = glm::vec3(
                -fSizeX / 2 + fSizeX*column / float(HM_SIZE_X - 1), // X Coordinate
                fHeights[i],                                    // Y Coordinate (it's height)
                -fSizeZ / 2 + fSizeZ*row / float(HM_SIZE_Y - 1) // Z Coordinate
            );
        }
    }

    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3)*HM_SIZE_X*HM_SIZE_Y, vHeightmapData, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glGenBuffers(1, &uiVBOIndices);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, uiVBOIndices);
    int iIndices[] =
    {
        0, 6, 1, 7, 2, 8, 3, 9, 4, 10, 5, 11, 36,
        6, 12, 7, 13, 8, 14, 9, 15, 10, 16, 11, 17, 36,
        12, 18, 13, 19, 14, 20, 15, 21, 16, 22, 17, 23, 36,
        18, 24, 19, 25, 20, 26, 21, 27, 22, 28, 23, 29, 36,
        24, 30, 25, 31, 26, 32, 27, 33, 28, 34, 29, 35
    };
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(iIndices), iIndices, GL_STATIC_DRAW);
    glEnable(GL_PRIMITIVE_RESTART);
    glPrimitiveRestartIndex(HM_SIZE_X*HM_SIZE_Y);
}

我还有一个相机课,我称之为getY功能。

HM_SIZE_X和HM_SIZE_Y均为6

0 个答案:

没有答案