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:
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
46
include/Cubed/render/frame_buffer.hpp
Normal file
46
include/Cubed/render/frame_buffer.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
#include "Cubed/render/texture.hpp"
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <span>
|
||||
|
||||
enum class Attachment : GLenum {
|
||||
COLOR_ATTACHMENT0 = GL_COLOR_ATTACHMENT0,
|
||||
COLOR_ATTACHMENT1 = GL_COLOR_ATTACHMENT1,
|
||||
DEPTH_ATTACHMENT = GL_DEPTH_ATTACHMENT
|
||||
};
|
||||
|
||||
enum class FrameBufferType : GLenum {
|
||||
FRAMEBUFFER = GL_FRAMEBUFFER,
|
||||
READ_FRAMEBUFFER = GL_READ_FRAMEBUFFER,
|
||||
DRAW_FRAMEBUFFER = GL_DRAW_FRAMEBUFFER
|
||||
};
|
||||
|
||||
namespace Cubed {
|
||||
class FrameBuffer {
|
||||
public:
|
||||
FrameBuffer();
|
||||
~FrameBuffer();
|
||||
FrameBuffer(const FrameBuffer&) = delete;
|
||||
FrameBuffer(FrameBuffer&&) noexcept;
|
||||
FrameBuffer& operator=(const FrameBuffer&) = delete;
|
||||
FrameBuffer& operator=(FrameBuffer&&) noexcept;
|
||||
|
||||
void bind(FrameBufferType type = FrameBufferType::FRAMEBUFFER) const;
|
||||
GLuint id() const;
|
||||
|
||||
void attach(Attachment attachment, const Texture& texture,
|
||||
GLint level = 0) const;
|
||||
static void unbind();
|
||||
|
||||
bool check_status() const;
|
||||
|
||||
void draw_buffer(GLenum buf) const;
|
||||
void read_buffer(GLenum src) const;
|
||||
void draw_buffer(GLsizei n, const GLenum* bufs) const;
|
||||
void draw_buffer(std::span<const GLenum> bufs) const;
|
||||
|
||||
private:
|
||||
GLuint m_fbo = 0;
|
||||
};
|
||||
} // namespace Cubed
|
||||
125
include/Cubed/render/renderer.hpp
Normal file
125
include/Cubed/render/renderer.hpp
Normal file
@@ -0,0 +1,125 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cubed/constants.hpp"
|
||||
#include "Cubed/primitive_data.hpp"
|
||||
#include "Cubed/render/player_renderer.hpp"
|
||||
#include "Cubed/render/shader_manager.hpp"
|
||||
#include "Cubed/render/vertex_array.hpp"
|
||||
#include "Cubed/render/vertex_buffer.hpp"
|
||||
#include "Cubed/render/world_renderer.hpp"
|
||||
#include "Cubed/shader.hpp"
|
||||
#include "Cubed/ui/text.hpp"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <vector>
|
||||
namespace Cubed {
|
||||
|
||||
class Camera;
|
||||
class TextureManager;
|
||||
class ClientWorld;
|
||||
class DevPanel;
|
||||
class Renderer {
|
||||
public:
|
||||
constexpr static int NUM_VAO = 7;
|
||||
|
||||
Renderer(const Camera& camera, ClientWorld& world,
|
||||
const TextureManager& texture_manager, DevPanel& dev_panel);
|
||||
~Renderer();
|
||||
void hot_reload();
|
||||
void init(bool debug_on);
|
||||
const Shader& get_shader(const std::string& name) const;
|
||||
void render();
|
||||
void update(float delta_time);
|
||||
void update_fov(float fov);
|
||||
void update_proj_matrix(float aspect, float width, float height);
|
||||
void updata_framebuffer(int width, int height);
|
||||
float& ambient_strength();
|
||||
|
||||
bool& discard_transparent();
|
||||
bool& shader_on();
|
||||
bool& water_perturb();
|
||||
bool& water_depth_fade();
|
||||
bool& pbr();
|
||||
bool& flip_y();
|
||||
int& shadow_mode();
|
||||
int& light_cull_face();
|
||||
int& light_size_uv();
|
||||
float& min_radius();
|
||||
float& max_radius();
|
||||
int& samples();
|
||||
float& specular_strength();
|
||||
float& cloud_speed();
|
||||
float& cloud_threshold_low();
|
||||
float& cloud_threshold_high();
|
||||
float& refract_strength();
|
||||
|
||||
float& underwater_fog_density();
|
||||
float& water_density();
|
||||
|
||||
const Camera& camera() const;
|
||||
const ClientWorld& world() const;
|
||||
ClientWorld& world();
|
||||
const glm::mat4& world_proj_matrix() const;
|
||||
const TextureManager& texture_mamger() const;
|
||||
float delta_time() const;
|
||||
|
||||
float height() const;
|
||||
float width() const;
|
||||
const glm::mat4& p_mat() const;
|
||||
|
||||
const std::vector<VertexArray>& vao() const;
|
||||
|
||||
private:
|
||||
const Camera& m_camera;
|
||||
DevPanel& m_dev_panel;
|
||||
const TextureManager& m_texture_manager;
|
||||
ClientWorld& m_world;
|
||||
|
||||
bool m_init = false;
|
||||
|
||||
float m_aspect = 0.0f;
|
||||
float m_fov = DEFAULT_FOV;
|
||||
|
||||
float m_delta_time = 0.0f;
|
||||
|
||||
float m_width = 0.0f;
|
||||
float m_height = 0.0f;
|
||||
|
||||
glm::mat4 m_world_proj_matrix;
|
||||
|
||||
std::unique_ptr<VertexBuffer> m_sky_vbo;
|
||||
std::unique_ptr<VertexBuffer> m_outline_indices_vbo;
|
||||
std::unique_ptr<VertexBuffer> m_outline_vbo;
|
||||
std::unique_ptr<VertexBuffer> m_ui_vbo;
|
||||
std::unique_ptr<VertexBuffer> m_player_vbo;
|
||||
std::unique_ptr<VertexBuffer> m_quad_vbo;
|
||||
|
||||
glm::mat4 m_ui_proj_matrix;
|
||||
glm::mat4 m_ui_model_matrix;
|
||||
ShaderManager m_shaders;
|
||||
|
||||
/*
|
||||
0 - quad vao
|
||||
1 - sky vao
|
||||
2 - outline vao
|
||||
3 - ui vao
|
||||
4 - text vao
|
||||
*/
|
||||
std::vector<VertexArray> m_vao;
|
||||
std::vector<Vertex2D> m_ui;
|
||||
|
||||
WorldRenderer m_world_renderer;
|
||||
|
||||
void init_quad();
|
||||
void init_text();
|
||||
|
||||
void day_night_calculation();
|
||||
|
||||
void render_sky();
|
||||
void render_text();
|
||||
void render_ui();
|
||||
|
||||
void render_dev_panel();
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
21
include/Cubed/render/renderer_constants.hpp
Normal file
21
include/Cubed/render/renderer_constants.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
constexpr glm::vec3 SUN_COLOR{1.00f, 0.95f, 0.80f};
|
||||
constexpr glm::vec3 MOON_COLOR{0.75f, 0.80f, 1.00f};
|
||||
|
||||
constexpr glm::vec3 SUNSET_SUNLIGHT_COLOR{1.00f, 0.45f, 0.15f};
|
||||
constexpr glm::vec3 NOON_SUNLIGHT_COLOR{1.00f, 0.90f, 0.65f};
|
||||
constexpr glm::vec3 SUNSET_AMBIENT_COLOR{0.18f, 0.12f, 0.35f};
|
||||
constexpr glm::vec3 NOON_AMBIENT_COLOR{0.35f, 0.50f, 0.85f};
|
||||
constexpr glm::vec3 MOONLIGHT_COLOR{0.55f, 0.70f, 1.00f};
|
||||
constexpr glm::vec3 NIGHT_AMBIENT_COLOR{0.08f, 0.10f, 0.18f};
|
||||
constexpr float FAR_PLANE = 1000.0f;
|
||||
constexpr float NEAR_PLANE = 0.1f;
|
||||
constexpr float SUN_SIZE = 50.0f;
|
||||
constexpr float MOON_SIZE = 50.0f;
|
||||
constexpr float DEPTH_MAP_SIZE = 4096.0f;
|
||||
constexpr float ANGLE_STEP_DEG = 0.5f;
|
||||
} // namespace Cubed
|
||||
28
include/Cubed/render/shader_manager.hpp
Normal file
28
include/Cubed/render/shader_manager.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cubed/shader.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
namespace Cubed {
|
||||
class ShaderManager {
|
||||
public:
|
||||
ShaderManager();
|
||||
~ShaderManager();
|
||||
ShaderManager(const ShaderManager&) = delete;
|
||||
ShaderManager(ShaderManager&&) = delete;
|
||||
ShaderManager& operator=(const ShaderManager&) = delete;
|
||||
ShaderManager& operator=(ShaderManager&&) = delete;
|
||||
|
||||
void init();
|
||||
|
||||
const Shader& get_shader(const std::string& name) const;
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, Shader> m_shaders;
|
||||
|
||||
void register_shader(const std::string& name, const std::string& v_shader,
|
||||
const std::string& f_shader);
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
94
include/Cubed/render/texture.hpp
Normal file
94
include/Cubed/render/texture.hpp
Normal file
@@ -0,0 +1,94 @@
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
#include <glad/glad.h>
|
||||
namespace Cubed {
|
||||
|
||||
enum TextureType : GLenum {
|
||||
TEXTURE_2D = GL_TEXTURE_2D,
|
||||
TEXTURE_2D_ARRAY = GL_TEXTURE_2D_ARRAY
|
||||
};
|
||||
|
||||
enum TexturePname : GLenum {
|
||||
MIN_FILTER = GL_TEXTURE_MIN_FILTER,
|
||||
MAG_FILTER = GL_TEXTURE_MAG_FILTER,
|
||||
WRAP_S = GL_TEXTURE_WRAP_S,
|
||||
WRAP_T = GL_TEXTURE_WRAP_T,
|
||||
WRAP_R = GL_TEXTURE_WRAP_R,
|
||||
BORDER_COLOR = GL_TEXTURE_BORDER_COLOR,
|
||||
COMPARE_MODE = GL_TEXTURE_COMPARE_MODE
|
||||
};
|
||||
|
||||
enum TextureParam : GLenum {
|
||||
LINEAR = GL_LINEAR,
|
||||
NEAREST = GL_NEAREST,
|
||||
LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR,
|
||||
CLAMP_TO_BORDER = GL_CLAMP_TO_BORDER,
|
||||
CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
|
||||
T_NONE = GL_NONE,
|
||||
REPEAT = GL_REPEAT
|
||||
};
|
||||
|
||||
enum TextureFormat : GLenum {
|
||||
DEPTH_COMPONENT32F = GL_DEPTH_COMPONENT32F,
|
||||
DEPTH_COMPONENT = GL_DEPTH_COMPONENT,
|
||||
R16F = GL_R16F,
|
||||
RGBA16F = GL_RGBA16F,
|
||||
RED = GL_RED,
|
||||
RGBA = GL_RGBA,
|
||||
RGB = GL_RGB,
|
||||
RGBA8 = GL_RGBA8,
|
||||
|
||||
};
|
||||
|
||||
class Texture {
|
||||
public:
|
||||
explicit Texture(TextureType type);
|
||||
~Texture();
|
||||
Texture(const Texture&) = delete;
|
||||
Texture(Texture&&) noexcept;
|
||||
Texture& operator=(const Texture&) = delete;
|
||||
Texture& operator=(Texture&&) noexcept;
|
||||
|
||||
void bind() const;
|
||||
void bind(size_t unit) const;
|
||||
static void unbind();
|
||||
static void active(size_t id);
|
||||
GLuint id() const;
|
||||
|
||||
void parameter(TexturePname pname, TextureParam param) const;
|
||||
void parameterfv(TexturePname pname, const float* param) const;
|
||||
|
||||
void tex_image_2d(TextureFormat internalformat, TextureFormat format,
|
||||
GLenum type, const void* data, GLsizei width,
|
||||
GLsizei height, GLint level = 0, GLint border = 0) const;
|
||||
|
||||
void tex_image_3d(TextureFormat internalformat, TextureFormat format,
|
||||
GLenum type, const void* data, GLsizei width,
|
||||
GLsizei height, GLsizei depth, GLint level = 0,
|
||||
GLint border = 0) const;
|
||||
|
||||
void tex_sub_image_3d(TextureFormat format, GLenum type, const void* data,
|
||||
GLint xoffset, GLint yoffset, GLint zoffset,
|
||||
GLsizei width, GLsizei height, GLsizei depth = 1,
|
||||
GLint level = 0) const;
|
||||
|
||||
void set_aniso(int aniso) const;
|
||||
|
||||
void gen_mipmap() const;
|
||||
|
||||
void set_linear() const;
|
||||
void set_nearest_and_minpmap() const;
|
||||
void set_nearest() const;
|
||||
void set_repeat(bool r = true, bool s = true, bool t = true) const;
|
||||
void set_clamp_to_border(bool r = true, bool s = true, bool t = true) const;
|
||||
void set_clamp_to_edge(bool r = true, bool s = true, bool t = true) const;
|
||||
|
||||
TextureType type() const;
|
||||
|
||||
private:
|
||||
GLuint m_id = 0;
|
||||
const TextureType M_TYPE;
|
||||
|
||||
GLenum get_gl_texture_type() const;
|
||||
};
|
||||
} // namespace Cubed
|
||||
26
include/Cubed/render/vertex_array.hpp
Normal file
26
include/Cubed/render/vertex_array.hpp
Normal 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
|
||||
36
include/Cubed/render/vertex_buffer.hpp
Normal file
36
include/Cubed/render/vertex_buffer.hpp
Normal 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
|
||||
@@ -1,65 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cubed/constants.hpp"
|
||||
#include "Cubed/player_renderer.hpp"
|
||||
#include "Cubed/primitive_data.hpp"
|
||||
#include "Cubed/shader.hpp"
|
||||
#include "Cubed/ui/text.hpp"
|
||||
#include "Cubed/render/frame_buffer.hpp"
|
||||
#include "Cubed/render/player_renderer.hpp"
|
||||
#include "Cubed/render/texture.hpp"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
namespace Cubed {
|
||||
|
||||
class Camera;
|
||||
class TextureManager;
|
||||
class Renderer;
|
||||
class ClientWorld;
|
||||
class DevPanel;
|
||||
class Renderer {
|
||||
class TextureManager;
|
||||
class Camera;
|
||||
class WorldRenderer {
|
||||
public:
|
||||
constexpr static int NUM_VAO = 7;
|
||||
|
||||
Renderer(const Camera& camera, ClientWorld& world,
|
||||
const TextureManager& texture_manager, DevPanel& dev_panel);
|
||||
~Renderer();
|
||||
void hot_reload();
|
||||
void init(bool debug_on);
|
||||
const Shader& get_shader(const std::string& name) const;
|
||||
void render();
|
||||
void update(float delta_time);
|
||||
void update_fov(float fov);
|
||||
void update_proj_matrix(float aspect, float width, float height);
|
||||
void updata_framebuffer(int width, int height);
|
||||
float& ambient_strength();
|
||||
|
||||
bool& discard_transparent();
|
||||
bool& shader_on();
|
||||
bool& water_perturb();
|
||||
bool& water_depth_fade();
|
||||
bool& pbr();
|
||||
bool& flip_y();
|
||||
int& shadow_mode();
|
||||
int& light_cull_face();
|
||||
int& light_size_uv();
|
||||
float& min_radius();
|
||||
float& max_radius();
|
||||
int& samples();
|
||||
float& specular_strength();
|
||||
float& cloud_speed();
|
||||
float& cloud_threshold_low();
|
||||
float& cloud_threshold_high();
|
||||
float& refract_strength();
|
||||
float& underwater_fog_density();
|
||||
float& water_density();
|
||||
|
||||
const Camera& camera() const;
|
||||
const ClientWorld& world() const;
|
||||
ClientWorld& world();
|
||||
const glm::mat4& proj_mat() const;
|
||||
const TextureManager& texture_mamger() const;
|
||||
|
||||
float delta_time() const;
|
||||
|
||||
private:
|
||||
struct ParallelLight {
|
||||
glm::vec3 sundir; // direction from sun to vertex
|
||||
glm::vec3 lightdir;
|
||||
@@ -79,132 +31,121 @@ private:
|
||||
float horizon_sharpness;
|
||||
float cloud_white_mix;
|
||||
};
|
||||
WorldRenderer(Renderer& renderer);
|
||||
~WorldRenderer();
|
||||
|
||||
static constexpr glm::vec3 SUN_COLOR{1.00f, 0.95f, 0.80f};
|
||||
static constexpr glm::vec3 MOON_COLOR{0.75f, 0.80f, 1.00f};
|
||||
WorldRenderer(const WorldRenderer&) = delete;
|
||||
WorldRenderer(WorldRenderer&&) = delete;
|
||||
WorldRenderer& operator=(const WorldRenderer&) = delete;
|
||||
WorldRenderer& operator=(WorldRenderer&&) = delete;
|
||||
void init();
|
||||
void render();
|
||||
void updata_framebuffer(int width, int height);
|
||||
|
||||
static constexpr glm::vec3 SUNSET_SUNLIGHT_COLOR{1.00f, 0.45f, 0.15f};
|
||||
static constexpr glm::vec3 NOON_SUNLIGHT_COLOR{1.00f, 0.90f, 0.65f};
|
||||
static constexpr glm::vec3 SUNSET_AMBIENT_COLOR{0.18f, 0.12f, 0.35f};
|
||||
static constexpr glm::vec3 NOON_AMBIENT_COLOR{0.35f, 0.50f, 0.85f};
|
||||
static constexpr glm::vec3 MOONLIGHT_COLOR{0.55f, 0.70f, 1.00f};
|
||||
static constexpr glm::vec3 NIGHT_AMBIENT_COLOR{0.08f, 0.10f, 0.18f};
|
||||
static constexpr float FAR_PLANE = 1000.0f;
|
||||
static constexpr float NEAR_PLANE = 0.1f;
|
||||
static constexpr float SUN_SIZE = 50.0f;
|
||||
static constexpr float MOON_SIZE = 50.0f;
|
||||
static constexpr float DEPTH_MAP_SIZE = 4096.0f;
|
||||
static constexpr float ANGLE_STEP_DEG = 0.5f;
|
||||
float m_ambient_strength = 0.1f;
|
||||
float& ambient_strength();
|
||||
bool& discard_transparent();
|
||||
bool& shader_on();
|
||||
bool& water_perturb();
|
||||
bool& water_depth_fade();
|
||||
bool& pbr();
|
||||
bool& flip_y();
|
||||
int& shadow_mode();
|
||||
int& light_cull_face();
|
||||
int& light_size_uv();
|
||||
float& min_radius();
|
||||
float& max_radius();
|
||||
int& samples();
|
||||
float& specular_strength();
|
||||
float& cloud_speed();
|
||||
float& cloud_threshold_low();
|
||||
float& cloud_threshold_high();
|
||||
float& refract_strength();
|
||||
float& underwater_fog_density();
|
||||
|
||||
const Camera& m_camera;
|
||||
DevPanel& m_dev_panel;
|
||||
const TextureManager& m_texture_manager;
|
||||
ClientWorld& m_world;
|
||||
float& water_density();
|
||||
|
||||
const FrameBuffer* world_fbo() const;
|
||||
|
||||
private:
|
||||
Renderer& m_renderer;
|
||||
PlayerRenderer m_player_renderer;
|
||||
bool m_discard_tranparent = true;
|
||||
bool m_shader_on = true;
|
||||
bool m_water_perturb = true;
|
||||
bool m_water_depth_fade = true;
|
||||
bool m_pbr = true;
|
||||
bool m_flip_y = false;
|
||||
std::unique_ptr<Texture> m_accum_texture;
|
||||
std::unique_ptr<Texture> m_reveal_texture;
|
||||
|
||||
bool m_init = false;
|
||||
std::unique_ptr<FrameBuffer> m_world_fbo;
|
||||
std::unique_ptr<Texture> m_screen_texture;
|
||||
std::unique_ptr<Texture> m_screen_depth_texture;
|
||||
|
||||
int m_shadow_mode = 0;
|
||||
int m_light_cull_face = 0;
|
||||
float m_aspect = 0.0f;
|
||||
float m_fov = DEFAULT_FOV;
|
||||
std::unique_ptr<FrameBuffer> m_oit_fbo;
|
||||
|
||||
float m_delta_time = 0.0f;
|
||||
std::unique_ptr<Texture> m_oit_depth_texture;
|
||||
|
||||
float m_cloud_time = 0.0f;
|
||||
float m_cloud_speed = 5.0f;
|
||||
|
||||
float m_width = 0.0f;
|
||||
float m_height = 0.0f;
|
||||
|
||||
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;
|
||||
GLuint m_fbo = 0;
|
||||
GLuint m_screen_texture = 0;
|
||||
GLuint m_screen_depth_texture = 0;
|
||||
|
||||
GLuint m_oit_fbo = 0;
|
||||
GLuint m_accum_texture = 0;
|
||||
GLuint m_reveal_texture = 0;
|
||||
GLuint m_oit_depth_texture = 0;
|
||||
|
||||
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;
|
||||
std::unique_ptr<FrameBuffer> m_depth_map_fbo;
|
||||
std::unique_ptr<Texture> m_depth_map_texture;
|
||||
|
||||
glm::vec3 m_blend_from_lightdir;
|
||||
glm::vec3 m_blend_to_lightdir;
|
||||
float m_blend_t = 1.0f;
|
||||
bool m_blend_initialized = false;
|
||||
static constexpr float BLEND_DURATION = 0.15f;
|
||||
int m_light_size_uv = 20;
|
||||
|
||||
float m_min_radius = 2.0f;
|
||||
float m_max_radius = 20.0f;
|
||||
int m_samples = 16;
|
||||
|
||||
float m_specular_strength = 0.5f;
|
||||
|
||||
float moon_intensity = 0.3f;
|
||||
float sun_intensity = 1.00f;
|
||||
|
||||
float m_cloud_threshold_low = 0.5f;
|
||||
float m_cloud_threshold_high = 0.75f;
|
||||
|
||||
float m_refract_strength = 0.03f;
|
||||
|
||||
float m_underwater_fog_density = 0.08f;
|
||||
|
||||
float m_water_density = 0.12f;
|
||||
|
||||
float m_ambient_strength = 0.1f;
|
||||
bool m_discard_tranparent = true;
|
||||
bool m_shader_on = true;
|
||||
bool m_water_perturb = true;
|
||||
bool m_water_depth_fade = true;
|
||||
bool m_pbr = true;
|
||||
bool m_flip_y = false;
|
||||
int m_shadow_mode = 0;
|
||||
int m_light_size_uv = 20;
|
||||
float m_min_radius = 2.0f;
|
||||
float m_max_radius = 20.0f;
|
||||
int m_samples = 16;
|
||||
float m_specular_strength = 0.5f;
|
||||
float m_cloud_threshold_low = 0.5f;
|
||||
float m_cloud_threshold_high = 0.75f;
|
||||
float m_cloud_time = 0.0f;
|
||||
float m_cloud_speed = 5.0f;
|
||||
float m_refract_strength = 0.03f;
|
||||
int m_light_cull_face = 0;
|
||||
|
||||
float moon_intensity = 0.3f;
|
||||
float sun_intensity = 1.00f;
|
||||
|
||||
ParallelLight m_parallel_light;
|
||||
SkyUniform m_sky_uniform;
|
||||
/*
|
||||
0 - quad vao
|
||||
1 - sky vao
|
||||
2 - outline vao
|
||||
3 - ui vao
|
||||
4 - text vao
|
||||
*/
|
||||
std::vector<GLuint> m_vao;
|
||||
std::vector<Vertex2D> m_ui;
|
||||
|
||||
void init_quad();
|
||||
void init_text();
|
||||
glm::mat4 view_matrix;
|
||||
|
||||
ClientWorld& m_world;
|
||||
const Camera& m_camera;
|
||||
const TextureManager& m_texture_manager;
|
||||
void day_night_calculation();
|
||||
|
||||
void render_outline();
|
||||
void render_sky();
|
||||
void render_text();
|
||||
void render_ui();
|
||||
|
||||
void render_world();
|
||||
void render_player();
|
||||
|
||||
void shadow_map_generate();
|
||||
|
||||
void render_underwater();
|
||||
void render_dev_panel();
|
||||
void render_outline();
|
||||
void render_player();
|
||||
|
||||
void render_normal_block(const glm::mat4& model_mat,
|
||||
const glm::mat4& mv_mat,
|
||||
const glm::mat4& norm_mat);
|
||||
|
||||
void render_transparent_block(const glm::mat4& mv_mat,
|
||||
const glm::mat4& norm_mat);
|
||||
|
||||
glm::vec3 quantize_sun_direction(const glm::vec3& sundir,
|
||||
float angle_step_deg) const;
|
||||
glm::vec3 get_smoothed_shadow_lightdir(const glm::vec3& raw_shadow_sundir,
|
||||
float dt);
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
@@ -1,7 +1,9 @@
|
||||
#pragma once
|
||||
#include "Cubed/gameplay/block.hpp"
|
||||
#include "Cubed/render/texture.hpp"
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <memory>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
@@ -9,19 +11,17 @@ class TextureManager {
|
||||
private:
|
||||
bool m_need_reload = false;
|
||||
bool m_init = false;
|
||||
GLuint m_block_status_array = 0;
|
||||
GLuint m_texture_array = 0;
|
||||
GLuint m_cross_plane_array = 0;
|
||||
GLuint m_ui_array = 0;
|
||||
GLuint m_normal_texture_array = 0;
|
||||
std::unique_ptr<Texture> m_block_status_array;
|
||||
std::unique_ptr<Texture> m_texture_array;
|
||||
std::unique_ptr<Texture> m_cross_plane_array;
|
||||
std::unique_ptr<Texture> m_ui_array;
|
||||
std::unique_ptr<Texture> m_normal_texture_array;
|
||||
std::unique_ptr<Texture> m_skin;
|
||||
std::vector<std::unique_ptr<Texture>> m_item_textures;
|
||||
GLfloat m_max_aniso = 0.0f;
|
||||
|
||||
GLuint m_skin = 0;
|
||||
|
||||
int m_aniso = 1;
|
||||
|
||||
std::vector<GLuint> m_item_textures;
|
||||
|
||||
void load_block_status(unsigned status_id);
|
||||
void load_block_texture(unsigned block_id);
|
||||
void load_block_item_texture(unsigned id);
|
||||
@@ -39,14 +39,14 @@ public:
|
||||
TextureManager();
|
||||
~TextureManager();
|
||||
|
||||
void delet_texture();
|
||||
GLuint get_block_status_array() const;
|
||||
GLuint get_texture_array() const;
|
||||
GLuint get_cross_plane_array() const;
|
||||
GLuint get_ui_array() const;
|
||||
GLuint get_pbr_texture() const;
|
||||
const std::vector<GLuint>& item_textures() const;
|
||||
GLuint get_skin() const;
|
||||
void delete_texture();
|
||||
const Texture* get_block_status_array() const;
|
||||
const Texture* get_texture_array() const;
|
||||
const Texture* get_cross_plane_array() const;
|
||||
const Texture* get_ui_array() const;
|
||||
const Texture* get_pbr_texture() const;
|
||||
const std::vector<std::unique_ptr<Texture>>& item_textures() const;
|
||||
const Texture* get_skin() const;
|
||||
// Must call after MapTable::init_map() and glfwMakeContextCurrent(window);
|
||||
void init_texture();
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
#include "Cubed/render/texture.hpp"
|
||||
|
||||
#include <ft2build.h>
|
||||
#include <memory>
|
||||
#include FT_FREETYPE_H
|
||||
|
||||
#include "Cubed/primitive_data.hpp"
|
||||
@@ -29,7 +32,7 @@ public:
|
||||
static std::vector<Vertex2D> vertices(const std::string& text,
|
||||
float x = 0.0f, float y = 0.0f,
|
||||
float scale = 1.0f);
|
||||
static GLuint text_texture();
|
||||
static const Texture* text_texture();
|
||||
static const std::string& font_path();
|
||||
|
||||
private:
|
||||
@@ -39,7 +42,7 @@ private:
|
||||
float m_texture_width = 64;
|
||||
float m_texture_height = 64;
|
||||
|
||||
static inline GLuint m_text_texture;
|
||||
static inline std::unique_ptr<Texture> m_text_texture;
|
||||
static inline std::string m_font_path{ASSETS_PATH
|
||||
"fonts/IBMPlexSans-Regular.ttf"};
|
||||
std::unordered_map<char8_t, Character> m_characters;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cubed/primitive_data.hpp"
|
||||
#include "Cubed/render/vertex_array.hpp"
|
||||
#include "Cubed/render/vertex_buffer.hpp"
|
||||
#include "Cubed/ui/color.hpp"
|
||||
|
||||
#include <glad/glad.h>
|
||||
@@ -27,8 +29,7 @@ public:
|
||||
Text& text(std::string_view str);
|
||||
|
||||
std::size_t uuid() const;
|
||||
static void set_loc(const Shader& shader);
|
||||
void render();
|
||||
void render(const Shader& shader);
|
||||
|
||||
bool operator==(const Text& other) const;
|
||||
|
||||
@@ -38,14 +39,14 @@ private:
|
||||
|
||||
const std::string NAME;
|
||||
const std::size_t UUID;
|
||||
|
||||
std::string m_text;
|
||||
glm::vec4 m_color{1.0f, 1.0f, 1.0f, 1.0f};
|
||||
glm::mat4 m_model_matrix;
|
||||
|
||||
std::vector<Vertex2D> m_vertices;
|
||||
GLuint m_vbo = 0;
|
||||
static inline GLuint m_color_loc = 0;
|
||||
static inline GLuint m_mv_loc = 0;
|
||||
std::unique_ptr<VertexBuffer> m_vbo;
|
||||
std::unique_ptr<VertexArray> m_vao;
|
||||
|
||||
void update_vertices();
|
||||
void upload_to_gpu();
|
||||
|
||||
Reference in New Issue
Block a user