Skip to content

Commit

Permalink
element drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Jan 2, 2025
1 parent c3e0f81 commit d72ba1c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/shaders/vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ const vec3 colors[3] = vec3[](
void main() {
gl_Position = vec4(vertexPos, 1.0);
fragmentColor = colors[colorIndex];
//fragmentColor = colors[gl_VertexID];
}
30 changes: 22 additions & 8 deletions src/triangle_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
#include "triangle_mesh.hpp"

TriangleMesh::TriangleMesh() {
vertex_count = 3;
vertex_count = 6;

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> 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<int> colorIndices = {
0, 1, 2
};
// 1st triangle: bottom left, bottom right, top left,
// 2nd triangle: top right, bottom right, top left
std::vector<int> elementIndices = {
//0, 1, 2, 2, 1, 3
0, 1, 2, 3, 1, 2
};

glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
Expand All @@ -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);
Expand All @@ -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<--...
Expand All @@ -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);
}
3 changes: 3 additions & 0 deletions src/triangle_mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit d72ba1c

Please sign in to comment.