rendlib/dev.c

189 lines
5.0 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <stdio.h>
#include <GLFW/glfw3.h>
#include "include/rendlib.h"
2024-10-08 17:43:14 +00:00
#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) {
fprintf(stderr, "--error: %d\n", ret);
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");
return EXIT_FAILURE;
}
struct object *o = create_object(&objects, 100.0f, m);
if (o == NULL) {
fprintf(stderr, "--error: creating object\n");
return EXIT_FAILURE;
}
2024-10-08 17:43:14 +00:00
struct camera *c = create_camera(CAMERA_PERSPECTIVE);
if (c == NULL) {
fprintf(stderr, "--error: creating camera\n");
return EXIT_FAILURE;
}
ret = rendlib_render();
if (ret < 0) {
fprintf(stderr, "--error: %d\n", ret);
return EXIT_FAILURE;
}
fprintf(stdout, "--rendering\n");
return EXIT_SUCCESS;
}