从侧面剪裁OpenGL顶点

OpenGL Vertices being clipped from the side

本文关键字:顶点 OpenGL 剪裁 侧面      更新时间:2023-10-16

我的顶点被修剪在边缘上,如本相册所示:

https://i.stack.imgur.com/a03e7.jpg

当我的地形大小为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);
        }
    }
}

_index是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转换为_string进行调试,但数据看起来很好,我的projectionMatrix是:

glm::透视(glm::弧度(_fov),_aspRatio,0.1f,1000.0f);

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

这真的很奇怪,它能很好地处理小的行xcolumns而不是大的行,我真的不确定问题是什么。

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

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

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

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