mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-18 00:27:02 +08:00
feat: grass (#11)
* feat: add grass texture and update grass_block texture * feat: add block data * feat: add blocks_tool * feat: add sync info and change function in blocks_tools * feat: add check and new function * refactor: make block texture loading data-driven * feat: add rendering for grass * feat: passable grass * feat: random grass place * fix: memory leak in TextureManager::load_cross_plane_texture
This commit is contained in:
@@ -15,5 +15,28 @@ void BiomeBuilder::build_bottom() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BiomeBuilder::place_grass() {
|
||||
ChunkGenerator& chunk_generator = get_chunk_generator();
|
||||
Chunk& chunk = chunk_generator.chunk();
|
||||
auto& blocks = chunk.blocks();
|
||||
const auto& heightmap = chunk.get_heightmap();
|
||||
auto& random = chunk_generator.random();
|
||||
for (int x = 0; x < SIZE_X; ++x) {
|
||||
for (int z = 0; z < SIZE_Z; ++z) {
|
||||
int y = heightmap[x][z];
|
||||
BlockType top_id = blocks[Chunk::index(x, y, z)];
|
||||
if (top_id != 1) {
|
||||
continue;
|
||||
}
|
||||
if (blocks[Chunk::index(x, y + 1, z)] != 0) {
|
||||
continue;
|
||||
}
|
||||
if (random.random_bool(0.2)) {
|
||||
if (y + 1 < SIZE_Y) {
|
||||
blocks[Chunk::index(x, y + 1, z)] = 9;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace Cubed
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "Cubed/gameplay/chunk_generator.hpp"
|
||||
#include "Cubed/gameplay/tree.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
|
||||
namespace Cubed {
|
||||
@@ -52,6 +53,7 @@ void ForestBuilder::build_vegetation() {
|
||||
}
|
||||
}
|
||||
}
|
||||
place_grass();
|
||||
}
|
||||
|
||||
ChunkGenerator& ForestBuilder::get_chunk_generator() {
|
||||
|
||||
@@ -29,7 +29,7 @@ void PlainBuilder::build_blocks() {
|
||||
}
|
||||
}
|
||||
|
||||
void PlainBuilder::build_vegetation() {}
|
||||
void PlainBuilder::build_vegetation() { place_grass(); }
|
||||
|
||||
ChunkGenerator& PlainBuilder::get_chunk_generator() {
|
||||
return m_chunk_generator;
|
||||
|
||||
@@ -12,38 +12,49 @@ Chunk::Chunk(World& world, ChunkPos chunk_pos)
|
||||
: m_chunk_pos(chunk_pos), m_world(world) {}
|
||||
|
||||
Chunk::~Chunk() {
|
||||
if (m_vbo != 0) {
|
||||
m_world.push_delete_vbo(m_vbo);
|
||||
if (m_normal_vbo != 0) {
|
||||
m_world.push_delete_vbo(m_normal_vbo);
|
||||
}
|
||||
if (m_cross_plane_vbo != 0) {
|
||||
m_world.push_delete_vbo(m_cross_plane_vbo);
|
||||
}
|
||||
}
|
||||
|
||||
Chunk::Chunk(Chunk&& other) noexcept
|
||||
: m_dirty(other.is_dirty()), m_need_upload(other.m_need_upload.load()),
|
||||
m_is_on_gen_vertex_data(other.m_is_on_gen_vertex_data.load()),
|
||||
m_vertex_sum(other.m_vertex_sum.load()), m_biome(other.m_biome.load()),
|
||||
m_chunk_pos(std::move(other.m_chunk_pos)), m_world(other.m_world),
|
||||
m_heightmap(std::move(other.m_heightmap)),
|
||||
m_blocks(std::move(other.m_blocks)), m_vbo(other.m_vbo),
|
||||
m_vertexs_data(std::move(other.m_vertexs_data)), m_seed(other.m_seed),
|
||||
m_conditions(other.m_conditions) {
|
||||
other.m_vbo = 0;
|
||||
m_normal_vertices_sum(other.m_normal_vertices_sum.load()),
|
||||
m_cross_vertices_sum(other.m_cross_vertices_sum.load()),
|
||||
m_biome(other.m_biome.load()), m_chunk_pos(std::move(other.m_chunk_pos)),
|
||||
m_world(other.m_world), m_heightmap(std::move(other.m_heightmap)),
|
||||
m_blocks(std::move(other.m_blocks)), m_normal_vbo(other.m_normal_vbo),
|
||||
m_cross_plane_vbo(other.m_cross_plane_vbo),
|
||||
m_normal_vertices(std::move(other.m_normal_vertices)),
|
||||
m_cross_plane_vertices(std::move(other.m_cross_plane_vertices)),
|
||||
m_seed(other.m_seed), m_conditions(other.m_conditions) {
|
||||
other.m_normal_vbo = 0;
|
||||
other.m_cross_plane_vbo = 0;
|
||||
}
|
||||
|
||||
Chunk& Chunk::operator=(Chunk&& other) noexcept {
|
||||
// Logger::info("other Chunk pos {} {} in Chunk& Chunk::operator=(Chunk&&
|
||||
// other) this {}", other.m_chunk_pos.x, other.m_chunk_pos.z,
|
||||
// static_cast<const void*>(&other));
|
||||
m_vbo = other.m_vbo;
|
||||
other.m_vbo = 0;
|
||||
m_normal_vbo = other.m_normal_vbo;
|
||||
other.m_normal_vbo = 0;
|
||||
m_cross_plane_vbo = other.m_cross_plane_vbo;
|
||||
other.m_cross_plane_vbo = 0;
|
||||
m_chunk_pos = std::move(other.m_chunk_pos);
|
||||
m_heightmap = std::move(other.m_heightmap);
|
||||
m_blocks = std::move(other.m_blocks);
|
||||
m_dirty = other.is_dirty();
|
||||
m_vertexs_data = std::move(other.m_vertexs_data);
|
||||
m_normal_vertices = std::move(other.m_normal_vertices);
|
||||
m_cross_plane_vertices = std::move(other.m_cross_plane_vertices);
|
||||
m_biome = other.m_biome.load();
|
||||
m_is_on_gen_vertex_data = other.m_is_on_gen_vertex_data.load();
|
||||
m_need_upload = other.m_need_upload.load();
|
||||
m_vertex_sum = other.m_vertex_sum.load();
|
||||
m_normal_vertices_sum = other.m_normal_vertices_sum.load();
|
||||
m_cross_vertices_sum = other.m_cross_vertices_sum.load();
|
||||
m_seed = other.m_seed;
|
||||
m_conditions = other.m_conditions;
|
||||
return *this;
|
||||
@@ -115,129 +126,24 @@ void Chunk::gen_vertex_data(
|
||||
}
|
||||
m_is_on_gen_vertex_data = true;
|
||||
std::lock_guard lk(m_vertexs_data_mutex);
|
||||
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 * CHUNK_SIZE;
|
||||
int world_z = z + m_chunk_pos.z * CHUNK_SIZE;
|
||||
int world_y = y;
|
||||
int cur_id = m_blocks[index(x, y, z)];
|
||||
// air
|
||||
if (cur_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_cull = 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_cull =
|
||||
[&](const std::vector<BlockType>* chunk_blocks) {
|
||||
if (chunk_blocks == nullptr) {
|
||||
return true;
|
||||
}
|
||||
int x, y, z;
|
||||
y = world_ny;
|
||||
x = world_nx - neighbor_x * CHUNK_SIZE;
|
||||
z = world_nz - neighbor_z * CHUNK_SIZE;
|
||||
if (x < 0 || y < 0 || z < 0 ||
|
||||
x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||
z >= CHUNK_SIZE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int idx = Chunk::index(x, y, z);
|
||||
// not init
|
||||
if (static_cast<size_t>(idx) >=
|
||||
chunk_blocks->size()) {
|
||||
// Logger::warn("not init");
|
||||
return true;
|
||||
}
|
||||
auto id = (*chunk_blocks)[idx];
|
||||
if (is_in_transparent_map(id)) {
|
||||
if (id == cur_id) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
if (m_chunk_pos.x + 1 == neighbor_x) {
|
||||
neighbor_cull = is_cull(neighbor_block[0]);
|
||||
} else if (m_chunk_pos.x - 1 == neighbor_x) {
|
||||
neighbor_cull = is_cull(neighbor_block[1]);
|
||||
} else if (m_chunk_pos.z + 1 == neighbor_z) {
|
||||
neighbor_cull = is_cull(neighbor_block[2]);
|
||||
} else if (m_chunk_pos.z - 1 == neighbor_z) {
|
||||
neighbor_cull = is_cull(neighbor_block[3]);
|
||||
}
|
||||
// neighbor_cull = m_world.is_block(glm::ivec3(world_x,
|
||||
// world_y, world_z) + DIR[face]);
|
||||
} else {
|
||||
auto id = m_blocks[index(nx, ny, nz)];
|
||||
if (!is_in_transparent_map(id)) {
|
||||
neighbor_cull = true;
|
||||
} else {
|
||||
if (id == cur_id) {
|
||||
neighbor_cull = true;
|
||||
} else {
|
||||
neighbor_cull = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (neighbor_cull) {
|
||||
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>(cur_id * 6 + face)
|
||||
|
||||
};
|
||||
m_vertexs_data.emplace_back(vex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
m_vertex_sum = m_vertexs_data.size();
|
||||
gen_normal_vertices(neighbor_block);
|
||||
gen_cross_plane_vertices();
|
||||
m_need_upload = true;
|
||||
m_is_on_gen_vertex_data = false;
|
||||
}
|
||||
|
||||
GLuint Chunk::get_vbo() const { return m_vbo; }
|
||||
GLuint Chunk::get_normal_vbo() const { return m_normal_vbo; }
|
||||
|
||||
size_t Chunk::get_vertex_sum() const {
|
||||
if (m_vertex_sum == 0) {
|
||||
Logger::warn("m_vertex_sum is 0");
|
||||
size_t Chunk::get_normal_vertices_sum() const {
|
||||
if (m_normal_vertices_sum == 0) {
|
||||
Logger::warn("m_normal_vertices_sum is 0");
|
||||
}
|
||||
return m_vertex_sum.load();
|
||||
return m_normal_vertices_sum.load();
|
||||
}
|
||||
|
||||
GLuint Chunk::get_cross_vbo() const { return m_cross_plane_vbo; }
|
||||
size_t Chunk::get_cross_vertices_sum() const {
|
||||
return m_cross_vertices_sum.load();
|
||||
}
|
||||
|
||||
void Chunk::gen_phase_one() {
|
||||
@@ -310,14 +216,25 @@ void Chunk::gen_phase_seven() {
|
||||
void Chunk::upload_to_gpu() {
|
||||
|
||||
ASSERT(is_need_upload());
|
||||
if (m_vbo == 0) {
|
||||
glGenBuffers(1, &m_vbo);
|
||||
|
||||
if (m_normal_vbo == 0) {
|
||||
glGenBuffers(1, &m_normal_vbo);
|
||||
}
|
||||
|
||||
std::lock_guard lk(m_vertexs_data_mutex);
|
||||
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, m_normal_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, m_normal_vertices.size() * sizeof(Vertex),
|
||||
m_normal_vertices.data(), GL_DYNAMIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
if (m_cross_plane_vertices.size() != 0) {
|
||||
if (m_cross_plane_vbo == 0) {
|
||||
glGenBuffers(1, &m_cross_plane_vbo);
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_cross_plane_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER,
|
||||
m_cross_plane_vertices.size() * sizeof(Vertex),
|
||||
m_cross_plane_vertices.data(), GL_DYNAMIC_DRAW);
|
||||
}
|
||||
// after fininshed it, can use
|
||||
clear_dirty();
|
||||
m_need_upload = false;
|
||||
@@ -356,4 +273,160 @@ unsigned Chunk::seed() const {
|
||||
|
||||
BiomeConditions& Chunk::conditions() { return m_conditions; }
|
||||
|
||||
void Chunk::gen_normal_vertices(
|
||||
const std::array<const std::vector<BlockType>*, 4>& neighbor_block) {
|
||||
m_normal_vertices.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 * CHUNK_SIZE;
|
||||
int world_z = z + m_chunk_pos.z * CHUNK_SIZE;
|
||||
int world_y = y;
|
||||
int cur_id = m_blocks[index(x, y, z)];
|
||||
// air
|
||||
if (cur_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_culled = 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_culled =
|
||||
[&](const std::vector<BlockType>* chunk_blocks) {
|
||||
if (chunk_blocks == nullptr) {
|
||||
return true;
|
||||
}
|
||||
int x, y, z;
|
||||
y = world_ny;
|
||||
x = world_nx - neighbor_x * CHUNK_SIZE;
|
||||
z = world_nz - neighbor_z * CHUNK_SIZE;
|
||||
if (x < 0 || y < 0 || z < 0 ||
|
||||
x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||
z >= CHUNK_SIZE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int idx = Chunk::index(x, y, z);
|
||||
// not init
|
||||
if (static_cast<size_t>(idx) >=
|
||||
chunk_blocks->size()) {
|
||||
// Logger::warn("not init");
|
||||
return true;
|
||||
}
|
||||
auto id = (*chunk_blocks)[idx];
|
||||
// transparent
|
||||
if (BlockManager::is_transparent(id)) {
|
||||
if (id == cur_id) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
if (m_chunk_pos.x + 1 == neighbor_x) {
|
||||
neighbor_culled = is_culled(neighbor_block[0]);
|
||||
} else if (m_chunk_pos.x - 1 == neighbor_x) {
|
||||
neighbor_culled = is_culled(neighbor_block[1]);
|
||||
} else if (m_chunk_pos.z + 1 == neighbor_z) {
|
||||
neighbor_culled = is_culled(neighbor_block[2]);
|
||||
} else if (m_chunk_pos.z - 1 == neighbor_z) {
|
||||
neighbor_culled = is_culled(neighbor_block[3]);
|
||||
}
|
||||
// neighbor_cull = m_world.is_block(glm::ivec3(world_x,
|
||||
// world_y, world_z) + DIR[face]);
|
||||
} else {
|
||||
auto neighbor_id = m_blocks[index(nx, ny, nz)];
|
||||
// transparent block
|
||||
if (!BlockManager::is_transparent(neighbor_id)) {
|
||||
neighbor_culled = true;
|
||||
} else {
|
||||
if (neighbor_id == cur_id) {
|
||||
neighbor_culled = true;
|
||||
} else {
|
||||
neighbor_culled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (neighbor_culled) {
|
||||
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>(cur_id * 6 + face)
|
||||
|
||||
};
|
||||
m_normal_vertices.emplace_back(vex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
m_normal_vertices_sum = m_normal_vertices.size();
|
||||
}
|
||||
void Chunk::gen_cross_plane_vertices() {
|
||||
|
||||
m_cross_plane_vertices.clear();
|
||||
|
||||
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 * CHUNK_SIZE;
|
||||
int world_z = z + m_chunk_pos.z * CHUNK_SIZE;
|
||||
int world_y = y;
|
||||
int id = m_blocks[index(x, y, z)];
|
||||
|
||||
if (!BlockManager::is_cross_plane(id)) {
|
||||
continue;
|
||||
}
|
||||
for (int face = 0; face < 2; face++) {
|
||||
for (int i = 0; i < 6; i++) {
|
||||
Vertex vex = {CROSS_VERTICES_POS[face][i][0] +
|
||||
(float)world_x * 1.0f,
|
||||
CROSS_VERTICES_POS[face][i][1] +
|
||||
(float)world_y * 1.0f,
|
||||
CROSS_VERTICES_POS[face][i][2] +
|
||||
(float)world_z * 1.0f,
|
||||
CROSS_TEX_COORDS[face][i][0],
|
||||
CROSS_TEX_COORDS[face][i][1],
|
||||
static_cast<float>(
|
||||
BlockManager::cross_plane_index(id))
|
||||
|
||||
};
|
||||
m_cross_plane_vertices.emplace_back(vex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
m_cross_vertices_sum = m_cross_plane_vertices.size();
|
||||
// Logger::info("Cross Sum {}", m_cross_vertices_sum.load());
|
||||
}
|
||||
|
||||
} // namespace Cubed
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "Cubed/gameplay/chunk.hpp"
|
||||
#include "Cubed/gameplay/tree.hpp"
|
||||
#include "Cubed/gameplay/world.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/cubed_hash.hpp"
|
||||
#include "Cubed/tools/math_tools.hpp"
|
||||
#include "Cubed/tools/perlin_noise.hpp"
|
||||
|
||||
@@ -421,7 +421,7 @@ void Player::update_x_move() {
|
||||
for (int x = minx; x <= maxx; ++x) {
|
||||
for (int y = miny; y <= maxy; ++y) {
|
||||
for (int z = minz; z <= maxz; ++z) {
|
||||
if (m_world.is_block(glm::vec3{x, y, z})) {
|
||||
if (!m_world.can_pass_block(glm::vec3{x, y, z})) {
|
||||
AABB block_box = {glm::vec3{static_cast<float>(x),
|
||||
static_cast<float>(y),
|
||||
static_cast<float>(z)},
|
||||
@@ -455,7 +455,7 @@ void Player::update_y_move() {
|
||||
for (int x = minx; x <= maxx; ++x) {
|
||||
for (int y = miny; y <= maxy; ++y) {
|
||||
for (int z = minz; z <= maxz; ++z) {
|
||||
if (m_world.is_block(glm::vec3{x, y, z})) {
|
||||
if (!m_world.can_pass_block(glm::vec3{x, y, z})) {
|
||||
AABB block_box = {glm::vec3{static_cast<float>(x),
|
||||
static_cast<float>(y),
|
||||
static_cast<float>(z)},
|
||||
@@ -493,7 +493,7 @@ void Player::update_z_move() {
|
||||
for (int x = minx; x <= maxx; ++x) {
|
||||
for (int y = miny; y <= maxy; ++y) {
|
||||
for (int z = minz; z <= maxz; ++z) {
|
||||
if (m_world.is_block(glm::vec3{x, y, z})) {
|
||||
if (!m_world.can_pass_block(glm::vec3{x, y, z})) {
|
||||
AABB block_box = {glm::vec3{static_cast<float>(x),
|
||||
static_cast<float>(y),
|
||||
static_cast<float>(z)},
|
||||
@@ -526,13 +526,13 @@ void Player::update_scroll(double yoffset) {
|
||||
if (m_game_mode == CREATIVE) {
|
||||
if (yoffset < 0) {
|
||||
m_place_block += 1;
|
||||
if (m_place_block >= MAX_BLOCK_NUM) {
|
||||
if (m_place_block >= BlockManager::sums()) {
|
||||
m_place_block = 1;
|
||||
}
|
||||
} else {
|
||||
m_place_block -= 1;
|
||||
if (m_place_block <= 0) {
|
||||
m_place_block = MAX_BLOCK_NUM - 1;
|
||||
m_place_block = BlockManager::sums() - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "Cubed/gameplay/tree.hpp"
|
||||
|
||||
#include "Cubed/gameplay/chunk.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
|
||||
#include <array>
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/debug_collector.hpp"
|
||||
#include "Cubed/gameplay/player.hpp"
|
||||
#include "Cubed/texture_manager.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/cubed_hash.hpp"
|
||||
#include "Cubed/tools/math_tools.hpp"
|
||||
@@ -290,13 +291,18 @@ void World::init_chunks() {
|
||||
}
|
||||
}
|
||||
*/
|
||||
void World::render(const glm::mat4& mvp_matrix) {
|
||||
void World::render(const glm::mat4& mvp_matrix,
|
||||
const TextureManager& texture_manager) {
|
||||
Math::extract_frustum_planes(mvp_matrix, m_planes);
|
||||
int rendered_sum = 0;
|
||||
auto player_pos = get_player("TestPlayer").get_player_pos();
|
||||
glm::vec2 player_pos_xz{player_pos.x, player_pos.z};
|
||||
for (const auto& snapshot : m_render_snapshots) {
|
||||
|
||||
if (is_aabb_in_frustum(snapshot.center, snapshot.half_extents)) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, snapshot.vbo);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY,
|
||||
texture_manager.get_texture_array());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, snapshot.normal_vbo);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||
(void*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||
@@ -308,8 +314,37 @@ void World::render(const glm::mat4& mvp_matrix) {
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, snapshot.vertex_count);
|
||||
glDrawArrays(GL_TRIANGLES, 0, snapshot.normal_vertices_count);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
if (snapshot.cross_vertices_count != 0) {
|
||||
glm::vec2 center_xz{snapshot.center.x, snapshot.center.z};
|
||||
float dist = glm::distance(player_pos_xz, center_xz);
|
||||
if (dist <= CROSS_PLANE_DISTANCE * 16) {
|
||||
|
||||
glDepthMask(GL_FALSE);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY,
|
||||
texture_manager.get_cross_plane_array());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, snapshot.cross_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.cross_vertices_count);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glDepthMask(GL_TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
rendered_sum++;
|
||||
}
|
||||
}
|
||||
@@ -745,6 +780,24 @@ bool World::is_block(const glm::ivec3& block_pos) const {
|
||||
}
|
||||
}
|
||||
|
||||
bool World::can_pass_block(const glm::ivec3& block_pos) const {
|
||||
auto [chunk_x, chunk_z] = chunk_pos(block_pos.x, block_pos.z);
|
||||
std::lock_guard lk(m_chunks_mutex);
|
||||
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
||||
|
||||
if (it == m_chunks.end()) {
|
||||
return true;
|
||||
}
|
||||
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
||||
auto [x, y, z] = Chunk::world_to_block(block_pos, {chunk_x, chunk_z});
|
||||
if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||
z >= CHUNK_SIZE) {
|
||||
return true;
|
||||
}
|
||||
auto id = chunk_blocks[Chunk::index(x, y, z)];
|
||||
return BlockManager::is_passable(id);
|
||||
}
|
||||
|
||||
void World::set_block(const glm::ivec3& block_pos, unsigned id) {
|
||||
|
||||
int world_x, world_y, world_z;
|
||||
@@ -841,7 +894,8 @@ void World::update(float delta_time) {
|
||||
chunk.upload_to_gpu();
|
||||
}
|
||||
m_render_snapshots.push_back(
|
||||
{chunk.get_vbo(), chunk.get_vertex_sum(),
|
||||
{chunk.get_normal_vbo(), chunk.get_normal_vertices_sum(),
|
||||
chunk.get_cross_vbo(), chunk.get_cross_vertices_sum(),
|
||||
glm::vec3(static_cast<float>(pos.x * CHUNK_SIZE) +
|
||||
static_cast<float>(CHUNK_SIZE / 2),
|
||||
static_cast<float>(WORLD_SIZE_Y / 2),
|
||||
|
||||
Reference in New Issue
Block a user