feat: add ui renderer

This commit is contained in:
2026-03-21 11:19:23 +08:00
parent 3916a72ca3
commit c62f2baf7c
12 changed files with 204 additions and 36 deletions

View File

@@ -20,15 +20,16 @@ public:
private:
Camera m_camera;
TextureManager m_texture_manager;
World m_world;
Renderer m_renderer{m_camera, m_world};
Renderer m_renderer{m_camera, m_world, m_texture_manager};
Window m_window{m_renderer};
GLuint m_texture_array;
TextureManager m_texture_manager;
void init();

View File

@@ -3,7 +3,7 @@ constexpr int WORLD_SIZE_X = 32;
constexpr int WORLD_SIZE_Z = 32;
constexpr int WORLD_SIZE_Y = 16;
constexpr int MAX_BLOCK_NUM = 2;
constexpr int MAX_UI_NUM = 1;
constexpr int CHUCK_SIZE = 16;
constexpr int DISTANCE = 8;
@@ -82,4 +82,34 @@ constexpr int OUTLINE_CUBE_INDICES[24] = {
0,1, 1,2, 2,3, 3,0,
4,5, 5,6, 6,7, 7,4,
0,4, 1,5, 2,6, 3,7
};
};
constexpr float SQUARE_VERTICES[6][2] = {
-0.5f, -0.5f, // bottom left
-0.5f, 0.5f, // top left
0.5f, 0.5f, // top right
0.5f, 0.5f, // top right
0.5f, -0.5f, // bottom right
-0.5f, -0.5f // bottom left
};
constexpr float SQUARE_TEXTURE_POS[6][2] = {
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 0.0f,
};
struct Vertex {
float x, y, z;
float s, t;
float layer;
};
struct Vertex2D {
float x, y;
float s, t;
float layer;
};

View File

@@ -13,8 +13,4 @@ struct ChunkPos {
};
};
struct Vertex {
float x, y, z;
float s, t;
float layer;
};

View File

@@ -1,22 +1,27 @@
#pragma once
#include <Cubed/config.hpp>
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <vector>
class Camera;
class TextureManager;
class World;
class Renderer {
public:
constexpr static int NUM_VAO = 1;
Renderer(const Camera& camera, World& world);
Renderer(const Camera& camera, World& world, const TextureManager& texture_manager);
~Renderer();
void init();
void render(GLuint texture_array);
void update_proj_matrix(float aspect);
void render();
void update_proj_matrix(float aspect, float width, float height);
private:
const Camera& m_camera;
const TextureManager& m_texture_manager;
World& m_world;
glm::mat4 m_p_mat, m_v_mat, m_m_mat, m_mv_mat, m_mvp_mat;
@@ -27,13 +32,20 @@ private:
GLuint m_sky_vbo;
GLuint m_outline_indices_vbo;
GLuint m_outline_vbo;
GLuint m_ui_vbo;
GLuint m_sky_program;
GLuint m_outline_program;
GLuint m_ui_program;
GLuint m_world_program;
glm::mat4 m_ui_proj;
glm::mat4 m_ui_m_matrix;
std::vector<GLuint> m_vao;
std::vector<Vertex2D> m_ui;
void render_outline();
void render_sky();
void render_ui();
};

View File

@@ -6,19 +6,20 @@
class TextureManager {
private:
GLuint m_texture_array;
GLuint m_block_status_array;
void load_block_status(int status_id);
GLuint m_texture_array;
GLuint m_ui_array;
void load_block_status(unsigned status_id);
void load_block_texture(unsigned block_id);
void load_ui_texture(unsigned id);
public:
TextureManager();
~TextureManager();
void delet_texture();
GLuint get_block_status_array();
GLuint get_texture_array();
GLuint get_block_status_array() const;
GLuint get_texture_array() const;
GLuint get_ui_array() const;
// Must call after MapTable::init_map() and glfwMakeContextCurrent(window);
void init_texture();
};