From c3e0f81a2881983d289a08ccda03683fb7d3137a Mon Sep 17 00:00:00 2001 From: Ravenwater Date: Thu, 2 Jan 2025 09:36:03 -0500 Subject: [PATCH] WIP: refactoring vertex info into separate position and color attributes --- src/shaders/fragment.glsl | 3 +-- src/shaders/vertex.glsl | 13 +++++++++---- src/triangle_mesh.cpp | 37 +++++++++++++++++++++++++------------ src/triangle_mesh.hpp | 8 ++++++-- 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/shaders/fragment.glsl b/src/shaders/fragment.glsl index d4d30c7..bb6e3ce 100644 --- a/src/shaders/fragment.glsl +++ b/src/shaders/fragment.glsl @@ -4,7 +4,6 @@ in vec3 fragmentColor; out vec4 screenColor; -void main() -{ +void main() { screenColor = vec4(fragmentColor, 1.0); } \ No newline at end of file diff --git a/src/shaders/vertex.glsl b/src/shaders/vertex.glsl index e9580e5..b4679d8 100644 --- a/src/shaders/vertex.glsl +++ b/src/shaders/vertex.glsl @@ -1,12 +1,17 @@ #version 330 core layout (location=0) in vec3 vertexPos; -layout (location=1) in vec3 vertexColor; +layout (location=1) in int colorIndex; out vec3 fragmentColor; -void main() -{ +const vec3 colors[3] = vec3[]( + vec3(1.0, 0.0, 0.0), + vec3(0.0, 1.0, 0.0), + vec3(0.0, 0.0, 1.0) +); + +void main() { gl_Position = vec4(vertexPos, 1.0); - fragmentColor = vertexColor; + fragmentColor = colors[colorIndex]; } \ No newline at end of file diff --git a/src/triangle_mesh.cpp b/src/triangle_mesh.cpp index 999a9a4..c5ee495 100644 --- a/src/triangle_mesh.cpp +++ b/src/triangle_mesh.cpp @@ -4,30 +4,43 @@ #include "triangle_mesh.hpp" TriangleMesh::TriangleMesh() { + vertex_count = 3; + + std::vector positions = { + -1.0f, -1.0f, 0.0f, + 1.0f, -1.0f, 0.0f, + -1.0f, 1.0f, 0.0f + }; - std::vector data = { - -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, - 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f + std::vector colorIndices = { + 0, 1, 2 }; - vertex_count = 3; glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); - glGenBuffers(1, &VBO); - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), data.data(), GL_STATIC_DRAW); + // Vertex Buffer objects + VBO.resize(2); + glGenBuffers(2, VBO.data()); // position - // vertexAttribPointer(index, size, type, normalized, stride, pointer) - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, (void*)0); + glBindBuffer(GL_ARRAY_BUFFER, VBO[0]); + glBufferData(GL_ARRAY_BUFFER, positions.size() * sizeof(float), positions.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); // color - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, (void*)12); + glBindBuffer(GL_ARRAY_BUFFER, VBO[1]); + glBufferData(GL_ARRAY_BUFFER, colorIndices.size() * sizeof(int), colorIndices.data(), GL_STATIC_DRAW); + // vertexAttribIPointer(index, size, type, stride, pointer) + glVertexAttribIPointer(1, 1, GL_INT, 4 /* stride to next attribute */, (void*)0); // <---- notice the attrib integer pointer function signature glEnableVertexAttribArray(1); + // 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<--... + // location=1 is glVertexAttribIPointer( -->1<--... } void TriangleMesh::draw() { @@ -37,5 +50,5 @@ void TriangleMesh::draw() { TriangleMesh::~TriangleMesh() { glDeleteVertexArrays(1, &VAO); - glDeleteBuffers(1, &VBO); + glDeleteBuffers(2, VBO.data()); } \ No newline at end of file diff --git a/src/triangle_mesh.hpp b/src/triangle_mesh.hpp index 31f0b3f..06bc622 100644 --- a/src/triangle_mesh.hpp +++ b/src/triangle_mesh.hpp @@ -1,3 +1,4 @@ +#include class TriangleMesh { public: @@ -8,7 +9,10 @@ class TriangleMesh { private: unsigned int vertex_count; - unsigned int VBO; // VBO - Vertex Buffer Object - unsigned int VAO; // VAO - Vertex Array Object + // VAO - Vertex Array Object + unsigned int VAO; + + // VBO - Vertex Buffer Objects + std::vector VBO; }; \ No newline at end of file