feat: vertex array class

This commit is contained in:
2025-10-30 18:31:48 +01:00
parent c6d83c1b51
commit 9b26cf909b
5 changed files with 41 additions and 14 deletions

View File

@ -35,6 +35,7 @@ namespace OpenGL {
BufferUsage m_usage;
};
// TODO: Implement custom fields structuring via ordered_map?
class ENGINE_API UniformBuffer : public Buffer {
public:
UniformBuffer(size_t size, unsigned int index);
@ -71,6 +72,16 @@ namespace OpenGL {
inline void StartConfigure() const { Bind(); }
inline void EndConfigure() const { Unbind(); }
};
class ENGINE_API VertexArray {
public:
VertexArray();
void Bind();
void Unbind();
private:
unsigned int m_id;
};
} // namespace OpenGL
} // namespace Core

View File

@ -6,18 +6,17 @@
#include <GL/glew.h>
#include "engine/renderer/basics.h"
#include "engine/opengl/buffers.h"
namespace Core {
class Mesh {
class Mesh : public OpenGL::VertexArray {
public: // TODO: abstract away
unsigned int m_vao, m_vbo, m_ebo;
unsigned int m_vbo, m_ebo;
std::vector<Vertex> m_vertexBuffer;
std::vector<unsigned int> m_indexBuffer;
public: // TODO: abstract away
void Bind() const { glBindVertexArray(m_vao); }
void Unbind() { glBindVertexArray(0); }
void Upload() const;
public:
void Upload();
public:
std::string materialName;
public:

View File

@ -1,3 +1,4 @@
#include <cassert>
#include "engine/opengl/buffers.h"
namespace Core {
@ -73,6 +74,22 @@ namespace OpenGL {
InstanceBuffer::InstanceBuffer(BufferUsage usage)
: ArrayBuffer(usage) {}
VertexArray::VertexArray() : m_id(0) {
glGenVertexArrays(1, &m_id);
}
void VertexArray::Bind() {
assert(m_id != 0 && "Vertex Array wasn't initialized.");
glBindVertexArray(m_id);
}
void VertexArray::Unbind() {
assert(m_id != 0 && "Vertex Array wasn't initialized.");
glBindVertexArray(0);
}
} // namespace OpenGL
} // namespace Core

View File

@ -5,15 +5,15 @@
namespace Core {
Mesh::Mesh() {
m_vao = 0;
// m_vao = 0;
m_vbo = 0;
m_ebo = 0;
glGenVertexArrays(1, &m_vao);
// glGenVertexArrays(1, &m_vao);
glGenBuffers(1, &m_vbo);
glGenBuffers(1, &m_ebo);
glBindVertexArray(m_vao);
Bind();
// VBO (vertex buffer)
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
@ -34,11 +34,11 @@ Mesh::Mesh() {
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
Unbind();
}
void Mesh::Upload() const {
glBindVertexArray(m_vao);
void Mesh::Upload() {
Bind();
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, m_vertexBuffer.size() * sizeof(Vertex), m_vertexBuffer.data(), GL_DYNAMIC_DRAW);
@ -47,7 +47,7 @@ void Mesh::Upload() const {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer.size() * sizeof(unsigned int), m_indexBuffer.data(), GL_DYNAMIC_DRAW);
glBindVertexArray(0);
Unbind();
}
void Mesh::Render(unsigned int count)

View File

@ -63,7 +63,7 @@ public:
assert(batchEntt.HasComponent<batch>() && "batch doesn't have any batch component!");
assert(batchEntt.HasComponent<mesh>() && "batch doesn't have any mesh component!");
// Generate 1000 random cubes
for (int i = 0; i < 10000; ++i) {
for (int i = 0; i < 100; ++i) {
auto cubeEntity = scene->CreateEntity();
float x = static_cast<float>(rand()) / RAND_MAX * 200.f - 100.f; // range [-100, 100]