This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block.h
103 lines (83 loc) · 2.48 KB
/
Block.h
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
#ifndef BLOCK_H
#define BLOCK_H
#include <SGL/Math/Vector2.h>
#include <SGL/Math/Vector3.h>
#include <SGL/Graphics/Color.h>
#include <cstdint>
#define R_MASK 0x000F // mask for light R channel
#define G_MASK 0x00F0 // mask for light G channel
#define B_MASK 0x0F00 // mask for light B channel
#define CHNL_MASK 0x000F // mask for a single channel of light
#define CHNL_BITS 4 // bits per channel
// get the R channel value
#define GET_LIGHT_LEVEL_R(light) ((light & R_MASK))
// get the G channel value
#define GET_LIGHT_LEVEL_G(light) ((light & G_MASK) >> CHNL_BITS )
// get the B channel value
#define GET_LIGHT_LEVEL_B(light) ((light & B_MASK) >> (CHNL_BITS * 2) )
// set the R channel value
#define SET_LIGHT_LEVEL_R(light, level) (light) = ( ( (light) & ~R_MASK ) | ((level) & CHNL_MASK) )
// get the G channel value
#define SET_LIGHT_LEVEL_G(light, level) (light) = ( ( (light) & ~G_MASK ) | ((level) & CHNL_MASK) << (CHNL_BITS) )
// get the B channel value
#define SET_LIGHT_LEVEL_B(light, level) (light) = ( ( (light) & ~B_MASK ) | ((level) & CHNL_MASK) << (CHNL_BITS * 2) )
// light data type
typedef uint16_t light_t;
/**
A single vertex for mesh creation
Contains position, normal, texCoord and color attributes
*/
struct Vertex
{
Vertex(sgl::Vector3& pos, sgl::Vector3& n, sgl::ColorRGB32f c) : position(pos), normal(n), color(c)
{
}
Vertex(sgl::Vector3& pos, sgl::Vector3& n) : Vertex(pos, n, sgl::ColorRGB32f(1, 1, 1))
{
}
Vertex(sgl::Vector3& pos) : Vertex(pos, sgl::Vector3(0, 0, 0), sgl::ColorRGB32f(1,1,1))
{
}
sgl::Vector3 position;
sgl::Vector3 normal;
sgl::Vector2 texCoord;
sgl::ColorRGB32f color;
};
struct Block
{
Block() : Block(0, 0, 0, 0)
{
}
Block(uint8_t t, uint8_t x, uint8_t y, uint8_t z) : t(t), x(x), y(y), z(z)
{
lights[0] = 0;
lights[1] = 0;
lights[2] = 0;
lights[3] = 0;
lights[4] = 0;
lights[5] = 0;
}
uint8_t t; // the block type
uint8_t x; // block x value in chunk
uint8_t y; // block y value in chunk
uint8_t z; // block z value in chunk
light_t lights[6]; // light values for each face
};
// block face number
enum class BlockFace
{
LEFT, RIGHT,
TOP, BOTTOM,
NEAR, FAR
};
// check if a block is opaque
static bool isBlockOpaque(Block& block)
{
return block.t != 0;
}
// check if the block has a light value
static bool hasLight(Block& block)
{
return (block.lights[0] | block.lights[1] | block.lights[2] | block.lights[3] | block.lights[4] | block.lights[5]) != 0;
}
#endif