ChunkRenderer.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "client/rendering/ChunkRenderer.h"
  2. #include "client/rendering/block/BlockRenderers.h"
  3. #include "client/rendering/block/BlockRenderer.h"
  4. ChunkRenderer::ChunkRenderer() : blockTexture("resources/textures.png")
  5. {
  6. mesh = new NormalTextureMesh[chunkX * chunkZ * Chunk::HEIGHT_PARTIONS];
  7. }
  8. ChunkRenderer::~ChunkRenderer()
  9. {
  10. delete[] mesh;
  11. }
  12. void ChunkRenderer::renderTick(Shader& shader, Camera3D camera, DirectRenderer& dr, float lag)
  13. {
  14. blockTexture.bind();
  15. for(int x = 0; x < chunkX; x++)
  16. {
  17. for(int z = 0; z < chunkZ; z++)
  18. {
  19. int sx = x * Chunk::WIDTH;
  20. int sz = z * Chunk::DEPTH;
  21. int ex = sx + Chunk::WIDTH;
  22. int ez = sz + Chunk::DEPTH;
  23. if(camera.isInFrustum(sx, 0, sz, ex, Chunk::HEIGHT, ez))
  24. {
  25. shader.translateTo(x * Chunk::WIDTH, 0, z * Chunk::DEPTH);
  26. shader.updateModelMatrix();
  27. for(int l = 0; l < Chunk::HEIGHT_PARTIONS; l++)
  28. {
  29. if(camera.isInFrustum(sx, l * Chunk::PARTION_HEIGHT, sz, ex, (l + 1) * Chunk::PARTION_HEIGHT, ez))
  30. {
  31. mesh[l + z * Chunk::HEIGHT_PARTIONS + x * chunkZ * Chunk::HEIGHT_PARTIONS].draw();
  32. }
  33. }
  34. }
  35. }
  36. }
  37. }
  38. void ChunkRenderer::updateChunk(Chunk& c, Chunk* north, Chunk* east, Chunk* south, Chunk* west)
  39. {
  40. int x = c.getChunkX();
  41. int z = c.getChunkZ();
  42. if(x < 0 || x >= chunkX || z < 0 || z >= chunkZ)
  43. {
  44. return;
  45. }
  46. cout << "UPDATE: " << x << " " << z << endl;
  47. for(int l = 0; l < Chunk::HEIGHT_PARTIONS; l++)
  48. {
  49. if(c.isDirty(l))
  50. {
  51. buildChunk(l, c, north, east, south, west);
  52. }
  53. }
  54. }
  55. void ChunkRenderer::buildChunk(int partionY, Chunk& c, Chunk* north, Chunk* east, Chunk* south, Chunk* west)
  56. {
  57. Mesh& m = mesh[partionY + c.getChunkZ() * Chunk::HEIGHT_PARTIONS + c.getChunkX() * chunkZ * Chunk::HEIGHT_PARTIONS];
  58. int max = (partionY + 1) * Chunk::PARTION_HEIGHT;
  59. for(int y = partionY * Chunk::PARTION_HEIGHT; y < max; y++)
  60. {
  61. for(int x = 0; x < Chunk::WIDTH; x++)
  62. {
  63. for(int z = 0; z < Chunk::DEPTH; z++)
  64. {
  65. const Block& block = c.getBlock(x, y, z);
  66. if(!block.isEmpty())
  67. {
  68. BlockRenderers::getBlockRenderer(block.getId()).addToMesh(m, x, y, z,
  69. Face::getCullData(
  70. // top
  71. y + 1 < Chunk::HEIGHT && c.getBlock(x, y + 1, z).isBlockingFace(Face::DOWN),
  72. // bottom
  73. y > 0 && c.getBlock(x, y - 1, z).isBlockingFace(Face::UP),
  74. // north
  75. (x + 1 >= Chunk::WIDTH || c.getBlock(x + 1, y, z).isBlockingFace(Face::SOUTH)) &&
  76. (x + 1 < Chunk::WIDTH || (north != nullptr && north->getBlock(x + 1 - Chunk::WIDTH, y, z).isBlockingFace(Face::SOUTH))),
  77. // south
  78. (x - 1 < 0 || c.getBlock(x - 1, y, z).isBlockingFace(Face::NORTH)) &&
  79. (x - 1 >= 0 || (south != nullptr && south->getBlock(x - 1 + Chunk::WIDTH, y, z).isBlockingFace(Face::NORTH))),
  80. // east
  81. (z + 1 >= Chunk::DEPTH || c.getBlock(x, y, z + 1).isBlockingFace(Face::WEST)) &&
  82. (z + 1 < Chunk::DEPTH || (east != nullptr && east->getBlock(x, y, z + 1 - Chunk::DEPTH).isBlockingFace(Face::WEST))),
  83. // west
  84. (z - 1 < 0 || c.getBlock(x, y, z - 1).isBlockingFace(Face::EAST)) &&
  85. (z - 1 >= 0 || (west != nullptr && west->getBlock(x, y, z - 1 + Chunk::DEPTH).isBlockingFace(Face::EAST)))));
  86. }
  87. }
  88. }
  89. }
  90. m.build();
  91. }