From 3bf42c6ff3805a0d42bbc661794a95ff31bedc26 Mon Sep 17 00:00:00 2001 From: untodesu Date: Sat, 15 Mar 2025 16:22:09 +0500 Subject: Add whatever I was working on for the last month --- game/shared/coord.hh | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 game/shared/coord.hh (limited to 'game/shared/coord.hh') diff --git a/game/shared/coord.hh b/game/shared/coord.hh new file mode 100644 index 0000000..e51624f --- /dev/null +++ b/game/shared/coord.hh @@ -0,0 +1,148 @@ +#ifndef SHARED_COORD_HH +#define SHARED_COORD_HH 1 +#pragma once + +#include "shared/const.hh" +#include "shared/types.hh" + +namespace coord +{ +constexpr chunk_pos to_chunk(const voxel_pos &vpos); +} // namespace coord + +namespace coord +{ +constexpr local_pos to_local(const voxel_pos &vpos); +constexpr local_pos to_local(const glm::fvec3 &fvec); +constexpr local_pos to_local(std::size_t index); +} // namespace coord + +namespace coord +{ +constexpr voxel_pos to_voxel(const chunk_pos &cpos, const local_pos &lpos); +constexpr voxel_pos to_voxel(const chunk_pos &cpos, const glm::fvec3 &fvec); +} // namespace coord + +namespace coord +{ +constexpr std::size_t to_index(const local_pos &lpos); +} // namespace coord + +namespace coord +{ +constexpr glm::fvec3 to_relative(const chunk_pos &pivot_cpos, const chunk_pos &cpos, const glm::fvec3 &fvec); +constexpr glm::fvec3 to_relative(const chunk_pos &pivot_cpos, const glm::fvec3 &pivot_fvec, const chunk_pos &cpos); +constexpr glm::fvec3 to_relative(const chunk_pos &pivot_cpos, const glm::fvec3 &pivot_fvec, const chunk_pos &cpos, const glm::fvec3 &fvec); +} // namespace coord + +namespace coord +{ +constexpr glm::fvec3 to_fvec3(const chunk_pos &cpos); +constexpr glm::fvec3 to_fvec3(const chunk_pos &cpos, const glm::fvec3 &fpos); +} // namespace coord + +inline constexpr chunk_pos coord::to_chunk(const voxel_pos &vpos) +{ + return chunk_pos { + static_cast(vpos.x >> CHUNK_BITSHIFT), + static_cast(vpos.y >> CHUNK_BITSHIFT), + static_cast(vpos.z >> CHUNK_BITSHIFT), + }; +} + +inline constexpr local_pos coord::to_local(const voxel_pos &vpos) +{ + return local_pos { + static_cast(cxpr::mod_signed(vpos.x, CHUNK_SIZE)), + static_cast(cxpr::mod_signed(vpos.y, CHUNK_SIZE)), + static_cast(cxpr::mod_signed(vpos.z, CHUNK_SIZE)), + }; +} + +inline constexpr local_pos coord::to_local(const glm::fvec3 &fvec) +{ + return local_pos { + static_cast(fvec.x), + static_cast(fvec.y), + static_cast(fvec.z), + }; +} + +inline constexpr local_pos coord::to_local(std::size_t index) +{ + return local_pos { + static_cast((index % CHUNK_SIZE)), + static_cast((index / CHUNK_SIZE) / CHUNK_SIZE), + static_cast((index / CHUNK_SIZE) % CHUNK_SIZE), + }; +} + +inline constexpr voxel_pos coord::to_voxel(const chunk_pos &cpos, const local_pos &lpos) +{ + return voxel_pos { + lpos.x + (static_cast(cpos.x) << CHUNK_BITSHIFT), + lpos.y + (static_cast(cpos.y) << CHUNK_BITSHIFT), + lpos.z + (static_cast(cpos.z) << CHUNK_BITSHIFT), + }; +} + +inline constexpr voxel_pos coord::to_voxel(const chunk_pos &cpos, const glm::fvec3 &fvec) +{ + return voxel_pos { + static_cast(fvec.x) + (static_cast(cpos.x) << CHUNK_BITSHIFT), + static_cast(fvec.y) + (static_cast(cpos.y) << CHUNK_BITSHIFT), + static_cast(fvec.z) + (static_cast(cpos.z) << CHUNK_BITSHIFT), + }; +} + +inline constexpr std::size_t coord::to_index(const local_pos &lpos) +{ + return static_cast((lpos.y * CHUNK_SIZE + lpos.z) * CHUNK_SIZE + lpos.x); +} + +inline constexpr glm::fvec3 coord::to_relative(const chunk_pos &pivot_cpos, const chunk_pos &cpos, const glm::fvec3 &fvec) +{ + return glm::fvec3 { + static_cast((cpos.x - pivot_cpos.x) << CHUNK_BITSHIFT) + fvec.x, + static_cast((cpos.y - pivot_cpos.y) << CHUNK_BITSHIFT) + fvec.y, + static_cast((cpos.z - pivot_cpos.z) << CHUNK_BITSHIFT) + fvec.z, + }; +} + +inline constexpr glm::fvec3 coord::to_relative(const chunk_pos &pivot_cpos, const glm::fvec3 &pivot_fvec, const chunk_pos &cpos) +{ + return glm::fvec3 { + static_cast((cpos.x - pivot_cpos.x) << CHUNK_BITSHIFT) - pivot_fvec.x, + static_cast((cpos.y - pivot_cpos.y) << CHUNK_BITSHIFT) - pivot_fvec.y, + static_cast((cpos.z - pivot_cpos.z) << CHUNK_BITSHIFT) - pivot_fvec.z, + }; +} + +inline constexpr glm::fvec3 coord::to_relative(const chunk_pos &pivot_cpos, const glm::fvec3 &pivot_fvec, const chunk_pos &cpos, const glm::fvec3 &fvec) +{ + return glm::fvec3 { + static_cast((cpos.x - pivot_cpos.x) << CHUNK_BITSHIFT) + (fvec.x - pivot_fvec.x), + static_cast((cpos.y - pivot_cpos.y) << CHUNK_BITSHIFT) + (fvec.y - pivot_fvec.y), + static_cast((cpos.z - pivot_cpos.z) << CHUNK_BITSHIFT) + (fvec.z - pivot_fvec.z), + }; +} + +inline constexpr glm::fvec3 coord::to_fvec3(const chunk_pos &cpos) +{ + return glm::fvec3 { + static_cast(cpos.x << CHUNK_BITSHIFT), + static_cast(cpos.y << CHUNK_BITSHIFT), + static_cast(cpos.z << CHUNK_BITSHIFT), + }; +} + +inline constexpr glm::fvec3 coord::to_fvec3(const chunk_pos &cpos, const glm::fvec3 &fpos) +{ + return glm::fvec3 { + fpos.x + static_cast(cpos.x << CHUNK_BITSHIFT), + fpos.y + static_cast(cpos.y << CHUNK_BITSHIFT), + fpos.z + static_cast(cpos.z << CHUNK_BITSHIFT), + }; +} + +#endif /* SHARED_COORD_HH */ -- cgit