]> git.lizzy.rs Git - dragonblocks3d.git/blob - src/dragonblocks/chunk.cpp
Frustum & Backface Culling
[dragonblocks3d.git] / src / dragonblocks / chunk.cpp
1 #include <stdexcept>
2 #include "block_def.hpp" 
3 #include "chunk.hpp" 
4 #include "cube.hpp"
5 #include "face_dir.hpp" 
6 #include "log.hpp" 
7 #include "map.hpp" 
8 #include "mesh.hpp" 
9 #include "texture.hpp" 
10
11 #define SIZE DRAGONBLOCKS_CHUNK_SIZE
12
13 using namespace std;
14 using namespace glm;
15 using namespace dragonblocks;
16
17 void Chunk::checkPos(const ivec3 &pos)
18 {
19         if (pos.x < 0 || pos.y < 0 || pos.z < 0 || pos.x >= SIZE || pos.y >= SIZE || pos.z >= SIZE)
20                 throw out_of_range("Block position out of range");
21 }
22
23 const Block *Chunk::getBlock(const ivec3 &pos) const
24 {
25         Chunk::checkPos(pos);
26         return &data.blocks[pos.x][pos.y][pos.z];       
27 }
28
29 const Block *Chunk::getBlockNoEx(const ivec3 &pos) const
30 {
31         try {
32                 return getBlock(pos);
33         } catch (out_of_range &) {
34                 return nullptr;
35         }
36 }
37
38 void Chunk::setBlock(const ivec3 &pos, const Block &block)
39 {
40         Chunk::checkPos(pos);
41         data.blocks[pos.x][pos.y][pos.z] = block;
42 }
43
44 void Chunk::setBlockNoEx(const ivec3 &pos, const Block &block)
45 {
46         try {
47                 setBlock(pos, block);
48         } catch (out_of_range &) {
49         }
50 }
51
52 void Chunk::addMeshUpdateTask()
53 {
54         mesh_gen_mgr->addTask(this);
55 }
56
57 void Chunk::addMeshUpdateTaskWithEdge()
58 {
59         addMeshUpdateTask();
60         for (int i = 0; i < 6; i++) {
61                 if (Chunk *neighbor = map->getChunk(pos + face_dir[i])) {
62                         neighbor->addMeshUpdateTask();
63                 }
64         }
65 }
66
67 void Chunk::updateMesh()
68 {
69         log(string("Update Chunk Mesh at ") + to_string(pos.x) + " " + to_string(pos.y) +  " " + to_string(pos.z));
70                 
71         if (mesh_created && ! animation_finished)
72                 return;
73         
74         bool mesh_created_before = mesh_created;
75         mesh_created = true;
76         
77         vector<GLfloat> vertices;
78         vector<Texture> textures;
79         bool any_drawable_block = false;
80         
81         for (int x = 0; x < SIZE; x++) {
82                 for (int y = 0; y < SIZE; y++) {
83                         for (int z = 0; z < SIZE; z++) {
84                                 Block *block = &data.blocks[x][y][z];
85                                 BlockDef *def = block->getDef();
86                                 if (! def->drawable)
87                                         continue;
88                                 ivec3 bpos(x, y, z);
89                                 vec3 pos_from_mesh_origin = vec3(bpos) - vec3(SIZE / 2 + 0.5);
90                                 for (int facenr = 0; facenr < 6; facenr++) {
91                                         ivec3 npos = bpos + face_dir[facenr];
92                                         const Block *neighbor_own, *neighbor;
93                                         neighbor_own = neighbor = getBlockNoEx(npos);
94                                         if (! neighbor)
95                                                 neighbor = map->getBlock(pos * SIZE + npos);
96                                         if (neighbor && ! neighbor->getDef()->drawable)
97                                                 any_drawable_block = true;
98                                         if (! mesh_created_before)
99                                                 neighbor = neighbor_own;
100                                         if (! mesh_created_before && ! neighbor || neighbor && ! neighbor->getDef()->drawable) {
101                                                 textures.push_back(def->tiles[facenr]);
102                                                 for (int vertex_index = 0; vertex_index < 6; vertex_index++) {
103                                                         for (int attribute_index = 0; attribute_index < 5; attribute_index++) {
104                                                                 int index = facenr * 6 * 5 + vertex_index * 5 + attribute_index;
105                                                                 GLdouble value = cube[index];
106                                                                 switch (attribute_index) {
107                                                                         case 0:
108                                                                         value += pos_from_mesh_origin.x;
109                                                                         break;
110                                                                         case 1:
111                                                                         value += pos_from_mesh_origin.y;
112                                                                         break;
113                                                                         case 2:
114                                                                         value += pos_from_mesh_origin.z;
115                                                                         break;
116                                                                 }
117                                                                 vertices.push_back(value);
118                                                         }
119                                                 }
120                                         }
121                                 }
122                         }
123                 }
124         }
125         
126         if (! any_drawable_block) {
127                 if (! mesh_created_before) {
128                         afterAnimation();
129                 } else if (mesh) {
130                         delete mesh;
131                         mesh = nullptr;
132                 }
133                 return;
134         }
135         
136         Mesh *oldmesh = mesh;
137         
138         mesh = new Mesh(scene, &vertices[0], vertices.size() * sizeof(GLfloat));
139         mesh->pos = pos * SIZE + SIZE / 2;
140         mesh->minp = vec3(- SIZE / 2 - 1);
141         mesh->maxp = vec3(+ SIZE / 2 + 1);
142         mesh->textures = textures;
143         mesh->vertices_per_texture = 6;
144         if (! mesh_created_before) {
145                 mesh->animation = Mesh::Animation(Mesh::Animation::Type::FLYIN, Chunk::staticAfterAnimation, this);
146         }
147         
148         if (oldmesh) {
149                 oldmesh->die();
150         }
151 }
152
153 Chunk::Chunk(Map *m, const glm::ivec3 &p, const Data &d, MeshGenMgr *mg, Scene *s) : map(m), data(d), pos(p), mesh_gen_mgr(mg), scene(s)
154 {
155         addMeshUpdateTask();
156 }
157
158 Chunk::~Chunk()
159 {
160         if (mesh) {
161                 delete mesh;
162         }
163 }
164
165 void Chunk::staticAfterAnimation(void *chunk)
166 {
167         if (chunk) {
168                 ((Chunk *)chunk)->afterAnimation();
169         }
170 }
171
172 void Chunk::afterAnimation()
173 {
174         animation_finished = true;
175         addMeshUpdateTaskWithEdge();
176 }