You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

voxelutils.h 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #pragma once
  2. #include <tuple>
  3. #include <glm/glm.hpp>
  4. #include <array>
  5. namespace vtk {
  6. enum class FaceDirection : unsigned int {
  7. TOP = 0,
  8. BOTTOM = 1,
  9. NORTH = 2,
  10. SOUTH = 3,
  11. EAST = 4,
  12. WEST = 5
  13. };
  14. typedef std::tuple<unsigned, unsigned, unsigned> uPos; //used for voxel positions
  15. glm::ivec3 worldPosToChunkPos(const glm::ivec3& pos);
  16. glm::ivec2 worldPosToChunkPos(const glm::ivec2& pos);
  17. //takes chunk and local positions, returns a world position
  18. glm::ivec3 chunkPosToWorldPos(const glm::ivec3& cPos, const glm::ivec3& lPos);
  19. //takes a chunk/local pos and returns a chunk,local pos pair (works for out of bounds coords)
  20. std::pair<glm::ivec3, glm::ivec3> worldPosToLocalPos(const glm::ivec3& pos);
  21. std::pair<glm::ivec3, glm::ivec3> localPosToLocalPos(const glm::ivec3& cPos, const glm::ivec3& lPos);
  22. /*
  23. //TODO: move this to cpp file
  24. constexpr glm::ivec3 FaceDirectionToVec(const FaceDirection& direction) {
  25. switch(direction) {
  26. case FaceDirection::TOP:
  27. return glm::ivec3(0,1,0);
  28. case FaceDirection::BOTTOM:
  29. return glm::ivec3(0,-1,0);
  30. case FaceDirection::NORTH:
  31. return glm::ivec3(0,0,1);
  32. case FaceDirection::SOUTH:
  33. return glm::ivec3(0,0,-1);
  34. case FaceDirection::EAST:
  35. return glm::ivec3(1,0,0);
  36. case FaceDirection::WEST:
  37. return glm::ivec3(-1,0,0);
  38. }
  39. */
  40. struct uPosHash : public std::unary_function<uPos, std::size_t> {
  41. std::size_t operator()(const uPos& k) const {
  42. return std::get<0>(k) ^ std::get<1>(k) ^ std::get<2>(k);
  43. }
  44. };
  45. struct uPosEqual : public std::binary_function<uPos, uPos, bool> {
  46. bool operator()(const uPos& a, const uPos& b) const {
  47. return (std::get<0>(a) == std::get<0>(b) &&
  48. std::get<1>(a) == std::get<1>(b) &&
  49. std::get<2>(a) == std::get<2>(b));
  50. }
  51. };
  52. typedef std::tuple<int, int, int> iPos; //used for chunk positions
  53. struct iPosHash : public std::unary_function<iPos, std::size_t> {
  54. std::size_t operator()(const iPos& k) const {
  55. return std::get<0>(k) ^ std::get<1>(k) ^ std::get<2>(k);
  56. }
  57. };
  58. struct iPosEqual : public std::binary_function<iPos, iPos, bool> {
  59. bool operator()(const iPos& a, const iPos& b) const {
  60. return (std::get<0>(a) == std::get<0>(b) &&
  61. std::get<1>(a) == std::get<1>(b) &&
  62. std::get<2>(a) == std::get<2>(b));
  63. }
  64. };
  65. // basic hash function for glm::ivec3
  66. struct ivec3Hash : public std::unary_function<glm::ivec3, std::size_t> {
  67. std::size_t operator()(const glm::ivec3& k) const {
  68. std::size_t seed = 3;
  69. for(int i = 0; i < 3; ++i) {
  70. seed ^= k[i] + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  71. }
  72. return seed;
  73. }
  74. };
  75. // hash function for glm::ivec2
  76. struct ivec2Hash : public std::unary_function<glm::ivec2, std::size_t> {
  77. std::size_t operator()(const glm::ivec2& k) const {
  78. std::size_t seed = 3;
  79. for(int i = 0; i < 2; ++i) {
  80. seed ^= k[i] + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  81. }
  82. return seed;
  83. }
  84. };
  85. }