Implement camera controller
This commit is contained in:
+41
-21
@@ -7,17 +7,19 @@
|
||||
package game
|
||||
|
||||
import "core:fmt"
|
||||
import "core:math"
|
||||
import "core:time"
|
||||
import "sindri:platform"
|
||||
|
||||
import "sindri:base"
|
||||
import "sindri:core"
|
||||
import "sindri:platform"
|
||||
|
||||
// -----------------------------------------
|
||||
// Types
|
||||
|
||||
Game_Memory :: struct {
|
||||
should_close: bool,
|
||||
camera: core.Thing_Ref,
|
||||
}
|
||||
|
||||
Test_Memory :: struct {
|
||||
@@ -34,7 +36,6 @@ t: Test_Memory
|
||||
engine: ^core.Engine_API
|
||||
|
||||
game_api := core.Game_API {
|
||||
settings = settings,
|
||||
register_api = register_api,
|
||||
register_memory = register_memory,
|
||||
init_once = init_once,
|
||||
@@ -47,6 +48,8 @@ game_api := core.Game_API {
|
||||
force_restart = force_restart,
|
||||
}
|
||||
|
||||
mouse_last: base.Vec2
|
||||
|
||||
// -----------------------------------------
|
||||
// Public functions
|
||||
|
||||
@@ -54,17 +57,6 @@ api :: proc() -> ^core.Game_API {
|
||||
return &game_api
|
||||
}
|
||||
|
||||
settings :: proc() -> base.Settings {
|
||||
return base.Settings {
|
||||
width = 960,
|
||||
height = 540,
|
||||
title = "WGPU Native Triangle",
|
||||
mode = base.WindowMode.Windowed,
|
||||
refresh = 60,
|
||||
vsync = true,
|
||||
}
|
||||
}
|
||||
|
||||
register_api :: proc(api: ^core.Engine_API) {
|
||||
engine = api
|
||||
}
|
||||
@@ -77,8 +69,11 @@ register_memory :: proc() {
|
||||
init_once :: proc() {
|
||||
fmt.println("init once")
|
||||
|
||||
camera := core.thing_add(engine.things)
|
||||
player := core.thing_add(engine.things)
|
||||
g.camera = core.thing_add(engine.things)
|
||||
|
||||
camera := core.thing_get(engine.things, g.camera)
|
||||
camera.pos = {0, 2, 5}
|
||||
camera.rot = {-(25 * math.PI / 180), 0, 0}
|
||||
}
|
||||
|
||||
init :: proc() {
|
||||
@@ -113,18 +108,43 @@ init :: proc() {
|
||||
)
|
||||
}
|
||||
|
||||
clicked: bool
|
||||
captured: bool
|
||||
update :: proc(dt: f32) {
|
||||
fmt.println("dt:", dt)
|
||||
|
||||
if core.key_state(.Key_Escape) == .Press {
|
||||
if platform.os_key_state(.Key_Escape) == .Press {
|
||||
g.should_close = true
|
||||
}
|
||||
|
||||
if core.key_state(.Key_F5) == .Press do t.asd = 555
|
||||
if core.key_state(.Key_F6) == .Press do t.asd = 666
|
||||
if platform.os_key_state(.Key_F5) == .Press do t.asd = 555
|
||||
if platform.os_key_state(.Key_F6) == .Press do t.asd = 666
|
||||
|
||||
if platform.os_mouse_button_state(.Left) == .Press && !clicked {
|
||||
clicked = true
|
||||
platform.os_set_mouse_capture(!captured)
|
||||
captured = !captured
|
||||
}
|
||||
if platform.os_mouse_button_state(.Left) == .Release {
|
||||
clicked = false
|
||||
}
|
||||
|
||||
render :: proc() { }
|
||||
// Mouse delta
|
||||
pos := platform.os_mouse_position()
|
||||
if mouse_last == 0 do mouse_last = pos // restore position after hot-reload
|
||||
mouse_delta := pos - mouse_last
|
||||
mouse_last = pos
|
||||
|
||||
// Update camera
|
||||
camera := core.thing_get(engine.things, g.camera)
|
||||
sensitivity := base.settings().mouse_sensitivity
|
||||
core.camera_update(dt, camera, mouse_delta, sensitivity)
|
||||
}
|
||||
|
||||
render :: proc() {
|
||||
camera := core.thing_get(engine.things, g.camera)
|
||||
platform.state.camera = {camera.pos, camera.rot}
|
||||
}
|
||||
|
||||
destroy :: proc() {
|
||||
fmt.println("bye")
|
||||
@@ -143,9 +163,9 @@ should_close :: proc() -> bool {
|
||||
}
|
||||
|
||||
force_reload :: proc() -> bool {
|
||||
return core.key_state(.Key_F5) == .Press
|
||||
return platform.os_key_state(.Key_F5) == .Press
|
||||
}
|
||||
|
||||
force_restart :: proc() -> bool {
|
||||
return core.key_state(.Key_F6) == .Press
|
||||
return platform.os_key_state(.Key_F6) == .Press
|
||||
}
|
||||
|
||||
@@ -84,6 +84,9 @@ Joystick_Disconnect_Event :: struct {
|
||||
id: i32,
|
||||
}
|
||||
|
||||
// TODO: Settings change event
|
||||
// Settings_Change_Event :: struct {}
|
||||
|
||||
// Message Receiver
|
||||
@(private)
|
||||
Listener :: struct {
|
||||
|
||||
@@ -7,14 +7,20 @@
|
||||
package base
|
||||
|
||||
// -----------------------------------------
|
||||
// Types
|
||||
|
||||
Settings :: struct {
|
||||
// Window
|
||||
width: u16,
|
||||
height: u16,
|
||||
title: string,
|
||||
mode: WindowMode,
|
||||
refresh: u16, // 0 = unlimited
|
||||
vsync: bool,
|
||||
fov: f32,
|
||||
// Input
|
||||
mouse_sensitivity: f32,
|
||||
mouse_captured: bool,
|
||||
// TODO:
|
||||
// windowed resizable y/n
|
||||
// exit key (ex: escape)
|
||||
@@ -25,3 +31,22 @@ WindowMode :: enum u8 {
|
||||
Borderless = 1,
|
||||
Fullscreen = 2,
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
// Public functions
|
||||
|
||||
settings :: proc() -> Settings {
|
||||
return {
|
||||
// Window
|
||||
width = 960,
|
||||
height = 540,
|
||||
title = "WGPU Native",
|
||||
mode = WindowMode.Windowed,
|
||||
refresh = 60,
|
||||
vsync = true,
|
||||
fov = 60,
|
||||
// Input
|
||||
mouse_sensitivity = 0.005,
|
||||
mouse_captured = false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,6 @@
|
||||
|
||||
package core
|
||||
|
||||
import "sindri:base"
|
||||
|
||||
// -----------------------------------------
|
||||
// Types
|
||||
|
||||
@@ -19,7 +17,6 @@ Engine_API :: struct {
|
||||
|
||||
// API for when the Engine talks to the Game.
|
||||
Game_API :: struct {
|
||||
settings: proc() -> base.Settings,
|
||||
register_api: proc(api: ^Engine_API),
|
||||
register_memory: proc(),
|
||||
init_once: proc(),
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
package core
|
||||
|
||||
import "core:math"
|
||||
import "core:math/linalg"
|
||||
|
||||
import "sindri:base"
|
||||
import "sindri:platform"
|
||||
|
||||
CAMERA_SPEED :: 5
|
||||
|
||||
// -----------------------------------------
|
||||
// Public functions
|
||||
|
||||
camera_update :: proc(
|
||||
dt: f32,
|
||||
camera: ^Thing,
|
||||
mouse_delta: base.Vec2,
|
||||
sensitivity: f32 = 0.005,
|
||||
) {
|
||||
// Look
|
||||
camera.rot.y -= mouse_delta.x * sensitivity
|
||||
camera.rot.x -= mouse_delta.y * sensitivity
|
||||
camera.rot.x = clamp(camera.rot.x, -math.PI / 2 + 0.01, math.PI / 2 - 0.01)
|
||||
|
||||
// Move (yaw-relative, world-Y up)
|
||||
yaw := linalg.matrix4_rotate(camera.rot.y, {0, 1, 0})
|
||||
forward := (yaw * base.Vec4{0, 0, -1, 0}).xyz
|
||||
right := (yaw * base.Vec4{1, 0, 0, 0}).xyz
|
||||
|
||||
move := base.Vec3{0, 0, 0}
|
||||
if platform.os_key_state(.Key_W) == .Press do move += forward
|
||||
if platform.os_key_state(.Key_S) == .Press do move -= forward
|
||||
if platform.os_key_state(.Key_A) == .Press do move -= right
|
||||
if platform.os_key_state(.Key_D) == .Press do move += right
|
||||
if platform.os_key_state(.Key_Space) == .Press do move += base.Vec3{0, 1, 0}
|
||||
if platform.os_key_state(.Key_Left_Shift) == .Press do move -= base.Vec3{0, 1, 0}
|
||||
|
||||
if move != {0, 0, 0} {
|
||||
move = linalg.normalize(move) * CAMERA_SPEED
|
||||
camera.pos += move * dt
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
package core
|
||||
|
||||
import "sindri:base"
|
||||
import "sindri:platform"
|
||||
|
||||
// -----------------------------------------
|
||||
// Public functions
|
||||
|
||||
// Returns the state of the keyboard key
|
||||
key_state :: proc(key: base.Key) -> base.Action {
|
||||
return platform.os_key_state(key)
|
||||
}
|
||||
|
||||
// Returns the state of the mouse button
|
||||
mouse_button_state :: proc(button: base.Mouse_Button) -> base.Action {
|
||||
return platform.os_mouse_button_state(button)
|
||||
}
|
||||
|
||||
// Returns the position of the mouse cursor
|
||||
mouse_position :: proc() -> (x_pos: f32, y_pos: f32) {
|
||||
return platform.os_mouse_position()
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
// Private functions
|
||||
+3
-2
@@ -38,7 +38,7 @@ main :: proc() {
|
||||
// Load game API & settings
|
||||
api := game.api()
|
||||
defer api.destroy()
|
||||
settings := api.settings()
|
||||
settings := base.settings()
|
||||
|
||||
// Event systems
|
||||
base.event_bus_init(allocator)
|
||||
@@ -53,6 +53,7 @@ main :: proc() {
|
||||
// Initialize Window
|
||||
platform.os_init(settings)
|
||||
platform.os_set_monitor(settings)
|
||||
platform.os_set_mouse_capture(settings.mouse_captured)
|
||||
defer platform.os_destroy()
|
||||
|
||||
// Initialize GPU resources
|
||||
@@ -111,8 +112,8 @@ main :: proc() {
|
||||
// loop through Things, if model -> render
|
||||
// register the Things inside game.odin
|
||||
|
||||
platform.frame()
|
||||
api.render()
|
||||
platform.frame()
|
||||
|
||||
dt = time.duration_seconds(time.tick_since(start))
|
||||
gt += dt
|
||||
|
||||
+28
-16
@@ -35,8 +35,8 @@ os_init :: proc(settings: base.Settings) {
|
||||
|
||||
// Create GLFW window
|
||||
state.os.window = glfw.CreateWindow(
|
||||
i32(settings.width),
|
||||
i32(settings.height),
|
||||
cast(i32)settings.width,
|
||||
cast(i32)settings.height,
|
||||
strings.unsafe_string_to_cstring(settings.title),
|
||||
nil,
|
||||
nil,
|
||||
@@ -65,8 +65,8 @@ os_set_monitor :: proc(settings: base.Settings) {
|
||||
monitor := glfw.GetPrimaryMonitor()
|
||||
x_pos: i32
|
||||
y_pos: i32
|
||||
width := i32(settings.width)
|
||||
height := i32(settings.height)
|
||||
width := cast(i32)settings.width
|
||||
height := cast(i32)settings.height
|
||||
target_monitor: glfw.MonitorHandle
|
||||
|
||||
mode := glfw.GetVideoMode(monitor)
|
||||
@@ -86,7 +86,7 @@ os_set_monitor :: proc(settings: base.Settings) {
|
||||
glfw.SetWindowAttrib(
|
||||
state.os.window,
|
||||
glfw.DECORATED,
|
||||
i32(glfw.FALSE),
|
||||
cast(i32)glfw.FALSE,
|
||||
)
|
||||
case .Windowed:
|
||||
// Put window in the center of the monitor
|
||||
@@ -96,11 +96,12 @@ os_set_monitor :: proc(settings: base.Settings) {
|
||||
glfw.SetWindowAttrib(
|
||||
state.os.window,
|
||||
glfw.DECORATED,
|
||||
i32(glfw.TRUE),
|
||||
cast(i32)glfw.TRUE,
|
||||
)
|
||||
}
|
||||
|
||||
refresh := settings.refresh == 0 ? glfw.DONT_CARE : i32(settings.refresh)
|
||||
refresh :=
|
||||
settings.refresh == 0 ? glfw.DONT_CARE : cast(i32)settings.refresh
|
||||
|
||||
glfw.SetWindowMonitor(
|
||||
state.os.window,
|
||||
@@ -123,7 +124,7 @@ os_should_close :: proc() -> bool {
|
||||
}
|
||||
|
||||
os_set_should_close :: proc(val: bool) {
|
||||
glfw.SetWindowShouldClose(state.os.window, b32(val))
|
||||
glfw.SetWindowShouldClose(state.os.window, cast(b32)val)
|
||||
}
|
||||
|
||||
os_poll_events :: proc() {
|
||||
@@ -132,7 +133,7 @@ os_poll_events :: proc() {
|
||||
|
||||
os_get_framebuffer_size :: proc() -> (width, height: u32) {
|
||||
iw, ih := glfw.GetFramebufferSize(state.os.window)
|
||||
return u32(iw), u32(ih)
|
||||
return cast(u32)iw, cast(u32)ih
|
||||
}
|
||||
|
||||
os_get_surface :: proc(instance: wgpu.Instance) -> wgpu.Surface {
|
||||
@@ -140,16 +141,24 @@ os_get_surface :: proc(instance: wgpu.Instance) -> wgpu.Surface {
|
||||
}
|
||||
|
||||
os_key_state :: proc(key: base.Key) -> base.Action {
|
||||
return input_action(glfw.GetKey(state.os.window, i32(key)))
|
||||
return input_action(glfw.GetKey(state.os.window, cast(i32)key))
|
||||
}
|
||||
|
||||
os_mouse_button_state :: proc(button: base.Mouse_Button) -> base.Action {
|
||||
return input_action(glfw.GetMouseButton(state.os.window, i32(button)))
|
||||
return input_action(glfw.GetMouseButton(state.os.window, cast(i32)button))
|
||||
}
|
||||
|
||||
os_mouse_position :: proc() -> (x_pos: f32, y_pos: f32) {
|
||||
os_mouse_position :: proc() -> base.Vec2 {
|
||||
x_pos_f64, y_pos_64 := glfw.GetCursorPos(state.os.window)
|
||||
return f32(x_pos_f64), f32(y_pos_64)
|
||||
return {cast(f32)x_pos_f64, cast(f32)y_pos_64}
|
||||
}
|
||||
|
||||
os_set_mouse_capture :: proc(capture: bool) {
|
||||
glfw.SetInputMode(
|
||||
state.os.window,
|
||||
glfw.CURSOR,
|
||||
capture ? glfw.CURSOR_DISABLED : glfw.CURSOR_NORMAL,
|
||||
)
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
@@ -265,7 +274,10 @@ cursor_pos_callback :: proc "c" (
|
||||
) {
|
||||
context = state.ctx
|
||||
base.on_event(
|
||||
base.Mouse_Position_Event{x_pos = f32(x_pos), y_pos = f32(y_pos)},
|
||||
base.Mouse_Position_Event {
|
||||
x_pos = cast(f32)x_pos,
|
||||
y_pos = cast(f32)y_pos,
|
||||
},
|
||||
)
|
||||
} // GLFWcursorposfun
|
||||
|
||||
@@ -278,8 +290,8 @@ scroll_callback :: proc "c" (
|
||||
context = state.ctx
|
||||
base.on_event(
|
||||
base.Mouse_Scroll_Event {
|
||||
x_offset = f32(x_offset),
|
||||
y_offset = f32(y_offset),
|
||||
x_offset = cast(f32)x_offset,
|
||||
y_offset = cast(f32)y_offset,
|
||||
},
|
||||
)
|
||||
} // GLFWscrollfun
|
||||
|
||||
+40
-18
@@ -17,9 +17,18 @@ import "wgpu:wgpu"
|
||||
|
||||
import "sindri:base"
|
||||
|
||||
CAMERA_NEAR :: 0.1
|
||||
CAMERA_FAR :: 100
|
||||
|
||||
// -----------------------------------------
|
||||
// Types
|
||||
|
||||
// Main camera
|
||||
Camera :: struct {
|
||||
pos: base.Vec3,
|
||||
rot: base.Vec3,
|
||||
}
|
||||
|
||||
// Updated once per frame
|
||||
Camera_Uniform :: struct {
|
||||
mvp: base.Mat4,
|
||||
@@ -76,9 +85,11 @@ state: struct {
|
||||
depth_texture: wgpu.Texture,
|
||||
depth_texture_view: wgpu.TextureView,
|
||||
// ----------------------------------------
|
||||
camera: Camera,
|
||||
camera_uniform_buffer: wgpu.Buffer,
|
||||
camera_bind_group_layout: wgpu.BindGroupLayout,
|
||||
camera_bind_group: wgpu.BindGroup,
|
||||
camera_fov: f32,
|
||||
// ----------------------------------------
|
||||
material_bind_group_layout: wgpu.BindGroupLayout,
|
||||
materials: map[string]Material,
|
||||
@@ -127,6 +138,7 @@ instance_init :: proc(
|
||||
// Present modes
|
||||
get_present_modes()
|
||||
state.vsync = settings.vsync
|
||||
state.camera_fov = settings.fov
|
||||
|
||||
// Device
|
||||
wgpu.AdapterRequestDevice(state.adapter, nil, {callback = on_device})
|
||||
@@ -201,17 +213,17 @@ resize :: proc "c" () {
|
||||
wgpu.SurfaceConfigure(state.surface, &state.config)
|
||||
|
||||
create_depth_texture()
|
||||
}
|
||||
|
||||
// The projection depends on aspect, so rebuild and re-upload the matrix
|
||||
frame :: proc "c" () {
|
||||
context = state.ctx
|
||||
|
||||
// Recompute and re-upload the camera matrix every frame, for pos/rot/aspect
|
||||
upload_camera(
|
||||
state.queue,
|
||||
state.camera_uniform_buffer,
|
||||
framebuffer_aspect(),
|
||||
)
|
||||
}
|
||||
|
||||
frame :: proc "c" () {
|
||||
context = state.ctx
|
||||
|
||||
surface_texture := wgpu.SurfaceGetCurrentTexture(state.surface)
|
||||
switch surface_texture.status {
|
||||
@@ -530,8 +542,8 @@ on_device :: proc "c" (
|
||||
// ----------------------------------------
|
||||
// Compile shader
|
||||
|
||||
uniforms_struct := to_wgsl_struct(Camera_Uniform, "Camera")
|
||||
defer delete(uniforms_struct)
|
||||
camera_struct := to_wgsl_struct(Camera_Uniform, "Camera")
|
||||
defer delete(camera_struct)
|
||||
vertex_input_struct := to_wgsl_struct(
|
||||
base.Vertex,
|
||||
"VertexInput",
|
||||
@@ -581,7 +593,7 @@ on_device :: proc "c" (
|
||||
return textureSample(t_diffuse, s_diffuse, in.uv) * vec4<f32>(in.col, 1.0);
|
||||
}}
|
||||
`,
|
||||
uniforms_struct,
|
||||
camera_struct,
|
||||
material_struct,
|
||||
renderable_struct,
|
||||
vertex_input_struct,
|
||||
@@ -772,13 +784,6 @@ on_device :: proc "c" (
|
||||
entries = raw_data(bind_group_entries[:]),
|
||||
},
|
||||
)
|
||||
|
||||
// Initial upload of the matrix
|
||||
upload_camera(
|
||||
state.queue,
|
||||
state.camera_uniform_buffer,
|
||||
framebuffer_aspect(),
|
||||
)
|
||||
}
|
||||
|
||||
@(private = "file")
|
||||
@@ -876,10 +881,27 @@ camera_perspective :: proc(fovy_degrees, aspect, near, far: f32) -> base.Mat4 {
|
||||
|
||||
@(private = "file")
|
||||
camera_mvp :: proc(aspect: f32) -> base.Mat4 {
|
||||
projection := camera_perspective(60, aspect, 0.1, 100)
|
||||
view := linalg.matrix4_look_at_f32({0, 2, 5}, {0, 0, 0}, {0, 1, 0})
|
||||
projection := camera_perspective(
|
||||
state.camera_fov,
|
||||
aspect,
|
||||
CAMERA_NEAR,
|
||||
CAMERA_FAR,
|
||||
)
|
||||
|
||||
yaw := linalg.matrix4_rotate(state.camera.rot.y, {0, 1, 0})
|
||||
pitch := linalg.matrix4_rotate(state.camera.rot.x, {1, 0, 0})
|
||||
rotation := yaw * pitch
|
||||
|
||||
forward := (rotation * base.Vec4{0, 0, -1, 0}).xyz
|
||||
up := (rotation * base.Vec4{0, 1, 0, 0}).xyz
|
||||
|
||||
view := linalg.matrix4_look_at_f32(
|
||||
state.camera.pos,
|
||||
state.camera.pos + forward,
|
||||
up,
|
||||
)
|
||||
// model = identity: the loader already centered the model at the origin
|
||||
return projection * view
|
||||
return projection * view // * model
|
||||
}
|
||||
|
||||
@(private = "file")
|
||||
|
||||
Reference in New Issue
Block a user