feat(render): add model loading and rendering pipeline

This commit is contained in:
2026-07-24 18:35:09 +08:00
parent f4174724c6
commit 94fed9a810
20 changed files with 393 additions and 11 deletions

View File

@@ -3,6 +3,7 @@
#include "Cubed/audio/audio_engine.hpp"
#include "Cubed/config.hpp"
#include "Cubed/dev_panel.hpp"
#include "Cubed/render/model_manager.hpp"
#include "Cubed/render/renderer.hpp"
#include "Cubed/scene/scene_manager.hpp"
#include "Cubed/texture_manager.hpp"
@@ -49,7 +50,7 @@ private:
Window m_window;
TextureManager m_texture_manager;
ModelManager m_model_manager;
AudioEngine m_audio;
Renderer m_renderer;

View File

@@ -0,0 +1,21 @@
#pragma once
#include "Cubed/render/model_node.hpp"
#include "Cubed/tools/model_loader.hpp"
namespace Cubed {
class ModelManager {
public:
ModelManager();
ModelManager(const ModelManager&) = delete;
ModelManager(ModelManager&&) = delete;
ModelManager& operator=(const ModelManager&) = delete;
ModelManager& operator=(ModelManager&&) = delete;
~ModelManager();
const ModelNode& get_model(const std::string& model_name);
void init();
private:
ModelLoader m_loader;
std::unordered_map<std::string, ModelNode> m_models;
const ModelNode& load_model(const std::string& model_name);
};
} // namespace Cubed

View File

@@ -0,0 +1,42 @@
#pragma once
#include "Cubed/primitive_data.hpp"
#include "Cubed/render/texture.hpp"
#include "Cubed/render/vertex_array.hpp"
#include "Cubed/render/vertex_buffer.hpp"
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <memory>
#include <string>
namespace Cubed {
struct Mesh {
std::vector<Vertex3D> vertices;
std::vector<uint32_t> indices;
std::unique_ptr<VertexBuffer> vbo;
std::unique_ptr<VertexBuffer> ebo;
std::unique_ptr<VertexArray> vao;
std::unique_ptr<Texture> texture;
void upload() {
vao = std::make_unique<VertexArray>();
vao->bind();
vbo = std::make_unique<VertexBuffer>();
vbo->buffer_data(vertices.data(), vertices.size() * sizeof(Vertex3D));
ebo = std::make_unique<VertexBuffer>(BufferType::ELEMENT_ARRAY_BUFFER);
ebo->buffer_data(indices.data(), indices.size() * sizeof(uint32_t));
vao->attribute(0, 3, GL_FLOAT, sizeof(Vertex3D), (void*)0);
vao->attribute(1, 2, GL_FLOAT, sizeof(Vertex3D),
(void*)offsetof(Vertex3D, s));
vao->attribute(2, 3, GL_FLOAT, sizeof(Vertex3D),
(void*)offsetof(Vertex3D, nx));
};
};
struct ModelNode {
std::string name;
glm::mat4 transform{1.0f};
std::vector<Mesh> meshes;
std::vector<ModelNode> children;
};
} // namespace Cubed

View File

@@ -0,0 +1,22 @@
#pragma once
#include "Cubed/render/model_node.hpp"
#include "Cubed/shader.hpp"
#include <glm/glm.hpp>
namespace Cubed {
class Renderer;
class Camera;
class ModelRender {
public:
ModelRender(Renderer& renderer);
void render_model(const std::string& name, const glm::vec3& pos,
Camera& camera);
private:
Renderer& m_renderer;
void render_node(const ModelNode& node, const glm::mat4& parent,
const glm::mat4& view, const Shader& shader);
void render_mesh(const Mesh& mesh);
};
} // namespace Cubed

View File

@@ -4,6 +4,7 @@
#include "Cubed/constants.hpp"
#include "Cubed/input/event.hpp"
#include "Cubed/primitive_data.hpp"
#include "Cubed/render/model_renderer.hpp"
#include "Cubed/render/player_renderer.hpp"
#include "Cubed/render/shader_manager.hpp"
#include "Cubed/render/vertex_array.hpp"
@@ -19,12 +20,14 @@
namespace Cubed {
class TextureManager;
class ClientWorld;
class ModelManager;
class DevPanel;
class Renderer {
public:
constexpr static int NUM_VAO = 7;
Renderer(TextureManager& texture_manager, Config& config);
Renderer(TextureManager& texture_manager, Config& config,
ModelManager& model_manager);
~Renderer();
void reload_config();
void init();
@@ -79,9 +82,14 @@ public:
bool handle_event(const Event& e);
void render_model(const std::string& name, const glm::vec3& pos,
Camera& camera);
ModelManager& model_manager();
private:
TextureManager& m_texture_manager;
ModelManager& m_model_manager;
bool m_init = false;
float m_aspect = 0.0f;
@@ -118,6 +126,7 @@ private:
std::vector<Vertex2D> m_ui;
WorldRenderer m_world_renderer;
ModelRender m_model_renderer;
Config& m_config;
bool handle_window_resize_event(const WindowResizeEvent& e);

View File

@@ -38,9 +38,11 @@ enum TextureFormat : GLenum {
R8 = GL_R8,
RGB = GL_RGB,
RGBA8 = GL_RGBA8,
BGRA = GL_BGRA
};
// You need to set the texture scaling method, otherwise it will render as
// black.
class Texture {
public:
explicit Texture(TextureType type);

View File

@@ -0,0 +1,22 @@
#pragma once
#include "Cubed/render/model_node.hpp"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
namespace Cubed {
class ModelLoader {
public:
ModelLoader();
ModelNode load(const std::string& path);
private:
Assimp::Importer m_importer;
ModelNode process_node(aiNode* node, const aiScene* scene);
Mesh process_mesh(aiMesh* mesh, const aiScene* scene);
bool process_texture(Mesh& mesh, aiMaterial* material, const aiScene* scene,
aiTextureType type);
glm::mat4 convert_matrix(const aiMatrix4x4& matrix);
};
} // namespace Cubed

View File

@@ -47,7 +47,7 @@ bool check_opengl_error();
std::string read_shader_source(const std::string& file_path);
ImageData load_image_data(const std::string& tex_image_path,
bool check_exist = true);
bool check_exist = true, bool full_path = false);
} // namespace Tools