voxel_data.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "voxel_data.h"
  2. std::unordered_map<BlockType, std::unordered_map<FaceOrientation, FaceType>> voxel_data::block_face_map_ =
  3. {
  4. {
  5. BlockType::GRASS_BLOCK,
  6. {
  7. {FaceOrientation::FRONT, FaceType::GRASS_SIDE},
  8. {FaceOrientation::BACK, FaceType::GRASS_SIDE},
  9. {FaceOrientation::LEFT, FaceType::GRASS_SIDE},
  10. {FaceOrientation::RIGHT, FaceType::GRASS_SIDE},
  11. {FaceOrientation::TOP, FaceType::GRASS_TOP},
  12. {FaceOrientation::BOTTOM, FaceType::DIRT}
  13. }
  14. },
  15. {
  16. BlockType::OAK_LOG,
  17. {
  18. {FaceOrientation::FRONT, FaceType::OAK_LOG_SIDE},
  19. {FaceOrientation::BACK, FaceType::OAK_LOG_SIDE},
  20. {FaceOrientation::LEFT, FaceType::OAK_LOG_SIDE},
  21. {FaceOrientation::RIGHT, FaceType::OAK_LOG_SIDE},
  22. {FaceOrientation::TOP, FaceType::OAK_LOG_TOP},
  23. {FaceOrientation::BOTTOM, FaceType::OAK_LOG_TOP}
  24. }
  25. },
  26. {
  27. BlockType::SANDSTONE,
  28. {
  29. {FaceOrientation::FRONT, FaceType::SANDSTONE_SIDE},
  30. {FaceOrientation::BACK, FaceType::SANDSTONE_SIDE},
  31. {FaceOrientation::LEFT, FaceType::SANDSTONE_SIDE},
  32. {FaceOrientation::RIGHT, FaceType::SANDSTONE_SIDE},
  33. {FaceOrientation::TOP, FaceType::SANDSTONE_TOP},
  34. {FaceOrientation::BOTTOM, FaceType::SANDSTONE_BOTTOM}
  35. }
  36. }
  37. };
  38. std::vector<VoxelFace> voxel_data::block_faces_{};
  39. Block voxel_data::get_block(const BlockType& block_type)
  40. {
  41. Block block;
  42. block.type = block_type;
  43. block.is_solid = is_block_solid(block_type);
  44. block.faces = get_faces();
  45. // Calculate the face types for the block
  46. for (int i = 0; i < 6; i++)
  47. {
  48. // If the block type is not in the map, it has the same texture on all faces
  49. if (block_face_map_.find(block_type) == block_face_map_.end())
  50. {
  51. block.faces[i].type = static_cast<FaceType>(block_type);
  52. continue;
  53. }
  54. // If the block type is in the map, use the face types from the map
  55. block.faces[i].type = block_face_map_[block_type][static_cast<FaceOrientation>(i)];
  56. }
  57. return block;
  58. }
  59. bool voxel_data::is_block_solid(const BlockType& block_type)
  60. {
  61. // TODO: Improve by using a lookup table
  62. return block_type != BlockType::AIR && block_type != BlockType::WATER;
  63. }
  64. std::vector<coral_3d::Vertex> voxel_data::get_vertices()
  65. {
  66. return
  67. {
  68. // FRONT FACE
  69. { {0.f, 0.f, 0.f}, {0.f, 0.f}, {0.f, 0.f, -1.f} },
  70. { {1.f, 0.f, 0.f}, {1.f, 0.f}, {0.f, 0.f, -1.f} },
  71. { {1.f, 1.f, 0.f}, {1.f, 1.f}, {0.f, 0.f, -1.f} },
  72. { {0.f, 1.f, 0.f}, {0.f, 1.f}, {0.f, 0.f, -1.f} },
  73. // BACK FACE
  74. { {1.f, 0.f, 1.f}, {0.f, 0.f}, {0.f, 0.f, 1.f} },
  75. { {0.f, 0.f, 1.f}, {1.f, 0.f}, {0.f, 0.f, 1.f} },
  76. { {0.f, 1.f, 1.f}, {1.f, 1.f}, {0.f, 0.f, 1.f} },
  77. { {1.f, 1.f, 1.f}, {0.f, 1.f}, {0.f, 0.f, 1.f} },
  78. // LEFT FACE
  79. { {0.f, 0.f, 1.f}, {0.f, 0.f}, {-1.f, 0.f, 0.f} },
  80. { {0.f, 0.f, 0.f}, {1.f, 0.f}, {-1.f, 0.f, 0.f} },
  81. { {0.f, 1.f, 0.f}, {1.f, 1.f}, {-1.f, 0.f, 0.f} },
  82. { {0.f, 1.f, 1.f}, {0.f, 1.f}, {-1.f, 0.f, 0.f} },
  83. // RIGHT FACE
  84. { {1.f, 0.f, 0.f}, {0.f, 0.f}, {1.f, 0.f, 0.f} },
  85. { {1.f, 0.f, 1.f}, {1.f, 0.f}, {1.f, 0.f, 0.f} },
  86. { {1.f, 1.f, 1.f}, {1.f, 1.f}, {1.f, 0.f, 0.f} },
  87. { {1.f, 1.f, 0.f}, {0.f, 1.f}, {1.f, 0.f, 0.f} },
  88. // TOP FACE
  89. { {0.f, 1.f, 0.f}, {0.f, 0.f}, {0.f, 1.f, 0.f} },
  90. { {1.f, 1.f, 0.f}, {1.f, 0.f}, {0.f, 1.f, 0.f} },
  91. { {1.f, 1.f, 1.f}, {1.f, 1.f}, {0.f, 1.f, 0.f} },
  92. { {0.f, 1.f, 1.f}, {0.f, 1.f}, {0.f, 1.f, 0.f} },
  93. // BOTTOM FACE
  94. { {0.f, 0.f, 1.f}, {0.f, 0.f}, {0.f, -1.f, 0.f} },
  95. { {1.f, 0.f, 1.f}, {1.f, 0.f}, {0.f, -1.f, 0.f} },
  96. { {1.f, 0.f, 0.f}, {1.f, 1.f}, {0.f, -1.f, 0.f} },
  97. { {0.f, 0.f, 0.f}, {0.f, 1.f}, {0.f, -1.f, 0.f} },
  98. };
  99. }
  100. std::vector<uint32_t> voxel_data::get_indices()
  101. {
  102. return
  103. {
  104. {
  105. 0, 1, 2, 2, 3, 0, // FRONT FACE
  106. 4, 5, 6, 6, 7, 4, // BACK FACE
  107. 8, 9, 10, 10, 11, 8, // LEFT FACE
  108. 12, 13, 14, 14, 15, 12, // RIGHT FACE
  109. 16, 17, 18, 18, 19, 16, // TOP FACE
  110. 20, 21, 22, 22, 23, 20 // BOTTOM FACE
  111. }
  112. };
  113. }
  114. std::vector<VoxelFace> voxel_data::get_faces()
  115. {
  116. if (!block_faces_.empty()) return block_faces_;
  117. auto verts = get_vertices();
  118. auto indices = get_indices();
  119. // Copy the vertices and indices for each face
  120. for (int i = 0; i < 6; i++)
  121. {
  122. VoxelFace block_face;
  123. block_face.vertices =
  124. std::vector<coral_3d::Vertex>(verts.begin() + (i * 4), verts.begin() + (i * 4) + 4);
  125. block_face.indices =
  126. std::vector<uint32_t>(indices.begin() + (i * 6), indices.begin() + (i * 6) + 6);
  127. block_face.type = FaceType::AIR;
  128. block_faces_.emplace_back(block_face);
  129. }
  130. return block_faces_;
  131. }