]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/terrain_gfx.c
bf9fd986c856842d594e9ec1d182bde4158d12fd
[dragonblocks_alpha.git] / src / client / terrain_gfx.c
1 #include <asprintf/asprintf.h>
2 #include <linmath.h/linmath.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include "client/client_config.h"
6 #include "client/client_node.h"
7 #include "client/client_terrain.h"
8 #include "client/cube.h"
9 #include "client/frustum.h"
10 #include "client/gl_debug.h"
11 #include "client/light.h"
12 #include "client/shader.h"
13 #include "client/terrain_gfx.h"
14
15 typedef struct {
16         bool visible;
17         bool transparent;
18         ModelBatch *batch;
19         ModelBatch *batch_transparent;
20         TerrainChunk *chunk;
21         v3s32 chunkp;
22         TerrainChunk *nbrs[6];
23         bool tried_nbrs[6];
24         bool cull_edges;
25 } ChunkRenderData;
26
27 static VertexLayout terrain_vertex_layout = {
28         .attributes = (VertexAttribute []) {
29                 {GL_FLOAT, 3, sizeof(v3f32)}, // position
30                 {GL_FLOAT, 3, sizeof(v3f32)}, // normal
31                 {GL_FLOAT, 2, sizeof(v2f32)}, // textureCoordinates
32                 {GL_FLOAT, 1, sizeof(f32  )}, // textureIndex
33                 {GL_FLOAT, 3, sizeof(v3f32)}, // color
34         },
35         .count = 5,
36         .size = sizeof(TerrainVertex),
37 };
38
39 static v3s32 face_dir[6] = {
40         {+0, +0, -1},
41         {+0, +0, +1},
42         {-1, +0, +0},
43         {+1, +0, +0},
44         {+0, -1, +0},
45         {+0, +1, +0},
46 };
47
48 static v3f32 center_offset = {
49         CHUNK_SIZE * 0.5f + 0.5f,
50         CHUNK_SIZE * 0.5f + 0.5f,
51         CHUNK_SIZE * 0.5f + 0.5f,
52 };
53
54 static GLuint shader_prog;
55 static GLint loc_VP;
56
57 static LightShader light_shader;
58 static ModelShader model_shader;
59
60 static inline bool cull_face(NodeType self, NodeType nbr)
61 {
62         switch (client_node_definitions[self].visibility) {
63                 case VISIBILITY_CLIP:
64                         return false;
65
66                 case VISIBILITY_BLEND:
67                         return self == nbr;
68
69                 case VISIBILITY_SOLID:
70                         return nbr == NODE_UNLOADED
71                                 || client_node_definitions[nbr].visibility == VISIBILITY_SOLID;
72
73                 default: // impossible
74                         break;
75         }
76
77         // impossible
78         return false;
79 }
80
81 static inline void render_node(ChunkRenderData *data, v3s32 offset)
82 {
83         NodeArgsRender args;
84
85         args.node = &data->chunk->data[offset.x][offset.y][offset.z];
86
87         ClientNodeDefinition *def = &client_node_definitions[args.node->type];
88         if (def->visibility == VISIBILITY_NONE)
89                 return;
90
91         v3f32 vertex_offset = v3f32_sub(v3s32_to_f32(offset), center_offset);
92         if (def->render)
93                 args.pos = v3s32_add(offset, data->chunkp);
94
95         for (args.f = 0; args.f < 6; args.f++) {
96                 v3s32 nbr_offset = v3s32_add(offset, face_dir[args.f]);
97
98                 TerrainChunk *nbr_chunk;
99
100                 if (nbr_offset.z >= 0 && nbr_offset.z < CHUNK_SIZE &&
101                         nbr_offset.x >= 0 && nbr_offset.x < CHUNK_SIZE &&
102                         nbr_offset.y >= 0 && nbr_offset.y < CHUNK_SIZE) {
103                         nbr_chunk = data->chunk;
104                 } else if (!(nbr_chunk = data->nbrs[args.f]) && !data->tried_nbrs[args.f]) {
105                         nbr_chunk = data->nbrs[args.f] = terrain_get_chunk(client_terrain,
106                                 v3s32_add(data->chunk->pos, face_dir[args.f]), false);
107                         data->tried_nbrs[args.f] = true;
108                 }
109
110                 NodeType nbr_node = NODE_UNLOADED;
111                 if (nbr_chunk)
112                         nbr_node = nbr_chunk->data
113                                 [(nbr_offset.x + CHUNK_SIZE) % CHUNK_SIZE]
114                                 [(nbr_offset.y + CHUNK_SIZE) % CHUNK_SIZE]
115                                 [(nbr_offset.z + CHUNK_SIZE) % CHUNK_SIZE].type;
116
117                 if (cull_face(args.node->type, nbr_node)) {
118                         // exception to culling rules: don't cull solid edge nodes, unless cull_edges
119                         if (data->cull_edges || nbr_chunk == data->chunk || def->visibility != VISIBILITY_SOLID)
120                                 continue;
121                 } else {
122                         data->visible = true;
123                 }
124
125                 ModelBatch *batch = data->batch;
126
127                 if (def->visibility == VISIBILITY_BLEND) {
128                         data->transparent = true;
129                         batch = data->batch_transparent;
130                 }
131
132                 for (args.v = 0; args.v < 6; args.v++) {
133                         args.vertex.cube = cube_vertices[args.f][args.v];
134                         args.vertex.cube.position = v3f32_add(args.vertex.cube.position, vertex_offset);
135                         args.vertex.color = (v3f32) {1.0f, 1.0f, 1.0f};
136
137                         if (def->render)
138                                 def->render(&args);
139
140                         model_batch_add_vertex(batch, def->tiles.textures[args.f]->txo, &args.vertex);
141                 }
142         }
143 }
144
145 static void animate_chunk_model(Model *model, f64 dtime)
146 {
147         if ((model->root->scale.x += dtime * 2.0f) > 1.0f) {
148                 model->root->scale.x = 1.0f;
149                 client_terrain_meshgen_task(model->extra);
150         }
151
152         model->root->scale.z
153                 = model->root->scale.y
154                 = model->root->scale.x;
155
156         model_node_transform(model->root);
157 }
158
159 static Model *create_chunk_model(TerrainChunk *chunk, bool animate)
160 {
161         ChunkRenderData data = {
162                 .visible = false,
163                 .transparent = false,
164                 .batch = model_batch_create(&model_shader, &terrain_vertex_layout, offsetof(TerrainVertex, textureIndex)),
165                 .batch_transparent = model_batch_create(&model_shader, &terrain_vertex_layout, offsetof(TerrainVertex, textureIndex)),
166                 .chunk = chunk,
167                 .chunkp = v3s32_scale(chunk->pos, CHUNK_SIZE),
168                 .nbrs = {NULL},
169                 .tried_nbrs = {false},
170                 .cull_edges = !animate,
171         };
172
173         CHUNK_ITERATE
174                 render_node(&data, (v3s32) {x, y, z});
175
176         if (!data.batch->textures.siz && !data.batch_transparent->textures.siz) {
177                 model_batch_free(data.batch);
178                 model_batch_free(data.batch_transparent);
179                 return NULL;
180         }
181
182         Model *model = model_create();
183         model->extra = chunk;
184         model->box = (aabb3f32) {
185                 v3f32_sub((v3f32) {-1.0f, -1.0f, -1.0f}, center_offset),
186                 v3f32_add((v3f32) {+1.0f, +1.0f, +1.0f}, center_offset)};
187         model->callbacks.step = animate ? &animate_chunk_model : NULL;
188         model->callbacks.delete = &model_free_meshes;
189         model->flags.frustum_culling = 1;
190         model->flags.transparent = data.transparent;
191
192         model->root->visible = data.visible;
193         model->root->pos = v3f32_add(v3s32_to_f32(data.chunkp), center_offset);
194         model->root->scale = (v3f32) {0.0f, 0.0f, 0.0f};
195
196         if (data.visible) {
197                 model_node_add_batch(model->root, data.batch);
198                 model_node_add_batch(model->root, data.batch_transparent);
199         } else {
200                 model_batch_free(data.batch);
201                 model_batch_free(data.batch_transparent);
202         }
203
204         return model;
205 }
206
207 bool terrain_gfx_init()
208 {
209         GLint texture_units;
210         glGetIntegerv(GL_MAX_TEXTURE_UNITS, &texture_units); GL_DEBUG
211
212         char *shader_defs;
213         asprintf(&shader_defs,
214                 "#define MAX_TEXTURE_UNITS %d\n"
215                 "#define VIEW_DISTANCE %lf\n",
216                 texture_units,
217                 client_config.view_distance
218         );
219
220         if (!shader_program_create(RESSOURCE_PATH "shaders/3d/terrain", &shader_prog, shader_defs)) {
221                 fprintf(stderr, "[error] failed to create terrain shader program\n");
222                 return false;
223         }
224
225         free(shader_defs);
226
227         loc_VP = glGetUniformLocation(shader_prog, "VP"); GL_DEBUG
228
229         GLint texture_indices[texture_units];
230         for (GLint i = 0; i < texture_units; i++)
231                 texture_indices[i] = i;
232
233         glProgramUniform1iv(shader_prog, glGetUniformLocation(shader_prog, "textures"), texture_units, texture_indices); GL_DEBUG
234
235         model_shader.prog = shader_prog;
236         model_shader.loc_transform = glGetUniformLocation(shader_prog, "model"); GL_DEBUG
237
238         light_shader.prog = shader_prog;
239         light_shader_locate(&light_shader);
240
241         return true;
242 }
243
244 void terrain_gfx_deinit()
245 {
246         glDeleteProgram(shader_prog); GL_DEBUG
247 }
248
249 void terrain_gfx_update()
250 {
251         glProgramUniformMatrix4fv(shader_prog, loc_VP, 1, GL_FALSE, frustum[0]); GL_DEBUG
252         light_shader_update(&light_shader);
253 }
254
255 void terrain_gfx_make_chunk_model(TerrainChunk *chunk)
256 {
257         TerrainChunkMeta *meta = chunk->extra;
258
259         bool animate = true;
260
261         pthread_mutex_lock(&chunk->mtx);
262         if (meta->model && meta->model->root->scale.x == 1.0f)
263                 animate = false;
264         pthread_mutex_unlock(&chunk->mtx);
265
266         Model *model = create_chunk_model(chunk, animate);
267
268         pthread_mutex_lock(&chunk->mtx);
269
270         if (meta->model) {
271                 if (model) {
272                         model->callbacks.step = meta->model->callbacks.step;
273                         model->root->scale = meta->model->root->scale;
274                         model_node_transform(model->root);
275                 }
276
277                 meta->model->replace = model;
278                 meta->model->flags.delete = 1;
279         } else if (model) {
280                 model_scene_add(model);
281         }
282
283         meta->model = model;
284         pthread_mutex_unlock(&chunk->mtx);
285 }