1 #include <asprintf/asprintf.h>
2 #include <linmath.h/linmath.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"
19 ModelBatch *batch_transparent;
22 TerrainChunk *nbrs[6];
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
36 .size = sizeof(TerrainVertex),
39 static v3s32 face_dir[6] = {
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,
54 static GLuint shader_prog;
57 static LightShader light_shader;
58 static ModelShader model_shader;
60 static inline bool cull_face(NodeType self, NodeType nbr)
62 switch (client_node_definitions[self].visibility) {
66 case VISIBILITY_BLEND:
69 case VISIBILITY_SOLID:
70 return nbr == NODE_UNLOADED
71 || client_node_definitions[nbr].visibility == VISIBILITY_SOLID;
73 default: // impossible
81 static inline void render_node(ChunkRenderData *data, v3s32 offset)
85 args.node = &data->chunk->data[offset.x][offset.y][offset.z];
87 ClientNodeDefinition *def = &client_node_definitions[args.node->type];
88 if (def->visibility == VISIBILITY_NONE)
91 v3f32 vertex_offset = v3f32_sub(v3s32_to_f32(offset), center_offset);
93 args.pos = v3s32_add(offset, data->chunkp);
95 for (args.f = 0; args.f < 6; args.f++) {
96 v3s32 nbr_offset = v3s32_add(offset, face_dir[args.f]);
98 TerrainChunk *nbr_chunk;
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;
110 NodeType nbr_node = NODE_UNLOADED;
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;
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)
122 data->visible = true;
125 ModelBatch *batch = data->batch;
127 if (def->visibility == VISIBILITY_BLEND) {
128 data->transparent = true;
129 batch = data->batch_transparent;
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};
140 model_batch_add_vertex(batch, def->tiles.textures[args.f]->txo, &args.vertex);
145 static void animate_chunk_model(Model *model, f64 dtime)
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);
153 = model->root->scale.y
154 = model->root->scale.x;
156 model_node_transform(model->root);
159 static Model *create_chunk_model(TerrainChunk *chunk, bool animate)
161 ChunkRenderData data = {
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)),
167 .chunkp = v3s32_scale(chunk->pos, CHUNK_SIZE),
169 .tried_nbrs = {false},
170 .cull_edges = !animate,
174 render_node(&data, (v3s32) {x, y, z});
176 if (!data.batch->textures.siz && !data.batch_transparent->textures.siz) {
177 model_batch_free(data.batch);
178 model_batch_free(data.batch_transparent);
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;
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};
197 model_node_add_batch(model->root, data.batch);
198 model_node_add_batch(model->root, data.batch_transparent);
200 model_batch_free(data.batch);
201 model_batch_free(data.batch_transparent);
207 bool terrain_gfx_init()
210 glGetIntegerv(GL_MAX_TEXTURE_UNITS, &texture_units); GL_DEBUG
213 asprintf(&shader_defs,
214 "#define MAX_TEXTURE_UNITS %d\n"
215 "#define VIEW_DISTANCE %lf\n",
217 client_config.view_distance
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");
227 loc_VP = glGetUniformLocation(shader_prog, "VP"); GL_DEBUG
229 GLint texture_indices[texture_units];
230 for (GLint i = 0; i < texture_units; i++)
231 texture_indices[i] = i;
233 glProgramUniform1iv(shader_prog, glGetUniformLocation(shader_prog, "textures"), texture_units, texture_indices); GL_DEBUG
235 model_shader.prog = shader_prog;
236 model_shader.loc_transform = glGetUniformLocation(shader_prog, "model"); GL_DEBUG
238 light_shader.prog = shader_prog;
239 light_shader_locate(&light_shader);
244 void terrain_gfx_deinit()
246 glDeleteProgram(shader_prog); GL_DEBUG
249 void terrain_gfx_update()
251 glProgramUniformMatrix4fv(shader_prog, loc_VP, 1, GL_FALSE, frustum[0]); GL_DEBUG
252 light_shader_update(&light_shader);
255 void terrain_gfx_make_chunk_model(TerrainChunk *chunk)
257 TerrainChunkMeta *meta = chunk->extra;
261 pthread_mutex_lock(&chunk->mtx);
262 if (meta->model && meta->model->root->scale.x == 1.0f)
264 pthread_mutex_unlock(&chunk->mtx);
266 Model *model = create_chunk_model(chunk, animate);
268 pthread_mutex_lock(&chunk->mtx);
272 model->callbacks.step = meta->model->callbacks.step;
273 model->root->scale = meta->model->root->scale;
274 model_node_transform(model->root);
277 meta->model->replace = model;
278 meta->model->flags.delete = 1;
280 model_scene_add(model);
284 pthread_mutex_unlock(&chunk->mtx);