fixed chunk meshing due to using unsigned int

This commit is contained in:
Tyler Murphy 2022-09-26 13:24:16 -04:00
parent b579882324
commit 8852ce1af9
3 changed files with 28 additions and 29 deletions

View file

@ -3,9 +3,9 @@
namespace app {
static std::map<std::pair<uint32_t, uint32_t>, Chunk*> chunks{};
static std::map<std::pair<int32_t, int32_t>, Chunk*> chunks{};
Chunk::Chunk(uint32_t gridX, uint32_t gridZ, uint32_t world_seed)
Chunk::Chunk(int32_t gridX, int32_t gridZ, uint32_t world_seed)
: world_seed{world_seed},
chunk_seed{(world_seed * gridX) + (world_seed * gridZ) / 2},
gridX{gridX},
@ -13,13 +13,13 @@ Chunk::Chunk(uint32_t gridX, uint32_t gridZ, uint32_t world_seed)
generate();
}
Chunk* Chunk::newChunk(uint32_t gridX, uint32_t gridZ, uint32_t world_seed) {
Chunk* Chunk::newChunk(int32_t gridX, int32_t gridZ, uint32_t world_seed) {
Chunk* chunk = new Chunk(gridX, gridZ, world_seed);
chunks[{gridX, gridZ}] = std::move(chunk);
return chunks[{gridX, gridZ}];
}
Chunk* Chunk::getChunk(uint32_t gridX, uint32_t gridZ) {
Chunk* Chunk::getChunk(int32_t gridX, int32_t gridZ) {
if(chunks.count({gridX, gridZ})) {
return chunks[{gridX, gridZ}];
} else {
@ -27,31 +27,29 @@ Chunk* Chunk::getChunk(uint32_t gridX, uint32_t gridZ) {
}
}
uint8_t Chunk::getBlock(uint32_t x, uint32_t y, uint32_t z) {
uint8_t Chunk::getBlock(int32_t x, int32_t y, int32_t z) {
if(y > 256) return AIR;
if(y < 0) return INVALID;
int chunkX = gridX;
int chunkZ = gridZ;
if(x < 0) {
chunkX--;
x = 15;
} else if(x > 15) {
chunkX++;
x = 0;
}
if(z < 0) {
chunkZ--;
z = 15;
} else if(z > 15) {
chunkZ++;
z = 0;
}
x = (x+16)%16;
z = (z+16)%16;
if(chunkX == gridX && chunkZ == gridZ) {
int index = x + (z * 16) + (y * 256);
return blocks[index];
} else {
Chunk* chunk = getChunk(chunkX, chunkZ);
if(chunk == nullptr) {
if(chunk == NULL) {
return INVALID;
} else {
int index = x + (z * 16) + (y * 256);
@ -60,7 +58,7 @@ uint8_t Chunk::getBlock(uint32_t x, uint32_t y, uint32_t z) {
}
}
void Chunk::setBlock(uint32_t x, uint32_t y, uint32_t z, uint8_t block) {
void Chunk::setBlock(int32_t x, int32_t y, int32_t z, uint8_t block) {
int index = x + (z * 16) + (y * 256);
blocks[index] = block;
}
@ -91,9 +89,9 @@ void Chunk::createMeshAsync() {
void Chunk::createMesh() {
working = true;
vertexData.clear();
for(int x=0;x<16;x++) {
for(int y=0; y<256; y++) {
for(int z=0; z<16; z++) {
for(int32_t x=0;x<16;x++) {
for(int32_t y=0; y<256; y++) {
for(int32_t z=0; z<16; z++) {
uint8_t block = getBlock(x,y,z);
if(block == AIR) continue;
if(getBlock(x+1,y,z) == AIR) {
@ -121,7 +119,7 @@ void Chunk::createMesh() {
reloadRequired = true;
}
void Chunk::addVerticies(uint32_t side, uint32_t x, uint32_t y, uint32_t z) {
void Chunk::addVerticies(uint8_t side, int32_t x, int32_t y, int32_t z) {
for(int i = 0; i < 6; i ++) {
vertexData.push_back(px[side * 6 + i][0] + x);
vertexData.push_back(px[side * 6 + i][1] + y);

View file

@ -11,9 +11,9 @@
#include <string>
#include <map>
#define INVALID -1
#define AIR 0
#define DIRT 1
#define INVALID 0
#define AIR 1
#define DIRT 2
namespace app {
@ -21,27 +21,28 @@ class Chunk {
public:
static Chunk* newChunk(uint32_t gridX, uint32_t gridZ, uint32_t world_seed);
static Chunk* newChunk(int32_t gridX, int32_t gridZ, uint32_t world_seed);
static void reset();
Chunk(uint32_t gridX, uint32_t gridZ, uint32_t world_seed);
Chunk(int32_t gridX, int32_t gridZ, uint32_t world_seed);
~Chunk() {};
const uint32_t gridX, gridZ, world_seed, chunk_seed;
const int32_t gridX, gridZ;
const uint32_t world_seed, chunk_seed;
void createMesh();
void createMeshAsync();
std::shared_ptr<xe::Model> getMesh();
uint8_t getBlock(uint32_t x, uint32_t y, uint32_t z);
void setBlock(uint32_t x, uint32_t y, uint32_t z, uint8_t block);
uint8_t getBlock(int32_t x, int32_t y, int32_t z);
void setBlock(int32_t x, int32_t y, int32_t z, uint8_t block);
static Chunk* getChunk(uint32_t x, uint32_t z);
static Chunk* getChunk(int32_t x, int32_t z);
private:
void generate();
void addVerticies(uint32_t side, uint32_t x, uint32_t y, uint32_t z);
void addVerticies(uint8_t side, int32_t x, int32_t y, int32_t z);
bool reloadRequired{false};
bool working{false};

View file

@ -46,14 +46,14 @@ void FirstApp::run() {
void FirstApp::loadGameObjects() {
for(int x = 0; x < 10; x++) {
for(int z = 0; z < 10; z++) {
for(int32_t x = 0; x < 10; x++) {
for(int32_t z = 0; z < 10; z++) {
Chunk* chunk = Chunk::newChunk(x, z, 53463);
}
}
for(int x = 0; x < 10; x++) {
for(int z = 0; z < 10; z++) {
for(int32_t x = 0; x < 10; x++) {
for(int32_t z = 0; z < 10; z++) {
Chunk* chunk = Chunk::getChunk(x,z);
chunk->createMesh();