From e9f7e9a4241d905153951cc15b302a8db39bcac4 Mon Sep 17 00:00:00 2001 From: Moritz Ruge Date: Thu, 6 Aug 2026 17:38:21 +0200 Subject: triangle exercise 2 --- main.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index ff31c74..c4ef368 100644 --- a/main.c +++ b/main.c @@ -15,7 +15,7 @@ int main(void) { int w_width = 1280; int w_height = 820; int shader_success; - unsigned int VBO, VAO, EBO; + unsigned int VBO[2], VAO[2], EBO; // fragment shader unsigned int vertex_shader; unsigned int fragment_shader; @@ -52,6 +52,13 @@ int main(void) { 0.9f, 0.7f, 0.0f, // exercise 1 right 0.7f, 0.7f, 0.0f, // exercise 1 left }; + + // exercise 2 + float verticies2[] = { + -1.0f, -1.0f, 0.0f, // left + -0.8f, -1.0f, 0.0f, // rigth + -0.9f, -0.8f, 0.0f // top + }; unsigned int indices[] = { 0, 1, 2, // first triangle 2, 3, 1 // second triangle @@ -137,15 +144,15 @@ int main(void) { // set up vertex data (and buffers) and configure vertex attributes // ---------------------------------------------------------------- - glGenVertexArrays(1, &VAO); - glGenBuffers(1, &VBO); + glGenVertexArrays(2, VAO); + glGenBuffers(2, VBO); glGenBuffers(1, &EBO); // bind the vertex array object first, then bind and set vertex buffers, and then configure vertex attributes. - glBindVertexArray(VAO); + glBindVertexArray(VAO[0]); // 0. copy our vertices array in a buffer for OpenGL to use - glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBindBuffer(GL_ARRAY_BUFFER, VBO[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(verticies), verticies, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); @@ -157,13 +164,20 @@ int main(void) { glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); + // exercise 2: 2 VAO VBO + glBindVertexArray(VAO[1]); + glBindBuffer(GL_ARRAY_BUFFER, VBO[1]); + glBufferData(GL_ARRAY_BUFFER, sizeof(verticies2), verticies2, GL_STATIC_DRAW); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); // uncomment this call to draw in wireframe polygons. - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // ------------------------------------------------------------------------- // render loop @@ -184,9 +198,11 @@ int main(void) { // render triangle glUseProgram(shader_program); - glBindVertexArray(VAO); + glBindVertexArray(VAO[0]); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glDrawArrays(GL_TRIANGLES, 4, 6); + glBindVertexArray(VAO[1]); + glDrawArrays(GL_TRIANGLES, 0, 3); // check and call events and swap the buffers // ------------------------------------------ @@ -195,8 +211,8 @@ int main(void) { } // de-allocate all resources - glDeleteVertexArrays(1, &VAO); - glDeleteBuffers(1, &VBO); + glDeleteVertexArrays(2, VAO); + glDeleteBuffers(2, VBO); glDeleteBuffers(1, &EBO); glDeleteProgram(shader_program); -- cgit v1.2.3