mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-18 00:27: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:
@@ -18,6 +18,9 @@ Chunk::~Chunk() {
|
||||
if (m_cross_plane_vbo != 0) {
|
||||
m_world.push_delete_vbo(m_cross_plane_vbo);
|
||||
}
|
||||
if (m_transparent_normal_vbo != 0) {
|
||||
m_world.push_delete_vbo(m_transparent_normal_vbo);
|
||||
}
|
||||
}
|
||||
|
||||
Chunk::Chunk(Chunk&& other) noexcept
|
||||
@@ -25,15 +28,20 @@ Chunk::Chunk(Chunk&& other) noexcept
|
||||
m_is_on_gen_vertex_data(other.m_is_on_gen_vertex_data.load()),
|
||||
m_normal_vertices_sum(other.m_normal_vertices_sum.load()),
|
||||
m_cross_vertices_sum(other.m_cross_vertices_sum.load()),
|
||||
m_transparent_vertices_sum(other.m_transparent_vertices_sum.load()),
|
||||
m_biome(other.m_biome.load()), m_chunk_pos(std::move(other.m_chunk_pos)),
|
||||
m_world(other.m_world), m_heightmap(std::move(other.m_heightmap)),
|
||||
m_blocks(std::move(other.m_blocks)), m_normal_vbo(other.m_normal_vbo),
|
||||
m_cross_plane_vbo(other.m_cross_plane_vbo),
|
||||
m_transparent_normal_vbo(other.m_transparent_normal_vbo),
|
||||
m_normal_vertices(std::move(other.m_normal_vertices)),
|
||||
m_cross_plane_vertices(std::move(other.m_cross_plane_vertices)),
|
||||
m_transparent_normal_vertices(
|
||||
std::move(other.m_transparent_normal_vertices)),
|
||||
m_seed(other.m_seed), m_conditions(other.m_conditions) {
|
||||
other.m_normal_vbo = 0;
|
||||
other.m_cross_plane_vbo = 0;
|
||||
other.m_transparent_normal_vbo = 0;
|
||||
}
|
||||
|
||||
Chunk& Chunk::operator=(Chunk&& other) noexcept {
|
||||
@@ -43,6 +51,8 @@ Chunk& Chunk::operator=(Chunk&& other) noexcept {
|
||||
m_normal_vbo = other.m_normal_vbo;
|
||||
other.m_normal_vbo = 0;
|
||||
m_cross_plane_vbo = other.m_cross_plane_vbo;
|
||||
m_transparent_normal_vbo = other.m_transparent_normal_vbo;
|
||||
other.m_transparent_normal_vbo = 0;
|
||||
other.m_cross_plane_vbo = 0;
|
||||
m_chunk_pos = std::move(other.m_chunk_pos);
|
||||
m_heightmap = std::move(other.m_heightmap);
|
||||
@@ -50,11 +60,14 @@ Chunk& Chunk::operator=(Chunk&& other) noexcept {
|
||||
m_dirty = other.is_dirty();
|
||||
m_normal_vertices = std::move(other.m_normal_vertices);
|
||||
m_cross_plane_vertices = std::move(other.m_cross_plane_vertices);
|
||||
m_transparent_normal_vertices =
|
||||
std::move(other.m_transparent_normal_vertices);
|
||||
m_biome = other.m_biome.load();
|
||||
m_is_on_gen_vertex_data = other.m_is_on_gen_vertex_data.load();
|
||||
m_need_upload = other.m_need_upload.load();
|
||||
m_normal_vertices_sum = other.m_normal_vertices_sum.load();
|
||||
m_cross_vertices_sum = other.m_cross_vertices_sum.load();
|
||||
m_transparent_vertices_sum = other.m_transparent_vertices_sum.load();
|
||||
m_seed = other.m_seed;
|
||||
m_conditions = other.m_conditions;
|
||||
return *this;
|
||||
@@ -146,6 +159,11 @@ size_t Chunk::get_cross_vertices_sum() const {
|
||||
return m_cross_vertices_sum.load();
|
||||
}
|
||||
|
||||
GLuint Chunk::get_transparent_vbo() const { return m_transparent_normal_vbo; }
|
||||
size_t Chunk::get_transparent_vertices_sum() const {
|
||||
return m_transparent_vertices_sum.load();
|
||||
}
|
||||
|
||||
void Chunk::gen_phase_one() {
|
||||
m_generator = std::make_unique<ChunkGenerator>(*this);
|
||||
if (!m_generator) {
|
||||
@@ -235,6 +253,15 @@ void Chunk::upload_to_gpu() {
|
||||
m_cross_plane_vertices.size() * sizeof(Vertex),
|
||||
m_cross_plane_vertices.data(), GL_DYNAMIC_DRAW);
|
||||
}
|
||||
if (m_transparent_normal_vertices.size() != 0) {
|
||||
if (m_transparent_normal_vbo == 0) {
|
||||
glGenBuffers(1, &m_transparent_normal_vbo);
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_transparent_normal_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER,
|
||||
m_transparent_normal_vertices.size() * sizeof(Vertex),
|
||||
m_transparent_normal_vertices.data(), GL_DYNAMIC_DRAW);
|
||||
}
|
||||
// after fininshed it, can use
|
||||
clear_dirty();
|
||||
m_need_upload = false;
|
||||
@@ -276,7 +303,7 @@ BiomeConditions& Chunk::conditions() { return m_conditions; }
|
||||
void Chunk::gen_normal_vertices(
|
||||
const std::array<const std::vector<BlockType>*, 4>& neighbor_block) {
|
||||
m_normal_vertices.clear();
|
||||
|
||||
m_transparent_normal_vertices.clear();
|
||||
static const glm::ivec3 DIR[6] = {{0, 0, 1}, {1, 0, 0}, {0, 0, -1},
|
||||
{-1, 0, 0}, {0, 1, 0}, {0, -1, 0}};
|
||||
|
||||
@@ -382,13 +409,18 @@ void Chunk::gen_normal_vertices(
|
||||
static_cast<float>(cur_id * 6 + face)
|
||||
|
||||
};
|
||||
m_normal_vertices.emplace_back(vex);
|
||||
if (BlockManager::is_transparent(cur_id)) {
|
||||
m_transparent_normal_vertices.emplace_back(vex);
|
||||
} else {
|
||||
m_normal_vertices.emplace_back(vex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
m_normal_vertices_sum = m_normal_vertices.size();
|
||||
m_transparent_vertices_sum = m_transparent_normal_vertices.size();
|
||||
}
|
||||
void Chunk::gen_cross_plane_vertices() {
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ bool Player::ray_cast(const glm::vec3& start, const glm::vec3& front,
|
||||
float t = 0.0f;
|
||||
normal = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
while (t <= distance) {
|
||||
if (m_world.is_block(glm::ivec3(ix, iy, iz))) {
|
||||
if (m_world.is_solid(glm::ivec3(ix, iy, iz))) {
|
||||
block_pos = glm::ivec3(ix, iy, iz);
|
||||
return true;
|
||||
}
|
||||
@@ -300,14 +300,14 @@ void Player::update_lookup_block() {
|
||||
|
||||
if (m_look_block != std::nullopt) {
|
||||
if (Input::get_input_state().mouse_state.left) {
|
||||
if (m_world.is_block(m_look_block->pos)) {
|
||||
if (m_world.is_solid(m_look_block->pos)) {
|
||||
m_world.set_block(m_look_block->pos, 0);
|
||||
}
|
||||
Input::get_input_state().mouse_state.left = false;
|
||||
}
|
||||
if (Input::get_input_state().mouse_state.right) {
|
||||
glm::ivec3 near_pos = m_look_block->pos + m_look_block->normal;
|
||||
if (!m_world.is_block(near_pos)) {
|
||||
if (!m_world.is_solid(near_pos)) {
|
||||
auto x = near_pos.x;
|
||||
auto y = near_pos.y;
|
||||
auto z = near_pos.z;
|
||||
@@ -421,7 +421,7 @@ void Player::update_x_move() {
|
||||
for (int x = minx; x <= maxx; ++x) {
|
||||
for (int y = miny; y <= maxy; ++y) {
|
||||
for (int z = minz; z <= maxz; ++z) {
|
||||
if (!m_world.can_pass_block(glm::vec3{x, y, z})) {
|
||||
if (!m_world.can_pass_block(glm::ivec3{x, y, z})) {
|
||||
AABB block_box = {glm::vec3{static_cast<float>(x),
|
||||
static_cast<float>(y),
|
||||
static_cast<float>(z)},
|
||||
@@ -455,7 +455,7 @@ void Player::update_y_move() {
|
||||
for (int x = minx; x <= maxx; ++x) {
|
||||
for (int y = miny; y <= maxy; ++y) {
|
||||
for (int z = minz; z <= maxz; ++z) {
|
||||
if (!m_world.can_pass_block(glm::vec3{x, y, z})) {
|
||||
if (!m_world.can_pass_block(glm::ivec3{x, y, z})) {
|
||||
AABB block_box = {glm::vec3{static_cast<float>(x),
|
||||
static_cast<float>(y),
|
||||
static_cast<float>(z)},
|
||||
@@ -493,7 +493,7 @@ void Player::update_z_move() {
|
||||
for (int x = minx; x <= maxx; ++x) {
|
||||
for (int y = miny; y <= maxy; ++y) {
|
||||
for (int z = minz; z <= maxz; ++z) {
|
||||
if (!m_world.can_pass_block(glm::vec3{x, y, z})) {
|
||||
if (!m_world.can_pass_block(glm::ivec3{x, y, z})) {
|
||||
AABB block_box = {glm::vec3{static_cast<float>(x),
|
||||
static_cast<float>(y),
|
||||
static_cast<float>(z)},
|
||||
@@ -547,5 +547,5 @@ float& Player::g() { return m_g; }
|
||||
unsigned Player::place_block() const { return m_place_block; };
|
||||
Gait& Player::gait() { return m_gait; }
|
||||
GameMode& Player::game_mode() { return m_game_mode; }
|
||||
|
||||
const World& Player::get_world() const { return m_world; }
|
||||
} // namespace Cubed
|
||||
|
||||
@@ -292,11 +292,10 @@ void World::init_chunks() {
|
||||
}
|
||||
*/
|
||||
void World::render(const glm::mat4& mvp_matrix,
|
||||
const TextureManager& texture_manager) {
|
||||
const TextureManager& texture_manager,
|
||||
const glm::vec3& camera_pos) {
|
||||
Math::extract_frustum_planes(mvp_matrix, m_planes);
|
||||
int rendered_sum = 0;
|
||||
auto player_pos = get_player("TestPlayer").get_player_pos();
|
||||
glm::vec2 player_pos_xz{player_pos.x, player_pos.z};
|
||||
for (const auto& snapshot : m_render_snapshots) {
|
||||
|
||||
if (is_aabb_in_frustum(snapshot.center, snapshot.half_extents)) {
|
||||
@@ -316,38 +315,88 @@ void World::render(const glm::mat4& mvp_matrix,
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, snapshot.normal_vertices_count);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
if (snapshot.cross_vertices_count != 0) {
|
||||
glm::vec2 center_xz{snapshot.center.x, snapshot.center.z};
|
||||
float dist = glm::distance(player_pos_xz, center_xz);
|
||||
if (dist <= CROSS_PLANE_DISTANCE * 16) {
|
||||
|
||||
glDepthMask(GL_FALSE);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY,
|
||||
texture_manager.get_cross_plane_array());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, snapshot.cross_vbo);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
|
||||
sizeof(Vertex), (void*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
|
||||
sizeof(Vertex),
|
||||
(void*)offsetof(Vertex, s));
|
||||
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE,
|
||||
sizeof(Vertex),
|
||||
(void*)offsetof(Vertex, layer));
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0,
|
||||
snapshot.cross_vertices_count);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glDepthMask(GL_TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
rendered_sum++;
|
||||
}
|
||||
}
|
||||
glDepthMask(GL_FALSE);
|
||||
|
||||
struct SortableSnapshot {
|
||||
const ChunkRenderSnapshot* snapshot;
|
||||
float distance;
|
||||
};
|
||||
|
||||
std::vector<SortableSnapshot> cross_list;
|
||||
std::vector<SortableSnapshot> transparent_list;
|
||||
|
||||
for (const auto& snapshot : m_render_snapshots) {
|
||||
|
||||
if (!is_aabb_in_frustum(snapshot.center, snapshot.half_extents)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
float dist = glm::distance(camera_pos, snapshot.center);
|
||||
glm::vec2 camera_pos_xz{camera_pos.x, camera_pos.z};
|
||||
if (snapshot.cross_vertices_count != 0) {
|
||||
glm::vec2 center_xz{snapshot.center.x, snapshot.center.z};
|
||||
float dist2d = glm::distance(camera_pos_xz, center_xz);
|
||||
if (dist2d <= CROSS_PLANE_DISTANCE * 16) {
|
||||
cross_list.push_back({&snapshot, dist});
|
||||
}
|
||||
}
|
||||
if (snapshot.transparent_vertices_count != 0) {
|
||||
transparent_list.push_back({&snapshot, dist});
|
||||
}
|
||||
}
|
||||
std::sort(transparent_list.begin(), transparent_list.end(),
|
||||
[](const SortableSnapshot& a, const SortableSnapshot& b) {
|
||||
return a.distance > b.distance;
|
||||
});
|
||||
std::sort(cross_list.begin(), cross_list.end(),
|
||||
[](const SortableSnapshot& a, const SortableSnapshot& b) {
|
||||
return a.distance > b.distance;
|
||||
});
|
||||
|
||||
for (const auto& item : cross_list) {
|
||||
const auto& snapshot = *item.snapshot;
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY,
|
||||
texture_manager.get_cross_plane_array());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, snapshot.cross_vbo);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||
(void*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||
(void*)offsetof(Vertex, s));
|
||||
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||
(void*)offsetof(Vertex, layer));
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, snapshot.cross_vertices_count);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
for (const auto& item : transparent_list) {
|
||||
const auto& snapshot = *item.snapshot;
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, texture_manager.get_texture_array());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, snapshot.transparent_vbo);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||
(void*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||
(void*)offsetof(Vertex, s));
|
||||
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||
(void*)offsetof(Vertex, layer));
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, snapshot.transparent_vertices_count);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
glDepthMask(GL_TRUE);
|
||||
DebugCollector::get().report(
|
||||
"rendered_chunk", "Rendered Chunk: " + std::to_string(rendered_sum));
|
||||
}
|
||||
@@ -758,7 +807,7 @@ int World::get_block(const glm::ivec3& block_pos) const {
|
||||
return chunk_blocks[Chunk::index(x, y, z)];
|
||||
}
|
||||
|
||||
bool World::is_block(const glm::ivec3& block_pos) const {
|
||||
bool World::is_solid(const glm::ivec3& block_pos) const {
|
||||
auto [chunk_x, chunk_z] = chunk_pos(block_pos.x, block_pos.z);
|
||||
std::lock_guard lk(m_chunks_mutex);
|
||||
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
||||
@@ -773,7 +822,7 @@ bool World::is_block(const glm::ivec3& block_pos) const {
|
||||
return false;
|
||||
}
|
||||
auto id = chunk_blocks[Chunk::index(x, y, z)];
|
||||
if (id == 0) {
|
||||
if (BlockManager::is_gas(id) || BlockManager::is_liquid(id)) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
@@ -798,6 +847,27 @@ bool World::can_pass_block(const glm::ivec3& block_pos) const {
|
||||
return BlockManager::is_passable(id);
|
||||
}
|
||||
|
||||
BlockType World::get_block_tpye(const glm::ivec3& block_pos) const {
|
||||
auto [chunk_x, chunk_z] = chunk_pos(block_pos.x, block_pos.z);
|
||||
std::lock_guard lk(m_chunks_mutex);
|
||||
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
||||
|
||||
if (it == m_chunks.end()) {
|
||||
Logger::error("Can't Find Block {} {} {}", block_pos.x, block_pos.y,
|
||||
block_pos.z);
|
||||
return 0;
|
||||
}
|
||||
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
||||
auto [x, y, z] = Chunk::world_to_block(block_pos, {chunk_x, chunk_z});
|
||||
if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||
z >= CHUNK_SIZE) {
|
||||
Logger::error("Can't Find Block {} {} {}", block_pos.x, block_pos.y,
|
||||
block_pos.z);
|
||||
return 0;
|
||||
}
|
||||
return chunk_blocks[Chunk::index(x, y, z)];
|
||||
}
|
||||
|
||||
void World::set_block(const glm::ivec3& block_pos, unsigned id) {
|
||||
|
||||
int world_x, world_y, world_z;
|
||||
@@ -896,6 +966,8 @@ void World::update(float delta_time) {
|
||||
m_render_snapshots.push_back(
|
||||
{chunk.get_normal_vbo(), chunk.get_normal_vertices_sum(),
|
||||
chunk.get_cross_vbo(), chunk.get_cross_vertices_sum(),
|
||||
chunk.get_transparent_vbo(),
|
||||
chunk.get_transparent_vertices_sum(),
|
||||
glm::vec3(static_cast<float>(pos.x * CHUNK_SIZE) +
|
||||
static_cast<float>(CHUNK_SIZE / 2),
|
||||
static_cast<float>(WORLD_SIZE_Y / 2),
|
||||
|
||||
Reference in New Issue
Block a user