C++ 从侧面剪裁的OpenGL顶点

C++ 从侧面剪裁的OpenGL顶点,c++,opengl,opengl-3,C++,Opengl,Opengl 3,我将在边上剪裁顶点,如本相册所示: 当我的地形尺寸为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>(

我将在边上剪裁顶点,如本相册所示:

当我的地形尺寸为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;
 }
}
还有我的抽签电话:

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

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

所以我怀疑剪辑是我的观点_比例为16/9


奇怪的是,它可以很好地处理较小的行和列,而不是较大的列,我真的不确定问题出在哪里。

我会检查
\u
的长度;我怀疑问题在于(取决于
\u行数
)在内部循环(以及外部循环,取决于
\u列
)的末尾生成了一个额外的点

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

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


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

就是这样,谢谢,我不应该使用浮点计数器,因为它的四舍五入值非常小(1/400);谢谢你的帮助^^
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();
}