Server now generates and sends an expanded 34x34 heightmap for each chunk, which includes a 1-vertex border containing height information from all adjacent chunks. Client ChunkMesh class updated to consume this extra data, allowing it to calcualte perfectly accurate normal vectors for all vertices, including those at the very edge of a chunk.
37 lines
1.2 KiB
C++
37 lines
1.2 KiB
C++
#include "world.h"
|
|
#include "bettola/game/chunk.h"
|
|
#include "bettola/noise/fast_noise_lite.h"
|
|
|
|
World::World(void) {
|
|
m_noise.SetNoiseType(FastNoiseLite::NoiseType_Perlin);
|
|
m_noise.SetFrequency(0.02f);
|
|
}
|
|
|
|
BettolaLib::Game::Chunk& World::get_chunk(int x, int z) {
|
|
ChunkPos pos = {x, z};
|
|
auto it = m_chunks.find(pos);
|
|
if(it != m_chunks.end()) {
|
|
return it->second;
|
|
}
|
|
|
|
/* If chunk doesn't exist, generate it. */
|
|
BettolaLib::Game::Chunk& new_chunk = m_chunks[pos];
|
|
_generate_chunk(new_chunk, x, z);
|
|
return new_chunk;
|
|
}
|
|
|
|
void World::_generate_chunk(BettolaLib::Game::Chunk& chunk, int chunk_x, int chunk_z) {
|
|
for(int z = 0; z < BettolaLib::Game::CHUNK_DATA_HEIGHT; ++z) {
|
|
for(int x = 0; x < BettolaLib::Game::CHUNK_DATA_WIDTH; ++x) {
|
|
/* Calculate world coordinates. */
|
|
float world_x = (float)(chunk_x * (BettolaLib::Game::CHUNK_WIDTH - 1)
|
|
+ (x - BettolaLib::Game::CHUNK_BORDER_SIZE));
|
|
float world_z = (float)(chunk_z * (BettolaLib::Game::CHUNK_HEIGHT - 1)
|
|
+ (z + BettolaLib::Game::CHUNK_BORDER_SIZE));
|
|
|
|
/* generate noise value. */
|
|
chunk.heightmap[z * BettolaLib::Game::CHUNK_DATA_WIDTH + x] = m_noise.GetNoise(world_x, world_z);
|
|
}
|
|
}
|
|
}
|