From 75b5e3bf3c16a6076296cce272c4d3e4f74bf114 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Fri, 27 Sep 2024 20:12:45 +0200 Subject: [PATCH] what the mesh?! --- src/mesh.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/mesh.h | 10 ++++++++++ src/renderer.cpp | 28 +++++++++------------------- 3 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 src/mesh.cpp create mode 100644 src/mesh.h diff --git a/src/mesh.cpp b/src/mesh.cpp new file mode 100644 index 0000000..6d8a1b6 --- /dev/null +++ b/src/mesh.cpp @@ -0,0 +1,36 @@ +#include +#include + +#include "mesh.h" + +Mesh::Mesh(int vertexCount, float *vertices) { + // Generate buffers + glGenBuffers(1, &VBO); + glGenVertexArrays(1, &VAO); + + // Bind vertex data to VBO + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, vertexCount * (3 + 3 + 2) * sizeof(float), vertices, GL_STATIC_DRAW); + + // Bind vertex attributes to VAO + glBindVertexArray(VAO); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(0 * sizeof(float))); + glEnableVertexAttribArray(0); + + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); + glEnableVertexAttribArray(1); + + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); + glEnableVertexAttribArray(2); +} + +Mesh::~Mesh() { + glDeleteBuffers(1, &VBO); + glDeleteVertexArrays(1, &VAO); +} + +void Mesh::bind() { + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBindVertexArray(VAO); +} \ No newline at end of file diff --git a/src/mesh.h b/src/mesh.h new file mode 100644 index 0000000..0b8d087 --- /dev/null +++ b/src/mesh.h @@ -0,0 +1,10 @@ +#pragma once + +class Mesh { + unsigned int VBO, VAO; + +public: + Mesh(int vertexCount, float* vertices); + ~Mesh(); + void bind(); +}; \ No newline at end of file diff --git a/src/renderer.cpp b/src/renderer.cpp index b9d38c8..be5fd73 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -1,38 +1,28 @@ #include #include #include -#include #include "shader.h" +#include "mesh.h" #include "renderer.h" float verts[] { - 0.5f, 1.0f, 0.0f, - 0.0f, 0.0f, 0.0f, - 1.0f, 0.0f, 0.0f, + // position // normals // tex coords + 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, }; -unsigned int VAO, VBO; Shader *shader = NULL; +Mesh *triangleMesh = NULL; void renderInit(GLFWwindow* window) { glViewport(0, 0, 500, 500); - glGenBuffers(1, &VBO); - glGenVertexArrays(1, &VAO); - - // Bind vertex data to VBO - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); - - // Bind vertex attributes to VAO - glBindVertexArray(VAO); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); - glEnableVertexAttribArray(0); - // Compile shader shader = new Shader("assets/shaders/orange.vs", "assets/shaders/orange.fs"); + + triangleMesh = new Mesh(3, verts); } void render(GLFWwindow* window) { @@ -42,6 +32,6 @@ void render(GLFWwindow* window) { // Use shader shader->use(); - glBindVertexArray(VAO); + triangleMesh->bind(); glDrawArrays(GL_TRIANGLES, 0, 3); } \ No newline at end of file