fix: input handling

This commit is contained in:
Kevin J. 2024-10-08 17:27:09 +02:00
parent a176afab7e
commit 5d19366642
3 changed files with 79 additions and 67 deletions

12
INSTALL Normal file
View File

@ -0,0 +1,12 @@
+=============+
| INSTALL |
+=============+
DEBIAN PACKAGES
sudo apt-get update
sudo apt-get install libglew-dev
sudo apt-get install libglfw3-dev
sudo apt-get install libcglm-dev
sudo apt-get install libassimp-dev

View File

@ -1,6 +1,13 @@
#ifndef RENDLIB_H
#define RENDLIB_H
#define RENDLIB_INPUT_ESC 0b00000001
#define RENDLIB_INPUT_REL 0b00000010
#define RENDLIB_INPUT_LEFT 0b00000100
#define RENDLIB_INPUT_RIGHT 0b00001000
#define RENDLIB_INPUT_BACKW 0b00010000
#define RENDLIB_INPUT_FORW 0b00100000
enum errs {
err_glfw_init = -1,
err_glfw_win = -2,

125
rendlib.c
View File

@ -14,25 +14,20 @@ int window_width = 960;
int window_height = 540;
char *window_title = "rendlib window";
// global settings
float fov = 80.0f; // default fov
float fov_change = 1.0f;
vec3 camera_pos = { 0.0f, 0.0f, 20.0f };
float fov = 90.0f;
float camera_yaw = -90.0f;
float camera_pitch = 0.0f;
float camera_sensitivity = 0.02f;
float top_movement_speed = 0.2f;
vec3 camera_pos = { 0.0f, 0.0f, 50.0f };
vec3 camera_front = { 0.0f, 0.0f, -1.0f };
vec3 camera_up = { 0.0f, 1.0f, 0.0f };
struct object *camera_lock = NULL; // is camera locked to any object?
float camera_yaw = -90.0f; // x rotation
float camera_pitch = 0.0f; // y rotation
float camera_sensitivity = 0.01f;
float top_movement_speed = 0.2f;
vec3 speed = { 0.0f, 0.0f, 0.0f };
GLint screen_viewport[4]; // viewport: x,y,width,height
int toggle_tracing = 0; // true or false
long added_particles = 0;
struct object *camera_lock = NULL;
GLint screen_viewport[4];
// tmp input
int inputs[8] = {0,0,0,0,0,0,};
char input = 0;
// tmp
struct model *sphere_model;
@ -47,6 +42,13 @@ unsigned int fragment_shader;
const char *object_vertex_shader_location = "assets/shaders/shader.vert";
const char *object_fragment_shader_location = "assets/shaders/shader.frag";
void update_screen_viewport(int x, int y, int width, int height) {
GLFWwindow *w = glfwGetCurrentContext();
glfwSetCursorPos(w, (width/2), (height/2));
glViewport(x, y, width, height);
glGetIntegerv(GL_VIEWPORT, screen_viewport);
}
int load_shader(const char *path, unsigned int shader) {
FILE *fp = fopen(path, "r");
if (fp == NULL) {
@ -140,21 +142,21 @@ int load_shaders(void) {
void handle_input(void) {
int ret = 0;
if (inputs[0]) {
if (input & RENDLIB_INPUT_ESC) {
exit(EXIT_SUCCESS);
inputs[0] = 0;
input &= ~RENDLIB_INPUT_ESC;
}
if (inputs[1]) {
if (input & RENDLIB_INPUT_REL) {
ret = load_shaders();
if (ret < 0) {
fprintf(stderr, "--error: reloading shaders\n");
exit(EXIT_FAILURE);
}
inputs[1] = 0;
input &= ~RENDLIB_INPUT_REL;
}
if (inputs[2]) {
if (input & RENDLIB_INPUT_LEFT) {
vec3 side_scalar = {top_movement_speed, top_movement_speed, top_movement_speed };
vec3 camera_side;
glm_cross(camera_front, camera_up, camera_side);
@ -163,7 +165,7 @@ void handle_input(void) {
glm_vec3_sub(camera_pos, camera_side, camera_pos);
}
if (inputs[3]) {
if (input & RENDLIB_INPUT_RIGHT) {
vec3 side_scalar = {top_movement_speed, top_movement_speed, top_movement_speed };
vec3 camera_side;
glm_cross(camera_front, camera_up, camera_side);
@ -172,13 +174,13 @@ void handle_input(void) {
glm_vec3_add(camera_pos, camera_side, camera_pos);
}
if (inputs[4]) {
if (input & RENDLIB_INPUT_BACKW) {
vec3 front_scalar = {top_movement_speed, top_movement_speed, top_movement_speed };
glm_vec3_mul(front_scalar, camera_front, front_scalar);
glm_vec3_sub(camera_pos, front_scalar, camera_pos);
}
if (inputs[5]) {
if (input & RENDLIB_INPUT_FORW) {
vec3 front_scalar = {top_movement_speed, top_movement_speed, top_movement_speed };
glm_vec3_mul(front_scalar, camera_front, front_scalar);
glm_vec3_add(camera_pos, front_scalar, camera_pos);
@ -186,7 +188,7 @@ void handle_input(void) {
}
void window_size(GLFWwindow *w, int width, int height) {
glViewport(0, 0, width, height);
update_screen_viewport(0, 0, width, height);
}
void display(void) {
@ -204,7 +206,6 @@ void display(void) {
glClearColor(0.13f, 0.13f, 0.13f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glGetIntegerv(GL_VIEWPORT, screen_viewport);
glUseProgram(shader_program);
glm_mat4_identity(view);
@ -259,13 +260,6 @@ void display(void) {
glm_vec3_add(camera_pos, obj->translation_force, camera_pos);
}
// record path
if (toggle_tracing == 1) {
if (record_path(obj) == -1) {
exit(EXIT_FAILURE);
}
}
glm_translate(translation_matrix, obj->position);
glUniformMatrix4fv(translation_uniform, 1, GL_FALSE, (float *) translation_matrix);
@ -338,65 +332,62 @@ void rendlib_bake_graphics(void) {
void keyboard(GLFWwindow *w, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS) {
switch (key) {
case GLFW_KEY_Q:
case GLFW_KEY_ESCAPE:
inputs[0] = 1;
input |= RENDLIB_INPUT_ESC;
break;
case GLFW_KEY_R:
inputs[1] = 1;
input |= RENDLIB_INPUT_REL;
break;
case GLFW_KEY_A:
inputs[2] = 1;
case GLFW_KEY_LEFT:
input |= RENDLIB_INPUT_LEFT;
break;
case GLFW_KEY_D:
inputs[3] = 1;
case GLFW_KEY_RIGHT:
input |= RENDLIB_INPUT_RIGHT;
break;
case GLFW_KEY_S:
inputs[4] = 1;
case GLFW_KEY_DOWN:
input |= RENDLIB_INPUT_BACKW;
break;
case GLFW_KEY_W:
inputs[5] = 1;
case GLFW_KEY_UP:
input |= RENDLIB_INPUT_FORW;
break;
case GLFW_KEY_F11: {
GLFWmonitor *m = glfwGetPrimaryMonitor();
const GLFWvidmode *vm = glfwGetVideoMode(m);
GLFWwindow *w = glfwGetCurrentContext();
glfwSetWindowMonitor(w, m, 0, 0, vm->width, vm->height, vm->refreshRate);
update_screen_viewport(0, 0, vm->width, vm->height);
break;
}
}
} else if (action == GLFW_RELEASE) {
switch (key) {
case GLFW_KEY_A:
inputs[2] = 0;
case GLFW_KEY_LEFT:
input &= ~RENDLIB_INPUT_LEFT;
break;
case GLFW_KEY_D:
inputs[3] = 0;
case GLFW_KEY_RIGHT:
input &= ~RENDLIB_INPUT_RIGHT;
break;
case GLFW_KEY_S:
inputs[4] = 0;
case GLFW_KEY_DOWN:
input &= ~RENDLIB_INPUT_BACKW;
break;
case GLFW_KEY_W:
inputs[5] = 0;
case GLFW_KEY_UP:
input &= ~RENDLIB_INPUT_FORW;
break;
}
}
}
void mouse(int button, int state, int x, int y) {
switch (button) {
case 3:
if (fov-fov_change < 0.0f) {
break;
}
fov -= fov_change;
break;
case 4:
if (fov+fov_change > 180.0f) {
break;
}
fov += fov_change;
break;
default:
break;
}
}
int warped_pointer = 0;
int first_pointer = 1;
void mouse_motion(GLFWwindow *window, double x, double y) {
@ -406,6 +397,7 @@ void mouse_motion(GLFWwindow *window, double x, double y) {
}
warped_pointer = 1;
glGetIntegerv(GL_VIEWPORT, screen_viewport);
GLFWwindow *w = glfwGetCurrentContext();
glfwSetCursorPos(w, (screen_viewport[2]/2), (screen_viewport[3]/2));
@ -428,10 +420,10 @@ void mouse_motion(GLFWwindow *window, double x, double y) {
camera_pitch = 89.9f;
}
vec3 view_direction;
view_direction[0] = cos(glm_rad(camera_yaw)) * cos(glm_rad(camera_pitch));
view_direction[1] = sin(glm_rad(camera_pitch));
view_direction[2] = sin(glm_rad(camera_yaw)) * cos(glm_rad(camera_pitch));
vec3 view_direction = { cos(glm_rad(camera_yaw)) * cos(glm_rad(camera_pitch)),
sin(glm_rad(camera_pitch)),
sin(glm_rad(camera_yaw)) * cos(glm_rad(camera_pitch))
};
glm_normalize_to(view_direction, camera_front);
}
@ -450,6 +442,7 @@ int rendlib_start_window(int argc, char *argv[]) {
glfwSetWindowSizeCallback(w, window_size);
glfwSetCursorPosCallback(w, mouse_motion);
glfwSetKeyCallback(w, keyboard);
glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
ret = glewInit();
if (ret != GLEW_OK) {