diff options
author | pryazha <pryadeiniv@mail.ru> | 2025-02-06 12:38:09 +0500 |
---|---|---|
committer | pryazha <pryadeiniv@mail.ru> | 2025-02-06 12:38:09 +0500 |
commit | 926cbd0d49890772f911e6a6bedb7835605ced89 (patch) | |
tree | ea27e2c84e32ab6d8d29af9e61cd432e30a46bc6 /libs | |
parent | bf1c59565096ac9774493846cfb15e259d3b0e66 (diff) |
change
Diffstat (limited to 'libs')
-rw-r--r-- | libs/common.h | 136 |
1 files changed, 124 insertions, 12 deletions
diff --git a/libs/common.h b/libs/common.h index 481e356..c7513a2 100644 --- a/libs/common.h +++ b/libs/common.h @@ -329,8 +329,7 @@ load_texture(char *texture_filename) format = GL_RGBA; glBindTexture(GL_TEXTURE_2D, texture_id); - glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, - GL_UNSIGNED_BYTE, data); + glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); @@ -350,6 +349,52 @@ load_texture(char *texture_filename) } U32 +load_texture_gamma(char *filename, B32 gamma_correction) +{ + S32 width, height, number_channels; + U32 texture_id; + + glGenTextures(1, &texture_id); + + stbi_set_flip_vertically_on_load(1); + U8 *data = stbi_load(filename, &width, &height, &number_channels, 0); + if (data) { + GLenum internal_format, data_format; + if (number_channels == 1) + { + internal_format = data_format = GL_RED; + } + else if (number_channels == 3) + { + internal_format = gamma_correction ? GL_SRGB : GL_RGB; + data_format = GL_RGB; + } + else if (number_channels == 4) + { + internal_format = gamma_correction ? GL_SRGB_ALPHA : GL_RGBA; + data_format = GL_RGBA; + } + + glBindTexture(GL_TEXTURE_2D, texture_id); + glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, + 0, data_format, GL_UNSIGNED_BYTE, data); + glGenerateMipmap(GL_TEXTURE_2D); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glBindTexture(GL_TEXTURE_2D, 0); + + fprintf(stdout, "[INFO]: Texture (\"%s\") is loaded successfully\n", filename); + } else { + fprintf(stderr, "[ERROR]: Failed to load texture: \"%s\"\n", filename); + } + stbi_image_free(data); + + return(texture_id); +} + +U32 load_cubemap(const char *texture_filenames[6]) { U32 texture_id; @@ -433,6 +478,38 @@ input_update_last_state(Input *input) input->exit.last = input->exit.state; } +void +process_glfw_key(GLFWwindow *window, S32 glfw_keycode, Key *key) +{ + if (glfwGetKey(window, glfw_keycode) == GLFW_PRESS) + key->state = KeyState_PRESS; + else + key->state = KeyState_RELEASE; +} + +void +process_glfw_keyboard(GLFWwindow *window, Input *input) +{ + process_glfw_key(window, GLFW_KEY_D, &input->move_right); + process_glfw_key(window, GLFW_KEY_W, &input->move_forward); + process_glfw_key(window, GLFW_KEY_A, &input->move_left); + process_glfw_key(window, GLFW_KEY_S, &input->move_backward); + process_glfw_key(window, GLFW_KEY_E, &input->move_up); + process_glfw_key(window, GLFW_KEY_Q, &input->move_down); + process_glfw_key(window, GLFW_KEY_SPACE, &input->jump); + process_glfw_key(window, GLFW_KEY_RIGHT, &input->action_right); + process_glfw_key(window, GLFW_KEY_UP, &input->action_up); + process_glfw_key(window, GLFW_KEY_LEFT, &input->action_left); + process_glfw_key(window, GLFW_KEY_DOWN, &input->action_down); + process_glfw_key(window, GLFW_KEY_ESCAPE, &input->exit); +} + +void +error_callback(int error, const char *desc) +{ + fprintf(stderr, "[ERROR] GLFW: %s\n", desc); +} + B32 key_is_pressed(Key key) { @@ -637,11 +714,24 @@ typedef struct { F32 fovx; F32 near; F32 far; + + /* NOTE(pryazha): In degrees */ + F32 yaw; + F32 pitch; } Camera; +MAT4 +get_view_matrix(Camera *camera) +{ + MAT4 view; + view = mat4_make_translate(v3f_negate(camera->pos)); + view = mat4_rotate_angles(view, v3f(camera->pitch, camera->yaw, 0.0f)); + return(view); +} + V3F get_dv_camera_orbital(Input *input, V3F pos, V3F target, - F32 dt, F32 speed) + F32 dt, F32 acceleration) { V3F up, f, l, u, dv; @@ -652,7 +742,6 @@ get_dv_camera_orbital(Input *input, V3F pos, V3F target, dv = v3f_zero(); - /* if (key_is_pressed(input->move_right)) dv = v3f_sub(dv, l); if (key_is_pressed(input->move_forward)) @@ -665,13 +754,35 @@ get_dv_camera_orbital(Input *input, V3F pos, V3F target, dv = v3f_add(dv, u); if (key_is_pressed(input->move_down)) dv = v3f_sub(dv, u); - */ + + dv = v3f_norm(dv); + + dv = v3f_scalef(dv, acceleration*dt); + + return(dv); +} + +void +get_camera_vectors(Camera *camera, V3F *l, V3F *u, V3F *f) +{ + MAT4 view; + view = get_view_matrix(camera); + *l = v3f(-view.m0.x, -view.m1.x, -view.m2.x); + *u = v3f(view.m0.y, view.m1.y, view.m2.y); + *f = v3f(-view.m0.z, -view.m1.z, -view.m2.z); +} + +V3F +get_dv_camera_first_person(Input *input, Camera *camera, + F32 acceleration, F32 dt) +{ + V3F f, l, u, dv; + get_camera_vectors(camera, &l, &u, &f); + + dv = v3f_zero(); + if (key_is_pressed(input->move_right)) - { dv = v3f_sub(dv, l); - dv = v3f_add(dv, f); - dv = v3f_norm(dv); - } if (key_is_pressed(input->move_forward)) dv = v3f_add(dv, f); if (key_is_pressed(input->move_left)) @@ -682,15 +793,16 @@ get_dv_camera_orbital(Input *input, V3F pos, V3F target, dv = v3f_add(dv, u); if (key_is_pressed(input->move_down)) dv = v3f_sub(dv, u); - - dv = v3f_scalef(dv, speed*dt); + + dv = v3f_norm(dv); + + dv = v3f_scalef(dv, acceleration*dt); return(dv); } typedef struct { Camera camera; - Mesh *mesh; F32 dt; } State; |