-
Notifications
You must be signed in to change notification settings - Fork 14
/
chunk.h
118 lines (94 loc) · 4.75 KB
/
chunk.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef CHUNK_H
#define CHUNK_H
#include <cstdint>
#include <vector>
#include <tuple>
#include <zlib.h>
#include "gl.h"
#include "gldrawarray.h"
#include "terrain.h"
#include "aabb.h"
#include "particle.h"
class World;
class Chunk
{
public:
Chunk(int x, int y, int z);
void logic(bool ticks_enabled);
void render();
void setDirty(bool dirty = true) { render_dirty = dirty; }
bool isDirty() { return render_dirty; }
BLOCK_WDATA getLocalBlock(const int x, const int y, const int z) const;
void setLocalBlock(const int x, const int y, const int z, const BLOCK_WDATA block, bool set_dirty = true);
void changeLocalBlock(const int x, const int y, const int z, const BLOCK_WDATA block); //Calls removeBlock and addBlock
void changeGlobalBlockRelative(const int x, const int y, const int z, const BLOCK_WDATA block);
BLOCK_WDATA getGlobalBlockRelative(const int x, const int y, const int z) const;
void setGlobalBlockRelative(const int x, const int y, const int z, const BLOCK_WDATA block, bool set_dirty = true);
AABB &getAABB() { return aabb; }
bool intersects(AABB &other);
bool intersectsRay(GLFix x, GLFix y, GLFix z, GLFix dx, GLFix dy, GLFix dz, GLFix &dist, VECTOR3 &pos, AABB::SIDE &side, bool ignore_water);
void generate();
bool saveToFile(gzFile file);
bool loadFromFile(gzFile file);
//Redstone power: See wirerenderer.cpp for details
//Whether the block receives power from the specified side.
bool gettingPowerFrom(const int x, const int y, const int z, BLOCK_SIDE side, bool ignore_redstone_wire = false);
//Whether the block receives power directly or indirectly.
bool isBlockPowered(const int x, const int y, const int z, bool ignore_redstone_wire = false);
GLFix absX() { return abs_x; }
GLFix absY() { return abs_y; }
GLFix absZ() { return abs_z; }
//Used by BlockRenderers, had to make it public because friendship is not inheritable
void addAlignedVertex(const int x, const int y, const int z, GLFix u, GLFix v, const COLOR c);
//Same as addAlignedVertex, but terrain_quad is the bound texture
void addAlignedVertexQuad(const int x, const int y, const int z, GLFix u, GLFix v, const COLOR c);
//Same as addAlignedVertex, but with nglForceColor on
void addAlignedVertexForceColor(const int x, const int y, const int z, GLFix u, GLFix v, const COLOR c);
// For unaligned vertices: If set, the two triangles in the quad do backface culling independently.
// This is necessary when they're not on the same plane.
static constexpr COLOR INDEPENDENT_TRIS = 0x0001;
//Doesn't have to be aligned, terrain_current is bound
void addUnalignedVertex(const GLFix x, const GLFix y, const GLFix z, const GLFix u, const GLFix v, const COLOR c);
void addUnalignedVertex(const VERTEX &v);
//The callback is called on every frame
struct Animation {
GLFix x, y, z;
void (*animate)(GLFix x, GLFix y, GLFix z, Chunk &c);
};
void addAnimation(const Animation &animation);
// Add a particle. It lives until it removes itself in Particle::logic
void addParticle(const Particle &particle);
// Spawn particles for destroying the block at that offset
void spawnDestructionParticles(const int x, const int y, const int z);
//Don't render something twice
void setLocalBlockSideRendered(const int x, const int y, const int z, const BLOCK_SIDE_BITFIELD side);
bool isLocalBlockSideRendered(const int x, const int y, const int z, const BLOCK_SIDE_BITFIELD side);
static constexpr int SIZE = 8;
const int x, y, z;
private:
//Terrain generation
void makeTree(unsigned int x, unsigned int y, unsigned int z);
//Data
unsigned int getPosition(unsigned int x, unsigned int y, unsigned int z);
//Rendering
void geometrySpecialBlock(BLOCK_WDATA block, unsigned int x, unsigned int y, unsigned int z, BLOCK_SIDE side);
void buildGeometry();
//Data
const GLFix abs_x, abs_y, abs_z;
AABB aabb;
BLOCK_WDATA blocks[SIZE][SIZE][SIZE];
//Rendering
bool render_dirty = true;
static int pos_indices[SIZE + 1][SIZE + 1][SIZE + 1];
BLOCK_SIDE_BITFIELD sides_rendered[SIZE][SIZE][SIZE] = {}; //It could be that other chunks already rendered parts of our blocks
std::vector<VECTOR3> positions;
std::vector<ProcessedPosition> positions_processed;
std::vector<IndexedVertex> vertices, vertices_quad, vertices_color;
std::vector<VERTEX> vertices_unaligned; //The optimized drawing with indices doesn't work with unaligned positions
std::vector<Animation> animations;
std::vector<Particle> particles;
int tick_counter = 1; //1 to trigger a tick the next frame
};
//Doesn't really belong here, but still more than everywhere else
void drawLoadingtext(const int i);
#endif // CHUNK_H