mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-18 00:27:02 +08:00
Compare commits
2 Commits
80c8691999
...
6773157000
| Author | SHA1 | Date | |
|---|---|---|---|
| 6773157000 | |||
| 16ff954166 |
@@ -97,19 +97,19 @@ add_executable(${PROJECT_NAME}
|
|||||||
src/window.cpp
|
src/window.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
#if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
# message(STATUS "Building with AddressSanitizer enabled for target: ${PROJECT_NAME}")
|
message(STATUS "Building with AddressSanitizer enabled for target: ${PROJECT_NAME}")
|
||||||
#
|
|
||||||
# target_compile_options(${PROJECT_NAME} PRIVATE
|
target_compile_options(${PROJECT_NAME} PRIVATE
|
||||||
# -fsanitize=address
|
-fsanitize=address
|
||||||
# -fno-omit-frame-pointer
|
-fno-omit-frame-pointer
|
||||||
# -g
|
-g
|
||||||
# )
|
)
|
||||||
#
|
|
||||||
# target_link_options(${PROJECT_NAME} PRIVATE
|
target_link_options(${PROJECT_NAME} PRIVATE
|
||||||
# -fsanitize=address
|
-fsanitize=address
|
||||||
# )
|
)
|
||||||
#endif()
|
endif()
|
||||||
|
|
||||||
target_include_directories(${PROJECT_NAME} PUBLIC ${INCLUDE_DIR})
|
target_include_directories(${PROJECT_NAME} PUBLIC ${INCLUDE_DIR})
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
#include <Cubed/config.hpp>
|
#include <Cubed/config.hpp>
|
||||||
@@ -7,12 +8,10 @@
|
|||||||
#include <Cubed/gameplay/block.hpp>
|
#include <Cubed/gameplay/block.hpp>
|
||||||
|
|
||||||
class World;
|
class World;
|
||||||
|
// if want to use, do init_chunk(), gen_vertex_data() and
|
||||||
class Chunk {
|
class Chunk {
|
||||||
private:
|
private:
|
||||||
|
std::atomic<bool> m_dirty {false};
|
||||||
bool m_is_gened = false;
|
|
||||||
bool m_dirty = false;
|
|
||||||
|
|
||||||
static constexpr int SIZE_X = CHUCK_SIZE;
|
static constexpr int SIZE_X = CHUCK_SIZE;
|
||||||
static constexpr int SIZE_Y = WORLD_SIZE_Y;
|
static constexpr int SIZE_Y = WORLD_SIZE_Y;
|
||||||
@@ -28,24 +27,33 @@ private:
|
|||||||
float frequency = 0.01f;
|
float frequency = 0.01f;
|
||||||
float height = 80;
|
float height = 80;
|
||||||
|
|
||||||
|
void clear_dirty();
|
||||||
public:
|
public:
|
||||||
Chunk(World& world, ChunkPos chunk_pos);
|
Chunk(World& world, ChunkPos chunk_pos);
|
||||||
~Chunk();
|
~Chunk();
|
||||||
Chunk(const Chunk&) = delete;
|
Chunk(const Chunk&) = delete;
|
||||||
Chunk& operator=(const Chunk&) = delete;
|
Chunk& operator=(const Chunk&) = delete;
|
||||||
Chunk(Chunk&&) = default;
|
Chunk(Chunk&&);
|
||||||
Chunk& operator=(Chunk&&) = delete;
|
Chunk& operator=(Chunk&&);
|
||||||
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);
|
||||||
|
|
||||||
|
void init_chunk();
|
||||||
void gen_vertex_data();
|
void gen_vertex_data();
|
||||||
|
// 0 : (1, 0)
|
||||||
|
// 1 : (-1, 0)
|
||||||
|
// 2 : (0, 1)
|
||||||
|
// 3 : (0, -1)
|
||||||
|
void gen_vertex_data(const std::vector<const std::vector<uint8_t>*>& neighbor_block);
|
||||||
|
void upload_to_gpu();
|
||||||
|
|
||||||
GLuint get_vbo() const;
|
GLuint get_vbo() const;
|
||||||
const std::vector<Vertex>& get_vertex_data() const;
|
const std::vector<Vertex>& get_vertex_data() const;
|
||||||
void init_chunk();
|
|
||||||
bool is_dirty() const;
|
bool is_dirty() const;
|
||||||
void mark_dirty();
|
void mark_dirty();
|
||||||
void clear_dirty();
|
|
||||||
void set_chunk_block(int index, unsigned id);
|
void set_chunk_block(int index, unsigned id);
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -1,20 +1,51 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <atomic>
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <mutex>
|
||||||
|
#include <thread>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
#include <Cubed/AABB.hpp>
|
#include <Cubed/AABB.hpp>
|
||||||
#include <Cubed/gameplay/chunk.hpp>
|
#include <Cubed/gameplay/chunk.hpp>
|
||||||
|
|
||||||
|
struct ChunkRenderSnapshot {
|
||||||
|
GLuint vbo;
|
||||||
|
size_t vertex_count;
|
||||||
|
glm::vec3 center;
|
||||||
|
glm::vec3 half_extents;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class Player;
|
class Player;
|
||||||
|
|
||||||
class World {
|
class World {
|
||||||
private:
|
private:
|
||||||
bool m_need_gen_chunk;
|
glm::vec3 m_gen_player_pos{0.0f, 0.0f, 0.0f};
|
||||||
std::unordered_map<ChunkPos , Chunk, ChunkPos::Hash> m_chunks;
|
std::unordered_map<ChunkPos , Chunk, ChunkPos::Hash> m_chunks;
|
||||||
std::unordered_map<std::size_t, Player> m_players;
|
std::unordered_map<std::size_t, Player> m_players;
|
||||||
std::vector<glm::vec4> m_planes;
|
std::vector<glm::vec4> m_planes;
|
||||||
|
|
||||||
void gen_chunks();
|
std::thread m_gen_thread;
|
||||||
|
std::mutex m_chunks_mutex;
|
||||||
|
std::mutex m_gen_signal_mutex;
|
||||||
|
std::mutex m_new_chunk_queue_mutex;
|
||||||
|
std::mutex m_delete_vbo_mutex;
|
||||||
|
std::mutex m_gen_player_pos_mutex;
|
||||||
|
std::vector<GLuint> m_pending_delete_vbo;
|
||||||
|
std::condition_variable m_gen_cv;
|
||||||
|
std::atomic<bool> m_gen_running{false};
|
||||||
|
std::atomic<bool> m_need_gen_chunk{false};
|
||||||
|
|
||||||
|
std::vector<ChunkPos> m_dirty_queue;
|
||||||
|
std::vector<ChunkRenderSnapshot> m_render_snapshots;
|
||||||
|
std::vector<std::pair<ChunkPos, Chunk>> m_new_chunk;
|
||||||
|
std::vector<std::pair<ChunkPos, Chunk>> m_new_chunk_queue;
|
||||||
|
|
||||||
|
void gen_chunks_internal();
|
||||||
|
|
||||||
|
void start_gen_thread();
|
||||||
|
void stop_gen_thread();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@@ -31,7 +62,7 @@ public:
|
|||||||
int get_block(const glm::ivec3& block_pos) const;
|
int get_block(const glm::ivec3& block_pos) const;
|
||||||
bool is_block(const glm::ivec3& block_pos) const;
|
bool is_block(const glm::ivec3& block_pos) const;
|
||||||
|
|
||||||
ChunkPos chunk_pos(int world_x, int world_z) const;
|
static ChunkPos chunk_pos(int world_x, int world_z);
|
||||||
|
|
||||||
void need_gen();
|
void need_gen();
|
||||||
void render(const glm::mat4& mvp_matrix);
|
void render(const glm::mat4& mvp_matrix);
|
||||||
@@ -39,5 +70,6 @@ public:
|
|||||||
void set_block(const glm::ivec3& pos, unsigned id);
|
void set_block(const glm::ivec3& pos, unsigned id);
|
||||||
void update(float delta_time);
|
void update(float delta_time);
|
||||||
|
|
||||||
|
void push_delete_vbo(GLuint vbo);
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <atomic>
|
|
||||||
#include <condition_variable>
|
|
||||||
#include <mutex>
|
|
||||||
#include <thread>
|
|
||||||
#include <optional>
|
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
#include <Cubed/AABB.hpp>
|
|
||||||
#include <Cubed/gameplay/chunk.hpp>
|
|
||||||
|
|
||||||
struct ChunkRenderSnapshot {
|
|
||||||
GLuint vbo;
|
|
||||||
size_t vertex_count;
|
|
||||||
glm::vec3 center;
|
|
||||||
glm::vec3 half_extents;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
class Player;
|
|
||||||
|
|
||||||
class World {
|
|
||||||
private:
|
|
||||||
glm::vec3 m_gen_player_pos{0.0f, 0.0f, 0.0f};
|
|
||||||
std::unordered_map<ChunkPos , Chunk, ChunkPos::Hash> m_chunks;
|
|
||||||
std::unordered_map<std::size_t, Player> m_players;
|
|
||||||
std::vector<glm::vec4> m_planes;
|
|
||||||
|
|
||||||
std::thread m_gen_thread;
|
|
||||||
std::mutex m_chunks_mutex;
|
|
||||||
std::mutex m_gen_signal_mutex; ;
|
|
||||||
std::condition_variable m_gen_cv;
|
|
||||||
std::atomic<bool> m_gen_running{false};
|
|
||||||
std::atomic<bool> m_need_gen_chunk{false};
|
|
||||||
|
|
||||||
std::vector<ChunkPos> m_dirty_queue;
|
|
||||||
std::vector<ChunkRenderSnapshot> m_render_snapshots;
|
|
||||||
|
|
||||||
void gen_chunks_internal();
|
|
||||||
|
|
||||||
void start_gen_thread();
|
|
||||||
void stop_gen_thread();
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
World();
|
|
||||||
~World();
|
|
||||||
|
|
||||||
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;
|
|
||||||
Player& get_player(const std::string& name);
|
|
||||||
void init_world();
|
|
||||||
bool is_aabb_in_frustum(const glm::vec3& center, const glm::vec3& half_extents);
|
|
||||||
|
|
||||||
int get_block(const glm::ivec3& block_pos) const;
|
|
||||||
bool is_block(const glm::ivec3& block_pos) const;
|
|
||||||
|
|
||||||
ChunkPos chunk_pos(int world_x, int world_z) const;
|
|
||||||
|
|
||||||
void need_gen();
|
|
||||||
void render(const glm::mat4& mvp_matrix);
|
|
||||||
|
|
||||||
void set_block(const glm::ivec3& pos, unsigned id);
|
|
||||||
void update(float delta_time);
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
7
lsan.supp
Normal file
7
lsan.supp
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
leak:libfontconfig.so
|
||||||
|
leak:libpangoft2
|
||||||
|
leak:libpangocairo
|
||||||
|
leak:libdecor-gtk.so
|
||||||
|
leak:libgtk-3.so
|
||||||
|
leak:libwayland-client.so
|
||||||
|
leak:libglfw.so
|
||||||
@@ -12,26 +12,45 @@ Chunk::Chunk(World& world, ChunkPos chunk_pos) :
|
|||||||
|
|
||||||
Chunk::~Chunk() {
|
Chunk::~Chunk() {
|
||||||
if (m_vbo != 0) {
|
if (m_vbo != 0) {
|
||||||
glDeleteBuffers(1, &m_vbo);
|
m_world.push_delete_vbo(m_vbo);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Chunk::Chunk(Chunk&& other) :
|
||||||
|
m_vbo(other.m_vbo),
|
||||||
|
m_chunk_pos(std::move(other.m_chunk_pos)),
|
||||||
|
m_world(other.m_world),
|
||||||
|
m_blocks(std::move(other.m_blocks)),
|
||||||
|
m_dirty(other.is_dirty()),
|
||||||
|
m_vertexs_data(std::move(other.m_vertexs_data))
|
||||||
|
{
|
||||||
|
other.m_vbo = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Chunk& Chunk::operator=(Chunk&& other) {
|
||||||
|
m_vbo = other.m_vbo;
|
||||||
|
other.m_vbo = 0;
|
||||||
|
m_chunk_pos = std::move(other.m_chunk_pos);
|
||||||
|
m_blocks = std::move(other.m_blocks);
|
||||||
|
m_dirty = other.is_dirty();
|
||||||
|
m_vertexs_data = std::move(other.m_vertexs_data);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int Chunk::get_index(int x, int y, int z) {
|
int Chunk::get_index(int x, int y, int z) {
|
||||||
|
CUBED_ASSERT(!(x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y || z >= CHUCK_SIZE));
|
||||||
if ((x * WORLD_SIZE_Y + y) * CHUCK_SIZE + z < 0 || (x * WORLD_SIZE_Y + y) * CHUCK_SIZE + z >= CHUCK_SIZE * CHUCK_SIZE * WORLD_SIZE_Y) {
|
if ((x * WORLD_SIZE_Y + y) * CHUCK_SIZE + z < 0 || (x * WORLD_SIZE_Y + y) * CHUCK_SIZE + z >= CHUCK_SIZE * CHUCK_SIZE * WORLD_SIZE_Y) {
|
||||||
Logger::error("block pos x {} y {} z {} range error", x, y, z);
|
Logger::error("block pos x {} y {} z {} range error", x, y, z);
|
||||||
CUBED_ASSERT(0);
|
CUBED_ASSERT(0);
|
||||||
}
|
}
|
||||||
return (x * WORLD_SIZE_Y + y) * CHUCK_SIZE + z;
|
return (x * WORLD_SIZE_Y + y) * CHUCK_SIZE + z;
|
||||||
}
|
}
|
||||||
|
// this is thread-unsafe!
|
||||||
void Chunk::gen_vertex_data() {
|
void Chunk::gen_vertex_data() {
|
||||||
m_vertexs_data.clear();
|
m_vertexs_data.clear();
|
||||||
|
|
||||||
@@ -84,18 +103,104 @@ void Chunk::gen_vertex_data() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
if (m_vbo == 0) {
|
void Chunk::gen_vertex_data(const std::vector<const std::vector<uint8_t>*>& neighbor_block) {
|
||||||
glGenBuffers(1, &m_vbo);
|
m_vertexs_data.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}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int x = 0; x < SIZE_X; x++) {
|
||||||
|
for (int y = 0; y < SIZE_Y; y++) {
|
||||||
|
for (int z = 0; z < SIZE_Z; z++) {
|
||||||
|
int world_x = x + m_chunk_pos.x * CHUCK_SIZE;
|
||||||
|
int world_z = z + m_chunk_pos.z * CHUCK_SIZE;
|
||||||
|
int world_y = y;
|
||||||
|
int id = m_blocks[get_index(x, y, z)];
|
||||||
|
// air
|
||||||
|
if (id == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (int face = 0; face < 6; face++) {
|
||||||
|
int nx = x + DIR[face].x;
|
||||||
|
int ny = y + DIR[face].y;
|
||||||
|
int nz = z + DIR[face].z;
|
||||||
|
bool neighbor_soild = false;
|
||||||
|
|
||||||
|
if (nx < 0 || nx >= SIZE_X || ny < 0 || ny >= SIZE_Y || nz < 0 || nz>= SIZE_Z) {
|
||||||
|
|
||||||
|
int world_nx = world_x + DIR[face].x;
|
||||||
|
int world_ny = world_y + DIR[face].y;
|
||||||
|
int world_nz = world_z + DIR[face].z;
|
||||||
|
|
||||||
|
auto [neighbor_x, neighbor_z] = World::chunk_pos(world_nx, world_nz);
|
||||||
|
|
||||||
|
auto is_block = [&](const std::vector<uint8_t>* chunk_blocks){
|
||||||
|
if (chunk_blocks == nullptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int x, y, z;
|
||||||
|
y = world_ny;
|
||||||
|
x = world_nx - neighbor_x * CHUCK_SIZE;
|
||||||
|
z = world_nz - neighbor_z * CHUCK_SIZE;
|
||||||
|
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y || z >= CHUCK_SIZE) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int idx = Chunk::get_index(x, y, z);
|
||||||
|
// not init
|
||||||
|
if (idx >= chunk_blocks->size()) {
|
||||||
|
Logger::warn("not init");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
auto id = (*chunk_blocks)[idx];
|
||||||
|
if (id == 0) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (m_chunk_pos.x + 1 == neighbor_x) {
|
||||||
|
neighbor_soild = is_block(neighbor_block[0]);
|
||||||
|
} else if (m_chunk_pos.x - 1 == neighbor_x) {
|
||||||
|
neighbor_soild = is_block(neighbor_block[1]);
|
||||||
|
} else if (m_chunk_pos.z + 1 == neighbor_z) {
|
||||||
|
neighbor_soild = is_block(neighbor_block[2]);
|
||||||
|
} else if (m_chunk_pos.z - 1 == neighbor_z) {
|
||||||
|
neighbor_soild = is_block(neighbor_block[3]);
|
||||||
|
}
|
||||||
|
//neighbor_soild = m_world.is_block(glm::ivec3(world_x, world_y, world_z) + DIR[face]);
|
||||||
|
} else {
|
||||||
|
if (m_blocks[get_index(nx, ny, nz)] != 0) {
|
||||||
|
neighbor_soild = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (neighbor_soild) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
Vertex vex = {
|
||||||
|
VERTICES_POS[face][i][0] + (float)world_x * 1.0f,
|
||||||
|
VERTICES_POS[face][i][1] + (float)world_y * 1.0f,
|
||||||
|
VERTICES_POS[face][i][2] + (float)world_z * 1.0f,
|
||||||
|
TEX_COORDS[face][i][0],
|
||||||
|
TEX_COORDS[face][i][1],
|
||||||
|
static_cast<float>(id * 6 + face)
|
||||||
|
|
||||||
|
};
|
||||||
|
m_vertexs_data.emplace_back(vex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, m_vertexs_data.size() * sizeof(Vertex), m_vertexs_data.data(), GL_DYNAMIC_DRAW);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GLuint Chunk::get_vbo() const{
|
GLuint Chunk::get_vbo() const{
|
||||||
@@ -135,12 +240,26 @@ void Chunk::init_chunk() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_is_gened = true;
|
mark_dirty();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Chunk::upload_to_gpu() {
|
||||||
|
|
||||||
|
CUBED_ASSERT(is_dirty());
|
||||||
|
if (m_vbo == 0) {
|
||||||
|
glGenBuffers(1, &m_vbo);
|
||||||
|
}
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, m_vertexs_data.size() * sizeof(Vertex), m_vertexs_data.data(), GL_DYNAMIC_DRAW);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
// after fininshed it, can use
|
||||||
|
clear_dirty();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
bool Chunk::is_dirty() const{
|
bool Chunk::is_dirty() const{
|
||||||
return m_dirty;
|
return m_dirty.load();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunk::mark_dirty() {
|
void Chunk::mark_dirty() {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include <Cubed/gameplay/player.hpp>
|
|
||||||
#include <Cubed/gameplay/world.hpp>
|
#include <Cubed/gameplay/world.hpp>
|
||||||
|
#include <Cubed/gameplay/player.hpp>
|
||||||
#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/cubed_hash.hpp>
|
#include <Cubed/tools/cubed_hash.hpp>
|
||||||
@@ -12,7 +12,15 @@ World::World() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
World::~World() {
|
World::~World() {
|
||||||
|
stop_gen_thread();
|
||||||
|
m_chunks.clear();
|
||||||
|
{
|
||||||
|
std::lock_guard lk(m_delete_vbo_mutex);
|
||||||
|
for (auto x : m_pending_delete_vbo) {
|
||||||
|
glDeleteBuffers(1, &x);
|
||||||
|
}
|
||||||
|
m_pending_delete_vbo.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool World::can_move(const AABB& player_box) const{
|
bool World::can_move(const AABB& player_box) const{
|
||||||
@@ -104,6 +112,7 @@ void World::init_world() {
|
|||||||
auto& [chunk_pos, chunk] = chunk_map;
|
auto& [chunk_pos, chunk] = chunk_map;
|
||||||
|
|
||||||
chunk.gen_vertex_data();
|
chunk.gen_vertex_data();
|
||||||
|
chunk.upload_to_gpu();
|
||||||
|
|
||||||
}
|
}
|
||||||
auto t2 = std::chrono::system_clock::now();
|
auto t2 = std::chrono::system_clock::now();
|
||||||
@@ -112,15 +121,18 @@ void World::init_world() {
|
|||||||
// init players
|
// init players
|
||||||
m_players.emplace(HASH::str("TestPlayer"), Player(*this, "TestPlayer"));
|
m_players.emplace(HASH::str("TestPlayer"), Player(*this, "TestPlayer"));
|
||||||
Logger::info("TestPlayer Create Finish");
|
Logger::info("TestPlayer Create Finish");
|
||||||
|
|
||||||
|
start_gen_thread();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
for (const auto& chunk_map : m_chunks) {
|
|
||||||
const auto& [pos, chunk] = chunk_map;
|
for (const auto& snapshot : m_render_snapshots) {
|
||||||
glm::vec3 center = glm::vec3(static_cast<float>(pos.x * CHUCK_SIZE) + static_cast<float>(CHUCK_SIZE / 2), static_cast<float>(WORLD_SIZE_Y/ 2), static_cast<float>(pos.z * CHUCK_SIZE) + static_cast<float>(CHUCK_SIZE / 2));
|
|
||||||
if (is_aabb_in_frustum(center, glm::vec3(static_cast<float>(CHUCK_SIZE / 2), static_cast<float>(WORLD_SIZE_Y / 2), static_cast<float>(CHUCK_SIZE / 2)))) {
|
if (is_aabb_in_frustum(snapshot.center, snapshot.half_extents)) {
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, chunk.get_vbo());
|
glBindBuffer(GL_ARRAY_BUFFER, snapshot.vbo);
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
|
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(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));
|
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, layer));
|
||||||
@@ -129,7 +141,7 @@ void World::render(const glm::mat4& mvp_matrix) {
|
|||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
glEnableVertexAttribArray(2);
|
glEnableVertexAttribArray(2);
|
||||||
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, chunk.get_vertex_data().size());
|
glDrawArrays(GL_TRIANGLES, 0, snapshot.vertex_count);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -139,7 +151,7 @@ void World::render(const glm::mat4& mvp_matrix) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ChunkPos World::chunk_pos(int world_x, int world_z) const{
|
ChunkPos World::chunk_pos(int world_x, int world_z) {
|
||||||
int chunk_x, chunk_z;
|
int chunk_x, chunk_z;
|
||||||
if (world_x < 0) {
|
if (world_x < 0) {
|
||||||
chunk_x = (world_x + 1) / CHUCK_SIZE - 1;
|
chunk_x = (world_x + 1) / CHUCK_SIZE - 1;
|
||||||
@@ -156,9 +168,13 @@ ChunkPos World::chunk_pos(int world_x, int world_z) const{
|
|||||||
return {chunk_x, chunk_z};
|
return {chunk_x, chunk_z};
|
||||||
}
|
}
|
||||||
|
|
||||||
void World::gen_chunks() {
|
void World::gen_chunks_internal() {
|
||||||
const auto& player = get_player("TestPlayer");
|
glm::vec3 player_pos;
|
||||||
const auto& player_pos = player.get_player_pos();
|
{
|
||||||
|
std::lock_guard lk(m_gen_player_pos_mutex);
|
||||||
|
player_pos = m_gen_player_pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int x = std::floor(player_pos.x);
|
int x = std::floor(player_pos.x);
|
||||||
int z = std::floor(player_pos.z);
|
int z = std::floor(player_pos.z);
|
||||||
@@ -173,20 +189,22 @@ void World::gen_chunks() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
CUBED_ASSERT_MSG(!cur_chunks.empty(), "cur chunks is empty!!");
|
CUBED_ASSERT_MSG(!cur_chunks.empty(), "cur chunks is empty!!");
|
||||||
|
std::vector<std::pair<ChunkPos, Chunk>> new_chunks;
|
||||||
for (auto it = m_chunks.begin(); it != m_chunks.end(); ) {
|
{
|
||||||
if (cur_chunks.find(it->first) == cur_chunks.end()) {
|
std::lock_guard lk(m_chunks_mutex);
|
||||||
it = m_chunks.erase(it);
|
for (auto it = m_chunks.begin(); it != m_chunks.end(); ) {
|
||||||
} else {
|
if (cur_chunks.find(it->first) == cur_chunks.end()) {
|
||||||
++it;
|
it = m_chunks.erase(it);
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for (auto pos: cur_chunks) {
|
for (auto pos: cur_chunks) {
|
||||||
auto it = m_chunks.find(pos);
|
auto it = m_chunks.find(pos);
|
||||||
if (it == m_chunks.end()) {
|
if (it == m_chunks.end()) {
|
||||||
m_chunks.emplace(pos, Chunk(*this, pos));
|
pre_gen_chunks.push_back(pos);
|
||||||
pre_gen_chunks.push_back(pos);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,27 +212,98 @@ void World::gen_chunks() {
|
|||||||
if (pre_gen_chunks.empty()) {
|
if (pre_gen_chunks.empty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (auto& pos : pre_gen_chunks) {
|
||||||
|
new_chunks.push_back({pos, Chunk(*this, pos)});
|
||||||
|
}
|
||||||
|
|
||||||
static const ChunkPos CHUNK_DIR[] {
|
static const ChunkPos CHUNK_DIR[] {
|
||||||
{1, 0}, {-1, 0}, {0, -1}, {0, 1}
|
{1, 0}, {-1, 0}, {0, 1}, {0, -1}
|
||||||
};
|
};
|
||||||
for (const auto& pos : pre_gen_chunks) {
|
|
||||||
auto it = m_chunks.find(pos);
|
std::unordered_map<ChunkPos, const Chunk&, ChunkPos::Hash> neighbor;
|
||||||
CUBED_ASSERT_MSG(it != m_chunks.end(), "Chunk Don't find");
|
|
||||||
//Logger::info("Init Chunk {} {}", pos.x, pos.z);
|
{
|
||||||
it->second.init_chunk();
|
std::lock_guard lk(m_chunks_mutex);
|
||||||
it->second.mark_dirty();
|
for (auto& [pos, chunk] : new_chunks) {
|
||||||
for (const auto& dir : CHUNK_DIR) {
|
for (const auto& dir : CHUNK_DIR) {
|
||||||
auto it = m_chunks.find(pos + dir);
|
auto it = m_chunks.find(pos + dir);
|
||||||
if (it != m_chunks.end()) {
|
if (it != m_chunks.end()) {
|
||||||
it->second.mark_dirty();
|
neighbor.insert({it->first, (it->second)});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<const std::vector<uint8_t>*> neighbor_block(4);
|
||||||
|
|
||||||
|
for (auto& [pos, chunk] : new_chunks) {
|
||||||
|
|
||||||
|
chunk.init_chunk();
|
||||||
|
neighbor.insert({pos, chunk});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& [pos, chunk] : new_chunks) {
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
auto it = neighbor.find(pos + CHUNK_DIR[i]);
|
||||||
|
if (it != neighbor.end()) {
|
||||||
|
neighbor_block[i] = &(it->second.get_chunk_blocks());
|
||||||
|
} else {
|
||||||
|
neighbor_block[i] = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chunk.gen_vertex_data(neighbor_block);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::lock_guard lk(m_new_chunk_queue_mutex);
|
||||||
|
for (auto& x : new_chunks) {
|
||||||
|
m_new_chunk_queue.emplace_back(std::move(x));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void World::start_gen_thread() {
|
||||||
|
m_gen_running = true;
|
||||||
|
Logger::info("Gen Thread Started");
|
||||||
|
m_gen_thread = std::thread([this](){
|
||||||
|
while (m_gen_running) {
|
||||||
|
std::unique_lock<std::mutex> lk(m_gen_signal_mutex);
|
||||||
|
|
||||||
|
m_gen_cv.wait(lk, [this](){
|
||||||
|
return m_need_gen_chunk.load() || !m_gen_running;
|
||||||
|
});
|
||||||
|
if (!m_gen_running) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
m_need_gen_chunk = false;
|
||||||
|
lk.unlock();
|
||||||
|
|
||||||
|
gen_chunks_internal();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void World::stop_gen_thread() {
|
||||||
|
m_gen_running = false;
|
||||||
|
m_gen_cv.notify_all();
|
||||||
|
if (m_gen_thread.joinable()) {
|
||||||
|
m_gen_thread.join();
|
||||||
|
}
|
||||||
|
Logger::info("Gen Thread Stopped");
|
||||||
}
|
}
|
||||||
|
|
||||||
void World::need_gen() {
|
void World::need_gen() {
|
||||||
|
{
|
||||||
|
std::lock_guard lk(m_gen_player_pos_mutex);
|
||||||
|
m_gen_player_pos = get_player("TestPlayer").get_player_pos();
|
||||||
|
}
|
||||||
|
|
||||||
m_need_gen_chunk = true;
|
m_need_gen_chunk = true;
|
||||||
|
m_gen_cv.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool World::is_aabb_in_frustum(const glm::vec3& center, const glm::vec3& half_extents) {
|
bool World::is_aabb_in_frustum(const glm::vec3& center, const glm::vec3& half_extents) {
|
||||||
@@ -254,13 +343,11 @@ int World::get_block(const glm::ivec3& block_pos) const {
|
|||||||
|
|
||||||
bool World::is_block(const glm::ivec3& block_pos) const{
|
bool World::is_block(const glm::ivec3& block_pos) const{
|
||||||
auto [chunk_x, chunk_z] = chunk_pos(block_pos.x, block_pos.z);
|
auto [chunk_x, chunk_z] = chunk_pos(block_pos.x, block_pos.z);
|
||||||
|
|
||||||
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
||||||
|
|
||||||
if (it == m_chunks.end()) {
|
if (it == m_chunks.end()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
||||||
int x, y, z;
|
int x, y, z;
|
||||||
y = block_pos.y;
|
y = block_pos.y;
|
||||||
@@ -279,11 +366,11 @@ bool World::is_block(const glm::ivec3& block_pos) const{
|
|||||||
|
|
||||||
void World::set_block(const glm::ivec3& block_pos, unsigned id) {
|
void World::set_block(const glm::ivec3& block_pos, unsigned id) {
|
||||||
|
|
||||||
|
|
||||||
int world_x, world_y, world_z;
|
int world_x, world_y, world_z;
|
||||||
world_x = block_pos.x;
|
world_x = block_pos.x;
|
||||||
world_y = block_pos.y;
|
world_y = block_pos.y;
|
||||||
world_z = block_pos.z;
|
world_z = block_pos.z;
|
||||||
|
|
||||||
auto [chunk_x, chunk_z] = chunk_pos(world_x, world_z);
|
auto [chunk_x, chunk_z] = chunk_pos(world_x, world_z);
|
||||||
|
|
||||||
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
||||||
@@ -323,17 +410,55 @@ void World::set_block(const glm::ivec3& block_pos, unsigned id) {
|
|||||||
void World::update(float delta_time) {
|
void World::update(float delta_time) {
|
||||||
for (auto& player : m_players) {
|
for (auto& player : m_players) {
|
||||||
player.second.update(delta_time);
|
player.second.update(delta_time);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::lock_guard lk(m_delete_vbo_mutex);
|
||||||
|
for (auto x : m_pending_delete_vbo) {
|
||||||
|
glDeleteBuffers(1, &x);
|
||||||
|
}
|
||||||
|
m_pending_delete_vbo.clear();
|
||||||
}
|
}
|
||||||
if (m_need_gen_chunk) {
|
{
|
||||||
gen_chunks();
|
std::scoped_lock lk(m_chunks_mutex, m_new_chunk_queue_mutex);
|
||||||
m_need_gen_chunk = false;
|
m_new_chunk.clear();
|
||||||
|
for (auto& x : m_new_chunk_queue) {
|
||||||
|
m_new_chunk.emplace_back(std::move(x));
|
||||||
|
}
|
||||||
|
m_new_chunk_queue.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (auto& x : m_new_chunk) {
|
||||||
|
x.second.upload_to_gpu();
|
||||||
|
}
|
||||||
|
|
||||||
// unified compute vertex data before rendering
|
// unified compute vertex data before rendering
|
||||||
for (auto& [key, chunk] : m_chunks) {
|
{
|
||||||
if (chunk.is_dirty()) {
|
std::lock_guard lk(m_chunks_mutex);
|
||||||
chunk.gen_vertex_data();
|
for (auto& x : m_new_chunk) {
|
||||||
chunk.clear_dirty();
|
m_chunks.insert_or_assign(x.first, std::move(x.second));
|
||||||
|
}
|
||||||
|
m_render_snapshots.clear();
|
||||||
|
for (auto& [pos, chunk] : m_chunks) {
|
||||||
|
if (chunk.is_dirty()) {
|
||||||
|
// the curial fator influence
|
||||||
|
chunk.gen_vertex_data();
|
||||||
|
chunk.upload_to_gpu();
|
||||||
|
}
|
||||||
|
if (!chunk.is_dirty()) {
|
||||||
|
m_render_snapshots.push_back({
|
||||||
|
chunk.get_vbo(),
|
||||||
|
chunk.get_vertex_data().size(),
|
||||||
|
glm::vec3(static_cast<float>(pos.x * CHUCK_SIZE) + static_cast<float>(CHUCK_SIZE / 2), static_cast<float>(WORLD_SIZE_Y/ 2), static_cast<float>(pos.z * CHUCK_SIZE) + static_cast<float>(CHUCK_SIZE / 2)),
|
||||||
|
glm::vec3(static_cast<float>(CHUCK_SIZE / 2), static_cast<float>(WORLD_SIZE_Y / 2), static_cast<float>(CHUCK_SIZE / 2))
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void World::push_delete_vbo(GLuint vbo) {
|
||||||
|
std::lock_guard lk(m_delete_vbo_mutex);
|
||||||
|
m_pending_delete_vbo.push_back(vbo);
|
||||||
}
|
}
|
||||||
@@ -1,392 +0,0 @@
|
|||||||
#include <Cubed/gameplay/world_thread_preview.hpp>
|
|
||||||
#include <Cubed/gameplay/player.hpp>
|
|
||||||
#include <Cubed/map_table.hpp>
|
|
||||||
#include <Cubed/tools/cubed_assert.hpp>
|
|
||||||
#include <Cubed/tools/cubed_hash.hpp>
|
|
||||||
#include <Cubed/tools/math_tools.hpp>
|
|
||||||
|
|
||||||
#include <unordered_set>
|
|
||||||
|
|
||||||
World::World() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
World::~World() {
|
|
||||||
stop_gen_thread();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool World::can_move(const AABB& player_box) const{
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,int world_z) {
|
|
||||||
auto [chunk_x, chunk_z] = chunk_pos(world_x, world_z);
|
|
||||||
//Logger::info("Chunk PosX : {} Chuch PosZ : {}", chunk_x, chunk_z);
|
|
||||||
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
|
||||||
CUBED_ASSERT_MSG(it != m_chunks.end(), "Chunk not find");
|
|
||||||
|
|
||||||
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
|
||||||
int x, y, z;
|
|
||||||
y = world_y;
|
|
||||||
x = world_x - chunk_x * CHUCK_SIZE;
|
|
||||||
z = world_z - chunk_z * CHUCK_SIZE;
|
|
||||||
//BlockRenderData m_block_render_data;
|
|
||||||
// block id
|
|
||||||
m_block_render_data.block_id = chunk_blocks[Chunk::get_index(x, y, z)];
|
|
||||||
if (m_block_render_data.block_id == 0) {
|
|
||||||
return m_block_render_data;
|
|
||||||
}
|
|
||||||
// draw_face
|
|
||||||
m_block_render_data.draw_face.assign(6, true);
|
|
||||||
static const std::vector<glm::ivec3> DIR = {
|
|
||||||
glm::ivec3(0, 0, 1),
|
|
||||||
glm::ivec3(1, 0, 0),
|
|
||||||
glm::ivec3(0 ,0, -1),
|
|
||||||
glm::ivec3(-1, 0, 0),
|
|
||||||
glm::ivec3(0, 1, 0),
|
|
||||||
glm::ivec3(0, -1, 0)
|
|
||||||
};
|
|
||||||
glm::ivec3 world_pos = glm::ivec3(world_x, world_y, world_z);
|
|
||||||
for (int i = 0; i < 6; i++) {
|
|
||||||
if (is_block(world_pos + DIR[i])) {
|
|
||||||
m_block_render_data.draw_face[i] = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return m_block_render_data;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
const std::optional<LookBlock>& World::get_look_block_pos(const std::string& name) const{
|
|
||||||
static std::optional<LookBlock> null_look_block = std::nullopt;
|
|
||||||
auto it = m_players.find(HASH::str(name));
|
|
||||||
if (it == m_players.end()) {
|
|
||||||
Logger::error("Can't find player {}", name);
|
|
||||||
CUBED_ASSERT(0);
|
|
||||||
return null_look_block;
|
|
||||||
}
|
|
||||||
|
|
||||||
return it->second.get_look_block_pos();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Player& World::get_player(const std::string& name){
|
|
||||||
auto it = m_players.find(HASH::str(name));
|
|
||||||
if (it == m_players.end()) {
|
|
||||||
Logger::error("Can't find player {}", name);
|
|
||||||
CUBED_ASSERT(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return it->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void World::init_world() {
|
|
||||||
auto t1 = std::chrono::system_clock::now();
|
|
||||||
for (int s = 0; s < DISTANCE; s++) {
|
|
||||||
for (int t = 0; t < DISTANCE; t++) {
|
|
||||||
int ns = s - DISTANCE / 2;
|
|
||||||
int nt = t - DISTANCE / 2;
|
|
||||||
|
|
||||||
ChunkPos pos{ns, nt};
|
|
||||||
|
|
||||||
m_chunks.emplace(pos, Chunk(*this, pos));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& chunk_map : m_chunks) {
|
|
||||||
auto& [chunk_pos, chunk] = chunk_map;
|
|
||||||
chunk.init_chunk();
|
|
||||||
|
|
||||||
}
|
|
||||||
// After block gen fininshed
|
|
||||||
for (auto& chunk_map : m_chunks) {
|
|
||||||
auto& [chunk_pos, chunk] = chunk_map;
|
|
||||||
|
|
||||||
chunk.gen_vertex_data();
|
|
||||||
|
|
||||||
}
|
|
||||||
auto t2 = std::chrono::system_clock::now();
|
|
||||||
auto d = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
|
|
||||||
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");
|
|
||||||
|
|
||||||
start_gen_thread();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void World::render(const glm::mat4& mvp_matrix) {
|
|
||||||
Math::extract_frustum_planes(mvp_matrix, m_planes);
|
|
||||||
|
|
||||||
for (const auto& snapshot : m_render_snapshots) {
|
|
||||||
|
|
||||||
if (is_aabb_in_frustum(snapshot.center, snapshot.half_extents)) {
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, snapshot.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.vertex_count);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
ChunkPos World::chunk_pos(int world_x, int world_z) const{
|
|
||||||
int chunk_x, chunk_z;
|
|
||||||
if (world_x < 0) {
|
|
||||||
chunk_x = (world_x + 1) / CHUCK_SIZE - 1;
|
|
||||||
}
|
|
||||||
if (world_x >= 0) {
|
|
||||||
chunk_x = world_x / CHUCK_SIZE;
|
|
||||||
}
|
|
||||||
if (world_z < 0) {
|
|
||||||
chunk_z = (world_z + 1) / CHUCK_SIZE - 1;
|
|
||||||
}
|
|
||||||
if (world_z >= 0) {
|
|
||||||
chunk_z = world_z / CHUCK_SIZE;
|
|
||||||
}
|
|
||||||
return {chunk_x, chunk_z};
|
|
||||||
}
|
|
||||||
|
|
||||||
void World::gen_chunks_internal() {
|
|
||||||
|
|
||||||
const auto& player_pos = m_gen_player_pos;
|
|
||||||
|
|
||||||
int x = std::floor(player_pos.x);
|
|
||||||
int z = std::floor(player_pos.z);
|
|
||||||
auto [chunk_x, chunk_z] = chunk_pos(x, z);
|
|
||||||
std::unordered_set<ChunkPos, ChunkPos::Hash> cur_chunks;
|
|
||||||
std::vector<ChunkPos> pre_gen_chunks;
|
|
||||||
cur_chunks.reserve(DISTANCE * DISTANCE);
|
|
||||||
int half = DISTANCE / 2;
|
|
||||||
for (int u = chunk_x - half; u <= chunk_x + half; ++u) {
|
|
||||||
for (int v = chunk_z - half; v <= chunk_z + half; ++v) {
|
|
||||||
cur_chunks.emplace(u, v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
CUBED_ASSERT_MSG(!cur_chunks.empty(), "cur chunks is empty!!");
|
|
||||||
std::vector<std::pair<ChunkPos, Chunk>> new_chunks;
|
|
||||||
{
|
|
||||||
std::lock_guard lk(m_chunks_mutex);
|
|
||||||
for (auto it = m_chunks.begin(); it != m_chunks.end(); ) {
|
|
||||||
if (cur_chunks.find(it->first) == cur_chunks.end()) {
|
|
||||||
it = m_chunks.erase(it);
|
|
||||||
} else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto pos: cur_chunks) {
|
|
||||||
auto it = m_chunks.find(pos);
|
|
||||||
if (it == m_chunks.end()) {
|
|
||||||
pre_gen_chunks.push_back(pos);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger::info("New Gen Chunks Sum: {}", pre_gen_chunks.size());
|
|
||||||
if (pre_gen_chunks.empty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& pos : pre_gen_chunks) {
|
|
||||||
new_chunks.push_back({pos, Chunk(*this, pos)});
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& [pos, chunk] : new_chunks) {
|
|
||||||
chunk.init_chunk();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static const ChunkPos CHUNK_DIR[] {
|
|
||||||
{1, 0}, {-1, 0}, {0, -1}, {0, 1}
|
|
||||||
};
|
|
||||||
|
|
||||||
{
|
|
||||||
std::lock_guard lk(m_chunks_mutex);
|
|
||||||
for (auto& [pos, chunk] : new_chunks) {
|
|
||||||
chunk.mark_dirty();
|
|
||||||
m_chunks.emplace(pos, std::move(chunk));
|
|
||||||
for (const auto& dir : CHUNK_DIR) {
|
|
||||||
auto it = m_chunks.find(pos + dir);
|
|
||||||
if (it != m_chunks.end()) {
|
|
||||||
it->second.mark_dirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void World::start_gen_thread() {
|
|
||||||
m_gen_running = true;
|
|
||||||
Logger::info("Gen Thread Started");
|
|
||||||
m_gen_thread = std::thread([this](){
|
|
||||||
while (m_gen_running) {
|
|
||||||
std::unique_lock<std::mutex> lk(m_gen_signal_mutex);
|
|
||||||
|
|
||||||
m_gen_cv.wait(lk, [this](){
|
|
||||||
return m_need_gen_chunk.load() || !m_gen_running;
|
|
||||||
});
|
|
||||||
if (!m_gen_running) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
m_need_gen_chunk = false;
|
|
||||||
lk.unlock();
|
|
||||||
|
|
||||||
gen_chunks_internal();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
void World::stop_gen_thread() {
|
|
||||||
m_gen_running = false;
|
|
||||||
m_gen_cv.notify_all();
|
|
||||||
if (m_gen_thread.joinable()) {
|
|
||||||
m_gen_thread.join();
|
|
||||||
}
|
|
||||||
Logger::info("Gen Thread Stopped");
|
|
||||||
}
|
|
||||||
|
|
||||||
void World::need_gen() {
|
|
||||||
m_gen_player_pos = get_player("TestPlayer").get_player_pos();
|
|
||||||
m_need_gen_chunk = true;
|
|
||||||
m_gen_cv.notify_one();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool World::is_aabb_in_frustum(const glm::vec3& center, const glm::vec3& half_extents) {
|
|
||||||
for (const auto& plane : m_planes) {
|
|
||||||
// distance
|
|
||||||
float d = glm::dot(glm::vec3(plane), center) + plane.w;
|
|
||||||
float r = half_extents.x * std::abs(plane.x) +
|
|
||||||
half_extents.y * std::abs(plane.y) +
|
|
||||||
half_extents.z * std::abs(plane.z);
|
|
||||||
if (d + r < 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
int World::get_block(const glm::ivec3& block_pos) const {
|
|
||||||
auto [chunk_x, chunk_z] = chunk_pos(block_pos.x, block_pos.z);
|
|
||||||
|
|
||||||
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
|
||||||
|
|
||||||
if (it == m_chunks.end()) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
|
||||||
int x, y, z;
|
|
||||||
y = block_pos.y;
|
|
||||||
x = block_pos.x - chunk_x * CHUCK_SIZE;
|
|
||||||
z = block_pos.z - chunk_z * CHUCK_SIZE;
|
|
||||||
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y || z >= CHUCK_SIZE) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return chunk_blocks[Chunk::get_index(x, y, z)];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool World::is_block(const glm::ivec3& block_pos) const{
|
|
||||||
auto [chunk_x, chunk_z] = chunk_pos(block_pos.x, block_pos.z);
|
|
||||||
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
|
||||||
|
|
||||||
if (it == m_chunks.end()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
|
||||||
int x, y, z;
|
|
||||||
y = block_pos.y;
|
|
||||||
x = block_pos.x - chunk_x * CHUCK_SIZE;
|
|
||||||
z = block_pos.z - chunk_z * CHUCK_SIZE;
|
|
||||||
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y || z >= CHUCK_SIZE) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
auto id = chunk_blocks[Chunk::get_index(x, y, z)];
|
|
||||||
if (id == 0) {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void World::set_block(const glm::ivec3& block_pos, unsigned id) {
|
|
||||||
|
|
||||||
|
|
||||||
int world_x, world_y, world_z;
|
|
||||||
world_x = block_pos.x;
|
|
||||||
world_y = block_pos.y;
|
|
||||||
world_z = block_pos.z;
|
|
||||||
auto [chunk_x, chunk_z] = chunk_pos(world_x, world_z);
|
|
||||||
|
|
||||||
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
|
||||||
|
|
||||||
if (it == m_chunks.end()) {
|
|
||||||
return ;
|
|
||||||
}
|
|
||||||
|
|
||||||
int x, y, z;
|
|
||||||
y = world_y;
|
|
||||||
x = world_x - chunk_x * CHUCK_SIZE;
|
|
||||||
z = world_z - chunk_z * CHUCK_SIZE;
|
|
||||||
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y || z >= CHUCK_SIZE) {
|
|
||||||
return ;
|
|
||||||
}
|
|
||||||
|
|
||||||
it->second.set_chunk_block(Chunk::get_index(x, y, z), id);
|
|
||||||
|
|
||||||
static const glm::ivec3 NEIGHBOR_DIRS[] = {
|
|
||||||
{1, 0, 0}, {-1, 0, 0}, {0, 0, -1}, {0, 0, 1}
|
|
||||||
};
|
|
||||||
|
|
||||||
for (const auto& dir : NEIGHBOR_DIRS) {
|
|
||||||
glm::ivec3 neighbor = block_pos + dir;
|
|
||||||
|
|
||||||
auto [cx, cz] = chunk_pos(neighbor.x, neighbor.z);
|
|
||||||
auto it = m_chunks.find({cx, cz});
|
|
||||||
if (it != m_chunks.end()) {
|
|
||||||
it->second.mark_dirty();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void World::update(float delta_time) {
|
|
||||||
for (auto& player : m_players) {
|
|
||||||
player.second.update(delta_time);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// unified compute vertex data before rendering
|
|
||||||
{
|
|
||||||
std::lock_guard lk(m_chunks_mutex);
|
|
||||||
m_render_snapshots.clear();
|
|
||||||
for (auto& [pos, chunk] : m_chunks) {
|
|
||||||
if (chunk.is_dirty()) {
|
|
||||||
chunk.gen_vertex_data();
|
|
||||||
chunk.clear_dirty();
|
|
||||||
}
|
|
||||||
m_render_snapshots.push_back({
|
|
||||||
chunk.get_vbo(),
|
|
||||||
chunk.get_vertex_data().size(),
|
|
||||||
glm::vec3(static_cast<float>(pos.x * CHUCK_SIZE) + static_cast<float>(CHUCK_SIZE / 2), static_cast<float>(WORLD_SIZE_Y/ 2), static_cast<float>(pos.z * CHUCK_SIZE) + static_cast<float>(CHUCK_SIZE / 2)),
|
|
||||||
glm::vec3(static_cast<float>(CHUCK_SIZE / 2), static_cast<float>(WORLD_SIZE_Y / 2), static_cast<float>(CHUCK_SIZE / 2))
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -11,7 +11,11 @@ Window::Window(Renderer& renderer) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
Window::~Window() {
|
Window::~Window() {
|
||||||
glfwDestroyWindow(m_window);
|
if (m_window) {
|
||||||
|
glfwDestroyWindow(m_window);
|
||||||
|
m_window = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user