feat: add cave (#8)

* feat: add cave generate

* fix: incorrect blocks on cave surface

* fix: non-deterministic cave generator

* refactor: move all chunk generation to dedicated generation thread

* refactor: remove inital cave

* feat: add cave parameter adjustment

* refactor: adjust cave probability
This commit is contained in:
zhenyan121
2026-05-09 20:13:55 +08:00
committed by GitHub
parent d986e03f9c
commit 1a26474a05
19 changed files with 456 additions and 33 deletions

View File

@@ -117,6 +117,8 @@ add_executable(${PROJECT_NAME}
src/gameplay/builders/river_builder.cpp src/gameplay/builders/river_builder.cpp
src/gameplay/builders/desert_builder.cpp src/gameplay/builders/desert_builder.cpp
src/gameplay/builders/forest_builder.cpp src/gameplay/builders/forest_builder.cpp
src/gameplay/cave_carver.cpp
src/gameplay/cave_path.cpp
) )
if(CMAKE_BUILD_TYPE STREQUAL "Debug") if(CMAKE_BUILD_TYPE STREQUAL "Debug")

View File

@@ -46,6 +46,7 @@ private:
int m_theme = 0; int m_theme = 0;
void show_about_table_bar(); void show_about_table_bar();
void show_biome_table_bar(); void show_biome_table_bar();
void show_cave_table_bar();
void show_settings_tab_item(); void show_settings_tab_item();
void show_world_tab_item(); void show_world_tab_item();
void show_player_tab_item(); void show_player_tab_item();

View File

@@ -0,0 +1,24 @@
#pragma once
#include "Cubed/gameplay/cave_path.hpp"
namespace Cubed {
class CaveCarver {
public:
CaveCarver();
std::unordered_map<int, CavePath>& paths();
void init(unsigned world_seed);
void reload(unsigned world_seed);
void add_path(const glm::vec3& pos);
void try_to_add_path(const ChunkPos& pos, unsigned chunk_seed);
void cleanup_finished_caves();
int cave_sum() const;
float& cave_probability();
private:
std::unordered_map<int, CavePath> m_paths;
unsigned m_seed = 0;
int m_sum = 0;
Random m_random;
float m_cave_probability = 0.035f;
};
} // namespace Cubed

View File

@@ -0,0 +1,85 @@
#pragma once
#include "Cubed/gameplay/chunk_pos.hpp"
#include "Cubed/tools/cubed_random.hpp"
#include <glm/glm.hpp>
#include <unordered_set>
namespace Cubed {
struct PathPoint {
glm::vec3 pos;
glm::vec3 tangent{0.0f, 0.0f, 1.0f};
float rad_xz;
float rad_y;
PathPoint(const glm::vec3& p, float rx, float ry)
: pos(p), rad_xz(rx), rad_y(ry) {}
bool contains(const glm::vec3& other_pos) const {
glm::vec3 to_point = other_pos - pos;
glm::vec3 world_up(0.0f, 1.0f, 0.0f);
glm::vec3 right = glm::normalize(glm::cross(tangent, world_up));
if (glm::length(right) < 0.001f) {
glm::vec3 alt_up(1.0f, 0.0f, 0.0f);
right = glm::normalize(glm::cross(tangent, alt_up));
}
glm::vec3 up = glm::normalize(glm::cross(right, tangent));
float horizontal_dist = glm::dot(to_point, right);
float vertical_dist = glm::dot(to_point, up);
float a = rad_xz;
float b = rad_y;
if (a <= 0.0f || b <= 0.0f)
return false;
float check = (horizontal_dist * horizontal_dist) / (a * a) +
(vertical_dist * vertical_dist) / (b * b);
return check <= 1.0f;
}
};
class CavePath {
public:
CavePath(unsigned int world_seed, int path_id, const glm::vec3& start_pos);
const std::vector<PathPoint>& points() const;
void clear_chunk(const ChunkPos& pos);
bool is_finished() const;
static float& radius_xz_min();
static float& radius_xz_max();
static float& radius_y_min();
static float& radius_y_max();
static float& delta_angle_min();
static float& delta_angle_max();
static int& step_min();
static int& step_max();
private:
static inline float m_radius_xz_min = 5.0f;
static inline float m_radius_xz_max = 15.0f;
static inline float m_radius_y_min = 4.0f;
static inline float m_radius_y_max = 10.0f;
static inline float m_delta_angle_min = -5.0f;
static inline float m_delta_angle_max = 5.0f;
static inline int m_step_min = 10;
static inline int m_step_max = 400;
int m_path_id = 0;
unsigned int m_seed = 0;
float m_yaw = 0.0f;
float m_pitch = 0.0f;
int m_step = 0;
float m_step_len = 1.0f;
PathPoint m_start_path_point{{0.0f, 0.0f, 0.0f}, 0.0f, 0.0f};
Random m_random;
std::vector<PathPoint> m_points;
std::unordered_set<ChunkPos, ChunkPos::Hash> m_pending_chunks;
void collect_path_points();
void precompute_chunk_coverage();
};
} // namespace Cubed

View File

@@ -38,7 +38,7 @@ private:
float frequency = 0.01f; float frequency = 0.01f;
float height = 80; float height = 80;
unsigned m_seed = 0;
void clear_dirty(); void clear_dirty();
public: public:
@@ -98,6 +98,8 @@ public:
void biome(BiomeType b); void biome(BiomeType b);
HeightMapArray& heightmap(); HeightMapArray& heightmap();
std::vector<uint8_t>& blocks(); std::vector<uint8_t>& blocks();
World& world();
unsigned seed() const;
}; };
} // namespace Cubed } // namespace Cubed

View File

@@ -20,7 +20,7 @@ public:
static void reload(); static void reload();
static const unsigned& seed(); static const unsigned& seed();
static void seed(unsigned s); static void seed(unsigned s);
unsigned chunk_seed() const;
// Generate Biome // Generate Biome
void assign_chunk_biome(); void assign_chunk_biome();
// Adjust Biome // Adjust Biome
@@ -54,7 +54,10 @@ private:
std::unique_ptr<BiomeBuilder> m_biome_builder{nullptr}; std::unique_ptr<BiomeBuilder> m_biome_builder{nullptr};
bool is_cur_chunk_ins = false; bool is_cur_chunk_ins = false;
std::array<BiomeType, 8> m_neighbor_biome; std::array<BiomeType, 8> m_neighbor_biome;
unsigned m_chunk_seed = 0;
void make_biome_builder(); void make_biome_builder();
void generate_cave();
}; };
} // namespace Cubed } // namespace Cubed

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include "Cubed/AABB.hpp" #include "Cubed/AABB.hpp"
#include "Cubed/gameplay/cave_carver.hpp"
#include "Cubed/gameplay/chunk.hpp" #include "Cubed/gameplay/chunk.hpp"
#include <atomic> #include <atomic>
@@ -45,6 +46,7 @@ private:
std::atomic<bool> m_gen_running{false}; std::atomic<bool> m_gen_running{false};
std::atomic<bool> m_need_gen_chunk{false}; std::atomic<bool> m_need_gen_chunk{false};
std::atomic<bool> m_is_rebuilding{false}; 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_could_gen{true};
std::atomic<int> m_rendering_distance{24}; std::atomic<int> m_rendering_distance{24};
std::atomic<float> m_chunk_gen_fraction{0.0f}; std::atomic<float> m_chunk_gen_fraction{0.0f};
@@ -53,6 +55,8 @@ private:
std::vector<std::pair<ChunkPos, Chunk>> m_new_chunk; std::vector<std::pair<ChunkPos, Chunk>> m_new_chunk;
std::vector<std::pair<ChunkPos, Chunk>> m_new_chunk_queue; std::vector<std::pair<ChunkPos, Chunk>> m_new_chunk_queue;
CaveCarver m_cave_carcer;
void init_chunks(); void init_chunks();
void gen_chunks_internal(); void gen_chunks_internal();
@@ -106,6 +110,8 @@ public:
void rendering_distance(int rendering_distance); void rendering_distance(int rendering_distance);
void start_gen_thread(); void start_gen_thread();
void stop_gen_thread(); void stop_gen_thread();
CaveCarver& cave_carcer();
}; };
} // namespace Cubed } // namespace Cubed

View File

@@ -27,6 +27,10 @@ inline uint32_t mix_hash(int32_t a, int32_t b, uint32_t fixed_seed) {
return h; return h;
} }
inline uint32_t combine_32(uint32_t seed, uint32_t v) {
seed ^= v + 0x9e3779b9 + (seed << 6) + (seed >> 2);
return seed;
}
} // namespace HASH } // namespace HASH
} // namespace Cubed } // namespace Cubed

View File

@@ -5,12 +5,14 @@ namespace Cubed {
class Random { class Random {
public: public:
Random(); Random();
Random(unsigned seed);
bool random_bool(double probability); bool random_bool(double probability);
std::mt19937& engine(); std::mt19937& engine();
unsigned seed(); unsigned seed();
void init(unsigned seed); void init(unsigned seed);
int random_int(int min, int max);
float random_float(float min, float max);
private: private:
unsigned int m_seed = 0; unsigned int m_seed = 0;

View File

@@ -4,8 +4,11 @@
namespace Cubed { namespace Cubed {
namespace Math { namespace Math {
void extract_frustum_planes(const glm::mat4& mvp_matrix, void extract_frustum_planes(const glm::mat4& mvp_matrix,
std::vector<glm::vec4>& planes); std::vector<glm::vec4>& planes);
}
float smootherstep(float edge0, float edge1, float x);
} // namespace Math
} // namespace Cubed } // namespace Cubed

View File

@@ -34,6 +34,17 @@ constexpr int AMPLITUDE_MAX = 80;
constexpr float TREE_FREQ_MIM = 0.001f; constexpr float TREE_FREQ_MIM = 0.001f;
constexpr float TREE_FREQ_MAX = 0.3f; constexpr float TREE_FREQ_MAX = 0.3f;
constexpr float CAVE_PROBABILITY_MIN = 0.005f;
constexpr float CAVE_PROBABILITY_MAX = 0.1f;
constexpr float RADIUS_XZ_MIN = 1.0f;
constexpr float RADIUS_XZ_MAX = 50.0f;
constexpr float RADIUS_Y_MIN = 1.0f;
constexpr float RADIUS_Y_MAX = 50.0f;
constexpr float DELTA_ANGLE_MIN = -30.0f;
constexpr float DELTA_ANGLE_MAX = 30.0f;
constexpr int CAVE_STEP_MIN = 1;
constexpr int CAVE_STEP_MAX = 1000;
static int filter_unsigned(ImGuiInputTextCallbackData* data) { static int filter_unsigned(ImGuiInputTextCallbackData* data) {
if (data->EventFlag == ImGuiInputTextFlags_CallbackCharFilter) { if (data->EventFlag == ImGuiInputTextFlags_CallbackCharFilter) {
char c = data->EventChar; char c = data->EventChar;
@@ -108,7 +119,7 @@ void DevPanel::show_about_table_bar() {
} }
void DevPanel::show_biome_table_bar() { void DevPanel::show_biome_table_bar() {
ImGui::Text("Biome");
if (ImGui::BeginTabBar("Biome")) { if (ImGui::BeginTabBar("Biome")) {
if (ImGui::BeginTabItem("Plain")) { if (ImGui::BeginTabItem("Plain")) {
ImGui::SliderFloat("MinTemp##plain", &plain_params().temp.first, ImGui::SliderFloat("MinTemp##plain", &plain_params().temp.first,
@@ -253,6 +264,30 @@ void DevPanel::show_biome_table_bar() {
} }
} }
void DevPanel::show_cave_table_bar() {
auto& cave_carcer = m_app.world().cave_carcer();
ImGui::Text("Total Cave Sum %d", cave_carcer.cave_sum());
ImGui::SliderFloat("Cave Probability", &cave_carcer.cave_probability(),
CAVE_PROBABILITY_MIN, CAVE_PROBABILITY_MAX);
ImGui::SliderFloat("Radius XZ Min", &CavePath::radius_xz_min(),
RADIUS_XZ_MIN, RADIUS_XZ_MAX);
ImGui::SliderFloat("Radius XZ Max", &CavePath::radius_xz_max(),
RADIUS_XZ_MIN, RADIUS_XZ_MAX);
ImGui::SliderFloat("Radius Y Min", &CavePath::radius_y_min(), RADIUS_Y_MIN,
RADIUS_Y_MAX);
ImGui::SliderFloat("Radius Y Max", &CavePath::radius_y_max(), RADIUS_Y_MIN,
RADIUS_Y_MAX);
ImGui::SliderFloat("Delta Angle Min", &CavePath::delta_angle_min(),
DELTA_ANGLE_MIN, 0.0f);
ImGui::SliderFloat("Delta Angle Max", &CavePath::delta_angle_max(), 0.0f,
DELTA_ANGLE_MAX);
ImGui::SliderInt("Step Min", &CavePath::step_min(), CAVE_STEP_MIN,
CAVE_STEP_MAX);
ImGui::SliderInt("Step Max", &CavePath::step_max(), CAVE_STEP_MIN,
CAVE_STEP_MAX);
}
void DevPanel::show_settings_tab_item() { void DevPanel::show_settings_tab_item() {
if (ImGui::BeginTabItem("settings")) { if (ImGui::BeginTabItem("settings")) {
if (ImGui::SliderFloat("FOV", &m_config.fov, 1.0f, 140.0f)) { if (ImGui::SliderFloat("FOV", &m_config.fov, 1.0f, 140.0f)) {
@@ -354,7 +389,8 @@ void DevPanel::show_settings_tab_item() {
void DevPanel::show_world_tab_item() { void DevPanel::show_world_tab_item() {
if (ImGui::BeginTabItem("world")) { if (ImGui::BeginTabItem("world")) {
if (m_text_editing.perlin_seed) { if (m_text_editing.perlin_seed) {
if (ImGui::InputText("Perlin Noise Seed", perlin_noise_input_buffer, if (ImGui::InputText("ChunkGenerator Seed",
perlin_noise_input_buffer,
sizeof(perlin_noise_input_buffer), sizeof(perlin_noise_input_buffer),
ImGuiInputTextFlags_CallbackCharFilter | ImGuiInputTextFlags_CallbackCharFilter |
ImGuiInputTextFlags_EnterReturnsTrue, ImGuiInputTextFlags_EnterReturnsTrue,
@@ -397,7 +433,17 @@ void DevPanel::show_world_tab_item() {
} }
ImGui::Text("Chunk Build Progress\n"); ImGui::Text("Chunk Build Progress\n");
ImGui::ProgressBar(m_app.world().chunk_gen_fraction()); ImGui::ProgressBar(m_app.world().chunk_gen_fraction());
show_biome_table_bar(); if (ImGui::BeginTabBar("World Settings")) {
if (ImGui::BeginTabItem("Cave")) {
show_cave_table_bar();
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Biome")) {
show_biome_table_bar();
ImGui::EndTabItem();
}
ImGui::EndTabBar();
}
ImGui::EndTabItem(); ImGui::EndTabItem();
} }
} }

View File

@@ -0,0 +1,52 @@
#include "Cubed/gameplay/cave_carver.hpp"
#include "Cubed/constants.hpp"
namespace Cubed {
CaveCarver::CaveCarver() {}
std::unordered_map<int, CavePath>& CaveCarver::paths() { return m_paths; }
void CaveCarver::init(unsigned world_seed) {
m_seed = world_seed;
m_sum = 0;
m_random.init(m_seed);
}
void CaveCarver::reload(unsigned world_seed) {
m_seed = world_seed;
m_paths.clear();
init(world_seed);
}
void CaveCarver::add_path(const glm::vec3& pos) {
m_paths.emplace(m_sum, CavePath{m_seed, m_sum, pos});
m_sum++;
}
void CaveCarver::try_to_add_path(const ChunkPos& chunk_pos,
unsigned chunk_seed) {
Random random{chunk_seed};
if (random.random_bool(static_cast<double>(m_cave_probability))) {
const int CHUNK_MIN_X = chunk_pos.x * CHUNK_SIZE;
const int CHUNK_MIN_Z = chunk_pos.z * CHUNK_SIZE;
const int CHUNK_MAX_X = CHUNK_MIN_X + SIZE_X - 1;
const int CHUNK_MAX_Z = CHUNK_MIN_Z + SIZE_Z - 1;
const int CHUNK_MIN_Y = 0;
const int CHUNK_MAX_Y = SIZE_Y - 1;
int max_y = std::min(CHUNK_MAX_Y, 40);
int x = random.random_int(CHUNK_MIN_X, CHUNK_MAX_X);
int y = random.random_int(CHUNK_MIN_Y + 1, max_y);
int z = random.random_int(CHUNK_MIN_Z, CHUNK_MAX_Z);
add_path(glm::vec3{x, y, z});
}
}
void CaveCarver::cleanup_finished_caves() {
std::erase_if(m_paths,
[](const auto& kv) { return kv.second.is_finished(); });
}
int CaveCarver::cave_sum() const { return m_sum; }
float& CaveCarver::cave_probability() { return m_cave_probability; }
} // namespace Cubed

View File

@@ -0,0 +1,95 @@
#include "Cubed/gameplay/cave_path.hpp"
#include "Cubed/constants.hpp"
#include "Cubed/tools/cubed_hash.hpp"
#include "Cubed/tools/math_tools.hpp"
#include <algorithm>
namespace Cubed {
CavePath::CavePath(unsigned int world_seed, int path_id,
const glm::vec3& start_pos) {
m_path_id = path_id;
m_seed = HASH::combine_32(world_seed, path_id);
m_random.init(m_seed);
m_yaw = m_random.random_float(0.0f, 360.0f);
m_pitch = m_random.random_float(-10.0f, 10.0f);
m_start_path_point.pos = start_pos;
m_start_path_point.rad_xz =
m_random.random_float(m_radius_xz_min, m_radius_xz_max);
m_start_path_point.rad_y =
m_random.random_float(m_radius_y_min, m_radius_y_max);
m_step = m_random.random_int(m_step_min, m_step_max);
m_points.reserve(m_step + 1);
m_points.push_back(m_start_path_point);
collect_path_points();
precompute_chunk_coverage();
}
void CavePath::collect_path_points() {
for (int i = 0; i < m_step; i++) {
m_yaw = std::fmod(m_yaw, 360.0f);
if (m_yaw < 0.0f)
m_yaw += 360.0f;
m_pitch = std::clamp(m_pitch, -90.0f, 90.0f);
float dx = std::cos(glm::radians(m_pitch)) *
std::sin(glm::radians(m_yaw)) * m_step_len;
float dy = std::sin(glm::radians(m_pitch)) * m_step_len;
float dz = std::cos(glm::radians(m_pitch)) *
std::cos(glm::radians(m_yaw)) * m_step_len;
m_points[i].tangent = glm::normalize(glm::vec3{dx, dy, dz});
float t = Math::smootherstep(0, m_step - 1, i);
float drad_xz = m_start_path_point.rad_xz * (1.0f - t);
float drad_y = m_start_path_point.rad_y * (1.0f - t);
drad_xz = std::max(drad_xz, 4.0f);
drad_y = std::max(drad_y, 4.0f);
m_points.emplace_back(m_points[i].pos + glm::vec3{dx, dy, dz}, drad_xz,
drad_y);
m_yaw += m_random.random_float(m_delta_angle_min, m_delta_angle_max);
m_pitch += m_random.random_float(m_delta_angle_min, m_delta_angle_max);
}
auto n = m_points.size();
if (n >= 2) {
m_points[n - 1].tangent = m_points[n - 2].tangent;
}
}
void CavePath::precompute_chunk_coverage() {
for (const auto& point : m_points) {
float rad = point.rad_xz;
const glm::vec3& center = point.pos;
int min_cx =
static_cast<int>(std::floor((center.x - rad) / CHUNK_SIZE));
int max_cx =
static_cast<int>(std::floor((center.x + rad) / CHUNK_SIZE));
int min_cz =
static_cast<int>(std::floor((center.z - rad) / CHUNK_SIZE));
int max_cz =
static_cast<int>(std::floor((center.z + rad) / CHUNK_SIZE));
for (int cx = min_cx; cx <= max_cx; ++cx)
for (int cz = min_cz; cz <= max_cz; ++cz)
m_pending_chunks.insert({cx, cz});
}
}
void CavePath::clear_chunk(const ChunkPos& pos) { m_pending_chunks.erase(pos); }
const std::vector<PathPoint>& CavePath::points() const { return m_points; }
bool CavePath::is_finished() const { return m_pending_chunks.empty(); }
float& CavePath::radius_xz_min() { return m_radius_xz_min; }
float& CavePath::radius_xz_max() { return m_radius_xz_max; }
float& CavePath::radius_y_min() { return m_radius_y_min; }
float& CavePath::radius_y_max() { return m_radius_y_max; }
float& CavePath::delta_angle_min() { return m_delta_angle_min; }
float& CavePath::delta_angle_max() { return m_delta_angle_max; }
int& CavePath::step_min() { return m_step_min; }
int& CavePath::step_max() { return m_step_max; }
} // namespace Cubed

View File

@@ -24,7 +24,7 @@ Chunk::Chunk(Chunk&& other) noexcept
m_chunk_pos(std::move(other.m_chunk_pos)), m_world(other.m_world), m_chunk_pos(std::move(other.m_chunk_pos)), m_world(other.m_world),
m_heightmap(std::move(other.m_heightmap)), m_heightmap(std::move(other.m_heightmap)),
m_blocks(std::move(other.m_blocks)), m_vbo(other.m_vbo), m_blocks(std::move(other.m_blocks)), m_vbo(other.m_vbo),
m_vertexs_data(std::move(other.m_vertexs_data)) { m_vertexs_data(std::move(other.m_vertexs_data)), m_seed(other.m_seed) {
other.m_vbo = 0; other.m_vbo = 0;
} }
@@ -43,6 +43,7 @@ Chunk& Chunk::operator=(Chunk&& other) noexcept {
m_is_on_gen_vertex_data = other.m_is_on_gen_vertex_data.load(); m_is_on_gen_vertex_data = other.m_is_on_gen_vertex_data.load();
m_need_upload = other.m_need_upload.load(); m_need_upload = other.m_need_upload.load();
m_vertex_sum = other.m_vertex_sum.load(); m_vertex_sum = other.m_vertex_sum.load();
m_seed = other.m_seed;
return *this; return *this;
} }
@@ -213,6 +214,7 @@ void Chunk::gen_phase_one() {
return; return;
} }
m_generator->assign_chunk_biome(); m_generator->assign_chunk_biome();
m_seed = m_generator->chunk_seed();
} }
void Chunk::gen_phase_two(const std::array<const Chunk*, 8>& adj_chunks) { void Chunk::gen_phase_two(const std::array<const Chunk*, 8>& adj_chunks) {
@@ -255,7 +257,7 @@ void Chunk::gen_phase_six(
Logger::error("ChunkGenerator is Nullptr"); Logger::error("ChunkGenerator is Nullptr");
return; return;
} }
m_generator->blend_surface_blocks_borders(neighbor_block); // m_generator->blend_surface_blocks_borders(neighbor_block);
} }
void Chunk::gen_phase_seven() { void Chunk::gen_phase_seven() {
@@ -307,4 +309,11 @@ void Chunk::biome(BiomeType b) { m_biome = b; }
HeightMapArray& Chunk::heightmap() { return m_heightmap; } HeightMapArray& Chunk::heightmap() { return m_heightmap; }
std::vector<uint8_t>& Chunk::blocks() { return m_blocks; } std::vector<uint8_t>& Chunk::blocks() { return m_blocks; }
World& Chunk::world() { return m_world; }
unsigned Chunk::seed() const {
if (m_seed == 0) {
Logger::warn("Seed Not Generator");
}
return m_seed;
}
} // namespace Cubed } // namespace Cubed

View File

@@ -7,9 +7,9 @@
#include "Cubed/gameplay/builders/river_builder.hpp" #include "Cubed/gameplay/builders/river_builder.hpp"
#include "Cubed/gameplay/chunk.hpp" #include "Cubed/gameplay/chunk.hpp"
#include "Cubed/gameplay/tree.hpp" #include "Cubed/gameplay/tree.hpp"
#include "Cubed/gameplay/world.hpp"
#include "Cubed/tools/cubed_hash.hpp" #include "Cubed/tools/cubed_hash.hpp"
#include "Cubed/tools/perlin_noise.hpp" #include "Cubed/tools/perlin_noise.hpp"
namespace Cubed { namespace Cubed {
using enum BiomeType; using enum BiomeType;
@@ -20,6 +20,7 @@ ChunkGenerator::ChunkGenerator(Chunk& chunk) : m_chunk(chunk) {
ChunkPos pos = m_chunk.get_chunk_pos(); ChunkPos pos = m_chunk.get_chunk_pos();
unsigned seed = HASH::mix_hash(pos.x, pos.z, m_generator_seed); unsigned seed = HASH::mix_hash(pos.x, pos.z, m_generator_seed);
m_random.init(seed); m_random.init(seed);
m_chunk_seed = seed;
} }
void ChunkGenerator::init() { void ChunkGenerator::init() {
@@ -43,7 +44,12 @@ void ChunkGenerator::seed(unsigned s) {
is_seed_change = true; is_seed_change = true;
m_generator_seed = s; m_generator_seed = s;
} }
unsigned ChunkGenerator::chunk_seed() const {
if (m_chunk_seed == 0) {
Logger::warn("Chunk Seed Generator Fail");
}
return m_chunk_seed;
}
void ChunkGenerator::assign_chunk_biome() { void ChunkGenerator::assign_chunk_biome() {
auto m_chunk_pos = m_chunk.chunk_pos(); auto m_chunk_pos = m_chunk.chunk_pos();
float x = static_cast<float>(m_chunk_pos.x); float x = static_cast<float>(m_chunk_pos.x);
@@ -356,6 +362,7 @@ void ChunkGenerator::generate_terrain_blocks() {
} }
m_chunk.blocks().assign(CHUNK_SIZE * CHUNK_SIZE * WORLD_SIZE_Y, 0); m_chunk.blocks().assign(CHUNK_SIZE * CHUNK_SIZE * WORLD_SIZE_Y, 0);
m_biome_builder->build_biome(); m_biome_builder->build_biome();
generate_cave();
} }
void ChunkGenerator::blend_surface_blocks_borders( void ChunkGenerator::blend_surface_blocks_borders(
@@ -511,6 +518,61 @@ void ChunkGenerator::make_biome_builder() {
} }
} }
void ChunkGenerator::generate_cave() {
auto& cave_carver = m_chunk.world().cave_carcer();
auto& paths = cave_carver.paths();
const auto& chunk_pos = m_chunk.chunk_pos();
auto& blocks = m_chunk.blocks();
const int CHUNK_MIN_X = chunk_pos.x * CHUNK_SIZE;
const int CHUNK_MIN_Z = chunk_pos.z * CHUNK_SIZE;
const int CHUNK_MAX_X = CHUNK_MIN_X + SIZE_X - 1;
const int CHUNK_MAX_Z = CHUNK_MIN_Z + SIZE_Z - 1;
const int CHUNK_MIN_Y = 0;
const int CHUNK_MAX_Y = SIZE_Y - 1;
for (auto& [id, path] : paths) {
for (const auto& point : path.points()) {
const glm::vec3& center = point.pos;
float rad_xz = point.rad_xz;
float rad_y = point.rad_y;
int min_x = static_cast<int>(std::floor(center.x - rad_xz));
int max_x = static_cast<int>(std::floor(center.x + rad_xz));
int min_z = static_cast<int>(std::floor(center.z - rad_xz));
int max_z = static_cast<int>(std::floor(center.z + rad_xz));
int min_y = static_cast<int>(std::floor(center.y - rad_y));
int max_y = static_cast<int>(std::floor(center.y + rad_y));
min_x = std::max(min_x, CHUNK_MIN_X);
max_x = std::min(max_x, CHUNK_MAX_X);
min_z = std::max(min_z, CHUNK_MIN_Z);
max_z = std::min(max_z, CHUNK_MAX_Z);
min_y = std::max(min_y, CHUNK_MIN_Y);
max_y = std::min(max_y, CHUNK_MAX_Y);
for (int wx = min_x; wx <= max_x; ++wx) {
int x = wx - CHUNK_MIN_X;
for (int wz = min_z; wz <= max_z; ++wz) {
int z = wz - CHUNK_MIN_Z;
for (int wy = min_y; wy <= max_y; ++wy) {
int y = wy;
glm::vec3 pos(static_cast<float>(wx),
static_cast<float>(wy),
static_cast<float>(wz));
if (point.contains(pos)) {
if (y == 0) {
continue;
}
blocks[Chunk::get_index(x, y, z)] = 0;
}
}
}
}
}
path.clear_chunk(chunk_pos);
}
}
Chunk& ChunkGenerator::chunk() { return m_chunk; } Chunk& ChunkGenerator::chunk() { return m_chunk; }
Random& ChunkGenerator::random() { return m_random; } Random& ChunkGenerator::random() { return m_random; }

View File

@@ -237,10 +237,7 @@ void Player::update_front_vec(float offset_x, float offset_y) {
m_yaw = std::fmod(m_yaw, 360.0); m_yaw = std::fmod(m_yaw, 360.0);
if (m_pitch > 89.0f) m_pitch = std::clamp(m_pitch, -89.0f, 89.0f);
m_pitch = 89.0f;
if (m_pitch < -89.0f)
m_pitch = -89.0f;
m_front.x = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch)); m_front.x = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
m_front.y = sin(glm::radians(m_pitch)); m_front.y = sin(glm::radians(m_pitch));

View File

@@ -7,8 +7,6 @@
#include "Cubed/tools/cubed_hash.hpp" #include "Cubed/tools/cubed_hash.hpp"
#include "Cubed/tools/math_tools.hpp" #include "Cubed/tools/math_tools.hpp"
#include <execution>
namespace Cubed { namespace Cubed {
struct ChunkRenderData { struct ChunkRenderData {
@@ -65,23 +63,30 @@ Player& World::get_player(const std::string& name) {
} }
void World::init_world() { void World::init_world() {
m_cave_carcer.init(ChunkGenerator::seed());
m_chunks.reserve(MAX_DISTANCE * MAX_DISTANCE * 4); m_chunks.reserve(MAX_DISTANCE * MAX_DISTANCE * 4);
auto t1 = std::chrono::system_clock::now(); auto t1 = std::chrono::system_clock::now();
Logger::info("Max Support Thread is {}", Logger::info("Max Support Thread is {}",
std::thread::hardware_concurrency()); std::thread::hardware_concurrency());
// init players
m_players.emplace(HASH::str("TestPlayer"), Player(*this, "TestPlayer"));
start_gen_thread();
init_chunks(); init_chunks();
auto t2 = std::chrono::system_clock::now(); auto t2 = std::chrono::system_clock::now();
auto d = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1); auto d = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
Logger::info("Chunk Block Init Finish, Time Consuming: {}", d); Logger::info("Chunk Block Init Finish, Time Consuming: {}", d);
// init players
m_players.emplace(HASH::str("TestPlayer"), Player(*this, "TestPlayer"));
Logger::info("TestPlayer Create Finish"); Logger::info("TestPlayer Create Finish");
start_gen_thread();
hot_reload();
} }
void World::init_chunks() {
hot_reload();
while (!m_chunk_gen_finished) {
std::this_thread::sleep_for(std::chrono::microseconds(200));
}
}
/*
void World::init_chunks() { void World::init_chunks() {
int dis_x = PRE_LOAD_DISTANCE; int dis_x = PRE_LOAD_DISTANCE;
@@ -112,11 +117,13 @@ void World::init_chunks() {
} }
} }
} }
for (auto& [pos, chunks] : m_chunks) { for (auto& [pos, chunk] : m_chunks) {
chunks.gen_phase_one(); chunk.gen_phase_one();
m_cave_carcer.try_to_add_path(pos, chunk.seed());
} }
for (auto& [pos, chunks] : temp_neighbor) { for (auto& [pos, chunk] : temp_neighbor) {
chunks.gen_phase_one(); chunk.gen_phase_one();
m_cave_carcer.try_to_add_path(pos, chunk.seed());
} }
std::array<const Chunk*, 8> neighbor_chunks; std::array<const Chunk*, 8> neighbor_chunks;
@@ -251,6 +258,8 @@ void World::init_chunks() {
sync.store(1, std::memory_order_release); sync.store(1, std::memory_order_release);
sync.load(std::memory_order_acquire); sync.load(std::memory_order_acquire);
m_cave_carcer.cleanup_finished_caves();
std::vector<ChunkRenderData> pending_gen_data; std::vector<ChunkRenderData> pending_gen_data;
pending_gen_data.reserve(m_chunks.size()); pending_gen_data.reserve(m_chunks.size());
for (auto& [pos, chunk] : m_chunks) { for (auto& [pos, chunk] : m_chunks) {
@@ -278,7 +287,7 @@ void World::init_chunks() {
chunk.upload_to_gpu(); chunk.upload_to_gpu();
} }
} }
*/
void World::render(const glm::mat4& mvp_matrix) { void World::render(const glm::mat4& mvp_matrix) {
Math::extract_frustum_planes(mvp_matrix, m_planes); Math::extract_frustum_planes(mvp_matrix, m_planes);
int rendered_sum = 0; int rendered_sum = 0;
@@ -325,6 +334,7 @@ ChunkPos World::chunk_pos(int world_x, int world_z) {
void World::gen_chunks_internal() { void World::gen_chunks_internal() {
m_chunk_gen_fraction = 0.0f; m_chunk_gen_fraction = 0.0f;
m_chunk_gen_finished = false;
ChunkPosSet required_chunks; ChunkPosSet required_chunks;
compute_required_chunks(required_chunks); compute_required_chunks(required_chunks);
@@ -356,9 +366,11 @@ void World::gen_chunks_internal() {
for (auto& [pos, chunk] : new_chunks) { for (auto& [pos, chunk] : new_chunks) {
chunk.gen_phase_one(); chunk.gen_phase_one();
m_cave_carcer.try_to_add_path(pos, chunk.seed());
} }
for (auto& [pos, chunk] : temp_neighbor) { for (auto& [pos, chunk] : temp_neighbor) {
chunk.gen_phase_one(); chunk.gen_phase_one();
m_cave_carcer.try_to_add_path(pos, chunk.seed());
} }
m_chunk_gen_fraction = 0.2f; m_chunk_gen_fraction = 0.2f;
std::array<const Chunk*, 8> neighbor_chunks; std::array<const Chunk*, 8> neighbor_chunks;
@@ -500,7 +512,9 @@ void World::gen_chunks_internal() {
m_new_chunk_queue.emplace_back(std::move(x)); m_new_chunk_queue.emplace_back(std::move(x));
} }
} }
m_cave_carcer.cleanup_finished_caves();
m_chunk_gen_fraction = 1.0f; m_chunk_gen_fraction = 1.0f;
m_chunk_gen_finished = true;
} }
void World::sync_player_pos(glm::vec3& player_pos) { void World::sync_player_pos(glm::vec3& player_pos) {
@@ -819,7 +833,7 @@ void World::rebuild_world() {
} }
m_is_rebuilding = true; m_is_rebuilding = true;
stop_gen_thread(); stop_gen_thread();
m_cave_carcer.reload(ChunkGenerator::seed());
{ {
std::scoped_lock lk(m_chunks_mutex, m_new_chunk_queue_mutex); std::scoped_lock lk(m_chunks_mutex, m_new_chunk_queue_mutex);
m_chunks.clear(); m_chunks.clear();
@@ -841,4 +855,6 @@ void World::rendering_distance(int rendering_distance) {
m_rendering_distance = rendering_distance; m_rendering_distance = rendering_distance;
} }
CaveCarver& World::cave_carcer() { return m_cave_carcer; }
} // namespace Cubed } // namespace Cubed

View File

@@ -1,11 +1,9 @@
#include "Cubed/tools/cubed_random.hpp" #include "Cubed/tools/cubed_random.hpp"
#include "Cubed/tools/log.hpp"
namespace Cubed { namespace Cubed {
Random::Random() {} Random::Random() {}
Random::Random(unsigned seed) { init(seed); }
bool Random::random_bool(double probability) { bool Random::random_bool(double probability) {
std::bernoulli_distribution dist(probability); std::bernoulli_distribution dist(probability);
return dist(m_engine); return dist(m_engine);
@@ -19,5 +17,13 @@ void Random::init(unsigned seed) {
m_seed = seed; m_seed = seed;
m_engine.seed(seed); m_engine.seed(seed);
} }
int Random::random_int(int min, int max) {
std::uniform_int_distribution<int> dist(min, max);
return dist(m_engine);
}
float Random::random_float(float min, float max) {
std::uniform_real_distribution<float> dist(min, max);
return dist(m_engine);
}
} // namespace Cubed } // namespace Cubed

View File

@@ -1,10 +1,11 @@
#include "Cubed/tools/math_tools.hpp" #include "Cubed/tools/math_tools.hpp"
#include <algorithm>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
namespace Cubed { namespace Cubed {
namespace Math { namespace Math {
void extract_frustum_planes(const glm::mat4& mvp_matrix, void extract_frustum_planes(const glm::mat4& mvp_matrix,
std::vector<glm::vec4>& planes) { std::vector<glm::vec4>& planes) {
if (planes.size() != 6) { if (planes.size() != 6) {
@@ -37,6 +38,13 @@ void extract_frustum_planes(const glm::mat4& mvp_matrix,
} }
} }
float smootherstep(float edge0, float edge1, float x) {
x = std::clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
return x * x * x * (x * (6.0f * x - 15.0f) + 10.0f);
}
} // namespace Math } // namespace Math
} // namespace Cubed } // namespace Cubed