ChunkRenderer.cpp 3.9 KB

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