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