OpenGL Vertex Array Object failing to bind Vertex Buffer [duplicate]

不想你离开。 提交于 2020-05-09 16:00:58

问题


I am learning OpenGL through https://learnopengl.com/. From that site, and my research on the internet while trying to solve this problem, I have learned that Vertex Array Objects are useful because you can bind one with glBindVertexArray, and

  1. Any associated attributes will be setup.
  2. The buffer bound to GL_ARRAY_BUFFER will be automatically be bound.
  3. The associated GL_ELEMENT_ARRAY_BUFFER will also be automatically be bound.

Thus, I would expect the following setup code to setup two vertex array objects that each have their own array buffers that will automatically get bound when the vertex array object is bound. Note that this is essentially two copies of the same code. Also, this is a code snippet of Zig, which is pretty similar to C.

{
    glGenBuffers(1, &rectangle_vertex_buffer_object);

    glGenBuffers(1, &rectangle_element_buffer_object);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, rectangle_element_buffer_object);
    var fill_indices = [_]u32{ 0, 1, 2, 1, 2, 3 };
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, fill_indices.len * @sizeOf(u32), &fill_indices, GL_STATIC_DRAW);

    glGenVertexArrays(1, &rectangle_vertex_array_object);
    glBindVertexArray(rectangle_vertex_array_object);
    glBindBuffer(GL_ARRAY_BUFFER, rectangle_vertex_buffer_object);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, rectangle_element_buffer_object);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * @sizeOf(f32), @intToPtr(*allowzero c_void, 0));
    glEnableVertexAttribArray(0);
    glBindVertexArray(0);
}

{
    glGenBuffers(1, &glyph_vertex_buffer_object);

    glGenBuffers(1, &glyph_element_buffer_object);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, glyph_element_buffer_object);
    var fill_indices = [_]u32{ 0, 1, 2, 1, 2, 3 };
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, fill_indices.len * @sizeOf(u32), &fill_indices, GL_STATIC_DRAW);

    glGenVertexArrays(1, &glyph_vertex_array_object);
    glBindVertexArray(glyph_vertex_array_object);
    glBindBuffer(GL_ARRAY_BUFFER, glyph_vertex_buffer_object);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, glyph_element_buffer_object);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * @sizeOf(f32), @intToPtr(*allowzero c_void, 0));
    glEnableVertexAttribArray(0);
    glBindVertexArray(0);
}

I have a function for drawing rectangles:

pub fn fillRectangle(x: f32, y: f32, width: f32, height: f32) void {
    var flip_y = -y;
    glBindVertexArray(rectangle_vertex_array_object);
    // glBindBuffer(GL_ARRAY_BUFFER, rectangle_vertex_buffer_object); -- Stuff works if I uncomment this line.
    const vertices = [_]f32{
        x,         flip_y,
        x + width, flip_y,
        x,         flip_y - height,
        x + width, flip_y - height,
    };
    glBufferData(GL_ARRAY_BUFFER, vertices.len * @sizeOf(f32), &vertices, GL_STATIC_DRAW);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, @intToPtr(*allowzero c_void, 0));
    glBindVertexArray(0);
}

If I explicitly call glBindBuffer after glBindVertexArray, then the function works just fine, but if I remove it, then I get GL_INVALID_OPERATION in glBufferData(no buffer bound). However, I would expect the glBindVertexArray call to bind the appropriate buffer.

Help!

来源:https://stackoverflow.com/questions/61647551/opengl-vertex-array-object-failing-to-bind-vertex-buffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!