diff --git a/libbettola/include/bettola/game/chunk.h b/libbettola/include/bettola/game/chunk.h index 6778aca..2b66dfb 100644 --- a/libbettola/include/bettola/game/chunk.h +++ b/libbettola/include/bettola/game/chunk.h @@ -8,10 +8,14 @@ namespace Game { const int CHUNK_WIDTH = 32; const int CHUNK_HEIGHT = 32; +const int CHUNK_BORDER_SIZE = 1; +const int CHUNK_DATA_WIDTH = CHUNK_WIDTH + CHUNK_BORDER_SIZE * 2; +const int CHUNK_DATA_HEIGHT = CHUNK_HEIGHT + CHUNK_BORDER_SIZE * 2; + struct Chunk { std::vector heightmap; - Chunk(void) : heightmap(CHUNK_WIDTH * CHUNK_HEIGHT) {} + Chunk(void) : heightmap(CHUNK_DATA_WIDTH * CHUNK_DATA_HEIGHT) {} }; } /* namespace Game. */ diff --git a/libbettola/include/bettola/game/terrain.h b/libbettola/include/bettola/game/terrain.h index eeb2e55..9e57b45 100644 --- a/libbettola/include/bettola/game/terrain.h +++ b/libbettola/include/bettola/game/terrain.h @@ -21,7 +21,10 @@ inline float get_height_at(const BettolaLib::Game::Chunk& chunk, float local_x, if(local_x < 0 || local_x > max_coord || local_z < 0 || local_z > max_coord) { int x = std::max(0, std::min((int)local_x, CHUNK_WIDTH - 1)); int z = std::max(0, std::min((int)local_z, CHUNK_HEIGHT - 1)); - return chunk.heightmap[z*CHUNK_WIDTH+x] * 5.0f; /* Apply height scaling. */ + /* Adjust index for border. */ + int data_x = x + CHUNK_BORDER_SIZE; + int data_z = z + CHUNK_BORDER_SIZE; + return chunk.heightmap[data_z*CHUNK_DATA_WIDTH+data_x] * 5.0f; /* Apply height scaling. */ } /* Get integer and fracctional parts of the coords. */ @@ -31,10 +34,14 @@ inline float get_height_at(const BettolaLib::Game::Chunk& chunk, float local_x, float z_frac = local_z - z_int; /* Get heights of the four corners of the grid cell. */ - float h00 = chunk.heightmap[z_int * CHUNK_WIDTH+x_int]; - float h10 = chunk.heightmap[z_int * CHUNK_WIDTH+(x_int+1)]; - float h01 = chunk.heightmap[(z_int+1) * CHUNK_WIDTH+x_int]; - float h11 = chunk.heightmap[(z_int+1) * CHUNK_WIDTH+(x_int+1)]; + /* Adjust indices for bordder. */ + int x0 = x_int + CHUNK_BORDER_SIZE; + int z0 = z_int + CHUNK_BORDER_SIZE; + + float h00 = chunk.heightmap[z0 * CHUNK_DATA_WIDTH + x0]; + float h10 = chunk.heightmap[z0 * CHUNK_DATA_WIDTH + (x0+1)]; + float h01 = chunk.heightmap[(z0+1) * CHUNK_DATA_WIDTH + x0]; + float h11 = chunk.heightmap[(z0+1) * CHUNK_DATA_WIDTH + (x0+1)]; /* Bilinear interpolation. */ float h_x0 = h00 * (1 - x_frac) + h10 * x_frac; diff --git a/libbettola/include/bettola/network/chunk_message.h b/libbettola/include/bettola/network/chunk_message.h index ddd832d..d367ae5 100644 --- a/libbettola/include/bettola/network/chunk_message.h +++ b/libbettola/include/bettola/network/chunk_message.h @@ -7,7 +7,7 @@ namespace Network { struct ChunkMessage { int chunk_x; int chunk_z; - float heightmap[BettolaLib::Game::CHUNK_WIDTH * BettolaLib::Game::CHUNK_HEIGHT]; + float heightmap[Game::CHUNK_DATA_WIDTH*Game::CHUNK_DATA_HEIGHT]; }; } /* namespace Network. */ diff --git a/src/graphics/chunk_mesh.cpp b/src/graphics/chunk_mesh.cpp index 07c62bd..fc4d017 100644 --- a/src/graphics/chunk_mesh.cpp +++ b/src/graphics/chunk_mesh.cpp @@ -10,8 +10,13 @@ ChunkMesh::ChunkMesh(int chunk_x, int chunk_z, const BettolaLib::Game::Chunk& ch for(int z = 0; z < BettolaLib::Game::CHUNK_HEIGHT; ++z) { for(int x = 0; x < BettolaLib::Game::CHUNK_WIDTH; ++x) { /* Vertex position. */ + /* Index into the full 34x34 data array, offsetting by the border size. */ + int data_x = x + BettolaLib::Game::CHUNK_BORDER_SIZE; + int data_z = z + BettolaLib::Game::CHUNK_BORDER_SIZE; + _vertices.push_back((float)(chunk_x * (BettolaLib::Game::CHUNK_WIDTH-1) + x)); - _vertices.push_back(chunk.heightmap[z * BettolaLib::Game::CHUNK_WIDTH+x] * 5.0f); /* 5x scale height */ + /* 5x scale height */ + _vertices.push_back(chunk.heightmap[data_z * BettolaLib::Game::CHUNK_DATA_WIDTH+data_x]*5.0f); _vertices.push_back((float)(chunk_z * (BettolaLib::Game::CHUNK_HEIGHT-1) + z)); /* Vertex normal. */ @@ -47,12 +52,15 @@ ChunkMesh::ChunkMesh(int chunk_x, int chunk_z, const BettolaLib::Game::Chunk& ch BettolaMath::Vec3 ChunkMesh::_calculate_normal(int x, int z, const BettolaLib::Game::Chunk& chunk) { /* Get heights of adjacent certices. */ - float height_l = chunk.heightmap[z*BettolaLib::Game::CHUNK_WIDTH+(x>0?x-1 : x)] * 5.0f; - float height_r = chunk.heightmap[z*BettolaLib::Game::CHUNK_WIDTH+ - (x0?z-1 : z)*BettolaLib::Game::CHUNK_WIDTH+x]*5.0f; - float height_u = chunk.heightmap[(z