World.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "client/World.h"
  2. #include "client/Player.h"
  3. #include "data/Array.h"
  4. #include "math/Frustum.h"
  5. #include "math/View.h"
  6. #include "rendering/VertexBuffer.h"
  7. #include "rendering/Window.h"
  8. #include "utils/Logger.h"
  9. static constexpr int WORLD_SIZE = 16;
  10. static Array<bool, WORLD_SIZE * WORLD_SIZE * WORLD_SIZE> world(true);
  11. static bool dirtyVertexBuffer = true;
  12. static int vertices = 0;
  13. static Frustum frustum(80.0f, 0.1f, 1000.0f);
  14. Shader World::shader;
  15. static VertexBuffer vertexBuffer;
  16. bool World::init() {
  17. Error e =
  18. shader.compile("resources/shader/test.vs", "resources/shader/test.fs");
  19. if(e.has()) {
  20. e.message.printLine();
  21. return true;
  22. }
  23. vertexBuffer.init(VertexBuffer::Attributes().addFloat(3).addFloat(3));
  24. return false;
  25. }
  26. IntVector3 World::getSize() {
  27. return IntVector3(WORLD_SIZE, WORLD_SIZE, WORLD_SIZE);
  28. }
  29. void World::set(int x, int y, int z, bool b) {
  30. if(x < 0 || x >= WORLD_SIZE || y < 0 || y >= WORLD_SIZE || z < 0 ||
  31. z >= WORLD_SIZE) {
  32. return;
  33. }
  34. world[x * WORLD_SIZE * WORLD_SIZE + y * WORLD_SIZE + z] = b;
  35. dirtyVertexBuffer = true;
  36. }
  37. bool World::isAir(int x, int y, int z) {
  38. if(x < 0 || x >= WORLD_SIZE || y < 0 || y >= WORLD_SIZE || z < 0 ||
  39. z >= WORLD_SIZE) {
  40. return true;
  41. }
  42. return !world[x * WORLD_SIZE * WORLD_SIZE + y * WORLD_SIZE + z];
  43. }
  44. static void addTriangle(Buffer& buffer, const Vector3& a, const Vector3& b,
  45. const Vector3& c) {
  46. Vector3 normal = (b - a).cross(c - a);
  47. buffer.add(a).add(normal).add(b).add(normal).add(c).add(normal);
  48. }
  49. static void addCube(Buffer& buffer, int x, int y, int z) {
  50. if(World::isAir(x, y, z)) {
  51. return;
  52. }
  53. Vector3 v000(x, y, z);
  54. Vector3 v001(x, y, z + 1);
  55. Vector3 v010(x, y + 1, z);
  56. Vector3 v011(x, y + 1, z + 1);
  57. Vector3 v100(x + 1, y, z);
  58. Vector3 v101(x + 1, y, z + 1);
  59. Vector3 v110(x + 1, y + 1, z);
  60. Vector3 v111(x + 1, y + 1, z + 1);
  61. if(World::isAir(x, y - 1, z)) {
  62. addTriangle(buffer, v000, v100, v001);
  63. addTriangle(buffer, v100, v101, v001);
  64. }
  65. if(World::isAir(x, y + 1, z)) {
  66. addTriangle(buffer, v010, v011, v110);
  67. addTriangle(buffer, v110, v011, v111);
  68. }
  69. if(World::isAir(x - 1, y, z)) {
  70. addTriangle(buffer, v000, v001, v010);
  71. addTriangle(buffer, v001, v011, v010);
  72. }
  73. if(World::isAir(x + 1, y, z)) {
  74. addTriangle(buffer, v100, v110, v101);
  75. addTriangle(buffer, v101, v110, v111);
  76. }
  77. if(World::isAir(x, y, z + 1)) {
  78. addTriangle(buffer, v001, v101, v011);
  79. addTriangle(buffer, v111, v011, v101);
  80. }
  81. if(World::isAir(x, y, z - 1)) {
  82. addTriangle(buffer, v000, v010, v100);
  83. addTriangle(buffer, v110, v100, v010);
  84. }
  85. }
  86. static void buildRenderingBuffer() {
  87. Buffer buffer(100);
  88. IntVector3 size = World::getSize();
  89. for(int x = 0; x < size[0]; x++) {
  90. for(int y = 0; y < size[1]; y++) {
  91. for(int z = 0; z < size[2]; z++) {
  92. addCube(buffer, x, y, z);
  93. }
  94. }
  95. }
  96. vertices = buffer.getLength() / (2 * sizeof(Vector3));
  97. vertexBuffer.setData(buffer, GL::STATIC_DRAW);
  98. }
  99. void World::render(float lag) {
  100. if(dirtyVertexBuffer) {
  101. dirtyVertexBuffer = false;
  102. buildRenderingBuffer();
  103. LOG_INFO("rebuilt buffer");
  104. }
  105. View view;
  106. view.updateDirections(Player::getLengthAngle(lag),
  107. Player::getWidthAngle(lag));
  108. Vector3 center = Player::getPosition(lag) + Vector3(0.0f, 0.8f, 0.0f);
  109. shader.use();
  110. GL::setViewport(Window::getSize()[0], Window::getSize()[1]);
  111. shader.setMatrix("proj", frustum.updateProjection(Window::getSize()));
  112. shader.setMatrix("view", view.updateMatrix(center));
  113. shader.setVector("color", Vector3(1.0f, 1.0f, 1.0f));
  114. shader.setMatrix("model", Matrix());
  115. vertexBuffer.draw(vertices);
  116. }
  117. static List<Box> getBoxes(const Box& box) {
  118. int minX = floorf(box.getMin()[0]);
  119. int minY = floorf(box.getMin()[1]);
  120. int minZ = floorf(box.getMin()[2]);
  121. int maxX = floorf(box.getMax()[0]);
  122. int maxY = floorf(box.getMax()[1]);
  123. int maxZ = floorf(box.getMax()[2]);
  124. Box base(Vector3(1.0f, 1.0f, 1.0f));
  125. List<Box> boxes;
  126. for(int x = minX; x <= maxX; x++) {
  127. for(int y = minY; y <= maxY; y++) {
  128. for(int z = minZ; z <= maxZ; z++) {
  129. if(!World::isAir(x, y, z)) {
  130. boxes.add(base.offset(Vector3(x, y, z)));
  131. }
  132. }
  133. }
  134. }
  135. return boxes;
  136. }
  137. Vector3 World::limitMove(const Box& box, Vector3 move) {
  138. List<Box> boxes = getBoxes(box.expand(move));
  139. if(boxes.getLength() == 0) {
  140. return move;
  141. }
  142. Vector3 realMove;
  143. constexpr float step = 0.05f;
  144. while(move[0] != 0.0f || move[1] != 0.0f || move[2] != 0.0f) {
  145. for(int i = 0; i < 3; i++) {
  146. Vector3 old = realMove;
  147. if(move[i] > step) {
  148. realMove[i] += step;
  149. move[i] -= step;
  150. } else if(move[i] < -step) {
  151. realMove[i] -= step;
  152. move[i] += step;
  153. } else if(move[i] != 0.0f) {
  154. realMove[i] += move[i];
  155. move[i] = 0.0f;
  156. } else {
  157. continue;
  158. }
  159. Box moved = box.offset(realMove);
  160. for(const Box& box : boxes) {
  161. if(box.collidesWith(moved)) {
  162. move[i] = 0.0f;
  163. realMove = old;
  164. break;
  165. }
  166. }
  167. }
  168. }
  169. return realMove;
  170. }
  171. RayTrace World::rayTrace(const Vector3& start, const Vector3& direction,
  172. float maxDistance) {
  173. RayTrace result{RayTrace::Type::AIR, IntVector3()};
  174. Vector3 pos = start;
  175. Vector3 step = direction;
  176. // prevent zero division
  177. for(int i = 0; i < 3; i++) {
  178. if(step[i] == 0.0f) {
  179. step[i] = 0.0001f;
  180. }
  181. }
  182. step.normalize();
  183. while(maxDistance > 0.0f) {
  184. IntVector3 lower(floorf(pos[0]), floorf(pos[1]), floorf(pos[2]));
  185. if(!World::isAir(lower[0], lower[1], lower[2])) {
  186. result.block = lower;
  187. result.type = RayTrace::Type::BLOCK;
  188. return result;
  189. }
  190. Vector3 goal(step[0] < 0.0f ? lower[0] : lower[0] + 1,
  191. step[1] < 0.0f ? lower[1] : lower[1] + 1,
  192. step[2] < 0.0f ? lower[2] : lower[2] + 1);
  193. Vector3 f = (goal - pos) / step;
  194. float min = Math::max(Math::min(f[0], f[1], f[2]), 0.001f);
  195. pos += step * min;
  196. maxDistance -= min;
  197. }
  198. return result;
  199. }