mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-22 02:27:01 +08:00
feat: lighting effects (#18)
* feat(rendering): add basic diffuse and ambient lighting to block rendering * feat(world): add day/night cycle with server tick system * feat(renderer): make ambient strength adjustable via dev panel * fix(game_time): use unsigned tick type and enforce positive tick speed * feat(renderer): add shadow mapping with PCF soft shadows Introduce shadow mapping using a dedicated depth framebuffer and shader. The block fragment shader now performs percentage-closer filtering (PCF) with Poisson disk sampling and random rotation for soft shadows. The vertex shader outputs light-space coordinates. A new depth shader pair handles rendering from the light's perspective, discarding transparent fragments. The renderer sets up the light projection based on the camera position and sun direction, and applies the shadow factor to diffuse lighting. Day/night cycle can now be toggled off in the world server thread. * perf(shadow): increase depth map resolution and refine PCF sampling * feat(dev-panel): add tick freeze toggle and fix TickType sign Add checkbox to freeze tick advancement in dev panel. Change TickType from unsigned long long to signed long long to prevent underflow. * chore(world): add missing <numbers> include * feat(renderer): add runtime shader and shadow mode controls Introduce user-controllable shader on/off, shadow mode (rotated Poisson disk, 3x3 grid, or off), light cull face, and discard transparent in depth pass. Expose all settings in new dev panel "shader" tab. Move ambient strength slider to the new tab. * fix(texture): set texture wrap mode to clamp to edge * feat(renderer): smooth shadow sun direction transitions using quantized directions and slerp * feat(renderer): add PCSS shadow mode with configurable samples and softness Implement Percentage Closer Soft Shadows (PCSS) as shadow mode 3. Add a FindBlocker function and three Poisson disk arrays (8, 16, 32 samples). Expose new uniforms (lightSizeUV, minRadius, maxRadius, samples) and provide slider/combo controls in the dev panel for sample count, light size, and penumbra radius limits. * feat(renderer): add roughness-based specular lighting to blocks * feat(renderer): compute ambient and sunlight colors based on sun height * feat(renderer): add moonlight and smooth day/night light blending * refactor(renderer): extract day/night calculation and add billboard shaders Add a new ParallelLight struct to encapsulate lighting parameters. Move day-night cycle computation from render_world() to a new day_night_calculation() method. Update sky shaders to use procedural colors based on sun position. Add separate billboard shaders for sun/moon. * feat(sky): add animated clouds to sky shader with speed control * feat(renderer): add configurable cloud thresholds and white mix
This commit is contained in:
@@ -32,6 +32,7 @@ public:
|
||||
const glm::vec3& get_camera_pos() const;
|
||||
|
||||
bool is_under_water() const;
|
||||
glm::vec3 get_camera_front() const;
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
|
||||
@@ -23,9 +23,9 @@ constexpr float DEFAULT_MAX_RUN_SPEED = 7.0f;
|
||||
constexpr float DEFAULT_ACCELERATION = 10.0f;
|
||||
constexpr float DEFAULT_DECELERATION = 15.0f;
|
||||
constexpr float DEFAULT_G = 22.5f;
|
||||
static constexpr int SIZE_X = CHUNK_SIZE;
|
||||
static constexpr int SIZE_Y = WORLD_SIZE_Y;
|
||||
static constexpr int SIZE_Z = CHUNK_SIZE;
|
||||
constexpr int SIZE_X = CHUNK_SIZE;
|
||||
constexpr int SIZE_Y = WORLD_SIZE_Y;
|
||||
constexpr int SIZE_Z = CHUNK_SIZE;
|
||||
|
||||
constexpr ChunkPos CHUNK_DIR[]{{1, 0}, {-1, 0}, {0, 1}, {0, -1},
|
||||
{1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
|
||||
|
||||
@@ -44,14 +44,20 @@ private:
|
||||
bool m_need_save_config = false;
|
||||
bool m_gen_thread_running = true;
|
||||
int m_theme = 0;
|
||||
int m_pre_set_day_tick = 0;
|
||||
int m_pre_set_tick_speed = 1;
|
||||
bool m_tick_frezze = false;
|
||||
int m_samples_idx = 1;
|
||||
void show_about_table_bar();
|
||||
void show_biome_table_bar();
|
||||
void show_time_table_bar();
|
||||
void show_cave_table_bar();
|
||||
void show_river_table_bar();
|
||||
void show_settings_tab_item();
|
||||
void show_world_tab_item();
|
||||
void show_player_tab_item();
|
||||
void show_items_tab_item();
|
||||
void show_shader_tab_item();
|
||||
|
||||
void update_config_view();
|
||||
void update_player_profile();
|
||||
|
||||
@@ -51,13 +51,14 @@ struct BlockData {
|
||||
bool is_discard = false;
|
||||
bool is_blend = false;
|
||||
bool is_transitional = false;
|
||||
float roughness = 1.0f;
|
||||
BlockData(BlockType b_id, std::string_view b_name, bool liquid,
|
||||
bool passable, bool cross_plane, bool transparent, bool gas,
|
||||
bool discard, bool blend, bool transitional)
|
||||
bool discard, bool blend, bool transitional, float r)
|
||||
: name(b_name), id(b_id), is_liquid(liquid), is_gas(gas),
|
||||
is_passable(passable), is_cross_plane(cross_plane),
|
||||
is_transparent(transparent), is_discard(discard), is_blend(blend),
|
||||
is_transitional(transitional) {}
|
||||
is_transitional(transitional), roughness(r) {}
|
||||
};
|
||||
|
||||
class BlockManager {
|
||||
@@ -79,6 +80,7 @@ public:
|
||||
static bool is_discard(BlockType id);
|
||||
static bool is_blend(BlockType id);
|
||||
static bool is_transitional(BlockType id);
|
||||
static float roughness(BlockType id);
|
||||
static BlockType cross_plane_index(BlockType id);
|
||||
|
||||
private:
|
||||
|
||||
10
include/Cubed/gameplay/game_time.hpp
Normal file
10
include/Cubed/gameplay/game_time.hpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
// Prevent unsigned underflow issues in subtraction
|
||||
using TickType = long long;
|
||||
|
||||
constexpr int DEFAULT_PER_TICK_TIME = 50;
|
||||
|
||||
constexpr TickType DAY_TIME = 24000;
|
||||
|
||||
constexpr TickType PER_HOUR = 1000;
|
||||
@@ -7,7 +7,7 @@
|
||||
namespace Cubed {
|
||||
class World;
|
||||
struct VertexData {
|
||||
std::vector<Vertex> m_vertices;
|
||||
std::vector<Vertex3D> m_vertices;
|
||||
GLuint m_vbo = 0;
|
||||
GLuint m_vao = 0;
|
||||
std::atomic<std::size_t> m_sum{0};
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "Cubed/AABB.hpp"
|
||||
#include "Cubed/gameplay/cave_carver.hpp"
|
||||
#include "Cubed/gameplay/chunk.hpp"
|
||||
#include "Cubed/gameplay/game_time.hpp"
|
||||
#include "Cubed/gameplay/river_worm.hpp"
|
||||
|
||||
#include <atomic>
|
||||
@@ -39,12 +40,21 @@ private:
|
||||
std::unordered_map<ChunkPos, const Chunk*, ChunkPos::Hash>;
|
||||
using ChunkPosSet = std::unordered_set<ChunkPos, ChunkPos::Hash>;
|
||||
using ChunkHashMap = std::unordered_map<ChunkPos, Chunk, ChunkPos::Hash>;
|
||||
|
||||
glm::vec3 m_gen_player_pos{0.0f, 0.0f, 0.0f};
|
||||
ChunkHashMap m_chunks;
|
||||
std::unordered_map<std::size_t, Player> m_players;
|
||||
std::vector<glm::vec4> m_planes;
|
||||
|
||||
std::thread m_gen_thread;
|
||||
std::thread m_server_thread;
|
||||
|
||||
std::stop_source m_server_stop_source;
|
||||
|
||||
std::atomic<int> m_per_tick_time = DEFAULT_PER_TICK_TIME; // ms
|
||||
|
||||
std::atomic<TickType> m_day_tick = 6000;
|
||||
|
||||
mutable std::mutex m_chunks_mutex;
|
||||
std::mutex m_gen_signal_mutex;
|
||||
std::mutex m_new_chunk_queue_mutex;
|
||||
@@ -59,8 +69,12 @@ private:
|
||||
std::atomic<bool> m_is_rebuilding{false};
|
||||
std::atomic<bool> m_chunk_gen_finished{false};
|
||||
std::atomic<bool> m_could_gen{true};
|
||||
std::atomic<bool> m_tick_running{true};
|
||||
std::atomic<int> m_rendering_distance{24};
|
||||
std::atomic<float> m_chunk_gen_fraction{0.0f};
|
||||
|
||||
std::atomic<TickType> m_game_ticks{0};
|
||||
|
||||
std::vector<ChunkPos> m_dirty_queue;
|
||||
std::vector<ChunkRenderSnapshot> m_render_snapshots;
|
||||
std::vector<std::pair<ChunkPos, Chunk>> m_new_chunk;
|
||||
@@ -119,12 +133,25 @@ public:
|
||||
int rendering_distance() const;
|
||||
void rendering_distance(int rendering_distance);
|
||||
void start_gen_thread();
|
||||
void start_server_thread();
|
||||
void stop_gen_thread();
|
||||
void stop_server_thread();
|
||||
void serever_run(std::stop_token stoken);
|
||||
|
||||
CaveCarver& cave_carcer();
|
||||
RiverWorm& river_worm();
|
||||
std::vector<glm::vec4>& planes();
|
||||
std::vector<ChunkRenderSnapshot>& render_snapshots();
|
||||
|
||||
glm::vec3 sunlight_dir() const;
|
||||
TickType game_tick() const;
|
||||
TickType day_tick() const;
|
||||
void day_tick(TickType tick);
|
||||
int per_tick_time() const;
|
||||
void per_tick_time(int ms);
|
||||
|
||||
bool is_tick_running() const;
|
||||
void tick_running(bool run);
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
|
||||
@@ -91,6 +91,51 @@ constexpr float TEX_COORDS[6][6][2] = {
|
||||
{0.0f, 1.0f}, // back left
|
||||
{0.0f, 0.0f}} // front left
|
||||
};
|
||||
|
||||
constexpr float NORMALS[6][6][3] = {
|
||||
// ===== front (z = +1) =====
|
||||
{{0.0f, 0.0f, 1.0f},
|
||||
{0.0f, 0.0f, 1.0f},
|
||||
{0.0f, 0.0f, 1.0f},
|
||||
{0.0f, 0.0f, 1.0f},
|
||||
{0.0f, 0.0f, 1.0f},
|
||||
{0.0f, 0.0f, 1.0f}},
|
||||
// ===== right (x = +1) =====
|
||||
{{1.0f, 0.0f, 0.0f},
|
||||
{1.0f, 0.0f, 0.0f},
|
||||
{1.0f, 0.0f, 0.0f},
|
||||
{1.0f, 0.0f, 0.0f},
|
||||
{1.0f, 0.0f, 0.0f},
|
||||
{1.0f, 0.0f, 0.0f}},
|
||||
// ===== back (z = -1) =====
|
||||
{{0.0f, 0.0f, -1.0f},
|
||||
{0.0f, 0.0f, -1.0f},
|
||||
{0.0f, 0.0f, -1.0f},
|
||||
{0.0f, 0.0f, -1.0f},
|
||||
{0.0f, 0.0f, -1.0f},
|
||||
{0.0f, 0.0f, -1.0f}},
|
||||
// ===== left (x = -1) =====
|
||||
{{-1.0f, 0.0f, 0.0f},
|
||||
{-1.0f, 0.0f, 0.0f},
|
||||
{-1.0f, 0.0f, 0.0f},
|
||||
{-1.0f, 0.0f, 0.0f},
|
||||
{-1.0f, 0.0f, 0.0f},
|
||||
{-1.0f, 0.0f, 0.0f}},
|
||||
// ===== top (y = +1) =====
|
||||
{{0.0f, 1.0f, 0.0f},
|
||||
{0.0f, 1.0f, 0.0f},
|
||||
{0.0f, 1.0f, 0.0f},
|
||||
{0.0f, 1.0f, 0.0f},
|
||||
{0.0f, 1.0f, 0.0f},
|
||||
{0.0f, 1.0f, 0.0f}},
|
||||
// ===== bottom (y = -1) =====
|
||||
{{0.0f, -1.0f, 0.0f},
|
||||
{0.0f, -1.0f, 0.0f},
|
||||
{0.0f, -1.0f, 0.0f},
|
||||
{0.0f, -1.0f, 0.0f},
|
||||
{0.0f, -1.0f, 0.0f},
|
||||
{0.0f, -1.0f, 0.0f}}};
|
||||
|
||||
#pragma endregion
|
||||
constexpr float CUBE_VER[24] = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0,
|
||||
0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0,
|
||||
@@ -148,6 +193,24 @@ constexpr float CROSS_TEX_COORDS[2][6][2] = {
|
||||
{1.0f, 1.0f}, // bottom right
|
||||
{0.0f, 1.0f}}, // bottom left
|
||||
};
|
||||
|
||||
constexpr float CROSS_NORMALS[2][6][3] = {
|
||||
// ===== Plane 1: upward =====
|
||||
{{0.0f, 1.0f, 0.0f},
|
||||
{0.0f, 1.0f, 0.0f},
|
||||
{0.0f, 1.0f, 0.0f},
|
||||
{0.0f, 1.0f, 0.0f},
|
||||
{0.0f, 1.0f, 0.0f},
|
||||
{0.0f, 1.0f, 0.0f}},
|
||||
|
||||
// ===== Plane 2: upward =====
|
||||
{{0.0f, 1.0f, 0.0f},
|
||||
{0.0f, 1.0f, 0.0f},
|
||||
{0.0f, 1.0f, 0.0f},
|
||||
{0.0f, 1.0f, 0.0f},
|
||||
{0.0f, 1.0f, 0.0f},
|
||||
{0.0f, 1.0f, 0.0f}}};
|
||||
|
||||
#pragma endregion
|
||||
|
||||
constexpr float QUAD_VERTICES[] = {
|
||||
@@ -156,10 +219,12 @@ constexpr float QUAD_VERTICES[] = {
|
||||
|
||||
-1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f};
|
||||
|
||||
struct Vertex {
|
||||
struct Vertex3D {
|
||||
float x = 0.0f, y = 0.0f, z = 0.0f;
|
||||
float s = 0.0f, t = 0.0f;
|
||||
float layer = 0.0f;
|
||||
float nx = 0.0f, ny = 0.0f, nz = 0.0f;
|
||||
float roughness = 1.0f;
|
||||
};
|
||||
|
||||
struct Vertex2D {
|
||||
|
||||
@@ -28,22 +28,71 @@ public:
|
||||
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();
|
||||
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();
|
||||
|
||||
private:
|
||||
struct ParallelLight {
|
||||
glm::vec3 sundir;
|
||||
glm::vec3 lightdir;
|
||||
float sun_height = 0.0f;
|
||||
float day_light = 0.0f;
|
||||
float day_factor = 0.0f;
|
||||
glm::vec3 sun_color;
|
||||
glm::vec3 directional_light_color;
|
||||
glm::vec3 finnal_ambient_color;
|
||||
};
|
||||
|
||||
static constexpr glm::vec3 SUN_COLOR{1.00f, 0.95f, 0.80f};
|
||||
static constexpr glm::vec3 MOON_COLOR{0.75f, 0.80f, 1.00f};
|
||||
|
||||
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;
|
||||
|
||||
const Camera& m_camera;
|
||||
DevPanel& m_dev_panel;
|
||||
const TextureManager& m_texture_manager;
|
||||
World& m_world;
|
||||
|
||||
bool m_discard_tranparent = true;
|
||||
bool m_shader_on = true;
|
||||
int m_shadow_mode = 0;
|
||||
int m_light_cull_face = 0;
|
||||
float m_aspect = 0.0f;
|
||||
float m_fov = DEFAULT_FOV;
|
||||
|
||||
float m_delta_time = 0.0f;
|
||||
|
||||
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;
|
||||
glm::mat4 m_p_mat, m_v_mat, m_m_mat, m_mv_mat, m_mvp_mat, m_norm_mat;
|
||||
|
||||
GLuint m_mv_loc = 0;
|
||||
GLuint m_proj_loc = 0;
|
||||
@@ -62,18 +111,43 @@ private:
|
||||
GLuint m_accum_texture = 0;
|
||||
GLuint m_reveal_texture = 0;
|
||||
GLuint m_oit_depth_render_buffer = 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;
|
||||
|
||||
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;
|
||||
|
||||
ParallelLight m_parallel_light;
|
||||
/*
|
||||
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;
|
||||
@@ -81,6 +155,8 @@ private:
|
||||
void init_quad();
|
||||
void init_text();
|
||||
|
||||
void day_night_calculation();
|
||||
|
||||
void render_outline();
|
||||
void render_sky();
|
||||
void render_text();
|
||||
@@ -88,6 +164,11 @@ private:
|
||||
void render_world();
|
||||
void render_underwater();
|
||||
void render_dev_panel();
|
||||
|
||||
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
|
||||
@@ -12,6 +12,8 @@ float smootherstep(float edge0, float edge1, float x);
|
||||
bool is_aabb_in_frustum(const glm::vec3& center, const glm::vec3& half_extents,
|
||||
const std::vector<glm::vec4>& planes);
|
||||
float deterministic_random(int x, int z, uint64_t seed);
|
||||
glm::vec3 slerp(const glm::vec3& from, const glm::vec3& to, float t);
|
||||
|
||||
} // namespace Math
|
||||
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user