VOX
A little voxel engine
Loading...
Searching...
No Matches
CreateMeshData.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "define.hpp"
4#include "vk_define.hpp"
5#include "DebugGui.hpp"
6#include "Timer.hpp"
7#include "logger.hpp"
8#include "ObjLoader.hpp"
9
10#define GLM_ENABLE_EXPERIMENTAL
11#include <glm/glm.hpp>
12#include <glm/gtx/hash.hpp>
13
14#include "Chunk.hpp"
15#include "hashes.hpp"
16
17#include <unordered_map>
18
19// vec3 pos: 96
20// vec3 normal: 96
21// vec2 texCoord: 64
22// uint32_t texLayer: 32
23// uint8_t ao: 8
24// uint8_t light: 8
25// Total: 304 bits
26
27// pos: 5+5+10 = 20 (x: [0..16], y: [0..512], z: [0..16])
28// normal: 3 (only 6 different normals, mapped to 6 values)
29// texCoord: 5+5 = 10 (x: [0..16], y: [0..16])
30// texLayer: 32
31// ao: 2 [0..3]
32// light: 8
33// Total: 75 bits
34
35// pos x pos z tex xy ao
36// ----- ----- ---------- --
37// 0000000000000000000000000000000000000000000000000000000000000000
38// ---------- --- --------
39// pos y normal light
40
41// texLayer
42// --------------------------------
43// 0000000000000000000000000000000000000000000000000000000000000000
44
46{
47 uint64_t data[2];
48
50 const glm::ivec3 & pos,
51 const uint32_t normal,
52 const glm::ivec2 & texCoord,
53 const uint32_t texLayer,
54 const uint8_t ao,
55 const uint8_t light
56 )
57 {
58 bzero(data, sizeof(data));
59 data[0] |= static_cast<uint64_t>(pos.x) & 0b11111;
60 data[0] |= (static_cast<uint64_t>(pos.y) & 0b1111111111) << 5;
61 data[0] |= (static_cast<uint64_t>(pos.z) & 0b11111) << 15;
62 data[0] |= (static_cast<uint64_t>(normal) & 0b111) << 20;
63 data[0] |= (static_cast<uint64_t>(texCoord.x) & 0b11111) << 23;
64 data[0] |= (static_cast<uint64_t>(texCoord.y) & 0b11111) << 28;
65 data[0] |= (static_cast<uint64_t>(light) & 0xFF) << 33;
66 data[0] |= (static_cast<uint64_t>(ao) & 0b11) << 41;
67
68 data[1] |= (static_cast<uint64_t>(texLayer) & 0xFFFFFFFF);
69 }
70
71 static constexpr uint32_t getMaxTextureCoord()
72 {
73 return 16;
74 }
75
76 static VkVertexInputBindingDescription getBindingDescription()
77 {
78 VkVertexInputBindingDescription bindingDescription{};
79 bindingDescription.binding = 0;
80 bindingDescription.stride = sizeof(BlockVertex);
81 bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
82
83 return bindingDescription;
84 }
85
86 static std::vector<VkVertexInputAttributeDescription> getAttributeDescriptions()
87 {
88 std::vector<VkVertexInputAttributeDescription> attributeDescriptions(1);
89
90 attributeDescriptions[0].binding = 0;
91 attributeDescriptions[0].location = 0;
92 attributeDescriptions[0].format = VK_FORMAT_R64G64_UINT;
93 attributeDescriptions[0].offset = offsetof(BlockVertex, data);
94
95 return attributeDescriptions;
96 }
97};
98
99// struct BlockVertex
100// {
101// glm::vec3 pos;
102// glm::vec3 normal;
103// glm::vec2 texCoord;
104// uint32_t texLayer;
105// uint8_t ao;
106// uint8_t light;
107
108// static VkVertexInputBindingDescription getBindingDescription()
109// {
110// VkVertexInputBindingDescription bindingDescription{};
111// bindingDescription.binding = 0;
112// bindingDescription.stride = sizeof(BlockVertex);
113// bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
114
115// return bindingDescription;
116// }
117
118// static std::vector<VkVertexInputAttributeDescription> getAttributeDescriptions()
119// {
120// std::vector<VkVertexInputAttributeDescription> attributeDescriptions(6);
121
122// attributeDescriptions[0].binding = 0;
123// attributeDescriptions[0].location = 0;
124// attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
125// attributeDescriptions[0].offset = offsetof(BlockVertex, pos);
126
127// attributeDescriptions[1].binding = 0;
128// attributeDescriptions[1].location = 1;
129// attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
130// attributeDescriptions[1].offset = offsetof(BlockVertex, normal);
131
132// attributeDescriptions[2].binding = 0;
133// attributeDescriptions[2].location = 2;
134// attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT;
135// attributeDescriptions[2].offset = offsetof(BlockVertex, texCoord);
136
137// attributeDescriptions[3].binding = 0;
138// attributeDescriptions[3].location = 3;
139// attributeDescriptions[3].format = VK_FORMAT_R32_UINT;
140// attributeDescriptions[3].offset = offsetof(BlockVertex, texLayer);
141
142// attributeDescriptions[4].binding = 0;
143// attributeDescriptions[4].location = 4;
144// attributeDescriptions[4].format = VK_FORMAT_R8_UINT;
145// attributeDescriptions[4].offset = offsetof(BlockVertex, ao);
146
147// attributeDescriptions[5].binding = 0;
148// attributeDescriptions[5].location = 5;
149// attributeDescriptions[5].format = VK_FORMAT_R8_UINT;
150// attributeDescriptions[5].offset = offsetof(BlockVertex, light);
151
152// return attributeDescriptions;
153// }
154
155// bool operator==(const BlockVertex& other) const
156// {
157// return pos == other.pos
158// && normal == other.normal
159// && texCoord == other.texCoord
160// && texLayer == other.texLayer
161// && ao == other.ao
162// && light == other.light;
163// }
164// };
165
166// namespace std
167// {
168// template<> struct hash<BlockVertex>
169// {
170// size_t operator()(const BlockVertex & vertex) const
171// {
172// return ((hash<glm::vec3>()(vertex.pos) ^
173// (hash<glm::vec3>()(vertex.normal) << 1)) >> 1) ^
174// (hash<glm::vec2>()(vertex.texCoord) << 1);
175// }
176// };
177// }
178
180{
181
182public:
183
184 enum
185 {
186 POS = 2,
187 NEUT = 1,
188 NEG = 0
189 };
190
191 enum class Dimensions
192 {
193 X = 0,
194 Y = 1,
195 Z = 2
196 };
197
205 CreateMeshData(const glm::ivec3 & pos, const glm::ivec3 & size, ChunkMap & chunk_map);
206
209
212
214
215 void unlock();
216
217 BlockInfo::Type getBlock(const int x, const int y, const int z);
218 BlockInfo::Type getBlock(const glm::ivec3 & pos);
219
220 uint8_t getLight(const int x, const int y, const int z);
221 uint8_t getLight(const glm::ivec3 & pos);
222
223 void create();
224
225 void createFace(
226 const int dim_1,
227 const int dim_2,
228 const glm::ivec3 & start,
229 const glm::ivec3 & max_iter,
230 const std::array<int, 6> & indices_order,
231 const std::array<int, 6> & indices_order_fliped,
232 const glm::ivec3 & abs_normal,
233 const int normal_signe,
234 const int face
235 );
236
237 void createFaceWater(
238 const int dim_1,
239 const int dim_2,
240 const glm::ivec3 & start,
241 const glm::ivec3 & max_iter,
242 const std::array<int, 6> & indices_order,
243 const std::array<int, 6> & indices_order_fliped,
244 const glm::ivec3 & abs_normal,
245 const int normal_signe,
246 const int face
247 );
248
249 std::array<uint8_t, 4> getAmbientOcclusion(
250 const glm::ivec3 & pos,
251 const int dim_1,
252 const int dim_2
253 );
255 BlockInfo::Type side_1,
256 BlockInfo::Type side_2,
257 BlockInfo::Type corner
258 );
259
260 std::array<uint8_t, 4> getLight(
261 const glm::ivec3 & pos,
262 const int dim_1,
263 const int dim_2
264 );
265 uint8_t getLight(uint8_t pos, uint8_t side_1, uint8_t side_2, uint8_t corner);
266
267
268 std::vector<std::vector<std::vector<std::shared_ptr<Chunk>>>> chunks;
269 std::vector<BlockVertex> vertices;
270 std::vector<uint32_t> indices;
271
272 std::vector<BlockVertex> water_vertices;
273 std::vector<uint32_t> water_indices;
274
275 std::shared_ptr<Chunk> getCenterChunk()
276 {
277 return chunks[NEUT][NEUT][NEUT];
278 }
279
280private:
281
282 struct FaceData
283 {
284 TextureID texture;
285 std::array<uint8_t, 4> ao;
286 std::array<uint8_t, 4> light;
287
288 bool operator==(const FaceData & other) const
289 {
290 return texture == other.texture && ao == other.ao && light == other.light;
291 }
292 };
293
294 std::vector<std::vector<std::vector<FaceData>>> face_data;
295 glm::ivec3 size;
296
297};
uint32_t TextureID
Definition: Block.hpp:10
std::unordered_map< glm::ivec3, std::shared_ptr< Chunk > > ChunkMap
Definition: Chunk.hpp:177
Type
Definition: Block.hpp:33
Definition: CreateMeshData.hpp:180
std::shared_ptr< Chunk > getCenterChunk()
Definition: CreateMeshData.hpp:275
std::vector< uint32_t > indices
Definition: CreateMeshData.hpp:270
std::vector< std::vector< std::vector< std::shared_ptr< Chunk > > > > chunks
Definition: CreateMeshData.hpp:268
std::vector< uint32_t > water_indices
Definition: CreateMeshData.hpp:273
uint8_t getLight(const int x, const int y, const int z)
Definition: CreateMeshData.cpp:127
@ NEG
Definition: CreateMeshData.hpp:188
@ POS
Definition: CreateMeshData.hpp:186
@ NEUT
Definition: CreateMeshData.hpp:187
~CreateMeshData()
Definition: CreateMeshData.cpp:44
Dimensions
Definition: CreateMeshData.hpp:192
CreateMeshData & operator=(const CreateMeshData &)=delete
CreateMeshData(const CreateMeshData &)=delete
void create()
Definition: CreateMeshData.cpp:150
BlockInfo::Type getBlock(const int x, const int y, const int z)
Definition: CreateMeshData.cpp:104
void createFace(const int dim_1, const int dim_2, const glm::ivec3 &start, const glm::ivec3 &max_iter, const std::array< int, 6 > &indices_order, const std::array< int, 6 > &indices_order_fliped, const glm::ivec3 &abs_normal, const int normal_signe, const int face)
Definition: CreateMeshData.cpp:308
std::vector< BlockVertex > water_vertices
Definition: CreateMeshData.hpp:272
std::vector< BlockVertex > vertices
Definition: CreateMeshData.hpp:269
void unlock()
Definition: CreateMeshData.cpp:81
std::array< uint8_t, 4 > getAmbientOcclusion(const glm::ivec3 &pos, const int dim_1, const int dim_2)
Definition: CreateMeshData.cpp:684
void createFaceWater(const int dim_1, const int dim_2, const glm::ivec3 &start, const glm::ivec3 &max_iter, const std::array< int, 6 > &indices_order, const std::array< int, 6 > &indices_order_fliped, const glm::ivec3 &abs_normal, const int normal_signe, const int face)
Definition: CreateMeshData.cpp:502
Definition: CreateMeshData.hpp:46
BlockVertex(const glm::ivec3 &pos, const uint32_t normal, const glm::ivec2 &texCoord, const uint32_t texLayer, const uint8_t ao, const uint8_t light)
Definition: CreateMeshData.hpp:49
static VkVertexInputBindingDescription getBindingDescription()
Definition: CreateMeshData.hpp:76
static constexpr uint32_t getMaxTextureCoord()
Definition: CreateMeshData.hpp:71
uint64_t data[2]
Definition: CreateMeshData.hpp:47
static std::vector< VkVertexInputAttributeDescription > getAttributeDescriptions()
Definition: CreateMeshData.hpp:86