从侧面剪裁OpenGL顶点
OpenGL Vertices being clipped from the side
我的顶点被修剪在边缘上,如本相册所示:
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
,您将向相同的初始值添加更多(且明显更小)的数字;这将加剧您的浮点错误。
无论如何,为了获得可靠的行为,应该使用整数循环变量来确定循环终止。单独累积浮点坐标,这样就不需要精确的精度。
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 使用 OpenGL 4.5 更改所选顶点的颜色
- OpenGL 相机移动程序顶点着色器问题
- 如何使用OpenGL知道所有顶点的当前坐标
- 如何在 OpenGL 中跨基元重用顶点
- 使用OpenGL和Eigen来存储顶点数据和glVertexAttribPointer
- OpenGL 顶点数组对象与 tinyobjloader
- OpenGL 顶点缓冲区类重定义和模板方法错误
- OpenGL ES:在顶点着色器中计算顶点位置时性能不佳
- opengl 如何知道有多少个顶点?
- OpenGL 透视投影裁剪多边形与顶点在视锥体之外 = 错误的纹理映射?
- C++ / OpenGL 3.3+:未渲染顶点
- OpenGL 顶点未渲染
- OpenGL:动态更改加载的顶点
- 在 OpenGL 中,在保持良好帧速率的同时,VBO 中有多少个顶点的好目标是什么
- 我的OpenGL顶点阵列将其抽象成课程不再起作用
- OpenGL - 不渲染顶点/三角形
- opengl:两个不同的矢量可以绑定到同一个顶点数组对象吗
- 在 OpenGL 中,我可以访问我在顶点着色器中绘制的缓冲区吗?
- OpenGL 无法将变量从顶点着色器发送到片段着色器