]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/interact.c
Fix client terrain crashes and performance
[dragonblocks_alpha.git] / src / client / interact.c
1 #include <linmath.h/linmath.h>
2 #include <stdio.h>
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/gl_debug.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"
17
18 struct InteractPointed interact_pointed;
19
20 static GLuint shader_prog;
21 static GLint loc_MVP;
22 static GLint loc_color;
23 static mat4x4 model;
24
25 typedef struct {
26         v3f32 position;
27 } __attribute__((packed)) SelectionVertex;
28 static Mesh selection_mesh = {
29         .layout = &(VertexLayout) {
30                 .attributes = (VertexAttribute[]) {
31                         {GL_FLOAT, 3, sizeof(v3f32)},
32                 },
33                 .count = 1,
34                 .size = sizeof(SelectionVertex),
35         },
36         .vao = 0,
37         .vbo = 0,
38         .data = NULL,
39         .count = 36,
40         .free_data = false,
41 };
42
43 bool interact_init()
44 {
45         if (!shader_program_create(RESSOURCE_PATH "shaders/3d/selection", &shader_prog, NULL)) {
46                 fprintf(stderr, "[error] failed to create selection shader program\n");
47                 return false;
48         }
49
50         loc_MVP = glGetUniformLocation(shader_prog, "MVP"); GL_DEBUG
51         loc_color = glGetUniformLocation(shader_prog, "color"); GL_DEBUG
52
53         SelectionVertex vertices[6][6];
54         for (int f = 0; f < 6; f++)
55                 for (int v = 0; v < 6; v++)
56                         vertices[f][v].position = v3f32_scale(cube_vertices[f][v].position, 1.1f);
57
58         selection_mesh.data = vertices;
59         mesh_upload(&selection_mesh);
60
61         gui_add(NULL, (GUIElementDef) {
62                 .pos = {0.5f, 0.5f},
63                 .z_index = 0.0f,
64                 .offset = {0, 0},
65                 .margin = {0, 0},
66                 .align = {0.5f, 0.5f},
67                 .scale = {1.0f, 1.0f},
68                 .scale_type = SCALE_IMAGE,
69                 .affect_parent_scale = false,
70                 .text = NULL,
71                 .image = texture_load(RESSOURCE_PATH "textures/crosshair.png", false),
72                 .text_color = {0.0f, 0.0f, 0.0f, 0.0f},
73                 .bg_color = {0.0f, 0.0f, 0.0f, 0.0f},
74         });
75
76         return true;
77 }
78
79 void interact_deinit()
80 {
81         glDeleteProgram(shader_prog); GL_DEBUG
82         mesh_destroy(&selection_mesh);
83 }
84
85 #include "client/client_terrain.h"
86
87 void interact_tick()
88 {
89         bool old_exists = interact_pointed.exists;
90         v3s32 old_pointed = interact_pointed.pos;
91         if ((interact_pointed.exists = raycast(
92                                 (v3f64) {camera.eye  [0], camera.eye  [1], camera.eye  [2]},
93                                 (v3f64) {camera.front[0], camera.front[1], camera.front[2]},
94                                 5, &interact_pointed.pos, &interact_pointed.node))
95                         && !v3s32_equals(interact_pointed.pos, old_pointed)) {
96                 mat4x4_translate(model,
97                         interact_pointed.pos.x, interact_pointed.pos.y, interact_pointed.pos.z);
98                 v3f32 *color = &client_node_def[interact_pointed.node].selection_color;
99                 glProgramUniform3f(shader_prog, loc_color, color->x, color->y, color->z); GL_DEBUG
100                 debug_menu_changed(ENTRY_POINTED);
101         }
102
103         if (old_exists && !interact_pointed.exists)
104                 debug_menu_changed(ENTRY_POINTED);
105 }
106
107 void interact_render()
108 {
109         if (!interact_pointed.exists)
110                 return;
111
112         mat4x4 mvp;
113         mat4x4_mul(mvp, frustum, model);
114
115         glUseProgram(shader_prog); GL_DEBUG
116         glUniformMatrix4fv(loc_MVP, 1, GL_FALSE, mvp[0]); GL_DEBUG
117         mesh_render(&selection_mesh);
118 }
119
120 void interact_use(bool left)
121 {
122         ClientEntity *entity = client_player_entity_local();
123         if (!entity)
124                 return;
125
126         ClientPlayerData *data = entity->extra;
127         pthread_mutex_lock(&data->mtx_inv);
128
129         ItemStack *stack = left ? &data->inventory.left : &data->inventory.right;
130         if (client_item_def[stack->type].use && client_item_def[stack->type].use(stack))
131                 dragonnet_peer_send_ToServerInteract(client, &(ToServerInteract) {
132                         .left = left,
133                         .pointed = interact_pointed.exists,
134                         .pos = interact_pointed.pos,
135                 });
136
137         pthread_mutex_unlock(&data->mtx_inv);
138         refcount_drp(&entity->rc);
139 }