2 #include <dragonstd/map.h>
6 #include "client/cube.h"
7 #include "client/client_config.h"
8 #include "client/client_entity.h"
9 #include "client/client_player.h"
10 #include "client/frustum.h"
11 #include "client/gl_debug.h"
12 #include "client/light.h"
13 #include "client/shader.h"
14 #include "client/window.h"
16 ClientEntityType client_entity_types[COUNT_ENTITY];
18 ModelShader client_entity_shader;
22 } __attribute__((packed)) EntityVertex;
23 Mesh client_entity_cube = {
24 .layout = &(VertexLayout) {
25 .attributes = (VertexAttribute[]) {
26 {GL_FLOAT, 3, sizeof(v3f32)}, // position
27 {GL_FLOAT, 3, sizeof(v3f32)}, // normal
30 .size = sizeof(EntityVertex),
39 static GLuint shader_prog;
41 static GLint loc_depthOffset;
42 static LightShader light_shader;
44 static List nametagged;
45 static pthread_mutex_t mtx_nametagged;
48 // called when adding, getting or removing an entity from the map
49 static int cmp_entity(const Refcount *entity, const u64 *id)
51 return u64_cmp(&((ClientEntity *) entity->obj)->data.id, id);
55 // called when server sent removal of entity
56 static void entity_drop(ClientEntity *entity)
58 if (entity->type->remove)
59 entity->type->remove(entity);
61 if (entity->nametag) {
62 pthread_mutex_lock(&mtx_nametagged);
63 list_del(&nametagged, &entity->rc, &cmp_ref, &refcount_drp, NULL, NULL);
64 pthread_mutex_unlock(&mtx_nametagged);
66 entity->nametag->visible = false;
69 refcount_drp(&entity->rc);
73 // called when all refs have been dropped
74 static void entity_delete(ClientEntity *entity)
76 if (entity->type->free)
77 entity->type->free(entity);
79 refcount_dst(&entity->rc);
81 if (entity->data.nametag)
82 free(entity->data.nametag);
84 pthread_rwlock_init(&entity->lock_pos_rot, NULL);
85 pthread_rwlock_init(&entity->lock_nametag, NULL);
86 pthread_rwlock_init(&entity->lock_box_off, NULL);
91 static void update_nametag(ClientEntity *entity)
93 if (entity->nametag) {
94 gui_text(entity->nametag, entity->data.nametag);
96 if (!entity->data.nametag)
97 entity->nametag->visible = false;
98 } else if (entity->data.nametag) {
99 entity->nametag = gui_add(NULL, (GUIElementDef) {
100 .pos = {-1.0f, -1.0f},
104 .align = {0.5f, 0.5f},
105 .scale = {1.0f, 1.0f},
106 .scale_type = SCALE_TEXT,
107 .affect_parent_scale = false,
108 .text = entity->data.nametag,
110 .text_color = (v4f32) {1.0f, 1.0f, 1.0f, 1.0f},
111 .bg_color = (v4f32) {0.0f, 0.0f, 0.0f, 0.5f},
114 pthread_mutex_lock(&mtx_nametagged);
115 list_apd(&nametagged, refcount_inc(&entity->rc));
116 pthread_mutex_unlock(&mtx_nametagged);
120 static void update_nametag_pos(ClientEntity *entity)
122 if (!entity->data.nametag)
125 pthread_rwlock_rdlock(&entity->lock_pos_rot);
126 pthread_rwlock_rdlock(&entity->lock_box_off);
129 if (entity->nametag_offset)
130 mat4x4_mul(mvp, frustum, *entity->nametag_offset);
132 mat4x4_dup(mvp, frustum);
134 vec4 dst, src = {0.0f, 0.0f, 0.0f, 1.0f};
135 mat4x4_mul_vec4(dst, mvp, src);
141 if ((entity->nametag->visible = dst[2] >= -1.0f && dst[2] <= 1.0f)) {
142 entity->nametag->def.pos = (v2f32) {dst[0] * 0.5f + 0.5f, 1.0f - (dst[1] * 0.5f + 0.5f)};
143 gui_transform(entity->nametag);
146 pthread_rwlock_unlock(&entity->lock_box_off);
147 pthread_rwlock_unlock(&entity->lock_pos_rot);
152 void client_entity_init()
155 list_ini(&nametagged);
156 pthread_mutex_init(&mtx_nametagged, NULL);
160 // called on shutdown
161 void client_entity_deinit()
163 // forget all entities
164 map_cnl(&entities, &refcount_drp, NULL, NULL, 0);
165 list_clr(&nametagged, &refcount_drp, NULL, NULL);
166 pthread_mutex_destroy(&mtx_nametagged);
169 void client_entity_gfx_init()
172 asprintf(&shader_def, "#define VIEW_DISTANCE %lf\n", client_config.view_distance);
173 shader_prog = shader_program_create(ASSET_PATH "shaders/3d/entity", shader_def);
176 loc_VP = glGetUniformLocation(shader_prog, "VP"); GL_DEBUG
177 loc_depthOffset = glGetUniformLocation(shader_prog, "depthOffset"); GL_DEBUG
179 EntityVertex vertices[6][6];
180 for (int f = 0; f < 6; f++) {
181 for (int v = 0; v < 6; v++) {
182 vertices[f][v].position = cube_vertices[f][v].position;
183 vertices[f][v].normal = cube_vertices[f][v].normal;
187 client_entity_cube.data = vertices;
188 mesh_upload(&client_entity_cube);
190 client_entity_shader.prog = shader_prog;
191 client_entity_shader.loc_transform = glGetUniformLocation(shader_prog, "model"); GL_DEBUG
193 light_shader.prog = shader_prog;
194 light_shader_locate(&light_shader);
196 client_entity_depth_offset(0.0f);
199 void client_entity_gfx_deinit()
201 glDeleteProgram(shader_prog); GL_DEBUG
202 mesh_destroy(&client_entity_cube);
205 void client_entity_gfx_update()
207 glProgramUniformMatrix4fv(shader_prog, loc_VP, 1, GL_FALSE, frustum[0]); GL_DEBUG
208 light_shader_update(&light_shader);
210 pthread_mutex_lock(&mtx_nametagged);
211 list_itr(&nametagged, &update_nametag_pos, NULL, &refcount_obj);
212 pthread_mutex_unlock(&mtx_nametagged);
215 void client_entity_depth_offset(f32 offset)
217 glProgramUniform1f(shader_prog, loc_depthOffset, offset);
220 ClientEntity *client_entity_grab(u64 id)
222 return id ? map_get(&entities, &id, &cmp_entity, &refcount_grb) : NULL;
225 void client_entity_transform(ClientEntity *entity)
230 entity->model->root->pos = v3f64_to_f32(entity->data.pos); // ToDo: the render pipeline needs to be updated to handle 64-bit positions
231 entity->model->root->rot = entity->data.rot;
233 if (entity->type->transform)
234 entity->type->transform(entity);
236 model_node_transform(entity->model->root);
239 void client_entity_add(__attribute__((unused)) void *peer, ToClientEntityAdd *pkt)
241 if (pkt->type >= COUNT_ENTITY)
244 ClientEntity *entity = malloc(sizeof *entity);
246 entity->data = pkt->data;
247 entity->type = &client_entity_types[pkt->type];
248 refcount_ini(&entity->rc, entity, &entity_delete);
250 pkt->data.nametag = NULL;
252 entity->model = NULL;
253 entity->nametag = NULL;
255 if (entity->type->add)
256 entity->type->add(entity);
258 update_nametag(entity);
260 pthread_rwlock_init(&entity->lock_pos_rot, NULL);
261 pthread_rwlock_init(&entity->lock_nametag, NULL);
262 pthread_rwlock_init(&entity->lock_box_off, NULL);
264 if (!map_add(&entities, &entity->data.id, &entity->rc, &cmp_entity, &refcount_inc))
265 fprintf(stderr, "[warning] failed to add entity %" PRIu64 "\n", entity->data.id);
267 refcount_drp(&entity->rc);
270 void client_entity_remove(__attribute__((unused)) void *peer, ToClientEntityRemove *pkt)
272 map_del(&entities, &pkt->id, &cmp_entity, &entity_drop, NULL, &refcount_obj);
275 void client_entity_update_pos_rot(__attribute__((unused)) void *peer, ToClientEntityUpdatePosRot *pkt)
277 ClientEntity *entity = client_entity_grab(pkt->id);
282 pthread_rwlock_wrlock(&entity->lock_pos_rot);
284 entity->data.pos = pkt->pos;
285 entity->data.rot = pkt->rot;
287 if (entity->type->update_pos_rot)
288 entity->type->update_pos_rot(entity);
290 client_entity_transform(entity);
292 pthread_rwlock_unlock(&entity->lock_pos_rot);
294 refcount_drp(&entity->rc);
297 void client_entity_update_nametag(__attribute__((unused)) void *peer, ToClientEntityUpdateNametag *pkt)
299 ClientEntity *entity = client_entity_grab(pkt->id);
304 pthread_rwlock_wrlock(&entity->lock_nametag);
306 if (entity->data.nametag)
307 free(entity->data.nametag);
309 entity->data.nametag = pkt->nametag;
312 if (entity->type->update_nametag)
313 entity->type->update_nametag(entity);
315 update_nametag(entity);
316 pthread_rwlock_unlock(&entity->lock_nametag);
318 refcount_drp(&entity->rc);