refactor(render): move render files to subdirectory, add RAII vertex buffer/array classes

This commit is contained in:
2026-07-09 11:53:57 +08:00
parent b1341d7da4
commit 29642d7da2
16 changed files with 310 additions and 157 deletions

View File

@@ -6,7 +6,7 @@
#define GLFW_INCLUDE_NONE
#include "Cubed/camera.hpp"
#include "Cubed/dev_panel.hpp"
#include "Cubed/renderer.hpp"
#include "Cubed/render/renderer.hpp"
#include "Cubed/texture_manager.hpp"
#include "Cubed/window.hpp"
namespace Cubed {

View File

@@ -58,8 +58,8 @@ public:
void rebuild_world();
void push_delete_vbo(GLuint vbo);
void push_delete_vao(GLuint vao);
void push_delete_vbo(std::unique_ptr<VertexBuffer>& vbo);
void push_delete_vao(std::unique_ptr<VertexArray>& vao);
// void hot_reload();
// void rebuild_world();
@@ -134,8 +134,8 @@ private:
tbb::concurrent_queue<std::unique_ptr<ClientChunk>> m_pending_upload_queue;
tbb::concurrent_queue<ChunkPos> m_dirty_chunk_queue;
tbb::concurrent_queue<PendingSound> m_pending_sound;
std::vector<GLuint> m_pending_delete_vbo;
std::vector<GLuint> m_pending_delete_vao;
std::vector<std::unique_ptr<VertexBuffer>> m_pending_delete_vbo;
std::vector<std::unique_ptr<VertexArray>> m_pending_delete_vao;
std::deque<ChunkPos> m_dirty_queue;
std::vector<const ChunkRenderSnapshot*> m_render_snapshots;

View File

@@ -1,15 +1,18 @@
#pragma once
#include "Cubed/primitive_data.hpp"
#include "Cubed/render/vertex_array.hpp"
#include "Cubed/render/vertex_buffer.hpp"
#include <atomic>
#include <glad/glad.h>
#include <memory>
#include <vector>
namespace Cubed {
class ClientWorld;
struct VertexData {
std::vector<Vertex3D> m_vertices;
GLuint m_vbo = 0;
GLuint m_vao = 0;
std::unique_ptr<VertexBuffer> m_vbo;
std::unique_ptr<VertexArray> m_vao;
std::atomic<std::size_t> m_sum{0};
ClientWorld& m_world;
VertexData(ClientWorld& world);

View File

@@ -1,8 +1,10 @@
#pragma once
#include "Cubed/constants.hpp"
#include "Cubed/player_renderer.hpp"
#include "Cubed/primitive_data.hpp"
#include "Cubed/render/player_renderer.hpp"
#include "Cubed/render/vertex_array.hpp"
#include "Cubed/render/vertex_buffer.hpp"
#include "Cubed/shader.hpp"
#include "Cubed/ui/text.hpp"
@@ -126,12 +128,14 @@ private:
glm::mat4 m_p_mat, m_v_mat, m_m_mat, m_mv_mat, m_mvp_mat, m_norm_mat;
GLuint m_sky_vbo = 0;
GLuint m_text_vbo = 0;
GLuint m_outline_indices_vbo = 0;
GLuint m_outline_vbo = 0;
GLuint m_ui_vbo = 0;
GLuint m_player_vbo = 0;
std::unique_ptr<VertexBuffer> m_sky_vbo;
std::unique_ptr<VertexBuffer> m_text_vbo = 0;
std::unique_ptr<VertexBuffer> m_outline_indices_vbo = 0;
std::unique_ptr<VertexBuffer> m_outline_vbo = 0;
std::unique_ptr<VertexBuffer> m_ui_vbo = 0;
std::unique_ptr<VertexBuffer> m_player_vbo = 0;
std::unique_ptr<VertexBuffer> m_quad_vbo = 0;
GLuint m_fbo = 0;
GLuint m_screen_texture = 0;
GLuint m_screen_depth_texture = 0;
@@ -144,8 +148,6 @@ private:
GLuint m_depth_map_fbo = 0;
GLuint m_depth_map_texture = 0;
GLuint m_quad_vbo = 0;
glm::mat4 m_ui_proj;
glm::mat4 m_ui_m_matrix;
std::unordered_map<std::size_t, Shader> m_shaders;
@@ -184,7 +186,7 @@ private:
3 - ui vao
4 - text vao
*/
std::vector<GLuint> m_vao;
std::vector<VertexArray> m_vao;
std::vector<Vertex2D> m_ui;
void init_quad();

View File

@@ -0,0 +1,26 @@
#pragma once
#include <glad/glad.h>
namespace Cubed {
class VertexArray {
public:
VertexArray();
VertexArray(const VertexArray&) = delete;
VertexArray(VertexArray&&) noexcept;
VertexArray& operator=(const VertexArray&) = delete;
VertexArray& operator=(VertexArray&&) noexcept;
~VertexArray();
void bind() const;
static void unbind();
GLuint id() const;
void attribute(GLuint index, GLint size, GLenum type, GLsizei stride,
const void* ptr, bool normalized = false) const;
private:
GLuint m_vao = 0;
};
} // namespace Cubed

View File

@@ -0,0 +1,36 @@
#pragma once
#include <glad/glad.h>
namespace Cubed {
enum class BufferType : GLenum {
ARRAY_BUFFER = GL_ARRAY_BUFFER,
ELEMENT_ARRAY_BUFFER = GL_ELEMENT_ARRAY_BUFFER
};
enum class BufferUsage : GLenum {
STATIC_DRAW = GL_STATIC_DRAW,
DYNAMIC_DRAW = GL_DYNAMIC_DRAW
};
class VertexBuffer {
public:
VertexBuffer(BufferType type = BufferType::ARRAY_BUFFER);
VertexBuffer(const VertexBuffer&) = delete;
VertexBuffer(VertexBuffer&&) noexcept;
VertexBuffer& operator=(const VertexBuffer&) = delete;
VertexBuffer& operator=(VertexBuffer&&) noexcept;
~VertexBuffer();
void bind() const;
static void unbind();
GLuint id() const;
void buffer_data(const void* data, GLsizeiptr size,
BufferUsage usage = BufferUsage::STATIC_DRAW) const;
private:
GLuint m_vbo = 0;
BufferType m_type = BufferType::ARRAY_BUFFER;
GLenum get_buffer_target() const;
};
} // namespace Cubed