diff --git a/src/main.cc b/src/main.cc index 2529983..22d1974 100644 --- a/src/main.cc +++ b/src/main.cc @@ -37,6 +37,10 @@ struct App { // ----------------------------------------------------------------------------------- GLuint vbo; + // Holds the indices of the vertices that draw each triangle. Each triangle constitutes what + // OpenGL refers to as element, hence the name Element Buffer Object + GLuint ebo; + GLuint shader_program; bool running; }; @@ -114,18 +118,32 @@ int init(App &app) { void create_vertex_spec(App &app) { const std::vector vertices = { - -0.8f, -0.8f, 0.0f, // position + // vert0 + -0.5f, -0.5f, 0.0f, // position 1.0f, 0.0f, 0.0f, // colour - 0.8f, -0.8f, 0.0f, // position + // vert1 + 0.5f, -0.5f, 0.0f, // position 0.0f, 1.0f, 0.0f, // colour - 0.0f, 0.8f, 0.0f, // position + // vert2 + -0.5f, 0.5f, 0.0f, // position 0.0f, 0.0f, 1.0f, // colour + // vert3 + 0.5f, 0.5f, 0.0f, // position + 1.0f, 1.0f, 0.0f, // colour + }; + + const std::vector indices = { + // first triangle + 0, 1, 2, + // second triangle + 2, 1, 3, }; // Create and activate the VAO glGenVertexArrays(1, &app.vao); glBindVertexArray(app.vao); + // Create and set up the VBO glGenBuffers(1, &app.vbo); glBindBuffer(GL_ARRAY_BUFFER, app.vbo); glBufferData(GL_ARRAY_BUFFER, @@ -134,6 +152,15 @@ void create_vertex_spec(App &app) { GL_STATIC_DRAW ); + // Create and set up the EBO + glGenBuffers(1, &app.ebo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, app.ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, + indices.size() * sizeof(GLuint), + (void *)indices.data(), + GL_STATIC_DRAW + ); + GLsizei vertex_stride = 6 * sizeof(GLfloat); GLvoid *position_start_offset = (void *)0; GLvoid *colour_start_offset = (void *)(3 * sizeof(GLfloat)); @@ -154,6 +181,7 @@ void create_vertex_spec(App &app) { // drawn during the rendering stage glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); } @@ -190,8 +218,7 @@ void run_main_loop(App &app) { // Draw call glBindVertexArray(app.vao); - glBindBuffer(GL_ARRAY_BUFFER, app.vbo); // Is this actually needed? - glDrawArrays(GL_TRIANGLES, 0, 3); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void *)0); // End draw call // Not necessary if we only have one shader program