1 #include <asprintf/asprintf.h>
2 #include <dragonstd/map.h>
5 #include "client/cube.h"
6 #include "client/client_config.h"
7 #include "client/client_entity.h"
8 #include "client/client_player.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/window.h"
15 ClientEntityType client_entity_types[COUNT_ENTITY];
17 ModelShader client_entity_shader;
21 } __attribute__((packed)) EntityVertex;
22 Mesh client_entity_cube = {
23 .layout = &(VertexLayout) {
24 .attributes = (VertexAttribute[]) {
25 {GL_FLOAT, 3, sizeof(v3f32)}, // position
26 {GL_FLOAT, 3, sizeof(v3f32)}, // normal
29 .size = sizeof(EntityVertex),
38 static GLuint shader_prog;
40 static LightShader light_shader;
42 static List nametagged;
43 static pthread_mutex_t mtx_nametagged;
46 // called when adding, getting or removing an entity from the map
47 static int cmp_entity(const Refcount *entity, const u64 *id)
49 return u64_cmp(&((ClientEntity *) entity->obj)->data.id, id);
53 // called when server sent removal of entity
54 static void entity_drop(ClientEntity *entity)
56 if (entity->type->remove)
57 entity->type->remove(entity);
59 if (entity->nametag) {
60 pthread_mutex_lock(&mtx_nametagged);
61 list_del(&nametagged, &entity->rc, &cmp_ref, &refcount_drp, NULL, NULL);
62 pthread_mutex_unlock(&mtx_nametagged);
64 entity->nametag->visible = false;
67 refcount_drp(&entity->rc);
71 // called when all refs have been dropped
72 static void entity_delete(ClientEntity *entity)
74 if (entity->type->free)
75 entity->type->free(entity);
77 refcount_dst(&entity->rc);
79 if (entity->data.nametag)
80 free(entity->data.nametag);
82 pthread_rwlock_init(&entity->lock_pos_rot, NULL);
83 pthread_rwlock_init(&entity->lock_nametag, NULL);
84 pthread_rwlock_init(&entity->lock_box_off, NULL);
89 static void update_nametag(ClientEntity *entity)
91 if (entity->nametag) {
92 gui_text(entity->nametag, entity->data.nametag);
94 if (!entity->data.nametag)
95 entity->nametag->visible = false;
96 } else if (entity->data.nametag) {
97 entity->nametag = gui_add(NULL, (GUIElementDefinition) {
98 .pos = {-1.0f, -1.0f},
102 .align = {0.5f, 0.5f},
103 .scale = {1.0f, 1.0f},
104 .scale_type = SCALE_TEXT,
105 .affect_parent_scale = false,
106 .text = entity->data.nametag,
108 .text_color = (v4f32) {1.0f, 1.0f, 1.0f, 1.0f},
109 .bg_color = (v4f32) {0.0f, 0.0f, 0.0f, 0.5f},
112 pthread_mutex_lock(&mtx_nametagged);
113 list_apd(&nametagged, refcount_inc(&entity->rc));
114 pthread_mutex_unlock(&mtx_nametagged);
118 static void update_nametag_pos(ClientEntity *entity)
120 if (!entity->data.nametag)
123 pthread_rwlock_rdlock(&entity->lock_pos_rot);
124 pthread_rwlock_rdlock(&entity->lock_box_off);
127 if (entity->nametag_offset)
128 mat4x4_mul(mvp, frustum, *entity->nametag_offset);
130 mat4x4_dup(mvp, frustum);
132 vec4 dst, src = {0.0f, 0.0f, 0.0f, 1.0f};
133 mat4x4_mul_vec4(dst, mvp, src);
139 if ((entity->nametag->visible = dst[2] >= -1.0f && dst[2] <= 1.0f)) {
140 entity->nametag->def.pos = (v2f32) {dst[0] * 0.5f + 0.5f, 1.0f - (dst[1] * 0.5f + 0.5f)};
141 gui_transform(entity->nametag);
144 pthread_rwlock_unlock(&entity->lock_box_off);
145 pthread_rwlock_unlock(&entity->lock_pos_rot);
150 void client_entity_init()
153 list_ini(&nametagged);
154 pthread_mutex_init(&mtx_nametagged, NULL);
158 // called on shutdown
159 void client_entity_deinit()
161 // forget all entities
162 map_cnl(&entities, &refcount_drp, NULL, NULL, 0);
163 list_clr(&nametagged, &refcount_drp, NULL, NULL);
164 pthread_mutex_destroy(&mtx_nametagged);
167 bool client_entity_gfx_init()
170 asprintf(&shader_defs, "#define VIEW_DISTANCE %lf\n", client_config.view_distance);
172 if (!shader_program_create(RESSOURCE_PATH "shaders/3d/entity", &shader_prog, shader_defs)) {
173 fprintf(stderr, "[error] failed to create entity shader program\n");
179 loc_VP = glGetUniformLocation(shader_prog, "VP"); GL_DEBUG
181 EntityVertex vertices[6][6];
182 for (int f = 0; f < 6; f++) {
183 for (int v = 0; v < 6; v++) {
184 vertices[f][v].position = cube_vertices[f][v].position;
185 vertices[f][v].normal = cube_vertices[f][v].normal;
189 client_entity_cube.data = vertices;
190 mesh_upload(&client_entity_cube);
192 client_entity_shader.prog = shader_prog;
193 client_entity_shader.loc_transform = glGetUniformLocation(shader_prog, "model"); GL_DEBUG
195 light_shader.prog = shader_prog;
196 light_shader_locate(&light_shader);
201 void client_entity_gfx_deinit()
203 glDeleteProgram(shader_prog); GL_DEBUG
204 mesh_destroy(&client_entity_cube);
207 void client_entity_gfx_update()
209 glProgramUniformMatrix4fv(shader_prog, loc_VP, 1, GL_FALSE, frustum[0]); GL_DEBUG
210 light_shader_update(&light_shader);
212 pthread_mutex_lock(&mtx_nametagged);
213 list_itr(&nametagged, &update_nametag_pos, NULL, &refcount_obj);
214 pthread_mutex_unlock(&mtx_nametagged);
217 ClientEntity *client_entity_grab(u64 id)
219 return id ? map_get(&entities, &id, &cmp_entity, &refcount_grb) : NULL;
222 void client_entity_transform(ClientEntity *entity)
227 entity->model->root->pos = v3f64_to_f32(entity->data.pos); // ToDo: the render pipeline needs to be updated to handle 64-bit positions
228 entity->model->root->rot = entity->data.rot;
230 if (entity->type->transform)
231 entity->type->transform(entity);
233 model_node_transform(entity->model->root);
236 void client_entity_add(__attribute__((unused)) DragonnetPeer *peer, ToClientEntityAdd *pkt)
238 if (pkt->type >= COUNT_ENTITY)
241 ClientEntity *entity = malloc(sizeof *entity);
243 entity->data = pkt->data;
244 entity->type = &client_entity_types[pkt->type];
245 refcount_ini(&entity->rc, entity, &entity_delete);
247 pkt->data.nametag = NULL;
249 entity->model = NULL;
250 entity->nametag = NULL;
252 if (entity->type->add)
253 entity->type->add(entity);
255 update_nametag(entity);
257 pthread_rwlock_init(&entity->lock_pos_rot, NULL);
258 pthread_rwlock_init(&entity->lock_nametag, NULL);
259 pthread_rwlock_init(&entity->lock_box_off, NULL);
261 if (!map_add(&entities, &entity->data.id, &entity->rc, &cmp_entity, &refcount_inc))
262 fprintf(stderr, "[warning] failed to add entity %lu\n", entity->data.id);
264 refcount_drp(&entity->rc);
267 void client_entity_remove(__attribute__((unused)) DragonnetPeer *peer, ToClientEntityRemove *pkt)
269 map_del(&entities, &pkt->id, &cmp_entity, &entity_drop, NULL, &refcount_obj);
272 void client_entity_update_pos_rot(__attribute__((unused)) DragonnetPeer *peer, ToClientEntityUpdatePosRot *pkt)
274 ClientEntity *entity = client_entity_grab(pkt->id);
279 pthread_rwlock_wrlock(&entity->lock_pos_rot);
281 entity->data.pos = pkt->pos;
282 entity->data.rot = pkt->rot;
284 if (entity->type->update_pos_rot)
285 entity->type->update_pos_rot(entity);
287 client_entity_transform(entity);
289 pthread_rwlock_unlock(&entity->lock_pos_rot);
291 refcount_drp(&entity->rc);
294 void client_entity_update_nametag(__attribute__((unused)) DragonnetPeer *peer, ToClientEntityUpdateNametag *pkt)
296 ClientEntity *entity = client_entity_grab(pkt->id);
301 pthread_rwlock_wrlock(&entity->lock_nametag);
303 if (entity->data.nametag)
304 free(entity->data.nametag);
306 entity->data.nametag = pkt->nametag;
309 if (entity->type->update_nametag)
310 entity->type->update_nametag(entity);
312 update_nametag(entity);
313 pthread_rwlock_unlock(&entity->lock_nametag);
315 refcount_drp(&entity->rc);