minecraftvulkan/src/chunk.hpp

135 lines
2.6 KiB
C++
Raw Normal View History

2022-09-25 23:05:56 +00:00
#pragma once
#include "xe_model.hpp"
#include "xe_engine.hpp"
#include "xe_image.hpp"
2022-09-25 23:05:56 +00:00
#include "PerlinNoise.hpp"
#include <glm/common.hpp>
#include <glm/fwd.hpp>
#include <vector>
#include <thread>
#include <memory>
#include <string>
#include <map>
#define INVALID -1
#define AIR 0
#define DIRT 1
#define GRASS 2
#define DIRT_TEXTURE "res/image/dirt.png"
#define GRASS_TEXTURE "res/image/grass.png"
#define GRASS_TOP_TEXTURE "res/image/grass_top.png"
2022-09-25 23:05:56 +00:00
namespace app {
struct Block {
uint32_t textures[6];
};
2022-09-25 23:05:56 +00:00
class Chunk {
public:
static void load();
static void unload();
static std::vector<xe::Image*>& getTextures();
static Chunk* newChunk(int32_t gridX, int32_t gridZ, uint32_t world_seed);
2022-09-27 17:35:49 +00:00
static Chunk* getChunk(int32_t gridX, int32_t gridZ);
2022-09-25 23:05:56 +00:00
2022-09-27 17:35:49 +00:00
static void createMesh(Chunk* c);
static void createMeshAsync(Chunk* c);
2022-09-25 23:05:56 +00:00
2022-09-27 17:35:49 +00:00
xe::Model* getMesh();
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);
2022-09-25 23:05:56 +00:00
2022-09-27 17:35:49 +00:00
const int32_t gridX, gridZ;
const uint32_t world_seed, chunk_seed;
2022-09-25 23:05:56 +00:00
private:
2022-09-27 17:35:49 +00:00
Chunk(int32_t gridX, int32_t gridZ, uint32_t world_seed);
~Chunk();
2022-09-25 23:05:56 +00:00
void generate();
2022-09-27 17:35:49 +00:00
static void addVerticies(Chunk* c, uint8_t side, int32_t x, int32_t y, int32_t z, uint8_t block);
2022-09-25 23:05:56 +00:00
bool reloadRequired{false};
bool working{false};
2022-09-27 17:35:49 +00:00
xe::Model* chunkMesh;
xe::Model::Data vertexData;
std::vector<uint8_t> cubes;
2022-09-25 23:05:56 +00:00
std::thread worker;
2022-09-25 23:05:56 +00:00
};
const float px[36][3] = {
// POS X
{0.5f,0.5f,0.5f},
{0.5f,-0.5f,0.5f},
{0.5f,-0.5f,-0.5f},
{0.5f,-0.5f,-0.5f},
{0.5f,0.5f,-0.5f},
{0.5f,0.5f,0.5f},
// NEG X
{-0.5f,0.5f,-0.5f},
{-0.5f,-0.5f,-0.5f},
{-0.5f,-0.5f,0.5f},
{-0.5f,-0.5f,0.5f},
{-0.5f,0.5f,0.5f},
{-0.5f,0.5f,-0.5f},
// POS Y
{0.5f,0.5f,-0.5f},
{-0.5f,0.5f,-0.5f},
{-0.5f,0.5f,0.5f},
{-0.5f,0.5f,0.5f},
{0.5f,0.5f,0.5f},
{0.5f,0.5f,-0.5f},
// NEG Y
{-0.5f,-0.5f,0.5f},
{-0.5f,-0.5f,-0.5f},
{0.5f,-0.5f,-0.5f},
{0.5f,-0.5f,-0.5f},
{0.5f,-0.5f,0.5f},
{-0.5f,-0.5f,0.5f},
// POS Z
{-0.5f,0.5f,0.5f},
{-0.5f,-0.5f,0.5f},
{0.5f,-0.5f,0.5f},
{0.5f,-0.5f,0.5f},
{0.5f,0.5f,0.5f},
{-0.5f,0.5f,0.5f},
// NEG Z
{0.5f,0.5f,-0.5f},
{0.5f,-0.5f,-0.5f},
{-0.5f,-0.5f,-0.5f},
{-0.5f,-0.5f,-0.5f},
{-0.5f,0.5f,-0.5f},
{0.5f,0.5f,-0.5f}
};
const float nm[6][3] = {
{1.f,0.f,0.f},
{-1.f,0.f,0.f},
{0.f,1.f,0.f},
{0.f,-1.f,0.f},
{0.f,0.f,1.f},
{0.f,0.f,-1.f}
};
const float uv[6][2] = {
2022-09-26 18:08:40 +00:00
{1.f,0.f},
2022-09-26 03:08:03 +00:00
{1.f,1.f},
2022-09-25 23:05:56 +00:00
{0.f,1.f},
2022-09-26 18:08:40 +00:00
{0.f,1.f},
2022-09-26 03:08:03 +00:00
{0.f,0.f},
2022-09-26 18:08:40 +00:00
{1.f,0.f}
2022-09-25 23:05:56 +00:00
};
}