fixed chunk meshing due to using unsigned int
This commit is contained in:
parent
b579882324
commit
8852ce1af9
3 changed files with 28 additions and 29 deletions
|
@ -3,9 +3,9 @@
|
||||||
|
|
||||||
namespace app {
|
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},
|
: world_seed{world_seed},
|
||||||
chunk_seed{(world_seed * gridX) + (world_seed * gridZ) / 2},
|
chunk_seed{(world_seed * gridX) + (world_seed * gridZ) / 2},
|
||||||
gridX{gridX},
|
gridX{gridX},
|
||||||
|
@ -13,13 +13,13 @@ Chunk::Chunk(uint32_t gridX, uint32_t gridZ, uint32_t world_seed)
|
||||||
generate();
|
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);
|
Chunk* chunk = new Chunk(gridX, gridZ, world_seed);
|
||||||
chunks[{gridX, gridZ}] = std::move(chunk);
|
chunks[{gridX, gridZ}] = std::move(chunk);
|
||||||
return chunks[{gridX, gridZ}];
|
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})) {
|
if(chunks.count({gridX, gridZ})) {
|
||||||
return chunks[{gridX, gridZ}];
|
return chunks[{gridX, gridZ}];
|
||||||
} else {
|
} 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 > 256) return AIR;
|
||||||
if(y < 0) return INVALID;
|
if(y < 0) return INVALID;
|
||||||
int chunkX = gridX;
|
int chunkX = gridX;
|
||||||
int chunkZ = gridZ;
|
int chunkZ = gridZ;
|
||||||
if(x < 0) {
|
if(x < 0) {
|
||||||
chunkX--;
|
chunkX--;
|
||||||
x = 15;
|
|
||||||
} else if(x > 15) {
|
} else if(x > 15) {
|
||||||
chunkX++;
|
chunkX++;
|
||||||
x = 0;
|
|
||||||
}
|
}
|
||||||
if(z < 0) {
|
if(z < 0) {
|
||||||
chunkZ--;
|
chunkZ--;
|
||||||
z = 15;
|
|
||||||
} else if(z > 15) {
|
} else if(z > 15) {
|
||||||
chunkZ++;
|
chunkZ++;
|
||||||
z = 0;
|
|
||||||
}
|
}
|
||||||
|
x = (x+16)%16;
|
||||||
|
z = (z+16)%16;
|
||||||
if(chunkX == gridX && chunkZ == gridZ) {
|
if(chunkX == gridX && chunkZ == gridZ) {
|
||||||
int index = x + (z * 16) + (y * 256);
|
int index = x + (z * 16) + (y * 256);
|
||||||
return blocks[index];
|
return blocks[index];
|
||||||
} else {
|
} else {
|
||||||
Chunk* chunk = getChunk(chunkX, chunkZ);
|
Chunk* chunk = getChunk(chunkX, chunkZ);
|
||||||
if(chunk == nullptr) {
|
if(chunk == NULL) {
|
||||||
return INVALID;
|
return INVALID;
|
||||||
} else {
|
} else {
|
||||||
int index = x + (z * 16) + (y * 256);
|
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);
|
int index = x + (z * 16) + (y * 256);
|
||||||
blocks[index] = block;
|
blocks[index] = block;
|
||||||
}
|
}
|
||||||
|
@ -91,9 +89,9 @@ void Chunk::createMeshAsync() {
|
||||||
void Chunk::createMesh() {
|
void Chunk::createMesh() {
|
||||||
working = true;
|
working = true;
|
||||||
vertexData.clear();
|
vertexData.clear();
|
||||||
for(int x=0;x<16;x++) {
|
for(int32_t x=0;x<16;x++) {
|
||||||
for(int y=0; y<256; y++) {
|
for(int32_t y=0; y<256; y++) {
|
||||||
for(int z=0; z<16; z++) {
|
for(int32_t z=0; z<16; z++) {
|
||||||
uint8_t block = getBlock(x,y,z);
|
uint8_t block = getBlock(x,y,z);
|
||||||
if(block == AIR) continue;
|
if(block == AIR) continue;
|
||||||
if(getBlock(x+1,y,z) == AIR) {
|
if(getBlock(x+1,y,z) == AIR) {
|
||||||
|
@ -121,7 +119,7 @@ void Chunk::createMesh() {
|
||||||
reloadRequired = true;
|
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 ++) {
|
for(int i = 0; i < 6; i ++) {
|
||||||
vertexData.push_back(px[side * 6 + i][0] + x);
|
vertexData.push_back(px[side * 6 + i][0] + x);
|
||||||
vertexData.push_back(px[side * 6 + i][1] + y);
|
vertexData.push_back(px[side * 6 + i][1] + y);
|
||||||
|
|
|
@ -11,9 +11,9 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
#define INVALID -1
|
#define INVALID 0
|
||||||
#define AIR 0
|
#define AIR 1
|
||||||
#define DIRT 1
|
#define DIRT 2
|
||||||
|
|
||||||
namespace app {
|
namespace app {
|
||||||
|
|
||||||
|
@ -21,27 +21,28 @@ class Chunk {
|
||||||
|
|
||||||
public:
|
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();
|
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() {};
|
~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 createMesh();
|
||||||
void createMeshAsync();
|
void createMeshAsync();
|
||||||
std::shared_ptr<xe::Model> getMesh();
|
std::shared_ptr<xe::Model> getMesh();
|
||||||
|
|
||||||
uint8_t getBlock(uint32_t x, uint32_t y, uint32_t z);
|
uint8_t getBlock(int32_t x, int32_t y, int32_t z);
|
||||||
void setBlock(uint32_t x, uint32_t y, uint32_t z, uint8_t block);
|
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:
|
private:
|
||||||
|
|
||||||
void generate();
|
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 reloadRequired{false};
|
||||||
bool working{false};
|
bool working{false};
|
||||||
|
|
|
@ -46,14 +46,14 @@ void FirstApp::run() {
|
||||||
|
|
||||||
void FirstApp::loadGameObjects() {
|
void FirstApp::loadGameObjects() {
|
||||||
|
|
||||||
for(int x = 0; x < 10; x++) {
|
for(int32_t x = 0; x < 10; x++) {
|
||||||
for(int z = 0; z < 10; z++) {
|
for(int32_t z = 0; z < 10; z++) {
|
||||||
Chunk* chunk = Chunk::newChunk(x, z, 53463);
|
Chunk* chunk = Chunk::newChunk(x, z, 53463);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int x = 0; x < 10; x++) {
|
for(int32_t x = 0; x < 10; x++) {
|
||||||
for(int z = 0; z < 10; z++) {
|
for(int32_t z = 0; z < 10; z++) {
|
||||||
Chunk* chunk = Chunk::getChunk(x,z);
|
Chunk* chunk = Chunk::getChunk(x,z);
|
||||||
chunk->createMesh();
|
chunk->createMesh();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue