Skip to content

Commit

Permalink
WIP: refactoring vertex info into separate position and color attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Jan 2, 2025
1 parent 73433cc commit c3e0f81
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
3 changes: 1 addition & 2 deletions src/shaders/fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ in vec3 fragmentColor;

out vec4 screenColor;

void main()
{
void main() {
screenColor = vec4(fragmentColor, 1.0);
}
13 changes: 9 additions & 4 deletions src/shaders/vertex.glsl
Original file line number Diff line number Diff line change
@@ -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];
}
37 changes: 25 additions & 12 deletions src/triangle_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,43 @@
#include "triangle_mesh.hpp"

TriangleMesh::TriangleMesh() {
vertex_count = 3;

std::vector<float> positions = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f
};

std::vector<float> 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<int> 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() {
Expand All @@ -37,5 +50,5 @@ void TriangleMesh::draw() {

TriangleMesh::~TriangleMesh() {
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(2, VBO.data());
}
8 changes: 6 additions & 2 deletions src/triangle_mesh.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <vector>

class TriangleMesh {
public:
Expand All @@ -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<unsigned int> VBO;
};

0 comments on commit c3e0f81

Please sign in to comment.