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"
21 TerrainChunk *nbrs[6];
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
35 .size = sizeof(TerrainVertex),
38 static v3s32 face_dir[6] = {
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,
53 static GLuint shader_prog;
56 static LightShader light_shader;
57 static ModelShader model_shader;
59 static inline bool cull_face(NodeType self, NodeType nbr)
61 switch (client_node_definitions[self].visibility) {
65 case VISIBILITY_BLEND:
68 case VISIBILITY_SOLID:
69 return nbr == NODE_UNLOADED
70 || client_node_definitions[nbr].visibility == VISIBILITY_SOLID;
72 default: // impossible
80 static inline void render_node(ChunkRenderData *data, v3s32 offset)
84 args.node = &data->chunk->data[offset.x][offset.y][offset.z];
86 ClientNodeDefinition *def = &client_node_definitions[args.node->type];
87 if (def->visibility == VISIBILITY_NONE)
90 v3f32 vertex_offset = v3f32_sub(v3s32_to_f32(offset), center_offset);
92 args.pos = v3s32_add(offset, data->chunkp);
94 for (args.f = 0; args.f < 6; args.f++) {
95 v3s32 nbr_offset = v3s32_add(offset, face_dir[args.f]);
97 TerrainChunk *nbr_chunk;
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;
109 NodeType nbr_node = NODE_UNLOADED;
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;
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)
121 data->visible = true;
124 if (def->visibility == VISIBILITY_BLEND)
125 data->transparent = true;
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};
135 model_batch_add_vertex(data->batch, def->tiles.textures[args.f]->txo, &args.vertex);
140 static void animate_chunk_model(Model *model, f64 dtime)
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);
148 = model->root->scale.y
149 = model->root->scale.x;
151 model_node_transform(model->root);
154 static Model *create_chunk_model(TerrainChunk *chunk, bool animate)
156 ChunkRenderData data = {
158 .transparent = false,
159 .batch = model_batch_create(&model_shader, &terrain_vertex_layout, offsetof(TerrainVertex, textureIndex)),
161 .chunkp = v3s32_scale(chunk->pos, CHUNK_SIZE),
163 .tried_nbrs = {false},
164 .cull_edges = !animate,
168 render_node(&data, (v3s32) {x, y, z});
170 if (!data.batch->textures.siz) {
171 model_batch_free(data.batch);
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;
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};
190 model_node_add_batch(model->root, data.batch);
192 model_batch_free(data.batch);
197 bool terrain_gfx_init()
200 glGetIntegerv(GL_MAX_TEXTURE_UNITS, &texture_units); GL_DEBUG
203 asprintf(&shader_defs,
204 "#define MAX_TEXTURE_UNITS %d\n"
205 "#define VIEW_DISTANCE %lf\n",
207 client_config.view_distance
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");
217 loc_VP = glGetUniformLocation(shader_prog, "VP"); GL_DEBUG
219 GLint texture_indices[texture_units];
220 for (GLint i = 0; i < texture_units; i++)
221 texture_indices[i] = i;
223 glProgramUniform1iv(shader_prog, glGetUniformLocation(shader_prog, "textures"), texture_units, texture_indices); GL_DEBUG
225 model_shader.prog = shader_prog;
226 model_shader.loc_transform = glGetUniformLocation(shader_prog, "model"); GL_DEBUG
228 light_shader.prog = shader_prog;
229 light_shader_locate(&light_shader);
234 void terrain_gfx_deinit()
236 glDeleteProgram(shader_prog); GL_DEBUG
239 void terrain_gfx_update()
241 glProgramUniformMatrix4fv(shader_prog, loc_VP, 1, GL_FALSE, frustum[0]); GL_DEBUG
242 light_shader_update(&light_shader);
245 void terrain_gfx_make_chunk_model(TerrainChunk *chunk)
247 TerrainChunkMeta *meta = chunk->extra;
251 pthread_mutex_lock(&chunk->mtx);
252 if (meta->model && meta->model->root->scale.x == 1.0f)
254 pthread_mutex_unlock(&chunk->mtx);
256 Model *model = create_chunk_model(chunk, animate);
258 pthread_mutex_lock(&chunk->mtx);
262 model->callbacks.step = meta->model->callbacks.step;
263 model->root->scale = meta->model->root->scale;
264 model_node_transform(model->root);
267 meta->model->replace = model;
268 meta->model->flags.delete = 1;
270 model_scene_add(model);
274 pthread_mutex_unlock(&chunk->mtx);