From d72ba1cf31286b55db5a156c8a17c6b1cc2af917 Mon Sep 17 00:00:00 2001 From: Ravenwater Date: Thu, 2 Jan 2025 11:30:12 -0500 Subject: [PATCH] element drawing --- src/shaders/vertex.glsl | 1 + src/triangle_mesh.cpp | 30 ++++++++++++++++++++++-------- src/triangle_mesh.hpp | 3 +++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/shaders/vertex.glsl b/src/shaders/vertex.glsl index b4679d8..1a5002a 100644 --- a/src/shaders/vertex.glsl +++ b/src/shaders/vertex.glsl @@ -14,4 +14,5 @@ const vec3 colors[3] = vec3[]( void main() { gl_Position = vec4(vertexPos, 1.0); fragmentColor = colors[colorIndex]; + //fragmentColor = colors[gl_VertexID]; } \ No newline at end of file diff --git a/src/triangle_mesh.cpp b/src/triangle_mesh.cpp index c5ee495..dcd012c 100644 --- a/src/triangle_mesh.cpp +++ b/src/triangle_mesh.cpp @@ -4,17 +4,24 @@ #include "triangle_mesh.hpp" TriangleMesh::TriangleMesh() { - vertex_count = 3; + vertex_count = 6; - std::vector positions = { - -1.0f, -1.0f, 0.0f, - 1.0f, -1.0f, 0.0f, - -1.0f, 1.0f, 0.0f + std::vector vertexCoordinates = { + -1.0f, -1.0f, 0.0f, // bottom left + 1.0f, -1.0f, 0.0f, // bottom right + -1.0f, 1.0f, 0.0f, // top left + 1.0f, 1.0f, 0.0f, // top right }; - + std::vector colorIndices = { 0, 1, 2 }; + // 1st triangle: bottom left, bottom right, top left, + // 2nd triangle: top right, bottom right, top left + std::vector elementIndices = { + //0, 1, 2, 2, 1, 3 + 0, 1, 2, 3, 1, 2 + }; glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); @@ -25,7 +32,7 @@ TriangleMesh::TriangleMesh() { // position glBindBuffer(GL_ARRAY_BUFFER, VBO[0]); - glBufferData(GL_ARRAY_BUFFER, positions.size() * sizeof(float), positions.data(), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, vertexCoordinates.size() * sizeof(float), vertexCoordinates.data(), GL_STATIC_DRAW); // vertexAttribPointer(index, size, type, normalized, stride, pointer) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 12 /* stride to next attribute */, (void*)0); glEnableVertexAttribArray(0); @@ -37,6 +44,11 @@ TriangleMesh::TriangleMesh() { glVertexAttribIPointer(1, 1, GL_INT, 4 /* stride to next attribute */, (void*)0); // <---- notice the attrib integer pointer function signature glEnableVertexAttribArray(1); + // element buffer + glGenBuffers(1, &EBO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementIndices.size() * sizeof(int), elementIndices.data(), GL_STATIC_DRAW); + // the VBO handles are stored in the VAO // and the specific VBO, that is VBO[0] and VBO[1] are tied to the location 0 and 1 of the vertex shader // location=0 is glVertexAttribPointer( -->0<--... @@ -45,10 +57,12 @@ TriangleMesh::TriangleMesh() { void TriangleMesh::draw() { glBindVertexArray(VAO); - glDrawArrays(GL_TRIANGLES, 0, vertex_count); + //glDrawArrays(GL_TRIANGLES, 0, vertex_count); // if you draw using glDrawArrays, you don't need to bind the EBO + glDrawElements(GL_TRIANGLES, vertex_count, GL_UNSIGNED_INT, 0); } TriangleMesh::~TriangleMesh() { glDeleteVertexArrays(1, &VAO); glDeleteBuffers(2, VBO.data()); + glDeleteBuffers(1, &EBO); } \ No newline at end of file diff --git a/src/triangle_mesh.hpp b/src/triangle_mesh.hpp index 06bc622..34e9f88 100644 --- a/src/triangle_mesh.hpp +++ b/src/triangle_mesh.hpp @@ -10,6 +10,9 @@ class TriangleMesh { private: unsigned int vertex_count; + // EOB - Element Buffer Object + unsigned int EBO; + // VAO - Vertex Array Object unsigned int VAO;