mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
refactor: renderer (#29)
* refactor(render): move render files to subdirectory, add RAII vertex buffer/array classes * refactor(render): encapsulate OpenGL textures into Texture class * refactor(texture): encapsulate textures with unique_ptr and bind methods Replace raw GLuint framebuffer textures with std::unique_ptr<Texture>. Update TextureManager to return const Texture* instead of GLuint. Use Texture::bind() for active texture binding. Add RGBA16F and RGB formats. Add texture parameter methods. Update destructors accordingly. * refactor(render): use Texture, VertexBuffer, VertexArray classes * feat(render): add FrameBuffer class to encapsulate framebuffer operations * refactor(render): extract world rendering into WorldRenderer class * refactor(render): rename projection and model matrix members for clarity * chore(render): remove unused matrix members * fix(texture-manager): correct delet_texture typo and add null checks for textures
This commit is contained in:
@@ -5,64 +5,60 @@
|
||||
namespace Cubed {
|
||||
VertexData::VertexData(ClientWorld& world) : m_world(world) {}
|
||||
VertexData::~VertexData() {
|
||||
if (m_vbo != 0) {
|
||||
m_world.push_delete_vbo(m_vbo);
|
||||
}
|
||||
if (m_vao != 0) {
|
||||
m_world.push_delete_vao(m_vao);
|
||||
}
|
||||
|
||||
m_world.push_delete_vbo(m_vbo);
|
||||
|
||||
m_world.push_delete_vao(m_vao);
|
||||
}
|
||||
VertexData::VertexData(VertexData&& o) noexcept
|
||||
: m_vertices(std::move(o.m_vertices)), m_vbo(o.m_vbo), m_vao(o.m_vao),
|
||||
m_sum(o.m_sum.load()), m_world(o.m_world) {
|
||||
o.m_vbo = 0;
|
||||
o.m_sum = 0;
|
||||
o.m_vao = 0;
|
||||
}
|
||||
: m_vertices(std::move(o.m_vertices)), m_vbo(std::move(o.m_vbo)),
|
||||
m_vao(std::move(o.m_vao)), m_sum(o.m_sum.exchange(0)),
|
||||
m_world(o.m_world) {}
|
||||
VertexData& VertexData::operator=(VertexData&& o) noexcept {
|
||||
m_vbo = o.m_vbo;
|
||||
o.m_vbo = 0;
|
||||
m_sum = o.m_sum.load();
|
||||
o.m_sum = 0;
|
||||
if (this == &o) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
m_world.push_delete_vao(m_vao);
|
||||
m_world.push_delete_vbo(m_vbo);
|
||||
|
||||
m_vbo = std::move(o.m_vbo);
|
||||
m_vao = std::move(o.m_vao);
|
||||
|
||||
m_sum = o.m_sum.exchange(0);
|
||||
|
||||
m_vertices = std::move(o.m_vertices);
|
||||
m_vao = o.m_vao;
|
||||
o.m_vao = 0;
|
||||
|
||||
return *this;
|
||||
}
|
||||
void VertexData::upload() {
|
||||
if (m_vertices.size() == 0) {
|
||||
return;
|
||||
}
|
||||
if (m_vao == 0) {
|
||||
glGenVertexArrays(1, &m_vao);
|
||||
if (!m_vao) {
|
||||
m_vao = std::make_unique<VertexArray>();
|
||||
}
|
||||
if (m_vbo == 0) {
|
||||
glGenBuffers(1, &m_vbo);
|
||||
if (!m_vbo) {
|
||||
m_vbo = std::make_unique<VertexBuffer>();
|
||||
}
|
||||
glBindVertexArray(m_vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, m_vertices.size() * sizeof(Vertex3D),
|
||||
m_vertices.data(), GL_DYNAMIC_DRAW);
|
||||
m_vao->bind();
|
||||
m_vbo->buffer_data(m_vertices.data(), m_vertices.size() * sizeof(Vertex3D),
|
||||
BufferUsage::DYNAMIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex3D),
|
||||
(void*)offsetof(Vertex3D, s));
|
||||
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex3D),
|
||||
(void*)offsetof(Vertex3D, layer));
|
||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D),
|
||||
(void*)offsetof(Vertex3D, nx));
|
||||
glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex3D),
|
||||
(void*)offsetof(Vertex3D, roughness));
|
||||
glVertexAttribPointer(5, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D),
|
||||
(void*)offsetof(Vertex3D, tx));
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
glEnableVertexAttribArray(3);
|
||||
glEnableVertexAttribArray(4);
|
||||
glEnableVertexAttribArray(5);
|
||||
glBindVertexArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
m_vao->attribute(0, 3, GL_FLOAT, sizeof(Vertex3D), (void*)0);
|
||||
m_vao->attribute(1, 2, GL_FLOAT, sizeof(Vertex3D),
|
||||
(void*)offsetof(Vertex3D, s));
|
||||
m_vao->attribute(2, 1, GL_FLOAT, sizeof(Vertex3D),
|
||||
(void*)offsetof(Vertex3D, layer));
|
||||
m_vao->attribute(3, 3, GL_FLOAT, sizeof(Vertex3D),
|
||||
(void*)offsetof(Vertex3D, nx));
|
||||
m_vao->attribute(4, 1, GL_FLOAT, sizeof(Vertex3D),
|
||||
(void*)offsetof(Vertex3D, roughness));
|
||||
m_vao->attribute(5, 3, GL_FLOAT, sizeof(Vertex3D),
|
||||
(void*)offsetof(Vertex3D, tx));
|
||||
|
||||
VertexArray::unbind();
|
||||
VertexBuffer::unbind();
|
||||
|
||||
// Release memory
|
||||
m_vertices.clear();
|
||||
|
||||
Reference in New Issue
Block a user