what the mesh?!

This commit is contained in:
maelstrom 2024-09-27 20:12:45 +02:00
parent 7215fd5433
commit 75b5e3bf3c
3 changed files with 55 additions and 19 deletions

36
src/mesh.cpp Normal file
View file

@ -0,0 +1,36 @@
#include <GL/glew.h>
#include <GL/gl.h>
#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);
}

10
src/mesh.h Normal file
View file

@ -0,0 +1,10 @@
#pragma once
class Mesh {
unsigned int VBO, VAO;
public:
Mesh(int vertexCount, float* vertices);
~Mesh();
void bind();
};

View file

@ -1,38 +1,28 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <memory>
#include "shader.h" #include "shader.h"
#include "mesh.h"
#include "renderer.h" #include "renderer.h"
float verts[] { float verts[] {
0.5f, 1.0f, 0.0f, // position // normals // tex coords
0.0f, 0.0f, 0.0f, 0.5f, 1.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, 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; Shader *shader = NULL;
Mesh *triangleMesh = NULL;
void renderInit(GLFWwindow* window) { void renderInit(GLFWwindow* window) {
glViewport(0, 0, 500, 500); 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 // Compile shader
shader = new Shader("assets/shaders/orange.vs", "assets/shaders/orange.fs"); shader = new Shader("assets/shaders/orange.vs", "assets/shaders/orange.fs");
triangleMesh = new Mesh(3, verts);
} }
void render(GLFWwindow* window) { void render(GLFWwindow* window) {
@ -42,6 +32,6 @@ void render(GLFWwindow* window) {
// Use shader // Use shader
shader->use(); shader->use();
glBindVertexArray(VAO); triangleMesh->bind();
glDrawArrays(GL_TRIANGLES, 0, 3); glDrawArrays(GL_TRIANGLES, 0, 3);
} }