Compare commits

...

5 Commits

25 changed files with 281 additions and 48 deletions

View File

@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.14...3.24) cmake_minimum_required(VERSION 3.14...3.24)
project(Cubed LANGUAGES C CXX) project(Cubed LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 382 B

After

Width:  |  Height:  |  Size: 418 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 382 B

After

Width:  |  Height:  |  Size: 418 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 382 B

After

Width:  |  Height:  |  Size: 418 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 382 B

After

Width:  |  Height:  |  Size: 418 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 382 B

After

Width:  |  Height:  |  Size: 418 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 382 B

After

Width:  |  Height:  |  Size: 418 B

View File

@@ -1,15 +1,15 @@
#pragma once #pragma once
constexpr int WORLD_SIZE_Y = 256; 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 MAX_UI_NUM = 1;
constexpr int CHUCK_SIZE = 16; constexpr int CHUCK_SIZE = 16;
constexpr int DISTANCE = 16; constexpr int DISTANCE = 24;
constexpr int MAX_BLOCK_STATUS = 1; constexpr int MAX_BLOCK_STATUS = 1;
constexpr int MAX_CHARACTER = 128; constexpr int MAX_CHARACTER = 128;
constexpr float NORMAL_FOV = 70.0f; constexpr float NORMAL_FOV = 70.0f;
constexpr int SEED = 999; constexpr int MAX_BIOME_SUM = 4;
constexpr float VERTICES_POS[6][6][3] = { constexpr float VERTICES_POS[6][6][3] = {
// ===== front (z = +1) ===== // ===== front (z = +1) =====

View File

@@ -0,0 +1,155 @@
#pragma once
#include <array>
#include <cmath>
#include <string>
#include <unordered_map>
#include <Cubed/tools/cubed_assert.hpp>
#include <Cubed/tools/log.hpp>
#include <Cubed/tools/perlin_noise.hpp>
constexpr float BIOME_NOISE_FREQUENCY = 0.003f;
constexpr float PLAIN_FREQ = 0.5f;
constexpr float FOREST_FREQ = 1.0f;
constexpr float DESERT_FREQ = 1.0f;
constexpr float MOUNTAIN_FREQ = 2.0f;
enum class Biome {
PLAIN = 0,
FOREST,
DESERT,
MOUNTAIN
};
struct BiomeHeightRange {
int base_y;
int amplitude;
};
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) {
auto weight = [](float t, float h, float ct, float ch) -> float {
float dt = t - ct;
float dh = h - ch;
float dist = std::sqrt(dt*dt + dh*dh);
return std::max(0.0f, 0.5f - dist);
};
float w_m = weight(temp, humid, 0.25f, 0.15f);
float w_p = weight(temp, humid, 0.50f, 0.40f);
float w_d = weight(temp, humid, 0.75f, 0.15f);
float w_f = weight(temp, humid, 0.75f, 0.75f);
w_m = pow(w_m, 8); w_p = pow(w_p, 8); w_d = pow(w_d, 8); w_f = pow(w_f, 8);
if (w_m >= w_p && w_m >= w_d && w_m >= w_f) return Biome::MOUNTAIN;
if (w_p >= w_m && w_p >= w_d && w_p >= w_f) return Biome::PLAIN;
if (w_d >= w_m && w_d >= w_p && w_d >= w_f) return Biome::DESERT;
return Biome::FOREST;
}
inline std::array<float, 3> get_noise_frequencies_for_biome(Biome biome) {
using enum Biome;
switch (biome) {
case PLAIN:
return {0.003f, 0.010f, 0.020f};
case FOREST:
return {0.004f, 0.012f, 0.022f};
case DESERT:
return {0.003f, 0.010f, 0.020f};
case MOUNTAIN:
return {0.006f, 0.015f, 0.030f};
}
Logger::warn("Unknown Biome");
return {0.003f, 0.015f, 0.06f};
}
inline BiomeHeightRange get_biome_height_range(Biome biome) {
using enum Biome;
switch (biome) {
case PLAIN:
return {62, 8};
case FOREST:
return {64, 12};
case DESERT:
return {61, 12};
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;
}
inline int get_interpolated_height(float world_x, float world_z, float temp, float humid) {
auto weight = [](float t, float h, float ct, float ch) -> float {
float dt = t - ct;
float dh = h - ch;
float dist = std::sqrt(dt*dt + dh*dh);
return std::max(0.0f, 0.5f - dist);
};
float w_mountain = weight(temp, humid, 0.25f, 0.15f);
float w_plain = weight(temp, humid, 0.50f, 0.40f);
float w_desert = weight(temp, humid, 0.75f, 0.15f);
float w_forest = weight(temp, humid, 0.75f, 0.75f);
// adjust transitions between chunks
float pow_n = 8.0f; // the larger n is, the purer the biome
w_mountain = std::pow(w_mountain, pow_n) * MOUNTAIN_FREQ;
w_plain = std::pow(w_plain, pow_n) * PLAIN_FREQ;
w_desert = std::pow(w_desert, pow_n) * DESERT_FREQ;
w_forest = std::pow(w_forest, pow_n) * FOREST_FREQ;
float total = w_mountain + w_plain + w_desert + w_forest;
w_mountain /= total; w_plain /= total; w_desert /= total; w_forest /= total;
auto sample_height = [&](Biome b) -> float {
auto range = get_biome_height_range(b);
auto [f1, f2, f3] = get_noise_frequencies_for_biome(b);
float n =
1.00f * PerlinNoise::noise(world_x * f1, 0.5f, world_z * f1) +
0.50f * PerlinNoise::noise(world_x * f2, 0.5f, world_z * f2) +
0.25f * PerlinNoise::noise(world_x * f3, 0.5f, world_z * f3);
n /= 1.75f;
return range.base_y + n * range.amplitude;
};
float h = w_mountain * sample_height(Biome::MOUNTAIN)
+ w_plain * sample_height(Biome::PLAIN)
+ w_desert * sample_height(Biome::DESERT)
+ w_forest * sample_height(Biome::FOREST);
return static_cast<int>(h);
}

View File

@@ -4,7 +4,8 @@
#include <cstdint> #include <cstdint>
#include <Cubed/config.hpp> #include <Cubed/config.hpp>
#include <Cubed/gameplay/chunk_status.hpp> #include <Cubed/gameplay/biome.hpp>
#include <Cubed/gameplay/chunk_pos.hpp>
#include <Cubed/gameplay/block.hpp> #include <Cubed/gameplay/block.hpp>
class World; class World;
@@ -17,6 +18,7 @@ private:
static constexpr int SIZE_Y = WORLD_SIZE_Y; static constexpr int SIZE_Y = WORLD_SIZE_Y;
static constexpr int SIZE_Z = CHUCK_SIZE; static constexpr int SIZE_Z = CHUCK_SIZE;
Biome m_biome = Biome::PLAIN;
ChunkPos m_chunk_pos; ChunkPos m_chunk_pos;
World& m_world; World& m_world;
// the index is a array of block id // the index is a array of block id
@@ -28,6 +30,10 @@ private:
float height = 80; float height = 80;
void clear_dirty(); void clear_dirty();
void resolve_biome();
void resolve_blocks();
public: public:
Chunk(World& world, ChunkPos chunk_pos); Chunk(World& world, ChunkPos chunk_pos);
~Chunk(); ~Chunk();
@@ -35,6 +41,9 @@ public:
Chunk& operator=(const Chunk&) = delete; Chunk& operator=(const Chunk&) = delete;
Chunk(Chunk&&); Chunk(Chunk&&);
Chunk& operator=(Chunk&&); Chunk& operator=(Chunk&&);
Biome get_biome() const;
const std::vector<uint8_t>& get_chunk_blocks() const; const std::vector<uint8_t>& get_chunk_blocks() const;
static int get_index(int x, int y, int z); static int get_index(int x, int y, int z);

View File

@@ -1,5 +1,9 @@
#pragma once #pragma once
#include <functional> #include <functional>
#include <Cubed/tools/log.hpp>
#include <Cubed/tools/cubed_assert.hpp>
struct ChunkPos { struct ChunkPos {
int x; int x;
int z; int z;
@@ -24,7 +28,3 @@ struct ChunkPos {
}; };
}; };

View File

@@ -4,7 +4,7 @@
#include <Cubed/AABB.hpp> #include <Cubed/AABB.hpp>
#include <Cubed/config.hpp> #include <Cubed/config.hpp>
#include <Cubed/gameplay/block.hpp> #include <Cubed/gameplay/block.hpp>
#include <Cubed/gameplay/chunk_status.hpp> #include <Cubed/gameplay/chunk_pos.hpp>
#include <Cubed/input.hpp> #include <Cubed/input.hpp>
#include <optional> #include <optional>

View File

@@ -27,7 +27,7 @@ private:
std::vector<glm::vec4> m_planes; std::vector<glm::vec4> m_planes;
std::thread m_gen_thread; 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_gen_signal_mutex;
std::mutex m_new_chunk_queue_mutex; std::mutex m_new_chunk_queue_mutex;
std::mutex m_delete_vbo_mutex; std::mutex m_delete_vbo_mutex;
@@ -54,7 +54,10 @@ public:
bool can_move(const AABB& player_box) const; bool can_move(const AABB& player_box) const;
//const BlockRenderData& get_block_render_data(int x, int y ,int z); //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 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); Player& get_player(const std::string& name);
void init_world(); void init_world();
bool is_aabb_in_frustum(const glm::vec3& center, const glm::vec3& half_extents); bool is_aabb_in_frustum(const glm::vec3& center, const glm::vec3& half_extents);

View File

@@ -21,6 +21,7 @@ void DebugCollector::init_text() {
Text cpu_text("cpu"); Text cpu_text("cpu");
Text gpu_text("gpu"); Text gpu_text("gpu");
Text opengl_version_text("opengl_version"); Text opengl_version_text("opengl_version");
Text biome_text("biome");
version_text version_text
.position(0.0f, 100.0f) .position(0.0f, 100.0f)
.scale(0.8f) .scale(0.8f)
@@ -68,6 +69,10 @@ void DebugCollector::init_text() {
.text("OpenGL: " + std::to_string(GLVersion.major) + "." + std::to_string(GLVersion.minor)) .text("OpenGL: " + std::to_string(GLVersion.major) + "." + std::to_string(GLVersion.minor))
.scale(0.7f) .scale(0.7f)
.position(0.0f, 450.0f); .position(0.0f, 450.0f);
biome_text
.text("Biome: ")
.scale(0.8f)
.position(0.0f, 500.0f);
m_texts.insert({version_text.uuid(), std::move(version_text)}); m_texts.insert({version_text.uuid(), std::move(version_text)});
m_texts.insert({fps_text.uuid(), std::move(fps_text)}); m_texts.insert({fps_text.uuid(), std::move(fps_text)});
m_texts.insert({player_pos_text.uuid(), std::move(player_pos_text)}); m_texts.insert({player_pos_text.uuid(), std::move(player_pos_text)});
@@ -77,6 +82,7 @@ void DebugCollector::init_text() {
m_texts.insert({cpu_text.uuid(), std::move(cpu_text)}); m_texts.insert({cpu_text.uuid(), std::move(cpu_text)});
m_texts.insert({gpu_text.uuid(), std::move(gpu_text)}); m_texts.insert({gpu_text.uuid(), std::move(gpu_text)});
m_texts.insert({opengl_version_text.uuid(), std::move(opengl_version_text)}); m_texts.insert({opengl_version_text.uuid(), std::move(opengl_version_text)});
m_texts.insert({biome_text.uuid(), std::move(biome_text)});
} }
std::unordered_map<std::size_t, Text>& DebugCollector::all_texts() { std::unordered_map<std::size_t, Text>& DebugCollector::all_texts() {

View File

@@ -2,7 +2,10 @@
#include <Cubed/gameplay/world.hpp> #include <Cubed/gameplay/world.hpp>
#include <Cubed/tools/cubed_assert.hpp> #include <Cubed/tools/cubed_assert.hpp>
#include <Cubed/tools/log.hpp> #include <Cubed/tools/log.hpp>
#include <Cubed/tools/math_tools.hpp>
#include <Cubed/tools/perlin_noise.hpp> #include <Cubed/tools/perlin_noise.hpp>
#include <utility>
Chunk::Chunk(World& world, ChunkPos chunk_pos) : Chunk::Chunk(World& world, ChunkPos chunk_pos) :
m_world(world), m_world(world),
m_chunk_pos(chunk_pos) m_chunk_pos(chunk_pos)
@@ -23,7 +26,8 @@ Chunk::Chunk(Chunk&& other) :
m_world(other.m_world), m_world(other.m_world),
m_blocks(std::move(other.m_blocks)), m_blocks(std::move(other.m_blocks)),
m_dirty(other.is_dirty()), m_dirty(other.is_dirty()),
m_vertexs_data(std::move(other.m_vertexs_data)) m_vertexs_data(std::move(other.m_vertexs_data)),
m_biome(other.m_biome)
{ {
other.m_vbo = 0; other.m_vbo = 0;
} }
@@ -35,9 +39,14 @@ Chunk& Chunk::operator=(Chunk&& other) {
m_blocks = std::move(other.m_blocks); m_blocks = std::move(other.m_blocks);
m_dirty = other.is_dirty(); m_dirty = other.is_dirty();
m_vertexs_data = std::move(other.m_vertexs_data); m_vertexs_data = std::move(other.m_vertexs_data);
m_biome = other.m_biome;
return *this; return *this;
} }
Biome Chunk::get_biome() const {
return m_biome;
}
const std::vector<uint8_t>& Chunk::get_chunk_blocks() const{ const std::vector<uint8_t>& Chunk::get_chunk_blocks() const{
return m_blocks; return m_blocks;
} }
@@ -212,41 +221,8 @@ const std::vector<Vertex>& Chunk::get_vertex_data() const{
} }
void Chunk::init_chunk() { void Chunk::init_chunk() {
m_blocks.assign(CHUCK_SIZE * CHUCK_SIZE * WORLD_SIZE_Y, 0); resolve_biome();
for (int x = 0; x < CHUCK_SIZE; x++) { resolve_blocks();
for (int y = 0; y < 5; y++) {
for (int z = 0; z < CHUCK_SIZE; z++) {
m_blocks[get_index(x, y, z)] = 3;
}
}
}
for (int x = 0; x < CHUCK_SIZE; x++) {
for (int z = 0; z < CHUCK_SIZE; z++) {
float world_x = static_cast<float>(x + m_chunk_pos.x * CHUCK_SIZE);
float world_z = static_cast<float>(z + m_chunk_pos.z * CHUCK_SIZE);
float noise =
0.5f * PerlinNoise::noise(world_x * 0.01f, world_z * 0.01f, 0.5f) +
0.25f * PerlinNoise::noise(world_x * 0.02f, world_z * 0.02f, 0.5f) +
0.125f * PerlinNoise::noise(world_x * 0.04f, world_z * 0.04f, 0.5f);
int y_max = height * noise;
for (int y = 5; y < y_max - 5; y++) {
m_blocks[get_index(x, y, z)] = 3;
}
for (int y = y_max - 5; y < y_max - 1; y++) {
m_blocks[get_index(x, y, z)] = 2;
}
for (int y = y_max - 1; y < y_max; y++) {
m_blocks[get_index(x, y, z)] = 1;
}
}
}
mark_dirty();
} }
void Chunk::upload_to_gpu() { void Chunk::upload_to_gpu() {
@@ -281,3 +257,66 @@ void Chunk::set_chunk_block(int index ,unsigned id) {
} }
void Chunk::resolve_biome() {
float cx = (m_chunk_pos.x + 0.5f) * CHUCK_SIZE;
float cz = (m_chunk_pos.z + 0.5f) * CHUCK_SIZE;
float temp = PerlinNoise::noise(cx * BIOME_NOISE_FREQUENCY, 0.0f, cz * BIOME_NOISE_FREQUENCY);
float humid = PerlinNoise::noise(cx * BIOME_NOISE_FREQUENCY, 1.0f, cz * BIOME_NOISE_FREQUENCY);
m_biome = get_biome_from_noise(temp, humid);
}
void Chunk::resolve_blocks() {
m_blocks.assign(CHUCK_SIZE * CHUCK_SIZE * WORLD_SIZE_Y, 0);
for (int x = 0; x < CHUCK_SIZE; x++) {
for (int y = 0; y < 5; y++) {
for (int z = 0; z < CHUCK_SIZE; z++) {
m_blocks[get_index(x, y, z)] = 3;
}
}
}
for (int x = 0; x < CHUCK_SIZE; x++) {
for (int z = 0; z < CHUCK_SIZE; z++) {
float world_x = static_cast<float>(x + m_chunk_pos.x * CHUCK_SIZE);
float world_z = static_cast<float>(z + m_chunk_pos.z * CHUCK_SIZE);
float temp = PerlinNoise::noise(world_x * BIOME_NOISE_FREQUENCY, 0.0f, world_z * BIOME_NOISE_FREQUENCY);
float humid = PerlinNoise::noise(world_x * BIOME_NOISE_FREQUENCY, 1.0f, world_z * BIOME_NOISE_FREQUENCY);
int height = get_interpolated_height(world_x, world_z, temp, humid);
auto biome = get_biome_from_noise(temp, humid);
for (int y = 5; y < height - 5; y++) {
m_blocks[get_index(x, y, z)] = 3;
}
if (biome == Biome::MOUNTAIN) {
for (int y = height - 5; y < height - 1; y++) {
if (y > 101) {
m_blocks[get_index(x, y, z)] = 3;
} else {
m_blocks[get_index(x, y, z)] = 2;
}
}
if (height - 1 > 101) {
m_blocks[get_index(x, height - 1, z)] = 3;
} else {
m_blocks[get_index(x, height - 1, z)] = 1;
}
} else if (biome == Biome::DESERT) {
for (int y = height - 5; y < height; y++) {
m_blocks[get_index(x, y, z)] = 4;
}
} else {
for (int y = height - 5; y < height - 1; y++) {
m_blocks[get_index(x, y, z)] = 2;
}
for (int y = height - 1; y < height; y++) {
m_blocks[get_index(x, y, z)] = 1;
}
}
}
}
mark_dirty();
}

View File

@@ -235,6 +235,14 @@ void Player::check_player_chunk_transition() {
if (cur_pos != m_player_chunk_pos) { if (cur_pos != m_player_chunk_pos) {
m_world.need_gen(); m_world.need_gen();
m_player_chunk_pos = cur_pos; m_player_chunk_pos = cur_pos;
auto chunk = m_world.get_chunk(cur_pos);
if (chunk == nullptr) {
DebugCollector::get().report("biome", "Biome: Unknown");
} else {
DebugCollector::get()
.report("biome", "Biome: " + get_biome_str(chunk->get_biome()));
}
} }
} }

View File

@@ -80,6 +80,15 @@ const std::optional<LookBlock>& World::get_look_block_pos(const std::string& nam
} }
const Chunk* World::get_chunk(const ChunkPos& pos) const {
std::lock_guard lk(m_chunks_mutex);
auto it = m_chunks.find(pos);
if (it == m_chunks.end()) {
return nullptr;
}
return &it->second;
}
Player& World::get_player(const std::string& name){ Player& World::get_player(const std::string& name){
auto it = m_players.find(HASH::str(name)); auto it = m_players.find(HASH::str(name));
if (it == m_players.end()) { if (it == m_players.end()) {

View File

@@ -12,7 +12,8 @@ constexpr std::array<std::string_view, MAX_BLOCK_NUM> BLOCK_REISTER{
"air", "air",
"grass_block", "grass_block",
"dirt", "dirt",
"stone" "stone",
"sand"
}; };

View File

@@ -1,6 +1,7 @@
#include <Cubed/tools/math_tools.hpp> #include <Cubed/tools/math_tools.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
namespace Math { namespace Math {
void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector<glm::vec4>& planes) { void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector<glm::vec4>& planes) {
if (planes.size() != 6) { if (planes.size() != 6) {
@@ -26,4 +27,6 @@ namespace Math {
p = glm::normalize(p); p = glm::normalize(p);
} }
} }
} }