Vertexbuffer混合了顶点
VertexBuffer getting the vertices mixed
我正在尝试从简单的2D渲染器更改为批处理渲染器,它几乎有效,除了似乎是第二个呼叫的事实使顶点混合在一起。我尝试更改所有内容,从缓冲区数据和属性转换为顶点和顶点本身,现在仍然可以使用代码,现在给定的位置(0,0(和大小(1,1(一个红色正方形,但在第二个红色正方形上,应该在第一个我得到一个红色三角形的情况下绘制,然后在第三个红色正方形,在第四个三角形等等。
着色器仅获取位置0 vec2,gl_position设置为。
#define RENDERER_MAX_SPRITES 10000
#define RENDERER_SPRITE_SIZE (sizeof(float) * 2)
#define RENDERER_BUFFER_SIZE (RENDERER_SPRITE_SIZE * 6 * RENDERER_MAX_SPRITES)
void BatchRenderer::Initialize() {
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, RENDERER_BUFFER_SIZE, nullptr, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, RENDERER_SPRITE_SIZE, (void*)0);
glBindVertexArray(0);
}
void BatchRenderer::Add(iVec2 position, iVec2 size, Color& color, Texture& texture) {
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
float vertices[12] = {
position.x, position.y,
position.x, position.y + size.y,
position.x + size.x, position.y + size.y,
position.x + size.x, position.y + size.y,
position.x + size.x, position.y,
position.x, position.y
};
glBufferSubData(GL_ARRAY_BUFFER, VertexSum, sizeof(vertices), &vertices);
VertexSum += 12;
VertexCount += 6;
glBindVertexArray(0);
}
void BatchRenderer::Flush() {
shader.Use();
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glDrawArrays(GL_TRIANGLES, 0, VertexCount);
glBindVertexArray(0);
VertexCount = 0;
VertexSum = 0;
}
对于第二个参数, glBufferSubData
期望字节中的偏移。这意味着,对于您Add
的每个六个顶点,偏移量应由6*2*sizeof(float)
提出,或等效于sizeof(vertices)
:
glBufferSubData(GL_ARRAY_BUFFER, VertexSum, sizeof(vertices), &vertices);
VertexSum += sizeof(vertices);
VertexCount += 6;
相关文章:
- 如何循环打印顶点结构
- 混合组合和继承的C++问题
- 在混合代码库中将C转换为C++时出现许多包含错误
- D3D11-将混合权重和索引传递到顶点着色器
- 从返回的顶点缓冲区查询顶点结构
- Vulkan 中的动态顶点缓冲区格式设置
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 如何将一半传递给顶点着色器?
- C++分数混合比较运算符错误
- 在 DirectX 11 中从 GPU 读回顶点缓冲区(并获取顶点)
- QT QOpenGLWidget:如何在不使用数据块复制的情况下修改VBO中的单个顶点值?
- 使用 OpenGL 4.5 更改所选顶点的颜色
- 是否可以混合使用SFINAE和模板专业化?
- 如何在 Python C++ 混合库中调试非确定性分段错误?
- 使用 Boost.Spirit 解析具有混合数据类型的 OBJ 文件?
- Direct3D 11 - HLSL - 获取顶点索引 ID
- 创建异构顶点数据数组的可移植方法
- 使用 glvertex4i 传递网格面索引时的顶点着色器错误
- Vertexbuffer混合了顶点
- OpenGL ES顶点着色器中的动态大小阵列;用于混合形状/变形目标