WorldRenderer.cpp 2.7 KB

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