顶点缓存器获取的顶点混乱

3
我正尝试从简单的2D渲染器转换为批处理渲染器,它基本上可以工作,但似乎第二次调用时顶点被混合了。我尝试更改一切,从缓冲数据和AttribPointer到顶点计数和顶点本身,但仍然无法解决问题。当前代码给定位置(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;
}

RENDERER_BUFFER_SIZE是什么? - Yakov Galka
抱歉,刚刚在帖子中添加了 #define。 - Aether
1个回答

4
对于第二个参数,glBufferSubData期望以字节为单位的偏移量。这意味着对于每六个顶点,您需要Add该偏移量应该增加6*2*sizeof(float),或者等同于sizeof(vertices)
glBufferSubData(GL_ARRAY_BUFFER, VertexSum, sizeof(vertices), &vertices);
VertexSum += sizeof(vertices);
VertexCount += 6;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接