summaryrefslogtreecommitdiffstats
path: root/game/shared/coord.hh
diff options
context:
space:
mode:
authoruntodesu <kirill@untode.su>2025-07-01 03:08:39 +0500
committeruntodesu <kirill@untode.su>2025-07-01 03:08:39 +0500
commit458e0005690ea9d579588a0a12368fc2c2c9a93a (patch)
tree588a9ca6cb3c76d9193b5bd4601d64f0e50e8c8c /game/shared/coord.hh
parentc7b0c8e0286a1b2bb7ec55e579137dfc3b22eeb9 (diff)
downloadvoxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.tar.bz2
voxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.zip
I hyper-focued on refactoring again
- I put a cool-sounding "we are number one" remix on repeat and straight up grinded the entire repository to a better state until 03:09 AM. I guess I have something wrong in my brain that makes me do this shit
Diffstat (limited to 'game/shared/coord.hh')
-rw-r--r--game/shared/coord.hh149
1 files changed, 149 insertions, 0 deletions
diff --git a/game/shared/coord.hh b/game/shared/coord.hh
new file mode 100644
index 0000000..18d45b5
--- /dev/null
+++ b/game/shared/coord.hh
@@ -0,0 +1,149 @@
+#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<chunk_pos::value_type>(vpos.x >> CHUNK_BITSHIFT),
+ static_cast<chunk_pos::value_type>(vpos.y >> CHUNK_BITSHIFT),
+ static_cast<chunk_pos::value_type>(vpos.z >> CHUNK_BITSHIFT),
+ };
+}
+
+inline constexpr local_pos coord::to_local(const voxel_pos& vpos)
+{
+ return local_pos {
+ static_cast<local_pos::value_type>(math::mod_signed<voxel_pos::value_type>(vpos.x, CHUNK_SIZE)),
+ static_cast<local_pos::value_type>(math::mod_signed<voxel_pos::value_type>(vpos.y, CHUNK_SIZE)),
+ static_cast<local_pos::value_type>(math::mod_signed<voxel_pos::value_type>(vpos.z, CHUNK_SIZE)),
+ };
+}
+
+inline constexpr local_pos coord::to_local(const glm::fvec3& fvec)
+{
+ return local_pos {
+ static_cast<local_pos::value_type>(fvec.x),
+ static_cast<local_pos::value_type>(fvec.y),
+ static_cast<local_pos::value_type>(fvec.z),
+ };
+}
+
+inline constexpr local_pos coord::to_local(std::size_t index)
+{
+ return local_pos {
+ static_cast<local_pos::value_type>((index % CHUNK_SIZE)),
+ static_cast<local_pos::value_type>((index / CHUNK_SIZE) / CHUNK_SIZE),
+ static_cast<local_pos::value_type>((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<voxel_pos::value_type>(cpos.x) << CHUNK_BITSHIFT),
+ lpos.y + (static_cast<voxel_pos::value_type>(cpos.y) << CHUNK_BITSHIFT),
+ lpos.z + (static_cast<voxel_pos::value_type>(cpos.z) << CHUNK_BITSHIFT),
+ };
+}
+
+inline constexpr voxel_pos coord::to_voxel(const chunk_pos& cpos, const glm::fvec3& fvec)
+{
+ return voxel_pos {
+ static_cast<voxel_pos::value_type>(fvec.x) + (static_cast<voxel_pos::value_type>(cpos.x) << CHUNK_BITSHIFT),
+ static_cast<voxel_pos::value_type>(fvec.y) + (static_cast<voxel_pos::value_type>(cpos.y) << CHUNK_BITSHIFT),
+ static_cast<voxel_pos::value_type>(fvec.z) + (static_cast<voxel_pos::value_type>(cpos.z) << CHUNK_BITSHIFT),
+ };
+}
+
+inline constexpr std::size_t coord::to_index(const local_pos& lpos)
+{
+ return static_cast<std::size_t>((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<float>((cpos.x - pivot_cpos.x) << CHUNK_BITSHIFT) + fvec.x,
+ static_cast<float>((cpos.y - pivot_cpos.y) << CHUNK_BITSHIFT) + fvec.y,
+ static_cast<float>((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<float>((cpos.x - pivot_cpos.x) << CHUNK_BITSHIFT) - pivot_fvec.x,
+ static_cast<float>((cpos.y - pivot_cpos.y) << CHUNK_BITSHIFT) - pivot_fvec.y,
+ static_cast<float>((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<float>((cpos.x - pivot_cpos.x) << CHUNK_BITSHIFT) + (fvec.x - pivot_fvec.x),
+ static_cast<float>((cpos.y - pivot_cpos.y) << CHUNK_BITSHIFT) + (fvec.y - pivot_fvec.y),
+ static_cast<float>((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<float>(cpos.x << CHUNK_BITSHIFT),
+ static_cast<float>(cpos.y << CHUNK_BITSHIFT),
+ static_cast<float>(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<float>(cpos.x << CHUNK_BITSHIFT),
+ fpos.y + static_cast<float>(cpos.y << CHUNK_BITSHIFT),
+ fpos.z + static_cast<float>(cpos.z << CHUNK_BITSHIFT),
+ };
+}
+
+#endif // SHARED_COORD_HH