从侧面剪切OpenGL顶点

时间:2016-04-06 17:00:17

标签: c++ opengl opengl-3

我的顶点被剪裁在边缘上,如此专辑所示:

http://imgur.com/a/VkCrJ

当我的地形大小为400 x 400时,我会得到剪裁,但是在40x40或更低的情况下,我没有得到任何剪辑。 这是我填写职位和指数的代码:

void Terrain::fillPosition()
{
  //start from the top right and work your way down to 1,1
  double x = -1, y = 1, z = 1;
  float rowValue = static_cast<float>((1.0f / _rows) * 2.0); // .05 if 40
  float colValue = static_cast<float>((1.0f / _columns) * 2.0); // .05 if 40

 for (y; y > -1; y -= colValue)
 {
    for (x; x < 1; x += rowValue)
    {
        _vertexPosition.emplace_back(glm::vec3(x, y, z));
    }
    x = -1;
 }
}

这恰当地确定了我的立场,我已经用GL_POINTS对其进行了测试。它可以在400x400和40x40以及其他值之间正常工作。 索引代码:

void Terrain::fillIndices()
{
    glm::ivec3 triangle1, triangle2;
    for (int y = 0; y < _columns - 1; y++)
    {
        for (int x = 0; x < _rows - 1; x++)
        {
            // Triangle 1
            triangle1.x = x       + y       * _rows;
            triangle1.y = x       + (y + 1) * _rows;
            triangle1.z =(x + 1)  + y       * _rows;
            // Triangle 2
            triangle2.x = triangle1.y;
            triangle2.y = (x + 1) + (y + 1) * _rows;
            triangle2.z = triangle1.z;

            // add our data to the vector
            _indices.emplace_back(triangle1.x);
            _indices.emplace_back(triangle1.y);
            _indices.emplace_back(triangle1.z);

            _indices.emplace_back(triangle2.x);
            _indices.emplace_back(triangle2.y);
            _indices.emplace_back(triangle2.z);
        }
    }
}

_indices是std :: vector。我不确定导致这种情况的原因,但我很确定这是我填写指数的方式。啮合。我重新编写了算法,最终得到了相同的结果,小值完全正常,超过~144的大值被修剪。我像这样填充我的缓冲区:

void Terrain::loadBuffers()
{
    // generate the buffers and vertex arrays
    glGenVertexArrays(1, &_vao);
    glGenBuffers(1, &_vbo);
    glGenBuffers(1, &_ebo);
    // bind the vertex array
    glBindVertexArray(_vao);
    // bind the buffer to the vao
    glBindBuffer(GL_ARRAY_BUFFER, _vbo);
    glBufferData(GL_ARRAY_BUFFER, _vertexPosition.size() * sizeof(_vertexPosition[0]), _vertexPosition.data(), GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, _indices.size() * sizeof(_indices[0]), _indices.data(), GL_STATIC_DRAW);
    // enable the shader locations
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
    // unbind our data
    glBindVertexArray(0);
}

和我的平局电话:

void Terrain::renderTerrain(ResourceManager& manager, ResourceIdTextures id)
{
    // set the active texture
    glActiveTexture(GL_TEXTURE0);
    // bind our texture
    glBindTexture(GL_TEXTURE_2D, manager.getTexture(id).getTexture());
    _shaders.use();
    // send data the our uniforms
    glUniformMatrix4fv(_modelLoc, 1, GL_FALSE, glm::value_ptr(_model));
    glUniformMatrix4fv(_viewLoc, 1, GL_FALSE, glm::value_ptr(_view));
    glUniformMatrix4fv(_projectionLoc, 1, GL_FALSE, glm::value_ptr(_projection));
    glUniform1i(_textureLoc, 0);
    glBindVertexArray(_vao);
    // Draw our terrain;
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glDrawElements(GL_TRIANGLES, _indices.size(), GL_UNSIGNED_INT, 0);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glBindVertexArray(0);
    _shaders.unuse();
}

我认为这是因为我对模型的转换,所以我删除了所有转换并且它的结果相同。我通过强制转换glm :: vec3 to_string来尝试调试,但数据看起来很好,我的projectionMatrix是:

glm :: perspective(glm :: radians(_fov),_ aspRatio,0.1f,1000.0f);

所以我怀疑这是我做剪辑的观点。 _aspRatio是16/9。

奇怪的是它适用于小行x列而不是大行,我真的不确定问题是什么。

1 个答案:

答案 0 :(得分:2)

我会检查_vertexPosition的长度;我怀疑问题是你是(取决于_rows的数量)在你的内循环结束时产生一个额外的点(以及你的外循环,取决于_columns)。

原因是顶点循环的终止条件取决于浮点数学的确切行为。具体来说,您将范围[-1,1]划分为_rows段,然后将它们一起添加并将其用作终止测试。目前还不清楚你是否期望最终点(每个内环产生_rows+1个点)或不产生(产生一个不覆盖整个[-1,1]范围的矩形)。不幸的是,浮点并不精确,所以这是一个不可靠行为的方法:根据浮点错误的方向,你可能得到一个或另一个。

对于更大数量的_rows,您将更多(并且显着更小)的数字添加到相同的初始值;这会加剧你的浮点错误。

无论如何,为了获得可靠的行为,您应该使用整数循环变量来确定循环终止。单独累积浮点坐标,这样就不需要精确的精度。

相关问题