WorldRenderer.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "client/rendering/renderer/WorldRenderer.h"
  2. #include "client/rendering/Engine.h"
  3. #include "utils/Logger.h"
  4. WorldRenderer::WorldRenderer(const World& world) : world(world) {
  5. }
  6. bool WorldRenderer::init() {
  7. mesh.init();
  8. Error error = texture.load("resources/textures.png", 4);
  9. if(error.has()) {
  10. LOG_ERROR(error.message);
  11. }
  12. return error.has();
  13. }
  14. void WorldRenderer::render() {
  15. if(world.dirty) {
  16. TypedBuffer<Triangle> buffer(100);
  17. for(int x = 0; x < world.getSize(); x++) {
  18. for(int y = 0; y < world.getHeight(); y++) {
  19. for(int z = 0; z < world.getSize(); z++) {
  20. if(world.getBlock(x, y, z).getId() != 0) {
  21. addCube(buffer, x, y, z);
  22. }
  23. }
  24. }
  25. }
  26. mesh.build(buffer);
  27. world.dirty = false;
  28. LOG_DEBUG("world render update");
  29. }
  30. texture.bindTo(0);
  31. for(int x = -1; x <= 1; x++) {
  32. for(int z = -1; z <= 1; z++) {
  33. Engine::matrix
  34. .translateTo(world.getSize() * x, 0.0f, world.getSize() * z)
  35. .update();
  36. mesh.draw();
  37. }
  38. }
  39. }
  40. bool WorldRenderer::isAir(int x, int y, int z) const {
  41. return world.getBlock(x, y, z).getId() == 0;
  42. }
  43. void WorldRenderer::addCube(TypedBuffer<Triangle>& buffer, float x, float y,
  44. float z) {
  45. Vector3 v000(x, y, z);
  46. Vector3 v001(x, y, z + 1);
  47. Vector3 v010(x, y + 1, z);
  48. Vector3 v011(x, y + 1, z + 1);
  49. Vector3 v100(x + 1, y, z);
  50. Vector3 v101(x + 1, y, z + 1);
  51. Vector3 v110(x + 1, y + 1, z);
  52. Vector3 v111(x + 1, y + 1, z + 1);
  53. Vector2 t1(0.1875f, 0.0f);
  54. Vector2 t2(0.25f, 0.0f);
  55. Vector2 t3(0.25f, 0.0625f);
  56. Vector2 t4(0.1875f, 0.0625f);
  57. if(isAir(x, y - 1, z)) {
  58. Vector2 tb(0.125f, 0.0625f);
  59. buffer.add(Triangle(Vertex(v000, Vector2(0.125f, 0.0f)),
  60. Vertex(v100, t1), Vertex(v001, tb)));
  61. buffer.add(
  62. Triangle(Vertex(v100, t1), Vertex(v101, t4), Vertex(v001, tb)));
  63. }
  64. if(isAir(x, y + 1, z)) {
  65. Vector2 tt(0.3125f, 0.0f);
  66. buffer.add(
  67. Triangle(Vertex(v010, t2), Vertex(v011, t3), Vertex(v110, tt)));
  68. buffer.add(Triangle(Vertex(v110, tt), Vertex(v011, t3),
  69. Vertex(v111, Vector2(0.3125f, 0.0625f))));
  70. }
  71. if(isAir(x - 1, y, z)) {
  72. buffer.add(
  73. Triangle(Vertex(v000, t4), Vertex(v001, t3), Vertex(v010, t1)));
  74. buffer.add(
  75. Triangle(Vertex(v001, t3), Vertex(v011, t2), Vertex(v010, t1)));
  76. }
  77. if(isAir(x + 1, y, z)) {
  78. buffer.add(
  79. Triangle(Vertex(v100, t3), Vertex(v110, t2), Vertex(v101, t4)));
  80. buffer.add(
  81. Triangle(Vertex(v101, t4), Vertex(v110, t2), Vertex(v111, t1)));
  82. }
  83. if(isAir(x, y, z + 1)) {
  84. buffer.add(
  85. Triangle(Vertex(v001, t4), Vertex(v101, t3), Vertex(v011, t1)));
  86. buffer.add(
  87. Triangle(Vertex(v111, t2), Vertex(v011, t1), Vertex(v101, t3)));
  88. }
  89. if(isAir(x, y, z - 1)) {
  90. buffer.add(
  91. Triangle(Vertex(v000, t3), Vertex(v010, t2), Vertex(v100, t4)));
  92. buffer.add(
  93. Triangle(Vertex(v110, t1), Vertex(v100, t4), Vertex(v010, t2)));
  94. }
  95. }