3 #include "client/camera.h"
4 #include "client/client.h"
5 #include "client/client_item.h"
6 #include "client/client_node.h"
7 #include "client/client_player.h"
8 #include "client/cube.h"
9 #include "client/debug_menu.h"
10 #include "client/frustum.h"
11 #include "client/opengl.h"
12 #include "client/gui.h"
13 #include "client/interact.h"
14 #include "client/mesh.h"
15 #include "client/raycast.h"
16 #include "client/shader.h"
18 struct InteractPointed interact_pointed;
20 static GLuint shader_prog;
22 static GLint loc_color;
27 } __attribute__((packed)) SelectionVertex;
28 static Mesh selection_mesh = {
29 .layout = &(VertexLayout) {
30 .attributes = (VertexAttribute[]) {
31 {GL_FLOAT, 3, sizeof(v3f32)},
34 .size = sizeof(SelectionVertex),
45 shader_prog = shader_program_create(ASSET_PATH "shaders/3d/selection", NULL);
46 loc_MVP = glGetUniformLocation(shader_prog, "MVP"); GL_DEBUG
47 loc_color = glGetUniformLocation(shader_prog, "color"); GL_DEBUG
49 SelectionVertex vertices[6][6];
50 for (int f = 0; f < 6; f++)
51 for (int v = 0; v < 6; v++)
52 vertices[f][v].position = v3f32_scale(cube_vertices[f][v].position, 1.1f);
54 selection_mesh.data = vertices;
55 mesh_upload(&selection_mesh);
57 gui_add(NULL, (GUIElementDef) {
62 .align = {0.5f, 0.5f},
63 .scale = {1.0f, 1.0f},
64 .scale_type = SCALE_IMAGE,
65 .affect_parent_scale = false,
67 .image = texture_load(ASSET_PATH "textures/crosshair.png", false),
68 .text_color = {0.0f, 0.0f, 0.0f, 0.0f},
69 .bg_color = {0.0f, 0.0f, 0.0f, 0.0f},
73 void interact_deinit()
75 glDeleteProgram(shader_prog); GL_DEBUG
76 mesh_destroy(&selection_mesh);
79 #include "client/client_terrain.h"
83 bool old_exists = interact_pointed.exists;
84 v3s32 old_pointed = interact_pointed.pos;
85 if ((interact_pointed.exists = raycast(
86 (v3f64) {camera.eye [0], camera.eye [1], camera.eye [2]},
87 (v3f64) {camera.front[0], camera.front[1], camera.front[2]},
88 5, &interact_pointed.pos, &interact_pointed.node))
89 && !v3s32_equals(interact_pointed.pos, old_pointed)) {
90 mat4x4_translate(model,
91 interact_pointed.pos.x, interact_pointed.pos.y, interact_pointed.pos.z);
92 v3f32 *color = &client_node_def[interact_pointed.node].selection_color;
93 glProgramUniform3f(shader_prog, loc_color, color->x, color->y, color->z); GL_DEBUG
94 debug_menu_changed(ENTRY_POINTED);
97 if (old_exists && !interact_pointed.exists)
98 debug_menu_changed(ENTRY_POINTED);
101 void interact_render()
103 if (!interact_pointed.exists)
107 mat4x4_mul(mvp, frustum, model);
109 glUseProgram(shader_prog); GL_DEBUG
110 glUniformMatrix4fv(loc_MVP, 1, GL_FALSE, mvp[0]); GL_DEBUG
111 mesh_render(&selection_mesh);
114 void interact_use(bool left)
116 ClientEntity *entity = client_player_entity_local();
120 ClientPlayerData *data = entity->extra;
121 pthread_mutex_lock(&data->mtx_inv);
123 ItemStack *stack = left ? &data->inventory.left : &data->inventory.right;
124 if (client_item_def[stack->type].use && client_item_def[stack->type].use(stack))
125 dragonnet_peer_send_ToServerInteract(client, &(ToServerInteract) {
127 .pointed = interact_pointed.exists,
128 .pos = interact_pointed.pos,
131 pthread_mutex_unlock(&data->mtx_inv);
132 refcount_drp(&entity->rc);