Draw quad

This commit is contained in:
Abdelrahman Said 2024-09-21 20:35:16 +01:00
parent d50a06d215
commit 63bdec4556

View File

@ -37,6 +37,10 @@ struct App {
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
GLuint vbo; 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; GLuint shader_program;
bool running; bool running;
}; };
@ -114,18 +118,32 @@ int init(App &app) {
void create_vertex_spec(App &app) { void create_vertex_spec(App &app) {
const std::vector<GLfloat> vertices = { const std::vector<GLfloat> vertices = {
-0.8f, -0.8f, 0.0f, // position // vert0
-0.5f, -0.5f, 0.0f, // position
1.0f, 0.0f, 0.0f, // colour 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, 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 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<GLuint> indices = {
// first triangle
0, 1, 2,
// second triangle
2, 1, 3,
}; };
// Create and activate the VAO // Create and activate the VAO
glGenVertexArrays(1, &app.vao); glGenVertexArrays(1, &app.vao);
glBindVertexArray(app.vao); glBindVertexArray(app.vao);
// Create and set up the VBO
glGenBuffers(1, &app.vbo); glGenBuffers(1, &app.vbo);
glBindBuffer(GL_ARRAY_BUFFER, app.vbo); glBindBuffer(GL_ARRAY_BUFFER, app.vbo);
glBufferData(GL_ARRAY_BUFFER, glBufferData(GL_ARRAY_BUFFER,
@ -134,6 +152,15 @@ void create_vertex_spec(App &app) {
GL_STATIC_DRAW 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); GLsizei vertex_stride = 6 * sizeof(GLfloat);
GLvoid *position_start_offset = (void *)0; GLvoid *position_start_offset = (void *)0;
GLvoid *colour_start_offset = (void *)(3 * sizeof(GLfloat)); GLvoid *colour_start_offset = (void *)(3 * sizeof(GLfloat));
@ -154,6 +181,7 @@ void create_vertex_spec(App &app) {
// drawn during the rendering stage // drawn during the rendering stage
glBindVertexArray(0); glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(0); glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1); glDisableVertexAttribArray(1);
} }
@ -190,8 +218,7 @@ void run_main_loop(App &app) {
// Draw call // Draw call
glBindVertexArray(app.vao); glBindVertexArray(app.vao);
glBindBuffer(GL_ARRAY_BUFFER, app.vbo); // Is this actually needed? glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void *)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
// End draw call // End draw call
// Not necessary if we only have one shader program // Not necessary if we only have one shader program