WorldRenderer.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "client/rendering/renderer/WorldRenderer.h"
  2. WorldRenderer::WorldRenderer(const World& world) : world(world), texture("resources/textures.png") {
  3. for(int x = 0; x < World::WORLD_SIZE; x++) {
  4. for(int y = 0; y < World::WORLD_SIZE; y++) {
  5. for(int z = 0; z < World::WORLD_SIZE; z++) {
  6. if(world.getBlock(x, y, z).getId() != 0) {
  7. addCube(x, y, z,
  8. isAir(x, y - 1, z), isAir(x, y + 1, z),
  9. isAir(x - 1, y, z), isAir(x + 1, y, z),
  10. isAir(x, y, z + 1), isAir(x, y, z - 1));
  11. }
  12. }
  13. }
  14. }
  15. mesh.build();
  16. }
  17. void WorldRenderer::render(float lag, Renderer& renderer) const {
  18. (void) lag;
  19. (void) renderer;
  20. texture.bind(0);
  21. mesh.draw();
  22. }
  23. bool WorldRenderer::isAir(int x, int y, int z) const {
  24. return world.getBlock(x, y, z).getId() == 0;
  25. }
  26. void WorldRenderer::addCube(float x, float y, float z, bool bottom, bool top, bool left, bool right, bool front, bool back) {
  27. Vector3 v000(x, y, z);
  28. Vector3 v001(x, y, z + 1);
  29. Vector3 v010(x, y + 1, z);
  30. Vector3 v011(x, y + 1, z + 1);
  31. Vector3 v100(x + 1, y, z);
  32. Vector3 v101(x + 1, y, z + 1);
  33. Vector3 v110(x + 1, y + 1, z);
  34. Vector3 v111(x + 1, y + 1, z + 1);
  35. Vector2 t1(0.1875f, 0.0f);
  36. Vector2 t2(0.25f, 0.0f);
  37. Vector2 t3(0.25f, 0.0625f);
  38. Vector2 t4(0.1875f, 0.0625f);
  39. if(bottom) {
  40. Vector2 tb(0.125f, 0.0625f);
  41. mesh.add(Triangle(Vertex(v000, Vector2(0.125f, 0.0f)), Vertex(v100, t1), Vertex(v001, tb)));
  42. mesh.add(Triangle(Vertex(v100, t1), Vertex(v101, t4), Vertex(v001, tb)));
  43. }
  44. if(top) {
  45. Vector2 tt(0.3125f, 0.0f);
  46. mesh.add(Triangle(Vertex(v010, t2), Vertex(v011, t3), Vertex(v110, tt)));
  47. mesh.add(Triangle(Vertex(v110, tt), Vertex(v011, t3), Vertex(v111, Vector2(0.3125f, 0.0625f))));
  48. }
  49. if(left) {
  50. mesh.add(Triangle(Vertex(v000, t4), Vertex(v001, t3), Vertex(v010, t1)));
  51. mesh.add(Triangle(Vertex(v001, t3), Vertex(v011, t2), Vertex(v010, t1)));
  52. }
  53. if(right) {
  54. mesh.add(Triangle(Vertex(v100, t3), Vertex(v110, t2), Vertex(v101, t4)));
  55. mesh.add(Triangle(Vertex(v101, t4), Vertex(v110, t2), Vertex(v111, t1)));
  56. }
  57. if(front) {
  58. mesh.add(Triangle(Vertex(v001, t4), Vertex(v101, t3), Vertex(v011, t1)));
  59. mesh.add(Triangle(Vertex(v111, t2), Vertex(v011, t1), Vertex(v101, t3)));
  60. }
  61. if(back) {
  62. mesh.add(Triangle(Vertex(v000, t3), Vertex(v010, t2), Vertex(v100, t4)));
  63. mesh.add(Triangle(Vertex(v110, t1), Vertex(v100, t4), Vertex(v010, t2)));
  64. }
  65. }