summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c26
1 files changed, 20 insertions, 6 deletions
diff --git a/main.c b/main.c
index 3e4754c..0ebccf8 100644
--- a/main.c
+++ b/main.c
@@ -40,12 +40,17 @@ int main(void) {
"FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
- // Triangle shenanigans
- // --------------------
+ // Triangle shenanigans and now two of them!
+ // -----------------------------------------
float verticies[] = {
- -0.5f, -0.5f, 0.0f, // left
- 0.5f, -0.5f, 0.0f, // right
- 0.0f, 0.5f, 0.0f // top
+ -0.5f, -0.5f, 0.0f, // bottom left
+ 0.5f, -0.5f, 0.0f, // bottom right
+ -0.5f, 0.5f, 0.0f, // top left
+ 0.5f, 0.5f, 0.0f // top right
+ };
+ unsigned int indices[] = {
+ 0, 1, 2, // first triangle
+ 2, 3, 1 // second triangle
};
@@ -130,6 +135,7 @@ int main(void) {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
+ glGenBuffers(1, &EBO);
// bind the vertex array object first, then bind and set vertex buffers, and then configure vertex attributes.
glBindVertexArray(VAO);
@@ -137,12 +143,19 @@ int main(void) {
// 0. copy our vertices array in a buffer for OpenGL to use
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticies), verticies, GL_STATIC_DRAW);
+
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
+
// 1. then set the vertex attributes pointer
// location of array, size of vertex attribute (vec3), type of data(float), normialition?, stride (space between data in array), offset of where the position data begins in the buffer
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
+ // uncomment this call to draw in wireframe polygons.
+ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+
// -------------------------------------------------------------------------
// render loop
// -------------------------------------------------------------------------
@@ -163,7 +176,7 @@ int main(void) {
// render triangle
glUseProgram(shader_program);
glBindVertexArray(VAO);
- glad_glDrawArrays(GL_TRIANGLES, 0, 3);
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// check and call events and swap the buffers
// ------------------------------------------
@@ -174,6 +187,7 @@ int main(void) {
// de-allocate all resources
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
+ glDeleteBuffers(1, &EBO);
glDeleteProgram(shader_program);
// Terminate program