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:
@@ -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