#include "client/rendering/ChunkRenderer.h" #include "client/rendering/block/BlockRenderers.h" #include "client/rendering/block/BlockRenderer.h" ChunkRenderer::ChunkRenderer() : blockTexture("resources/textures.png") { mesh = new NormalTextureMesh[chunkX * chunkZ * Chunk::HEIGHT_PARTIONS]; } ChunkRenderer::~ChunkRenderer() { delete[] mesh; } void ChunkRenderer::renderTick(Shader& shader, Camera3D camera, DirectRenderer& dr, float lag) { blockTexture.bind(); for(int x = 0; x < chunkX; x++) { for(int z = 0; z < chunkZ; z++) { int sx = x * Chunk::WIDTH; int sz = z * Chunk::DEPTH; int ex = sx + Chunk::WIDTH; int ez = sz + Chunk::DEPTH; if(camera.isInFrustum(sx, 0, sz, ex, Chunk::HEIGHT, ez)) { shader.translateTo(x * Chunk::WIDTH, 0, z * Chunk::DEPTH); shader.updateModelMatrix(); for(int l = 0; l < Chunk::HEIGHT_PARTIONS; l++) { if(camera.isInFrustum(sx, l * Chunk::PARTION_HEIGHT, sz, ex, (l + 1) * Chunk::PARTION_HEIGHT, ez)) { mesh[l + z * Chunk::HEIGHT_PARTIONS + x * chunkZ * Chunk::HEIGHT_PARTIONS].draw(); } } } } } } void ChunkRenderer::updateChunk(Chunk& c, Chunk* north, Chunk* east, Chunk* south, Chunk* west) { int x = c.getChunkX(); int z = c.getChunkZ(); if(x < 0 || x >= chunkX || z < 0 || z >= chunkZ) { return; } cout << "UPDATE: " << x << " " << z << endl; for(int l = 0; l < Chunk::HEIGHT_PARTIONS; l++) { if(c.isDirty(l)) { buildChunk(l, c, north, east, south, west); } } } void ChunkRenderer::buildChunk(int partionY, Chunk& c, Chunk* north, Chunk* east, Chunk* south, Chunk* west) { Mesh& m = mesh[partionY + c.getChunkZ() * Chunk::HEIGHT_PARTIONS + c.getChunkX() * chunkZ * Chunk::HEIGHT_PARTIONS]; int max = (partionY + 1) * Chunk::PARTION_HEIGHT; for(int y = partionY * Chunk::PARTION_HEIGHT; y < max; y++) { for(int x = 0; x < Chunk::WIDTH; x++) { for(int z = 0; z < Chunk::DEPTH; z++) { const Block& block = c.getBlock(x, y, z); if(!block.isEmpty()) { BlockRenderers::getBlockRenderer(block.getId()).addToMesh(m, x, y, z, Face::getCullData( // top y + 1 < Chunk::HEIGHT && c.getBlock(x, y + 1, z).isBlockingFace(Face::DOWN), // bottom y > 0 && c.getBlock(x, y - 1, z).isBlockingFace(Face::UP), // north (x + 1 >= Chunk::WIDTH || c.getBlock(x + 1, y, z).isBlockingFace(Face::SOUTH)) && (x + 1 < Chunk::WIDTH || (north != nullptr && north->getBlock(x + 1 - Chunk::WIDTH, y, z).isBlockingFace(Face::SOUTH))), // south (x - 1 < 0 || c.getBlock(x - 1, y, z).isBlockingFace(Face::NORTH)) && (x - 1 >= 0 || (south != nullptr && south->getBlock(x - 1 + Chunk::WIDTH, y, z).isBlockingFace(Face::NORTH))), // east (z + 1 >= Chunk::DEPTH || c.getBlock(x, y, z + 1).isBlockingFace(Face::WEST)) && (z + 1 < Chunk::DEPTH || (east != nullptr && east->getBlock(x, y, z + 1 - Chunk::DEPTH).isBlockingFace(Face::WEST))), // west (z - 1 < 0 || c.getBlock(x, y, z - 1).isBlockingFace(Face::EAST)) && (z - 1 >= 0 || (west != nullptr && west->getBlock(x, y, z - 1 + Chunk::DEPTH).isBlockingFace(Face::EAST))))); } } } } m.build(); }