openglrenderervertexvertex-bufferindex-buffer

OpenGL Index Buffer Object element order incorrectly drawn


enter image description here

Notice how my program draws a single triangle, but instead what I am trying to express in code is to draw a square. My tri_indicies index buffer object I believe correctly orders these elements such that a square should be drawn, but when executing the program the draw order I have defined in the tri_indicies is not reflected in the window. Not sure if the error is rooted in tri_indicies, despite my changes to the element order not effecting my rendered output I want to believe it is here, but is it most likely somewhere else.

My program uses abstractions to notably the VertexBuffer, VertexArray, and IndexBuffer all detailed below.

const int buffer_object_size = 8;
const int index_buffer_object_size = 6;
float tri_verticies[buffer_object_size] = {
    -0.7f, -0.7f, // 0
     0.7f, -0.7f, // 1
     0.7f,  0.7f, // 2
    -0.7f,  0.7f  // 3
};
unsigned int tri_indicies[index_buffer_object_size] = {
    0, 1, 2,
    2, 3, 0
};

VertexArray vertexArray;                
VertexBuffer vertexBuffer(tri_verticies, buffer_object_size * sizeof(float)); // no call vertexBuffer.bind() constructor does it    

VertexBufferLayout vertexBufferLayout;
vertexBufferLayout.push<float>(3);
vertexArray.add_buffer(vertexBuffer, vertexBufferLayout);

IndexBuffer indexBuffer(tri_indicies, index_buffer_object_size);        

ShaderManager shaderManager;
ShaderSource shaderSource = shaderManager.parse_shader("BasicUniform.shader");  // ensure debug working dir is relative to $(ProjectDir)
unsigned int shader = shaderManager.create_shader(shaderSource.vertex_source, shaderSource.fragment_source);
MyGLCall(glUseProgram(shader));

Later in main I have a loop that is supposed to draw my square to the screen and fade the blue color value between 1.0f and 0.0f.

while (!glfwWindowShouldClose(window))
{
    MyGLCall(glClear(GL_COLOR_BUFFER_BIT));

    vertexArray.bind();
    indexBuffer.bind();

    MyGLCall(glDrawElements(GL_TRIANGLES, index_buffer_object_size, GL_UNSIGNED_INT, nullptr)); // nullptr since we bind buffers using glGenBuffers

    if (blue > 1.0f) {
        increment_color = -0.05f;
    }
    else if (blue < 0.0f) {
        increment_color = 0.05f;
    }

    blue += increment_color;

    glfwSwapBuffers(window);
    glfwPollEvents();
}

Solution

  • The array tri_verticies consists of vertex coordinates with 2 components (x, y). So the tuple size for the specification of the array of generic vertex attribute data has to be 2 rather than 3:

    vertexBufferLayout.push<float>(3);
    vertexBufferLayout.push<float>(2);

    What you actually do, is to specify an array with the following coordinates:

    -0.7, -0.7,  0.7 // 0
    -0.7,  0.7,  0.7 // 1
     ???,  ???,  ??? // 2
     ???,  ???,  ??? // 3
    

    In general out-of-bound access to buffer objects has undefined results.
    See OpenGL 4.6 API Core Profile Specification - 6.4 Effects of Accessing Outside Buffer Bounds, page 79