mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-17 16:17:02 +08:00
feat: water rendering (#13)
* refactor: update water texture * feat(gameplay): implement transparent block rendering with depth sorting * fix(world): use camera pos for distance calculations, make water passable * refactor(player): use ivec3 for block pass check * feat(camera): add underwater detection * feat(renderer): add underwater effect with framebuffer post-processing * feat(block): add gas property and refactor solid block check * fix(assets): set leaf block transparency to false * fix(block): set leaf as transparent
This commit is contained in:
@@ -16,6 +16,7 @@ private:
|
||||
Player* m_player;
|
||||
float m_last_mouse_x, m_last_mouse_y;
|
||||
glm::vec3 m_camera_pos;
|
||||
bool m_under_water = false;
|
||||
|
||||
public:
|
||||
Camera();
|
||||
@@ -29,6 +30,8 @@ public:
|
||||
|
||||
const glm::mat4 get_camera_lookat() const;
|
||||
const glm::vec3& get_camera_pos() const;
|
||||
|
||||
bool is_under_water() const;
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
|
||||
@@ -42,14 +42,16 @@ struct BlockData {
|
||||
BlockType id = 0;
|
||||
|
||||
bool is_liquid = false;
|
||||
bool is_gas = false;
|
||||
|
||||
bool is_passable = false;
|
||||
bool is_cross_plane = false;
|
||||
bool is_transparent = false;
|
||||
BlockData(BlockType b_id, std::string_view b_name, bool liquid,
|
||||
bool passable, bool cross_plane, bool transparent)
|
||||
: name(b_name), id(b_id), is_liquid(liquid), is_passable(passable),
|
||||
is_cross_plane(cross_plane), is_transparent(transparent) {}
|
||||
bool passable, bool cross_plane, bool transparent, bool gas)
|
||||
: name(b_name), id(b_id), is_liquid(liquid), is_gas(gas),
|
||||
is_passable(passable), is_cross_plane(cross_plane),
|
||||
is_transparent(transparent) {}
|
||||
};
|
||||
|
||||
class BlockManager {
|
||||
@@ -61,6 +63,9 @@ public:
|
||||
static unsigned cross_plane_sum();
|
||||
static const std::string& name_form_id(BlockType id);
|
||||
|
||||
static bool is_gas(BlockType id);
|
||||
static bool is_liquid(BlockType id);
|
||||
|
||||
static bool is_cross_plane(BlockType id);
|
||||
static bool is_transparent(BlockType id);
|
||||
static bool is_passable(BlockType id);
|
||||
|
||||
@@ -23,6 +23,7 @@ private:
|
||||
std::atomic<bool> m_is_on_gen_vertex_data{false};
|
||||
std::atomic<size_t> m_normal_vertices_sum = 0;
|
||||
std::atomic<size_t> m_cross_vertices_sum = 0;
|
||||
std::atomic<size_t> m_transparent_vertices_sum = 0;
|
||||
std::atomic<BiomeType> m_biome = BiomeType::PLAIN;
|
||||
std::mutex m_vertexs_data_mutex;
|
||||
|
||||
@@ -35,8 +36,10 @@ private:
|
||||
std::vector<BlockType> m_blocks;
|
||||
GLuint m_normal_vbo = 0;
|
||||
GLuint m_cross_plane_vbo = 0;
|
||||
GLuint m_transparent_normal_vbo = 0;
|
||||
std::vector<Vertex> m_normal_vertices;
|
||||
std::vector<Vertex> m_cross_plane_vertices;
|
||||
std::vector<Vertex> m_transparent_normal_vertices;
|
||||
float frequency = 0.01f;
|
||||
float height = 80;
|
||||
unsigned m_seed = 0;
|
||||
@@ -100,9 +103,13 @@ public:
|
||||
|
||||
GLuint get_normal_vbo() const;
|
||||
size_t get_normal_vertices_sum() const;
|
||||
|
||||
GLuint get_cross_vbo() const;
|
||||
size_t get_cross_vertices_sum() const;
|
||||
|
||||
GLuint get_transparent_vbo() const;
|
||||
size_t get_transparent_vertices_sum() const;
|
||||
|
||||
bool is_dirty() const;
|
||||
void mark_dirty();
|
||||
|
||||
|
||||
@@ -104,6 +104,7 @@ public:
|
||||
|
||||
Gait& gait();
|
||||
GameMode& game_mode();
|
||||
const World& get_world() const;
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
|
||||
@@ -19,6 +19,8 @@ struct ChunkRenderSnapshot {
|
||||
size_t normal_vertices_count;
|
||||
GLuint cross_vbo;
|
||||
size_t cross_vertices_count;
|
||||
GLuint transparent_vbo;
|
||||
size_t transparent_vertices_count;
|
||||
glm::vec3 center;
|
||||
glm::vec3 half_extents;
|
||||
};
|
||||
@@ -92,14 +94,15 @@ public:
|
||||
const glm::vec3& half_extents);
|
||||
|
||||
int get_block(const glm::ivec3& block_pos) const;
|
||||
bool is_block(const glm::ivec3& block_pos) const;
|
||||
bool is_solid(const glm::ivec3& block_pos) const;
|
||||
bool can_pass_block(const glm::ivec3& block_pos) const;
|
||||
|
||||
BlockType get_block_tpye(const glm::ivec3& block_pos) const;
|
||||
static ChunkPos chunk_pos(int world_x, int world_z);
|
||||
|
||||
void need_gen();
|
||||
void render(const glm::mat4& mvp_matrix,
|
||||
const TextureManager& texture_manager);
|
||||
const TextureManager& texture_manager,
|
||||
const glm::vec3& camera_pos);
|
||||
|
||||
void set_block(const glm::ivec3& pos, unsigned id);
|
||||
void update(float delta_time);
|
||||
|
||||
@@ -149,6 +149,13 @@ constexpr float CROSS_TEX_COORDS[2][6][2] = {
|
||||
{0.0f, 1.0f}}, // bottom left
|
||||
};
|
||||
#pragma endregion
|
||||
|
||||
constexpr float QUAD_VERTICES[] = {
|
||||
// postion // texcoorlds
|
||||
-1.0f, 1.0f, 0.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, 0.0f,
|
||||
|
||||
-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 {
|
||||
float x = 0.0f, y = 0.0f, z = 0.0f;
|
||||
float s = 0.0f, t = 0.0f;
|
||||
|
||||
@@ -15,7 +15,7 @@ class World;
|
||||
class DevPanel;
|
||||
class Renderer {
|
||||
public:
|
||||
constexpr static int NUM_VAO = 5;
|
||||
constexpr static int NUM_VAO = 6;
|
||||
|
||||
Renderer(const Camera& camera, World& world,
|
||||
const TextureManager& texture_manager, DevPanel& dev_panel);
|
||||
@@ -24,8 +24,10 @@ public:
|
||||
void init();
|
||||
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);
|
||||
|
||||
private:
|
||||
const Camera& m_camera;
|
||||
@@ -35,16 +37,25 @@ private:
|
||||
|
||||
float m_aspect = 0.0f;
|
||||
float m_fov = DEFAULT_FOV;
|
||||
|
||||
float m_delta_time = 0.0f;
|
||||
|
||||
glm::mat4 m_p_mat, m_v_mat, m_m_mat, m_mv_mat, m_mvp_mat;
|
||||
|
||||
GLuint m_mv_loc;
|
||||
GLuint m_proj_loc;
|
||||
GLuint m_mv_loc = 0;
|
||||
GLuint m_proj_loc = 0;
|
||||
|
||||
GLuint m_sky_vbo;
|
||||
GLuint m_text_vbo;
|
||||
GLuint m_outline_indices_vbo;
|
||||
GLuint m_outline_vbo;
|
||||
GLuint m_ui_vbo;
|
||||
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_fbo = 0;
|
||||
GLuint m_screen_texture = 0;
|
||||
GLuint m_depth_render_buffer = 0;
|
||||
|
||||
GLuint m_quad_vbo = 0;
|
||||
|
||||
glm::mat4 m_ui_proj;
|
||||
glm::mat4 m_ui_m_matrix;
|
||||
@@ -52,6 +63,7 @@ private:
|
||||
std::vector<GLuint> m_vao;
|
||||
std::vector<Vertex2D> m_ui;
|
||||
|
||||
void init_underwater();
|
||||
void init_text();
|
||||
|
||||
void render_outline();
|
||||
@@ -59,6 +71,7 @@ private:
|
||||
void render_text();
|
||||
void render_ui();
|
||||
void render_world();
|
||||
void render_underwater();
|
||||
void render_dev_panel();
|
||||
};
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
#include <glad/glad.h>
|
||||
#include <string>
|
||||
|
||||
#include <unordered_map>
|
||||
namespace Cubed {
|
||||
|
||||
class Shader {
|
||||
@@ -26,6 +26,7 @@ private:
|
||||
GLuint m_program = 0;
|
||||
std::size_t m_hash = 0;
|
||||
std::string m_name = "-1";
|
||||
mutable std::unordered_map<std::string, GLint> m_uniform_cache;
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user