feat: add 4 biomes

This commit is contained in:
2026-04-17 19:55:47 +08:00
parent be176ff18a
commit 5723c17f94
23 changed files with 259 additions and 40 deletions

View File

@@ -1,6 +1,6 @@
#pragma once
constexpr int WORLD_SIZE_Y = 256;
constexpr int MAX_BLOCK_NUM = 4;
constexpr int MAX_BLOCK_NUM = 5;
constexpr int MAX_UI_NUM = 1;
constexpr int CHUCK_SIZE = 16;
@@ -9,7 +9,8 @@ constexpr int MAX_BLOCK_STATUS = 1;
constexpr int MAX_CHARACTER = 128;
constexpr float NORMAL_FOV = 70.0f;
constexpr int SEED = 999;
constexpr int MAX_BIOME_SUM = 4;
constexpr float BIOME_NOISE_FREQUENCY = 0.005f;
constexpr float VERTICES_POS[6][6][3] = {
// ===== front (z = +1) =====

View File

@@ -17,6 +17,7 @@ private:
static constexpr int SIZE_Y = WORLD_SIZE_Y;
static constexpr int SIZE_Z = CHUCK_SIZE;
Biome m_biome = Biome::PLAIN;
ChunkPos m_chunk_pos;
World& m_world;
// the index is a array of block id
@@ -28,6 +29,10 @@ private:
float height = 80;
void clear_dirty();
void resolve_biome();
void resolve_blocks();
public:
Chunk(World& world, ChunkPos chunk_pos);
~Chunk();
@@ -35,6 +40,9 @@ public:
Chunk& operator=(const Chunk&) = delete;
Chunk(Chunk&&);
Chunk& operator=(Chunk&&);
Biome get_biome() const;
const std::vector<uint8_t>& get_chunk_blocks() const;
static int get_index(int x, int y, int z);

View File

@@ -1,5 +1,9 @@
#pragma once
#include <functional>
#include <string>
#include <Cubed/tools/log.hpp>
#include <Cubed/tools/cubed_assert.hpp>
struct ChunkPos {
int x;
int z;
@@ -24,6 +28,90 @@ struct ChunkPos {
};
};
enum class Biome {
PLAIN = 0,
FOREST,
DESERT,
MOUNTAIN
};
struct BiomeHeightRange {
int base_y;
int amplitude;
};
constexpr inline std::string get_biome_str(Biome biome) {
std::string str;
using enum Biome;
switch (biome) {
case PLAIN:
str = "Plain";
break;
case FOREST:
str = "Forest";
break;
case DESERT:
str = "Desert";
break;
case MOUNTAIN:
str = "Mountain";
break;
}
return str;
};
inline Biome get_biome_from_noise(float temp, float humid) {
if (temp < 0.5f && humid < 0.5f) return Biome::MOUNTAIN;
if (temp < 0.5f && humid >= 0.5f) return Biome::PLAIN;
if (temp >= 0.5f && humid < 0.5f) return Biome::DESERT;
return Biome::FOREST;
}
constexpr inline std::array<float, 3> get_noise_frequencies_for_biome(Biome biome) {
using enum Biome;
switch (biome) {
case PLAIN:
return {0.003f, 0.008f, 0.018f};
case FOREST:
return {0.004f, 0.012f, 0.022f};
case DESERT:
return {0.003f, 0.008f, 0.018f};
case MOUNTAIN:
return {0.006f, 0.015f, 0.03f};
}
Logger::warn("Unknown Biome");
return {0.003f, 0.015f, 0.06f};
}
constexpr inline BiomeHeightRange get_biome_height_range(Biome biome) {
using enum Biome;
switch (biome) {
case PLAIN:
return {62, 4};
case FOREST:
return {64, 8};
case DESERT:
return {61, 8};
case MOUNTAIN:
return {70, 70};
}
Logger::warn("Unknown Biome");
return {62, 4};
}
inline Biome safe_int_to_biome(int x) {
using enum Biome;
static const std::unordered_map<int, Biome> INT_TO_BIOME_MAP {
{0, PLAIN},
{1, FOREST},
{2, DESERT},
{3, MOUNTAIN}
};
auto it = INT_TO_BIOME_MAP.find(x);
CUBED_ASSERT_MSG(it != INT_TO_BIOME_MAP.end(), ":Can't Find");
return it->second;
}

View File

@@ -27,7 +27,7 @@ private:
std::vector<glm::vec4> m_planes;
std::thread m_gen_thread;
std::mutex m_chunks_mutex;
mutable std::mutex m_chunks_mutex;
std::mutex m_gen_signal_mutex;
std::mutex m_new_chunk_queue_mutex;
std::mutex m_delete_vbo_mutex;
@@ -54,7 +54,10 @@ public:
bool can_move(const AABB& player_box) const;
//const BlockRenderData& get_block_render_data(int x, int y ,int z);
const std::optional<LookBlock>& get_look_block_pos(const std::string& name) const;
const Chunk* get_chunk(const ChunkPos& pos) const;
Player& get_player(const std::string& name);
void init_world();
bool is_aabb_in_frustum(const glm::vec3& center, const glm::vec3& half_extents);

View File

@@ -2,4 +2,5 @@
#include <glm/glm.hpp>
namespace Math {
void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector<glm::vec4>& planes);
int get_interpolated_height(float world_x, float world_z, float biome_noise, float temp, float humid);
}