【问题标题】:VertexBuffer getting the vertices mixedVertexBuffer 得到混合的顶点
【发布时间】:2019-05-19 03:06:32
【问题描述】:

我正在尝试从简单的 2D 渲染器更改为批处理渲染器,它几乎可以工作,除了似乎第二次调用将顶点混合在一起的事实。我尝试更改所有内容,从 Buffer Data 和 AttribPointer 到 VertexCount 和顶点本身,现在仍然使用代码,给定位置 (0,0) 和大小 (1,1) 一个红色方块,但在第二个红色方块上应该画在第一个我得到一个红色三角形,然后在第三个相同的红色正方形和第四个三角形等等......

Shader 仅获取位置 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;
}

【问题讨论】:

  • RENDERER_BUFFER_SIZE 是什么?
  • 抱歉,刚刚在帖子中添加了#defines

标签: c++ opengl


【解决方案1】:

对于第二个参数,glBufferSubData 期望偏移量以字节为单位。这意味着对于您Add 的每六个顶点,偏移量应提前6*2*sizeof(float),或等效地,sizeof(vertices)

glBufferSubData(GL_ARRAY_BUFFER, VertexSum, sizeof(vertices), &vertices);
VertexSum += sizeof(vertices);
VertexCount += 6;

【讨论】:

    猜你喜欢
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多