1 #include <linmath.h/linmath.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"
14 static v3s32 node_pos;
15 static GLuint shader_prog;
17 static GLint loc_color;
22 } __attribute__((packed)) SelectionVertex;
23 static Mesh selection_mesh = {
24 .layout = &(VertexLayout) {
25 .attributes = (VertexAttribute[]) {
26 {GL_FLOAT, 3, sizeof(v3f32)},
29 .size = sizeof(SelectionVertex),
40 if (!shader_program_create(RESSOURCE_PATH "shaders/3d/selection", &shader_prog, NULL)) {
41 fprintf(stderr, "[error] failed to create selection shader program\n");
45 loc_MVP = glGetUniformLocation(shader_prog, "MVP"); GL_DEBUG
46 loc_color = glGetUniformLocation(shader_prog, "color"); GL_DEBUG
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);
53 selection_mesh.data = vertices;
54 mesh_upload(&selection_mesh);
59 void interact_deinit()
61 glDeleteProgram(shader_prog); GL_DEBUG
62 mesh_destroy(&selection_mesh);
67 v3s32 old_node_pos = node_pos;
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
80 void interact_render()
86 mat4x4_mul(mvp, frustum, model);
88 glUseProgram(shader_prog); GL_DEBUG
89 glUniformMatrix4fv(loc_MVP, 1, GL_FALSE, mvp[0]); GL_DEBUG
90 mesh_render(&selection_mesh);