diff --git a/dev.c b/dev.c index aeba1cd..1f56abd 100644 --- a/dev.c +++ b/dev.c @@ -1,9 +1,152 @@ #include #include +#include #include "include/rendlib.h" #include "include/camera.h" #include "include/object.h" +#define INPUT_ESCAPE 0b00000001 +#define INPUT_RELOAD 0b00000010 +#define INPUT_LEFT 0b00000100 +#define INPUT_RIGHT 0b00001000 +#define INPUT_DOWN 0b00010000 +#define INPUT_UP 0b00100000 + +#define MOVEMENT_SPEED 0.2f + +char input = 0; + +void keyboardp(int key, int scancode, int action, int mods) { + if (action == GLFW_PRESS) { + switch (key) { + case GLFW_KEY_Q: + case GLFW_KEY_ESCAPE: + input |= INPUT_ESCAPE; + break; + case GLFW_KEY_R: + input |= INPUT_RELOAD; + break; + case GLFW_KEY_A: + case GLFW_KEY_LEFT: + input |= INPUT_LEFT; + break; + case GLFW_KEY_D: + case GLFW_KEY_RIGHT: + input |= INPUT_RIGHT; + break; + case GLFW_KEY_S: + case GLFW_KEY_DOWN: + input |= INPUT_DOWN; + break; + case GLFW_KEY_W: + case GLFW_KEY_UP: + input |= INPUT_UP; + break; + } + } else if (action == GLFW_RELEASE) { + switch (key) { + case GLFW_KEY_A: + case GLFW_KEY_LEFT: + input &= ~INPUT_LEFT; + break; + case GLFW_KEY_D: + case GLFW_KEY_RIGHT: + input &= ~INPUT_RIGHT; + break; + case GLFW_KEY_S: + case GLFW_KEY_DOWN: + input &= ~INPUT_DOWN; + break; + case GLFW_KEY_W: + case GLFW_KEY_UP: + input &= ~INPUT_UP; + break; + } + } +} + +int warped_pointer = 0; +int first_pointer = 1; +void mousep(double x, double y) { + GLint screen_viewport[4]; + if (warped_pointer == 1) { + warped_pointer = 0; + return; + } + + warped_pointer = 1; + glGetIntegerv(GL_VIEWPORT, screen_viewport); + GLFWwindow *w = glfwGetCurrentContext(); + glfwSetCursorPos(w, (screen_viewport[2]/2), (screen_viewport[3]/2)); + + if (first_pointer == 1) { + first_pointer = 0; + return; + } + + float offset_x = (float) (x - (screen_viewport[2]/2)) * ac->sensitivity; + float offset_y = (float) (y - (screen_viewport[3]/2)) * ac->sensitivity; + + ac->yaw += offset_x; + ac->pitch -= offset_y; + + if (ac->pitch < -89.9f) { + ac->pitch = -89.9f; + } + if (ac->pitch > 89.9f) { + ac->pitch = 89.9f; + } + + vec3 view_direction = { cos(glm_rad(ac->yaw)) * cos(glm_rad(ac->pitch)), + sin(glm_rad(ac->pitch)), + sin(glm_rad(ac->yaw)) * cos(glm_rad(ac->pitch)) + }; + glm_normalize_to(view_direction, ac->front); +} + +void periodic_input(void) { + int ret = 0; + if (input & INPUT_ESCAPE) { + exit(EXIT_SUCCESS); + input &= ~INPUT_ESCAPE; + } + + if (input & INPUT_LEFT) { + vec3 side_scalar = {MOVEMENT_SPEED, MOVEMENT_SPEED, MOVEMENT_SPEED}; + vec3 camera_side; + glm_cross(ac->front, ac->up, camera_side); + glm_normalize(camera_side); + glm_vec3_mul(camera_side, side_scalar, camera_side); + glm_vec3_sub(ac->pos, camera_side, ac->pos); + } + + if (input & INPUT_RIGHT) { + vec3 side_scalar = {MOVEMENT_SPEED, MOVEMENT_SPEED, MOVEMENT_SPEED}; + vec3 camera_side; + glm_cross(ac->front, ac->up, camera_side); + glm_normalize(camera_side); + glm_vec3_mul(camera_side, side_scalar, camera_side); + glm_vec3_add(ac->pos, camera_side, ac->pos); + } + + if (input & INPUT_DOWN) { + vec3 front_scalar = {MOVEMENT_SPEED, MOVEMENT_SPEED, MOVEMENT_SPEED}; + glm_vec3_mul(front_scalar, ac->front, front_scalar); + glm_vec3_sub(ac->pos, front_scalar, ac->pos); + } + + if (input & INPUT_UP) { + vec3 front_scalar = {MOVEMENT_SPEED, MOVEMENT_SPEED, MOVEMENT_SPEED}; + glm_vec3_mul(front_scalar, ac->front, front_scalar); + glm_vec3_add(ac->pos, front_scalar, ac->pos); + } + +} + +void updatep(void) { + periodic_input(); +} + int main(int argc, char *argv[]) { int ret = rendlib_start_window(argc, argv); if (ret < 0) { @@ -11,6 +154,10 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } + keyboard_probe = &keyboardp; + mouse_probe = &mousep; + update_probe = &updatep; + struct model *m = load_model("assets/models/sphere.obj"); if (m == NULL) { fprintf(stderr, "--error: loading model\n"); diff --git a/include/rendlib.h b/include/rendlib.h index aee7fa8..b65d95e 100644 --- a/include/rendlib.h +++ b/include/rendlib.h @@ -1,13 +1,6 @@ #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, @@ -15,8 +8,13 @@ enum errs { err_shaders_init = -4, }; -extern struct camera *active_camera; +extern struct camera *ac; extern struct object *objects; + +extern void (*keyboard_probe)(int key, int scancode, int action, int mods); +extern void (*mouse_probe)(double x, double y); +extern void (*update_probe)(void); + int rendlib_start_window(int argc, char *argv[]); int rendlib_render(void); diff --git a/rendlib.c b/rendlib.c index 5b1fc51..e379672 100644 --- a/rendlib.c +++ b/rendlib.c @@ -16,10 +16,13 @@ char *window_title = "rendlib window"; struct camera *ac; +void (*keyboard_probe)(int key, int scancode, int action, int mods); +void (*mouse_probe)(double x, double y); +void (*update_probe)(void); + float top_movement_speed = 0.4f; vec3 speed = { 0.0f, 0.0f, 0.0f }; GLint screen_viewport[4]; -char input = 0; struct object *objects; @@ -127,50 +130,6 @@ int load_shaders(void) { } void handle_input(void) { - int ret = 0; - if (input & RENDLIB_INPUT_ESC) { - exit(EXIT_SUCCESS); - input &= ~RENDLIB_INPUT_ESC; - } - - if (input & RENDLIB_INPUT_REL) { - ret = load_shaders(); - if (ret < 0) { - fprintf(stderr, "--error: reloading shaders\n"); - exit(EXIT_FAILURE); - } - input &= ~RENDLIB_INPUT_REL; - } - - if (input & RENDLIB_INPUT_LEFT) { - vec3 side_scalar = {top_movement_speed, top_movement_speed, top_movement_speed }; - vec3 camera_side; - glm_cross(ac->front, ac->up, camera_side); - glm_normalize(camera_side); - glm_vec3_mul(camera_side, side_scalar, camera_side); - glm_vec3_sub(ac->pos, camera_side, ac->pos); - } - - if (input & RENDLIB_INPUT_RIGHT) { - vec3 side_scalar = {top_movement_speed, top_movement_speed, top_movement_speed }; - vec3 camera_side; - glm_cross(ac->front, ac->up, camera_side); - glm_normalize(camera_side); - glm_vec3_mul(camera_side, side_scalar, camera_side); - glm_vec3_add(ac->pos, camera_side, ac->pos); - } - - if (input & RENDLIB_INPUT_BACKW) { - vec3 front_scalar = {top_movement_speed, top_movement_speed, top_movement_speed }; - glm_vec3_mul(front_scalar, ac->front, front_scalar); - glm_vec3_sub(ac->pos, front_scalar, ac->pos); - } - - if (input & RENDLIB_INPUT_FORW) { - vec3 front_scalar = {top_movement_speed, top_movement_speed, top_movement_speed }; - glm_vec3_mul(front_scalar, ac->front, front_scalar); - glm_vec3_add(ac->pos, front_scalar, ac->pos); - } } void window_size(GLFWwindow *w, int width, int height) { @@ -261,100 +220,19 @@ void rendlib_bake_graphics(void) { glEnable(GL_DEPTH_TEST); } 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: - input |= RENDLIB_INPUT_ESC; - break; - case GLFW_KEY_R: - input |= RENDLIB_INPUT_REL; - break; - case GLFW_KEY_A: - case GLFW_KEY_LEFT: - input |= RENDLIB_INPUT_LEFT; - break; - case GLFW_KEY_D: - case GLFW_KEY_RIGHT: - input |= RENDLIB_INPUT_RIGHT; - break; - case GLFW_KEY_S: - case GLFW_KEY_DOWN: - input |= RENDLIB_INPUT_BACKW; - break; - case GLFW_KEY_W: - 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: - case GLFW_KEY_LEFT: - input &= ~RENDLIB_INPUT_LEFT; - break; - case GLFW_KEY_D: - case GLFW_KEY_RIGHT: - input &= ~RENDLIB_INPUT_RIGHT; - break; - case GLFW_KEY_S: - case GLFW_KEY_DOWN: - input &= ~RENDLIB_INPUT_BACKW; - break; - case GLFW_KEY_W: - case GLFW_KEY_UP: - input &= ~RENDLIB_INPUT_FORW; - break; - } + if (keyboard_probe == NULL) { + return; } + + (keyboard_probe)(key, scancode, action, mods); } -int warped_pointer = 0; -int first_pointer = 1; void mouse_motion(GLFWwindow *window, double x, double y) { - if (warped_pointer == 1) { - warped_pointer = 0; + if (mouse_probe == NULL) { return; } - warped_pointer = 1; - glGetIntegerv(GL_VIEWPORT, screen_viewport); - GLFWwindow *w = glfwGetCurrentContext(); - glfwSetCursorPos(w, (screen_viewport[2]/2), (screen_viewport[3]/2)); - - if (first_pointer == 1) { - first_pointer = 0; - return; - } - - float offset_x = (float) (x - (screen_viewport[2]/2)) * ac->sensitivity; - float offset_y = (float) (y - (screen_viewport[3]/2)) * ac->sensitivity; - - ac->yaw += offset_x; - ac->pitch -= offset_y; - - if (ac->pitch < -89.9f) { - ac->pitch = -89.9f; - } - if (ac->pitch > 89.9f) { - ac->pitch = 89.9f; - } - - vec3 view_direction = { cos(glm_rad(ac->yaw)) * cos(glm_rad(ac->pitch)), - sin(glm_rad(ac->pitch)), - sin(glm_rad(ac->yaw)) * cos(glm_rad(ac->pitch)) - }; - glm_normalize_to(view_direction, ac->front); + (mouse_probe)(x, y); } int rendlib_start_window(int argc, char *argv[]) { @@ -403,6 +281,10 @@ int rendlib_render(void) { } rendlib_bake_graphics(); for (;;) { + if (update_probe != NULL) { + (update_probe)(); + } + display(); } return 0;