feat: add tree generation

This commit is contained in:
2026-04-18 10:59:37 +08:00
parent 63930dcdc7
commit bb888fd7b7
27 changed files with 224 additions and 30 deletions

View File

@@ -85,12 +85,14 @@ add_executable(${PROJECT_NAME}
src/gameplay/biome.cpp src/gameplay/biome.cpp
src/gameplay/chunk.cpp src/gameplay/chunk.cpp
src/gameplay/player.cpp src/gameplay/player.cpp
src/gameplay/tree.cpp
src/gameplay/world.cpp src/gameplay/world.cpp
src/input.cpp src/input.cpp
src/map_table.cpp src/map_table.cpp
src/renderer.cpp src/renderer.cpp
src/shader.cpp src/shader.cpp
src/texture_manager.cpp src/texture_manager.cpp
src/tools/cubed_random.cpp
src/tools/math_tools.cpp src/tools/math_tools.cpp
src/tools/shader_tools.cpp src/tools/shader_tools.cpp
src/tools/font.cpp src/tools/font.cpp

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B

View File

@@ -6,6 +6,7 @@
#include <Cubed/renderer.hpp> #include <Cubed/renderer.hpp>
#include <Cubed/texture_manager.hpp> #include <Cubed/texture_manager.hpp>
#include <Cubed/window.hpp> #include <Cubed/window.hpp>
namespace Cubed {
class App { class App {
public: public:
@@ -35,7 +36,6 @@ private:
inline static double fps_time_count = 0.0f; inline static double fps_time_count = 0.0f;
inline static int frame_count = 0; inline static int frame_count = 0;
inline static int fps = 0; inline static int fps = 0;
inline static unsigned int m_seed = 0;
void init(); void init();
@@ -47,3 +47,5 @@ private:
void run(); void run();
void update(); void update();
}; };
}

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
constexpr int WORLD_SIZE_Y = 256; constexpr int WORLD_SIZE_Y = 256;
constexpr int MAX_BLOCK_NUM = 5; constexpr int MAX_BLOCK_NUM = 7;
constexpr int MAX_UI_NUM = 1; constexpr int MAX_UI_NUM = 1;
constexpr int CHUCK_SIZE = 16; constexpr int CHUCK_SIZE = 16;

View File

@@ -4,9 +4,9 @@
constexpr float BIOME_NOISE_FREQUENCY = 0.003f; constexpr float BIOME_NOISE_FREQUENCY = 0.003f;
constexpr float PLAIN_FREQ = 0.5f; constexpr float PLAIN_FREQ = 0.4f;
constexpr float FOREST_FREQ = 1.0f; constexpr float FOREST_FREQ = 1.2f;
constexpr float DESERT_FREQ = 1.0f; constexpr float DESERT_FREQ = 1.2f;
constexpr float MOUNTAIN_FREQ = 2.0f; constexpr float MOUNTAIN_FREQ = 2.0f;
enum class Biome { enum class Biome {

View File

@@ -47,7 +47,7 @@ public:
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);
static int get_index(const glm::vec3& pos);
void init_chunk(); void init_chunk();
void gen_vertex_data(); void gen_vertex_data();
// 0 : (1, 0) // 0 : (1, 0)

View File

@@ -0,0 +1,12 @@
#pragma once
#include <glm/glm.hpp>
class Chunk;
struct TreeStructNode {
glm::ivec3 offset{0, 0, 0};
unsigned id = 0;
};
bool build_tree(Chunk& chunk, const glm::ivec3& pos);

View File

@@ -0,0 +1,21 @@
#pragma once
#include <random>
namespace Cubed {
class Random {
public:
Random();
static Random& get();
bool random_bool(double probability);
std::mt19937& engine();
unsigned seed();
private:
unsigned int m_seed = 0;
std::mt19937 m_engine;
};
}

View File

@@ -4,7 +4,7 @@
class PerlinNoise { class PerlinNoise {
public: public:
static void init(unsigned int seed); static void init();
static float noise(float x, float y, float z); static float noise(float x, float y, float z);
private: private:
static inline bool is_init = false; static inline bool is_init = false;

View File

@@ -4,11 +4,14 @@
#include <Cubed/map_table.hpp> #include <Cubed/map_table.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/cubed_random.hpp>
#include <Cubed/tools/system_info.hpp> #include <Cubed/tools/system_info.hpp>
#include <Cubed/tools/perlin_noise.hpp> #include <Cubed/tools/perlin_noise.hpp>
#include <exception> #include <exception>
#include <random>
namespace Cubed {
App::App() { App::App() {
@@ -36,10 +39,8 @@ void App::init() {
glfwSetWindowFocusCallback(m_window.get_glfw_window(), window_focus_callback); glfwSetWindowFocusCallback(m_window.get_glfw_window(), window_focus_callback);
glfwSetWindowSizeCallback(m_window.get_glfw_window(), window_reshape_callback); glfwSetWindowSizeCallback(m_window.get_glfw_window(), window_reshape_callback);
glfwSetKeyCallback(m_window.get_glfw_window(), key_callback); glfwSetKeyCallback(m_window.get_glfw_window(), key_callback);
std::random_device d;
m_seed = d(); PerlinNoise::init();
Logger::info("Seed: {}", m_seed);
PerlinNoise::init(m_seed);
m_renderer.init(); m_renderer.init();
Logger::info("Renderer Init Success"); Logger::info("Renderer Init Success");
@@ -204,10 +205,6 @@ int App::start_cubed_application(int argc, char** argv) {
return 1; return 1;
} }
unsigned int App::seed() {
return m_seed;
}
float App::delte_time() { float App::delte_time() {
return delta_time; return delta_time;
} }
@@ -215,3 +212,5 @@ float App::delte_time() {
float App::get_fps() { float App::get_fps() {
return fps; return fps;
} }
}

View File

@@ -1,6 +1,8 @@
#include <Cubed/gameplay/chunk.hpp> #include <Cubed/gameplay/chunk.hpp>
#include <Cubed/gameplay/tree.hpp>
#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/cubed_random.hpp>
#include <Cubed/tools/log.hpp> #include <Cubed/tools/log.hpp>
#include <Cubed/tools/math_tools.hpp> #include <Cubed/tools/math_tools.hpp>
#include <Cubed/tools/perlin_noise.hpp> #include <Cubed/tools/perlin_noise.hpp>
@@ -59,6 +61,11 @@ int Chunk::get_index(int x, int y, int z) {
} }
return (x * WORLD_SIZE_Y + y) * CHUCK_SIZE + z; return (x * WORLD_SIZE_Y + y) * CHUCK_SIZE + z;
} }
int Chunk::get_index(const glm::vec3& pos) {
return Chunk::get_index(pos.x, pos.y, pos.z);
}
// this is thread-unsafe! // this is thread-unsafe!
void Chunk::gen_vertex_data() { void Chunk::gen_vertex_data() {
m_vertexs_data.clear(); m_vertexs_data.clear();
@@ -274,7 +281,7 @@ void Chunk::resolve_blocks() {
} }
} }
} }
std::array<std::array<int, SIZE_Z>, SIZE_X> heights;
for (int x = 0; x < CHUCK_SIZE; x++) { for (int x = 0; x < CHUCK_SIZE; x++) {
for (int z = 0; z < CHUCK_SIZE; z++) { for (int z = 0; z < CHUCK_SIZE; z++) {
@@ -284,12 +291,13 @@ void Chunk::resolve_blocks() {
float temp = PerlinNoise::noise(world_x * BIOME_NOISE_FREQUENCY, 0.0f, world_z * BIOME_NOISE_FREQUENCY); 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); 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); int height = get_interpolated_height(world_x, world_z, temp, humid);
heights[x][z] = height;
auto biome = get_biome_from_noise(temp, humid); auto biome = get_biome_from_noise(temp, humid);
for (int y = 5; y < height - 5; y++) { for (int y = 5; y < height - 5; y++) {
m_blocks[get_index(x, y, z)] = 3; m_blocks[get_index(x, y, z)] = 3;
} }
if (biome == Biome::MOUNTAIN) { if (biome == Biome::MOUNTAIN) {
for (int y = height - 5; y < height - 1; y++) { for (int y = height - 5; y <= height - 1; y++) {
if (y > 101) { if (y > 101) {
m_blocks[get_index(x, y, z)] = 3; m_blocks[get_index(x, y, z)] = 3;
} else { } else {
@@ -297,26 +305,43 @@ void Chunk::resolve_blocks() {
} }
} }
if (height - 1 > 101) { if (height > 101) {
m_blocks[get_index(x, height - 1, z)] = 3; m_blocks[get_index(x, height - 1, z)] = 3;
} else { } else {
m_blocks[get_index(x, height - 1, z)] = 1; m_blocks[get_index(x, height - 1, z)] = 1;
} }
} else if (biome == Biome::DESERT) { } else if (biome == Biome::DESERT) {
for (int y = height - 5; y < height; y++) { for (int y = height - 5; y <= height; y++) {
m_blocks[get_index(x, y, z)] = 4; m_blocks[get_index(x, y, z)] = 4;
} }
} else { } else {
for (int y = height - 5; y < height - 1; y++) { for (int y = height - 5; y <= height - 1; y++) {
m_blocks[get_index(x, y, z)] = 2; m_blocks[get_index(x, y, z)] = 2;
} }
for (int y = height - 1; y < height; y++) { for (int y = height; y <= height; y++) {
m_blocks[get_index(x, y, z)] = 1; m_blocks[get_index(x, y, z)] = 1;
} }
} }
} }
} }
if (m_biome == Biome::FOREST) {
std::array<int, SIZE_X> x_arr;
std::ranges::iota(x_arr, 0);
std::shuffle(x_arr.begin(), x_arr.end(), Cubed::Random::get().engine());
std::array<int, SIZE_Z> z_arr;
std::ranges::iota(z_arr, 0);
std::shuffle(z_arr.begin(), z_arr.end(), Cubed::Random::get().engine());
for (auto x : x_arr) {
for (auto z : z_arr) {
if (Cubed::Random::get().random_bool(0.8)) {
build_tree(*this, {x, heights[x][z], z});
}
}
}
}
mark_dirty(); mark_dirty();
} }

98
src/gameplay/tree.cpp Normal file
View File

@@ -0,0 +1,98 @@
#include <Cubed/gameplay/tree.hpp>
#include <Cubed/gameplay/chunk.hpp>
#include <array>
using glm::ivec3;
static constexpr std::array<TreeStructNode, 62> TREE {{
{{0, 1, 0}, 5},
{{0, 2, 0}, 5},
{{0, 3, 0}, 5},
{{0, 4, 0}, 5},
{{0, 5, 0}, 5},
{{0, 6, 0}, 6},
{{0, 5, 1}, 6},
{{1, 5, 0}, 6},
{{0, 5, -1}, 6},
{{-1, 5, 0}, 6},
{{1, 5, 1}, 6},
{{1, 5, -1}, 6},
{{-1, 5, -1}, 6},
{{-1, 5, 1}, 6},
{{0, 4, 1}, 6},
{{1, 4, 0}, 6},
{{0, 4, -1}, 6},
{{-1, 4, 0}, 6},
{{1, 4, 1}, 6},
{{1, 4, -1}, 6},
{{-1, 4, -1}, 6},
{{-1, 4, 1}, 6},
{{0, 4, 2}, 6},
{{2, 4, 0}, 6},
{{0, 4, -2}, 6},
{{-2, 4, 0}, 6},
{{2, 4, 2}, 6},
{{2, 4, -2}, 6},
{{-2, 4, -2}, 6},
{{-2, 4, 2}, 6},
{{1, 4, 2}, 6},
{{2, 4, 1}, 6},
{{-1, 4, 2}, 6},
{{2, 4, -1}, 6},
{{1, 4, -2}, 6},
{{-2, 4, 1}, 6},
{{-1, 4, -2}, 6},
{{-2, 4, -1}, 6},
{{0, 3, 1}, 6},
{{1, 3, 0}, 6},
{{0, 3, -1}, 6},
{{-1, 3, 0}, 6},
{{1, 3, 1}, 6},
{{1, 3, -1}, 6},
{{-1, 3, -1}, 6},
{{-1, 3, 1}, 6},
{{0, 3, 2}, 6},
{{2, 3, 0}, 6},
{{0, 3, -2}, 6},
{{-2, 3, 0}, 6},
{{2, 3, 2}, 6},
{{2, 3, -2}, 6},
{{-2, 3, -2}, 6},
{{-2, 3, 2}, 6},
{{1, 3, 2}, 6},
{{2, 3, 1}, 6},
{{-1, 3, 2}, 6},
{{2, 3, -1}, 6},
{{1, 3, -2}, 6},
{{-2, 3, 1}, 6},
{{-1, 3, -2}, 6},
{{-2, 3, -1}, 6},
}};
bool build_tree(Chunk& chunk, const glm::ivec3& pos) {
auto& block = chunk.get_chunk_blocks();
if (block[Chunk::get_index(pos)] != 1) {
Logger::info("Root is not Grass Block");
return false;
}
for (const auto& d : TREE) {
auto tree_node = pos + d.offset;
int x = tree_node.x;
int y = tree_node.y;
int z = tree_node.z;
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y || z >= CHUCK_SIZE) {
return false;
}
if (block[Chunk::get_index(tree_node)] != 0) {
return false;
}
}
for (const auto& d : TREE) {
auto tree_node = pos + d.offset;
chunk.set_chunk_block(Chunk::get_index(tree_node), d.id);
}
return true;
}

View File

@@ -1,7 +1,7 @@
#include <Cubed/app.hpp> #include <Cubed/app.hpp>
int main(int argc, char** argv) { int main(int argc, char** argv) {
return App::start_cubed_application(argc, argv); return Cubed::App::start_cubed_application(argc, argv);
} }

View File

@@ -13,7 +13,9 @@ constexpr std::array<std::string_view, MAX_BLOCK_NUM> BLOCK_REISTER{
"grass_block", "grass_block",
"dirt", "dirt",
"stone", "stone",
"sand" "sand",
"log",
"leaf"
}; };

View File

@@ -0,0 +1,34 @@
#include <Cubed/tools/cubed_random.hpp>
#include <Cubed/tools/log.hpp>
namespace Cubed {
Random::Random() {
std::random_device d;
m_seed = d();
Logger::info("Seed: {}", m_seed);
m_engine.seed(m_seed);
}
Random& Random::get() {
static Random instance;
return instance;
}
bool Random::random_bool(double probability) {
std::bernoulli_distribution dist(probability);
return dist(m_engine);
}
std::mt19937& Random::engine() {
return m_engine;
}
unsigned Random::seed() {
return m_seed;
}
}

View File

@@ -2,17 +2,16 @@
#include <Cubed/config.hpp> #include <Cubed/config.hpp>
#include <Cubed/tools/cubed_assert.hpp> #include <Cubed/tools/cubed_assert.hpp>
#include <Cubed/tools/cubed_random.hpp>
#include <algorithm> #include <algorithm>
#include <numeric> #include <numeric>
#include <random>
void PerlinNoise::init(unsigned int seed) {
void PerlinNoise::init() {
p.resize(256); p.resize(256);
std::iota(p.begin(), p.end(), 0); std::iota(p.begin(), p.end(), 0);
std::mt19937 engine(seed); std::shuffle(p.begin(), p.end(), Cubed::Random::get().engine());
std::shuffle(p.begin(), p.end(), engine);
p.insert(p.end(), p.begin(), p.end()); p.insert(p.end(), p.begin(), p.end());
is_init = true; is_init = true;