From e9076f22fe2a49d1cd8933e54b7b00c5dd943269 Mon Sep 17 00:00:00 2001 From: untodesu Date: Fri, 12 Sep 2025 13:33:52 +0500 Subject: It compiles --- game/client/world/chunk_mesher.cc | 192 +++++++++++++++---------------------- game/client/world/chunk_quad.hh | 6 +- game/client/world/player_target.cc | 15 +-- game/client/world/player_target.hh | 3 +- game/client/world/voxel_sounds.cc | 53 +++++----- game/client/world/voxel_sounds.hh | 6 +- 6 files changed, 116 insertions(+), 159 deletions(-) (limited to 'game/client/world') diff --git a/game/client/world/chunk_mesher.cc b/game/client/world/chunk_mesher.cc index a8ee817..bc90a03 100644 --- a/game/client/world/chunk_mesher.cc +++ b/game/client/world/chunk_mesher.cc @@ -8,6 +8,7 @@ #include "shared/world/chunk.hh" #include "shared/world/dimension.hh" +#include "shared/world/voxel.hh" #include "shared/world/voxel_registry.hh" #include "shared/coord.hh" @@ -56,37 +57,6 @@ static const CachedChunkCoord get_cached_cpos(const chunk_pos& pivot, const chun return CPOS_ITSELF; } -static world::voxel_facing get_facing(world::voxel_face face, world::voxel_type type) -{ - if(type == world::voxel_type::CROSS) { - switch(face) { - case world::voxel_face::CROSS_NESW: - return world::voxel_facing::NESW; - case world::voxel_face::CROSS_NWSE: - return world::voxel_facing::NWSE; - default: - return world::voxel_facing::NORTH; - } - } - - switch(face) { - case world::voxel_face::CUBE_NORTH: - return world::voxel_facing::NORTH; - case world::voxel_face::CUBE_SOUTH: - return world::voxel_facing::SOUTH; - case world::voxel_face::CUBE_EAST: - return world::voxel_facing::EAST; - case world::voxel_face::CUBE_WEST: - return world::voxel_facing::WEST; - case world::voxel_face::CUBE_TOP: - return world::voxel_facing::UP; - case world::voxel_face::CUBE_BOTTOM: - return world::voxel_facing::DOWN; - default: - return world::voxel_facing::NORTH; - } -} - class GL_MeshingTask final : public Task { public: explicit GL_MeshingTask(entt::entity entity, const chunk_pos& cpos); @@ -95,11 +65,10 @@ public: virtual void finalize(void) override; private: - bool vis_test(voxel_id voxel, const world::VoxelInfo* info, const local_pos& lpos) const; - void push_quad_a(const world::VoxelInfo* info, const glm::fvec3& pos, const glm::fvec2& size, world::voxel_face face); - void push_quad_v(const world::VoxelInfo* info, const glm::fvec3& pos, const glm::fvec2& size, world::voxel_face face, - std::size_t entropy); - void make_cube(voxel_id voxel, const world::VoxelInfo* info, const local_pos& lpos, world::voxel_vis vis, std::size_t entropy); + bool vis_test(const world::Voxel* voxel, const local_pos& lpos) const; + void push_quad_a(const world::Voxel* voxel, const glm::fvec3& pos, const glm::fvec2& size, world::VoxelFace face); + void push_quad_v(const world::Voxel* voxel, const glm::fvec3& pos, const glm::fvec2& size, world::VoxelFace face, std::size_t entropy); + void make_cube(const world::Voxel* voxel, const local_pos& lpos, world::VoxelVisBits vis, std::size_t entropy); void cache_chunk(const chunk_pos& cpos); private: @@ -138,41 +107,39 @@ void GL_MeshingTask::process(void) return; } - const auto voxel = voxels[i]; const auto lpos = coord::to_local(i); + const auto voxel = world::voxel_registry::find(voxels[i]); - const auto info = world::voxel_registry::find(voxel); - - if(info == nullptr) { + if(voxel == nullptr) { // Either a NULL_VOXEL_ID or something went // horribly wrong and we don't what this is continue; } - world::voxel_vis vis = 0; + unsigned int vis = 0U; - if(vis_test(voxel, info, lpos + DIR_NORTH)) { - vis |= world::VIS_NORTH; + if(vis_test(voxel, lpos + DIR_NORTH)) { + vis |= world::VVIS_NORTH; } - if(vis_test(voxel, info, lpos + DIR_SOUTH)) { - vis |= world::VIS_SOUTH; + if(vis_test(voxel, lpos + DIR_SOUTH)) { + vis |= world::VVIS_SOUTH; } - if(vis_test(voxel, info, lpos + DIR_EAST)) { - vis |= world::VIS_EAST; + if(vis_test(voxel, lpos + DIR_EAST)) { + vis |= world::VVIS_EAST; } - if(vis_test(voxel, info, lpos + DIR_WEST)) { - vis |= world::VIS_WEST; + if(vis_test(voxel, lpos + DIR_WEST)) { + vis |= world::VVIS_WEST; } - if(vis_test(voxel, info, lpos + DIR_UP)) { - vis |= world::VIS_UP; + if(vis_test(voxel, lpos + DIR_UP)) { + vis |= world::VVIS_UP; } - if(vis_test(voxel, info, lpos + DIR_DOWN)) { - vis |= world::VIS_DOWN; + if(vis_test(voxel, lpos + DIR_DOWN)) { + vis |= world::VVIS_DOWN; } const auto vpos = coord::to_voxel(m_cpos, lpos); @@ -180,7 +147,7 @@ void GL_MeshingTask::process(void) const auto entropy = math::crc64(&entropy_src, sizeof(entropy_src)); // FIXME: handle different voxel types - make_cube(voxel, info, lpos, vis, entropy); + make_cube(voxel, lpos, world::VoxelVisBits(vis), entropy); } } @@ -254,7 +221,7 @@ void GL_MeshingTask::finalize(void) } } -bool GL_MeshingTask::vis_test(voxel_id voxel, const world::VoxelInfo* info, const local_pos& lpos) const +bool GL_MeshingTask::vis_test(const world::Voxel* voxel, const local_pos& lpos) const { const auto pvpos = coord::to_voxel(m_cpos, lpos); const auto pcpos = coord::to_chunk(pvpos); @@ -263,26 +230,18 @@ bool GL_MeshingTask::vis_test(voxel_id voxel, const world::VoxelInfo* info, cons const auto cached_cpos = get_cached_cpos(m_cpos, pcpos); const auto& voxels = m_cache.at(cached_cpos); - const auto neighbour = voxels[index]; + const auto neighbour = world::voxel_registry::find(voxels[index]); bool result; - if(neighbour == NULL_VOXEL_ID) { + if(neighbour == nullptr) { result = true; } else if(neighbour == voxel) { result = false; } - else if(auto neighbour_info = world::voxel_registry::find(neighbour)) { - if(neighbour_info->blending != info->blending) { - // Voxel types that use blending are semi-transparent; - // this means they're rendered using a different setup - // and they must have visible faces with opaque voxels - result = neighbour_info->blending; - } - else { - result = false; - } + else if(neighbour->get_render_mode() != voxel->get_render_mode()) { + result = true; } else { result = false; @@ -291,88 +250,95 @@ bool GL_MeshingTask::vis_test(voxel_id voxel, const world::VoxelInfo* info, cons return result; } -void GL_MeshingTask::push_quad_a(const world::VoxelInfo* info, const glm::fvec3& pos, const glm::fvec2& size, world::voxel_face face) +void GL_MeshingTask::push_quad_a(const world::Voxel* voxel, const glm::fvec3& pos, const glm::fvec2& size, world::VoxelFace face) { - const world::voxel_facing facing = get_facing(face, info->type); - const world::VoxelTexture& vtex = info->textures[static_cast(face)]; - - if(info->blending) { - m_quads_b[vtex.cached_plane].push_back(make_chunk_quad(pos, size, facing, vtex.cached_offset, vtex.paths.size())); - } - else { - m_quads_s[vtex.cached_plane].push_back(make_chunk_quad(pos, size, facing, vtex.cached_offset, vtex.paths.size())); + auto cached_offset = voxel->get_cached_face_offset(face); + auto cached_plane = voxel->get_cached_face_plane(face); + auto& textures = voxel->get_face_textures(face); + + switch(voxel->get_render_mode()) { + case world::VRENDER_OPAQUE: + m_quads_s[cached_plane].push_back(make_chunk_quad(pos, size, face, cached_offset, textures.size())); + break; + + case world::VRENDER_BLEND: + m_quads_b[cached_plane].push_back(make_chunk_quad(pos, size, face, cached_offset, textures.size())); + break; } } -void GL_MeshingTask::push_quad_v(const world::VoxelInfo* info, const glm::fvec3& pos, const glm::fvec2& size, world::voxel_face face, +void GL_MeshingTask::push_quad_v(const world::Voxel* voxel, const glm::fvec3& pos, const glm::fvec2& size, world::VoxelFace face, std::size_t entropy) { - const world::voxel_facing facing = get_facing(face, info->type); - const world::VoxelTexture& vtex = info->textures[static_cast(face)]; - const std::size_t entropy_mod = entropy % vtex.paths.size(); - - if(info->blending) { - m_quads_b[vtex.cached_plane].push_back(make_chunk_quad(pos, size, facing, vtex.cached_offset + entropy_mod, 0)); - } - else { - m_quads_s[vtex.cached_plane].push_back(make_chunk_quad(pos, size, facing, vtex.cached_offset + entropy_mod, 0)); + auto cached_offset = voxel->get_cached_face_offset(face); + auto cached_plane = voxel->get_cached_face_plane(face); + auto& textures = voxel->get_face_textures(face); + auto index = entropy % textures.size(); + + switch(voxel->get_render_mode()) { + case world::VRENDER_OPAQUE: + m_quads_s[cached_plane].push_back(make_chunk_quad(pos, size, face, cached_offset + index, 0)); + break; + + case world::VRENDER_BLEND: + m_quads_b[cached_plane].push_back(make_chunk_quad(pos, size, face, cached_offset + index, 0)); + break; } } -void GL_MeshingTask::make_cube(voxel_id voxel, const world::VoxelInfo* info, const local_pos& lpos, world::voxel_vis vis, - std::size_t entropy) +void GL_MeshingTask::make_cube(const world::Voxel* voxel, const local_pos& lpos, world::VoxelVisBits vis, std::size_t entropy) { const glm::fvec3 fpos = glm::fvec3(lpos); const glm::fvec2 fsize = glm::fvec2(1.0f, 1.0f); - if(info->animated) { - if(vis & world::VIS_NORTH) { - push_quad_a(info, fpos, fsize, world::voxel_face::CUBE_NORTH); + if(voxel->is_animated()) { + if(vis & world::VVIS_NORTH) { + push_quad_a(voxel, fpos, fsize, world::VFACE_NORTH); } - if(vis & world::VIS_SOUTH) { - push_quad_a(info, fpos, fsize, world::voxel_face::CUBE_SOUTH); + if(vis & world::VVIS_SOUTH) { + push_quad_a(voxel, fpos, fsize, world::VFACE_SOUTH); } - if(vis & world::VIS_EAST) { - push_quad_a(info, fpos, fsize, world::voxel_face::CUBE_EAST); + if(vis & world::VVIS_EAST) { + push_quad_a(voxel, fpos, fsize, world::VFACE_EAST); } - if(vis & world::VIS_WEST) { - push_quad_a(info, fpos, fsize, world::voxel_face::CUBE_WEST); + if(vis & world::VVIS_WEST) { + push_quad_a(voxel, fpos, fsize, world::VFACE_WEST); } - if(vis & world::VIS_UP) { - push_quad_a(info, fpos, fsize, world::voxel_face::CUBE_TOP); + if(vis & world::VVIS_UP) { + push_quad_a(voxel, fpos, fsize, world::VFACE_TOP); } - if(vis & world::VIS_DOWN) { - push_quad_a(info, fpos, fsize, world::voxel_face::CUBE_BOTTOM); + if(vis & world::VVIS_DOWN) { + push_quad_a(voxel, fpos, fsize, world::VFACE_BOTTOM); } } else { - if(vis & world::VIS_NORTH) { - push_quad_v(info, fpos, fsize, world::voxel_face::CUBE_NORTH, entropy); + if(vis & world::VVIS_NORTH) { + push_quad_v(voxel, fpos, fsize, world::VFACE_NORTH, entropy); } - if(vis & world::VIS_SOUTH) { - push_quad_v(info, fpos, fsize, world::voxel_face::CUBE_SOUTH, entropy); + if(vis & world::VVIS_SOUTH) { + push_quad_v(voxel, fpos, fsize, world::VFACE_SOUTH, entropy); } - if(vis & world::VIS_EAST) { - push_quad_v(info, fpos, fsize, world::voxel_face::CUBE_EAST, entropy); + if(vis & world::VVIS_EAST) { + push_quad_v(voxel, fpos, fsize, world::VFACE_EAST, entropy); } - if(vis & world::VIS_WEST) { - push_quad_v(info, fpos, fsize, world::voxel_face::CUBE_WEST, entropy); + if(vis & world::VVIS_WEST) { + push_quad_v(voxel, fpos, fsize, world::VFACE_WEST, entropy); } - if(vis & world::VIS_UP) { - push_quad_v(info, fpos, fsize, world::voxel_face::CUBE_TOP, entropy); + if(vis & world::VVIS_UP) { + push_quad_v(voxel, fpos, fsize, world::VFACE_TOP, entropy); } - if(vis & world::VIS_DOWN) { - push_quad_v(info, fpos, fsize, world::voxel_face::CUBE_BOTTOM, entropy); + if(vis & world::VVIS_DOWN) { + push_quad_v(voxel, fpos, fsize, world::VFACE_BOTTOM, entropy); } } } diff --git a/game/client/world/chunk_quad.hh b/game/client/world/chunk_quad.hh index c15bb7a..d68977e 100644 --- a/game/client/world/chunk_quad.hh +++ b/game/client/world/chunk_quad.hh @@ -13,8 +13,8 @@ using ChunkQuad = std::array; namespace world { -constexpr inline static ChunkQuad make_chunk_quad(const glm::fvec3& position, const glm::fvec2& size, voxel_facing facing, - std::size_t texture, std::size_t frames) +constexpr inline static ChunkQuad make_chunk_quad(const glm::fvec3& position, const glm::fvec2& size, VoxelFace face, std::size_t texture, + std::size_t frames) { ChunkQuad result = {}; result[0] = 0x00000000; @@ -30,7 +30,7 @@ constexpr inline static ChunkQuad make_chunk_quad(const glm::fvec3& position, co result[0] |= (0x0000000FU & static_cast(size.y * 16.0f - 1.0f)); // [1] FFFF---------------------------- - result[1] |= (0x0000000FU & static_cast(facing)) << 28U; + result[1] |= (0x0000000FU & static_cast(face)) << 28U; // [1] ----TTTTTTTTTTTAAAAA------------ result[1] |= (0x000007FFU & static_cast(texture)) << 17U; diff --git a/game/client/world/player_target.cc b/game/client/world/player_target.cc index f0550c0..3ede47e 100644 --- a/game/client/world/player_target.cc +++ b/game/client/world/player_target.cc @@ -16,17 +16,15 @@ constexpr static float MAX_REACH = 16.0f; -voxel_id world::player_target::voxel; voxel_pos world::player_target::coord; voxel_pos world::player_target::normal; -const world::VoxelInfo* world::player_target::info; +const world::Voxel* world::player_target::voxel; void world::player_target::init(void) { - world::player_target::voxel = NULL_VOXEL_ID; world::player_target::coord = voxel_pos(); world::player_target::normal = voxel_pos(); - world::player_target::info = nullptr; + world::player_target::voxel = nullptr; } void world::player_target::update(void) @@ -37,29 +35,26 @@ void world::player_target::update(void) do { world::player_target::voxel = ray.step(); - if(world::player_target::voxel != NULL_VOXEL_ID) { + if(world::player_target::voxel) { world::player_target::coord = ray.vpos; world::player_target::normal = ray.vnormal; - world::player_target::info = world::voxel_registry::find(world::player_target::voxel); break; } world::player_target::coord = voxel_pos(); world::player_target::normal = voxel_pos(); - world::player_target::info = nullptr; } while(ray.distance < MAX_REACH); } else { - world::player_target::voxel = NULL_VOXEL_ID; + world::player_target::voxel = nullptr; world::player_target::coord = voxel_pos(); world::player_target::normal = voxel_pos(); - world::player_target::info = nullptr; } } void world::player_target::render(void) { - if((world::player_target::voxel != NULL_VOXEL_ID) && !client_game::hide_hud) { + if(world::player_target::voxel && !client_game::hide_hud) { auto cpos = coord::to_chunk(world::player_target::coord); auto fpos = coord::to_local(world::player_target::coord); diff --git a/game/client/world/player_target.hh b/game/client/world/player_target.hh index f0db9be..34532c3 100644 --- a/game/client/world/player_target.hh +++ b/game/client/world/player_target.hh @@ -4,10 +4,9 @@ namespace world::player_target { -extern voxel_id voxel; extern voxel_pos coord; extern voxel_pos normal; -extern const VoxelInfo* info; +extern const Voxel* voxel; } // namespace world::player_target namespace world::player_target diff --git a/game/client/world/voxel_sounds.cc b/game/client/world/voxel_sounds.cc index 481e615..fe91f01 100644 --- a/game/client/world/voxel_sounds.cc +++ b/game/client/world/voxel_sounds.cc @@ -4,24 +4,21 @@ #include "client/resource/sound_effect.hh" -constexpr static std::size_t NUM_SURFACES = static_cast(world::voxel_surface::COUNT); - -static std::vector> footsteps_sounds[NUM_SURFACES]; +static std::vector> footsteps_sounds[world::VMAT_COUNT]; static std::mt19937_64 randomizer; -static void add_footsteps_effect(world::voxel_surface surface, std::string_view name) +static void add_footsteps_effect(world::VoxelMaterial material, std::string_view name) { if(auto effect = resource::load(name)) { - auto surface_index = static_cast(surface); - footsteps_sounds[surface_index].push_back(effect); + footsteps_sounds[material].push_back(effect); } } -static resource_ptr get_footsteps_effect(world::voxel_surface surface) +static resource_ptr get_footsteps_effect(world::VoxelMaterial material) { - auto surface_index = static_cast(surface); + auto surface_index = static_cast(material); - if(surface_index >= NUM_SURFACES) { + if(surface_index >= world::VMAT_COUNT) { // Surface index out of range return nullptr; } @@ -39,48 +36,48 @@ static resource_ptr get_footsteps_effect(world::voxel_surface surfa void world::voxel_sounds::init(void) { - add_footsteps_effect(voxel_surface::DEFAULT, "sounds/surface/default1.wav"); - add_footsteps_effect(voxel_surface::DEFAULT, "sounds/surface/default2.wav"); - add_footsteps_effect(voxel_surface::DEFAULT, "sounds/surface/default3.wav"); - add_footsteps_effect(voxel_surface::DEFAULT, "sounds/surface/default4.wav"); + add_footsteps_effect(VMAT_DEFAULT, "sounds/surface/default1.wav"); + add_footsteps_effect(VMAT_DEFAULT, "sounds/surface/default2.wav"); + add_footsteps_effect(VMAT_DEFAULT, "sounds/surface/default3.wav"); + add_footsteps_effect(VMAT_DEFAULT, "sounds/surface/default4.wav"); - add_footsteps_effect(voxel_surface::DIRT, "sounds/surface/dirt1.wav"); + add_footsteps_effect(VMAT_DIRT, "sounds/surface/dirt1.wav"); - add_footsteps_effect(voxel_surface::GRASS, "sounds/surface/grass1.wav"); - add_footsteps_effect(voxel_surface::GRASS, "sounds/surface/grass2.wav"); - add_footsteps_effect(voxel_surface::GRASS, "sounds/surface/grass3.wav"); + add_footsteps_effect(VMAT_GRASS, "sounds/surface/grass1.wav"); + add_footsteps_effect(VMAT_GRASS, "sounds/surface/grass2.wav"); + add_footsteps_effect(VMAT_GRASS, "sounds/surface/grass3.wav"); - add_footsteps_effect(voxel_surface::GRAVEL, "sounds/surface/gravel1.wav"); + add_footsteps_effect(VMAT_GRAVEL, "sounds/surface/gravel1.wav"); - add_footsteps_effect(voxel_surface::SAND, "sounds/surface/sand1.wav"); - add_footsteps_effect(voxel_surface::SAND, "sounds/surface/sand2.wav"); + add_footsteps_effect(VMAT_SAND, "sounds/surface/sand1.wav"); + add_footsteps_effect(VMAT_SAND, "sounds/surface/sand2.wav"); - add_footsteps_effect(voxel_surface::WOOD, "sounds/surface/wood1.wav"); - add_footsteps_effect(voxel_surface::WOOD, "sounds/surface/wood2.wav"); - add_footsteps_effect(voxel_surface::WOOD, "sounds/surface/wood3.wav"); + add_footsteps_effect(VMAT_WOOD, "sounds/surface/wood1.wav"); + add_footsteps_effect(VMAT_WOOD, "sounds/surface/wood2.wav"); + add_footsteps_effect(VMAT_WOOD, "sounds/surface/wood3.wav"); } void world::voxel_sounds::shutdown(void) { - for(std::size_t i = 0; i < NUM_SURFACES; ++i) { + for(std::size_t i = 0; i < world::VMAT_COUNT; ++i) { footsteps_sounds[i].clear(); } } -resource_ptr world::voxel_sounds::get_footsteps(voxel_surface surface) +resource_ptr world::voxel_sounds::get_footsteps(world::VoxelMaterial material) { - if(auto effect = get_footsteps_effect(surface)) { + if(auto effect = get_footsteps_effect(material)) { return effect; } - if(auto effect = get_footsteps_effect(voxel_surface::DEFAULT)) { + if(auto effect = get_footsteps_effect(VMAT_DEFAULT)) { return effect; } return nullptr; } -resource_ptr world::voxel_sounds::get_placebreak(voxel_surface surface) +resource_ptr world::voxel_sounds::get_placebreak(world::VoxelMaterial material) { return nullptr; } diff --git a/game/client/world/voxel_sounds.hh b/game/client/world/voxel_sounds.hh index 09f5e2e..d0f3e07 100644 --- a/game/client/world/voxel_sounds.hh +++ b/game/client/world/voxel_sounds.hh @@ -2,7 +2,7 @@ #include "core/resource/resource.hh" -#include "shared/world/voxel_registry.hh" +#include "shared/world/voxel.hh" struct SoundEffect; @@ -14,6 +14,6 @@ void shutdown(void); namespace world::voxel_sounds { -resource_ptr get_footsteps(voxel_surface surface); -resource_ptr get_placebreak(voxel_surface surface); +resource_ptr get_footsteps(VoxelMaterial material); +resource_ptr get_placebreak(VoxelMaterial material); } // namespace world::voxel_sounds -- cgit