summary refs log tree commit diff
path: root/src/main.odin
diff options
context:
space:
mode:
authoriurii plugatarov <[email protected]>2024-08-17 23:49:23 +0200
committeriurii plugatarov <[email protected]>2024-08-17 23:49:23 +0200
commit7ccea9305f0ee31c5e17a8c25cb3b774b91a4055 (patch)
treeecadf354eeb4f35f336cc76160db95cdcfbb0ecc /src/main.odin
parentc3ccbf088a9be28360d62b0b3b82f179b516408f (diff)
downloadfunhalla-7ccea9305f0ee31c5e17a8c25cb3b774b91a4055.tar.gz
no input from mouse
Diffstat (limited to '')
-rw-r--r--src/main.odin68
1 files changed, 22 insertions, 46 deletions
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])