1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
#pragma once
#include "xe_model.hpp"
#include "xe_engine.hpp"
#include "xe_image.hpp"
#include "PerlinNoise.hpp"
#include <glm/common.hpp>
#include <glm/fwd.hpp>
#include <vector>
#include <thread>
#include <memory>
#include <string>
#include <map>
#include <algorithm>
#define INVALID -1
#define AIR 0
#define DIRT 1
#define GRASS 2
#define FULL_GRASS 3
#define STONE 4
#define SNOW 5
#define SAND 6
#define WATER 7
#define SHRUB 8
#define FULL_SHRUB 9
#define DIRT_TEXTURE "res/image/dirt.png"
#define GRASS_TEXTURE "res/image/grass.png"
#define GRASS_TOP_TEXTURE "res/image/grass_top.png"
#define STONE_TEXTURE "res/image/stone.png"
#define SNOW_TEXTURE "res/image/snow.png"
#define SAND_TEXTURE "res/image/sand.png"
#define WATER_TEXTURE "res/image/water.png"
#define SHRUB_TEXTURE "res/image/shrub.png"
#define SHRUB_TOP_TEXTURE "res/image/shrub_top.png"
static constexpr int WATER_LEVEL = 20;
namespace app {
struct Block {
uint32_t textures[6];
};
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);
static Chunk* getChunk(int32_t gridX, int32_t gridZ);
static void deleteChunk(int32_t gridX, int32_t gridZ);
static void createMesh(Chunk* c);
static void createMeshAsync(Chunk* c);
static void generate(Chunk* c);
static void generateAsync(Chunk* c);
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);
static bool isGenerated(int32_t gridX, int32_t gridZ);
const int32_t gridX, gridZ;
const uint32_t world_seed, chunk_seed;
private:
Chunk(int32_t gridX, int32_t gridZ, uint32_t world_seed);
~Chunk();
static void addVerticies(Chunk* c, uint8_t side, int32_t x, int32_t y, int32_t z, uint8_t block);
bool generated{false};
bool reload{false};
bool working{false};
xe::Model* chunkMesh;
xe::Model::Data vertexData;
std::vector<uint8_t> cubes;
std::thread worker;
};
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] = {
{1.f,0.f},
{1.f,1.f},
{0.f,1.f},
{0.f,1.f},
{0.f,0.f},
{1.f,0.f}
};
}
|