From 7ccea9305f0ee31c5e17a8c25cb3b774b91a4055 Mon Sep 17 00:00:00 2001 From: iurii plugatarov Date: Sun, 18 Aug 2024 00:49:23 +0300 Subject: no input from mouse --- src/camera/camera.odin | 49 +++++++++++++++++++----------------- src/main.odin | 68 ++++++++++++++++---------------------------------- 2 files changed, 48 insertions(+), 69 deletions(-) diff --git a/src/camera/camera.odin b/src/camera/camera.odin index 1ab1f39..42925e7 100644 --- a/src/camera/camera.odin +++ b/src/camera/camera.odin @@ -44,12 +44,20 @@ camera_init :: proc( yaw: f32 = YAW, pitch: f32 = PITCH, front: Vec3 = DEFAULT_FRONT, + move_speed: f32 = SPEED, + mouse_sensitivity: f32 = SENSITIVITY, + zoom: f32 = ZOOM, ) -> ^Camera { c := new(Camera) + c.front = front c.position = position c.world_up = up c.yaw = yaw c.pitch = pitch + c.up = up + c.mouse_sensitivity = mouse_sensitivity + c.zoom = zoom + c.move_speed = move_speed _update_camera_vectors(c) return c } @@ -79,11 +87,11 @@ process_keyboard :: proc(using camera: ^Camera, direction: CameraMovement, dt: f process_mouse_move :: proc( using camera: ^Camera, - xoffset, yoffset: f32, + xoffset_in, yoffset_in: f32, constraint_pitch: bool = true, ) { - xoffset *= mouse_sensitivity - yoffset *= mouse_sensitivity + xoffset: f32 = xoffset_in * mouse_sensitivity + yoffset: f32 = yoffset_in * mouse_sensitivity yaw += xoffset pitch += yoffset @@ -99,32 +107,27 @@ process_mouse_move :: proc( } } -process_mouse_scroll :: proc( - using camera: ^Camera, - yoffset : f32 -) -> { - zoom -= yoffset +process_mouse_scroll :: proc(using camera: ^Camera, yoffset: f32) { + zoom -= yoffset - if zoom < 1.0 { - zoom = 1.0 - } + if zoom < 1.0 { + zoom = 1.0 + } - if zoom > 45.0 { - zooom = 45.0 - } + if zoom > 45.0 { + zoom = 45.0 + } } @(private) -_update_camera_vectors :: proc(camera: ^Camera) { - using math - using la +_update_camera_vectors :: proc(using camera: ^Camera) { - front_x: f32 = cos(to_radians(yaw)) * cos(to_radians(pitch)) - front_y: f32 = cos(to_radians(pitch)) - front_z: f32 = sin(to_radians(yaw)) * cos(to_radians(pitch)) + front_x: f32 = math.cos(la.to_radians(yaw)) * math.cos(la.to_radians(pitch)) + front_y: f32 = math.sin(la.to_radians(pitch)) + front_z: f32 = math.sin(la.to_radians(yaw)) * math.cos(la.to_radians(pitch)) - camera.front = vector_normalize(Vec3{front_x, front_y, front_z}) - camera.right = vector_normalize(vector_cross3(camera.front, camera.world_up)) - camera.up = vector_normalize(vector_cross3(camera.right, camera.front)) + front = la.vector_normalize(Vec3{front_x, front_y, front_z}) + right = la.vector_normalize(la.vector_cross3(camera.front, camera.world_up)) + up = la.vector_normalize(la.vector_cross3(camera.right, camera.front)) } diff --git a/src/main.odin b/src/main.odin index ce8ac19..f399fbb 100644 --- a/src/main.odin +++ b/src/main.odin @@ -11,6 +11,7 @@ import "core:math" import "core:math/linalg" import "core:os" +import cam "camera" import "shader" GL_MAJOR_VERSION :: 3 @@ -18,9 +19,6 @@ GL_MINOR_VERSION :: 3 Vec3 :: linalg.Vector3f32 -camera_pos := Vec3{0.0, 0.0, 3.0} -camera_front := Vec3{0.0, 0.0, -1.0} -camera_up := Vec3{0.0, 1.0, 0.0} delta_time: f32 = 0.0 last_frame: f32 = 0.0 @@ -28,18 +26,20 @@ last_frame: f32 = 0.0 SCREEN_WIDTH: f32 : 800.0 SCREEN_HEIGTH: f32 : 600.0 + +camera := cam.camera_init(Vec3{0.0, 0.0, 0.3}) + first_mouse := true -yaw: f32 = -90.0 -pitch: f32 = 0.0 last_x: f32 = 800.0 / 2.0 last_y: f32 = 800.0 / 2.0 -fov: f32 = 45.0 framebuffer_size_callback :: proc "cdecl" (window: glfw.WindowHandle, width, height: i32) { gl.Viewport(0, 0, width, height) } mouse_callback :: proc "cdecl" (window: glfw.WindowHandle, xpos_in, ypos_in: f64) { + context = runtime.default_context() + using cam xpos: f32 = f32(xpos_in) ypos: f32 = f32(ypos_in) @@ -54,63 +54,35 @@ mouse_callback :: proc "cdecl" (window: glfw.WindowHandle, xpos_in, ypos_in: f64 last_x = xpos last_y = ypos - sensitivity: f32 = 0.1 - - xoffset *= sensitivity - yoffset *= sensitivity - - yaw += xoffset - pitch += yoffset - - if pitch > 89.0 { - pitch = 89.0 - } - if pitch < -89.0 { - pitch = -89.0 - } - - front_x: f32 = math.cos(linalg.to_radians(yaw)) * math.cos(linalg.to_radians(pitch)) - front_y: f32 = math.sin(linalg.to_radians(pitch)) - front_z: f32 = math.sin(linalg.to_radians(yaw)) * math.cos(linalg.to_radians(pitch)) - - camera_front = linalg.vector_normalize(Vec3{front_x, front_y, front_z}) + process_mouse_move(camera, xoffset, yoffset) } mouse_scroll_callback :: proc "cdecl" (window: glfw.WindowHandle, xoffset, yoffset: f64) { - fov -= f32(yoffset) - - if fov < 1.0 { - fov = 1.0 - } - if fov > 45.0 { - fov = 45.0 - } + using cam + context = runtime.default_context() + process_mouse_scroll(camera, f32(yoffset)) } process_input :: proc(window: ^glfw.WindowHandle) { + using cam if glfw.GetKey(window^, glfw.KEY_ESCAPE) == glfw.PRESS { glfw.SetWindowShouldClose(window^, true) } - camera_speed: f32 = 2.5 * delta_time - if glfw.GetKey(window^, glfw.KEY_W) == glfw.PRESS { - camera_pos += camera_speed * camera_front + process_keyboard(camera, CameraMovement.FORWARD, delta_time) } if glfw.GetKey(window^, glfw.KEY_S) == glfw.PRESS { - camera_pos -= camera_speed * camera_front + process_keyboard(camera, CameraMovement.BACKWARD, delta_time) } if glfw.GetKey(window^, glfw.KEY_A) == glfw.PRESS { - cross_a := linalg.vector_cross3(camera_front, camera_up) - camera_pos -= linalg.vector_normalize(cross_a) * camera_speed - + process_keyboard(camera, CameraMovement.LEFT, delta_time) } if glfw.GetKey(window^, glfw.KEY_D) == glfw.PRESS { - cross_d := linalg.vector_cross3(camera_front, camera_up) - camera_pos += linalg.vector_normalize(cross_d) * camera_speed + process_keyboard(camera, CameraMovement.RIGHT, delta_time) } } @@ -459,10 +431,14 @@ main :: proc() { aspect: f32 = 800.0 / 600.0 - view := linalg.matrix4_look_at(camera_pos, camera_pos + camera_front, camera_up) + view := cam.get_view_matrix(camera) - - projection := linalg.matrix4_perspective_f32(linalg.to_radians(fov), aspect, 0.1, 100.0) + projection := linalg.matrix4_perspective_f32( + linalg.to_radians(camera.zoom), + aspect, + 0.1, + 100.0, + ) view_location := gl.GetUniformLocation(shdr.id, "view") gl.UniformMatrix4fv(view_location, 1, gl.FALSE, &view[0][0]) -- cgit 1.4.1-2-gfad0