]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/interact.c
728bc2934756ff9567dbecf804fd21bc9ace4db3
[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_node.h"
5 #include "client/cube.h"
6 #include "client/frustum.h"
7 #include "client/gl_debug.h"
8 #include "client/interact.h"
9 #include "client/mesh.h"
10 #include "client/raycast.h"
11 #include "client/shader.h"
12
13 static bool pointed;
14 static v3s32 node_pos;
15 static GLuint shader_prog;
16 static GLint loc_MVP;
17 static GLint loc_color;
18 static mat4x4 model;
19
20 typedef struct {
21         v3f32 position;
22 } __attribute__((packed)) SelectionVertex;
23 static Mesh selection_mesh = {
24         .layout = &(VertexLayout) {
25                 .attributes = (VertexAttribute[]) {
26                         {GL_FLOAT, 3, sizeof(v3f32)},
27                 },
28                 .count = 1,
29                 .size = sizeof(SelectionVertex),
30         },
31         .vao = 0,
32         .vbo = 0,
33         .data = NULL,
34         .count = 36,
35         .free_data = false,
36 };
37
38 bool interact_init()
39 {
40         if (!shader_program_create(RESSOURCE_PATH "shaders/3d/selection", &shader_prog, NULL)) {
41                 fprintf(stderr, "[error] failed to create selection shader program\n");
42                 return false;
43         }
44
45         loc_MVP = glGetUniformLocation(shader_prog, "MVP"); GL_DEBUG
46         loc_color = glGetUniformLocation(shader_prog, "color"); GL_DEBUG
47
48         SelectionVertex vertices[6][6];
49         for (int f = 0; f < 6; f++)
50                 for (int v = 0; v < 6; v++)
51                         vertices[f][v].position = v3f32_scale(cube_vertices[f][v].position, 1.1f);
52
53         selection_mesh.data = vertices;
54         mesh_upload(&selection_mesh);
55
56         return true;
57 }
58
59 void interact_deinit()
60 {
61         glDeleteProgram(shader_prog); GL_DEBUG
62         mesh_destroy(&selection_mesh);
63 }
64
65 void interact_tick()
66 {
67         v3s32 old_node_pos = node_pos;
68
69         NodeType node;
70         if ((pointed = raycast(
71                         (v3f64) {camera.eye  [0], camera.eye  [1], camera.eye  [2]}, 
72                         (v3f64) {camera.front[0], camera.front[1], camera.front[2]},
73                         5, &node_pos, &node)) && !v3s32_equals(node_pos, old_node_pos)) {
74                 mat4x4_translate(model, node_pos.x, node_pos.y, node_pos.z);
75                 v3f32 *color = &client_node_definitions[node].selection_color;
76                 glProgramUniform3f(shader_prog, loc_color, color->x, color->y, color->z); GL_DEBUG
77         }
78 }
79
80 void interact_render()
81 {
82         if (!pointed)
83                 return;
84
85         mat4x4 mvp;
86         mat4x4_mul(mvp, frustum, model);
87
88         glUseProgram(shader_prog); GL_DEBUG
89         glUniformMatrix4fv(loc_MVP, 1, GL_FALSE, mvp[0]); GL_DEBUG
90         mesh_render(&selection_mesh);
91 }