Implement camera controller
This commit is contained in:
+41
-21
@@ -7,17 +7,19 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
|
import "core:math"
|
||||||
import "core:time"
|
import "core:time"
|
||||||
import "sindri:platform"
|
|
||||||
|
|
||||||
import "sindri:base"
|
import "sindri:base"
|
||||||
import "sindri:core"
|
import "sindri:core"
|
||||||
|
import "sindri:platform"
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
// Types
|
// Types
|
||||||
|
|
||||||
Game_Memory :: struct {
|
Game_Memory :: struct {
|
||||||
should_close: bool,
|
should_close: bool,
|
||||||
|
camera: core.Thing_Ref,
|
||||||
}
|
}
|
||||||
|
|
||||||
Test_Memory :: struct {
|
Test_Memory :: struct {
|
||||||
@@ -34,7 +36,6 @@ t: Test_Memory
|
|||||||
engine: ^core.Engine_API
|
engine: ^core.Engine_API
|
||||||
|
|
||||||
game_api := core.Game_API {
|
game_api := core.Game_API {
|
||||||
settings = settings,
|
|
||||||
register_api = register_api,
|
register_api = register_api,
|
||||||
register_memory = register_memory,
|
register_memory = register_memory,
|
||||||
init_once = init_once,
|
init_once = init_once,
|
||||||
@@ -47,6 +48,8 @@ game_api := core.Game_API {
|
|||||||
force_restart = force_restart,
|
force_restart = force_restart,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mouse_last: base.Vec2
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
// Public functions
|
// Public functions
|
||||||
|
|
||||||
@@ -54,17 +57,6 @@ api :: proc() -> ^core.Game_API {
|
|||||||
return &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) {
|
register_api :: proc(api: ^core.Engine_API) {
|
||||||
engine = api
|
engine = api
|
||||||
}
|
}
|
||||||
@@ -77,8 +69,11 @@ register_memory :: proc() {
|
|||||||
init_once :: proc() {
|
init_once :: proc() {
|
||||||
fmt.println("init once")
|
fmt.println("init once")
|
||||||
|
|
||||||
camera := core.thing_add(engine.things)
|
g.camera = core.thing_add(engine.things)
|
||||||
player := 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() {
|
init :: proc() {
|
||||||
@@ -113,18 +108,43 @@ init :: proc() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clicked: bool
|
||||||
|
captured: bool
|
||||||
update :: proc(dt: f32) {
|
update :: proc(dt: f32) {
|
||||||
fmt.println("dt:", dt)
|
fmt.println("dt:", dt)
|
||||||
|
|
||||||
if core.key_state(.Key_Escape) == .Press {
|
if platform.os_key_state(.Key_Escape) == .Press {
|
||||||
g.should_close = true
|
g.should_close = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if core.key_state(.Key_F5) == .Press do t.asd = 555
|
if platform.os_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_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
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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() { }
|
render :: proc() {
|
||||||
|
camera := core.thing_get(engine.things, g.camera)
|
||||||
|
platform.state.camera = {camera.pos, camera.rot}
|
||||||
|
}
|
||||||
|
|
||||||
destroy :: proc() {
|
destroy :: proc() {
|
||||||
fmt.println("bye")
|
fmt.println("bye")
|
||||||
@@ -143,9 +163,9 @@ should_close :: proc() -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
force_reload :: 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 {
|
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,
|
id: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Settings change event
|
||||||
|
// Settings_Change_Event :: struct {}
|
||||||
|
|
||||||
// Message Receiver
|
// Message Receiver
|
||||||
@(private)
|
@(private)
|
||||||
Listener :: struct {
|
Listener :: struct {
|
||||||
|
|||||||
+31
-6
@@ -7,14 +7,20 @@
|
|||||||
package base
|
package base
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
|
// Types
|
||||||
|
|
||||||
Settings :: struct {
|
Settings :: struct {
|
||||||
width: u16,
|
// Window
|
||||||
height: u16,
|
width: u16,
|
||||||
title: string,
|
height: u16,
|
||||||
mode: WindowMode,
|
title: string,
|
||||||
refresh: u16, // 0 = unlimited
|
mode: WindowMode,
|
||||||
vsync: bool,
|
refresh: u16, // 0 = unlimited
|
||||||
|
vsync: bool,
|
||||||
|
fov: f32,
|
||||||
|
// Input
|
||||||
|
mouse_sensitivity: f32,
|
||||||
|
mouse_captured: bool,
|
||||||
// TODO:
|
// TODO:
|
||||||
// windowed resizable y/n
|
// windowed resizable y/n
|
||||||
// exit key (ex: escape)
|
// exit key (ex: escape)
|
||||||
@@ -25,3 +31,22 @@ WindowMode :: enum u8 {
|
|||||||
Borderless = 1,
|
Borderless = 1,
|
||||||
Fullscreen = 2,
|
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
|
package core
|
||||||
|
|
||||||
import "sindri:base"
|
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
// Types
|
// Types
|
||||||
|
|
||||||
@@ -19,7 +17,6 @@ Engine_API :: struct {
|
|||||||
|
|
||||||
// API for when the Engine talks to the Game.
|
// API for when the Engine talks to the Game.
|
||||||
Game_API :: struct {
|
Game_API :: struct {
|
||||||
settings: proc() -> base.Settings,
|
|
||||||
register_api: proc(api: ^Engine_API),
|
register_api: proc(api: ^Engine_API),
|
||||||
register_memory: proc(),
|
register_memory: proc(),
|
||||||
init_once: 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
|
// Load game API & settings
|
||||||
api := game.api()
|
api := game.api()
|
||||||
defer api.destroy()
|
defer api.destroy()
|
||||||
settings := api.settings()
|
settings := base.settings()
|
||||||
|
|
||||||
// Event systems
|
// Event systems
|
||||||
base.event_bus_init(allocator)
|
base.event_bus_init(allocator)
|
||||||
@@ -53,6 +53,7 @@ main :: proc() {
|
|||||||
// Initialize Window
|
// Initialize Window
|
||||||
platform.os_init(settings)
|
platform.os_init(settings)
|
||||||
platform.os_set_monitor(settings)
|
platform.os_set_monitor(settings)
|
||||||
|
platform.os_set_mouse_capture(settings.mouse_captured)
|
||||||
defer platform.os_destroy()
|
defer platform.os_destroy()
|
||||||
|
|
||||||
// Initialize GPU resources
|
// Initialize GPU resources
|
||||||
@@ -111,8 +112,8 @@ main :: proc() {
|
|||||||
// loop through Things, if model -> render
|
// loop through Things, if model -> render
|
||||||
// register the Things inside game.odin
|
// register the Things inside game.odin
|
||||||
|
|
||||||
platform.frame()
|
|
||||||
api.render()
|
api.render()
|
||||||
|
platform.frame()
|
||||||
|
|
||||||
dt = time.duration_seconds(time.tick_since(start))
|
dt = time.duration_seconds(time.tick_since(start))
|
||||||
gt += dt
|
gt += dt
|
||||||
|
|||||||
+28
-16
@@ -35,8 +35,8 @@ os_init :: proc(settings: base.Settings) {
|
|||||||
|
|
||||||
// Create GLFW window
|
// Create GLFW window
|
||||||
state.os.window = glfw.CreateWindow(
|
state.os.window = glfw.CreateWindow(
|
||||||
i32(settings.width),
|
cast(i32)settings.width,
|
||||||
i32(settings.height),
|
cast(i32)settings.height,
|
||||||
strings.unsafe_string_to_cstring(settings.title),
|
strings.unsafe_string_to_cstring(settings.title),
|
||||||
nil,
|
nil,
|
||||||
nil,
|
nil,
|
||||||
@@ -65,8 +65,8 @@ os_set_monitor :: proc(settings: base.Settings) {
|
|||||||
monitor := glfw.GetPrimaryMonitor()
|
monitor := glfw.GetPrimaryMonitor()
|
||||||
x_pos: i32
|
x_pos: i32
|
||||||
y_pos: i32
|
y_pos: i32
|
||||||
width := i32(settings.width)
|
width := cast(i32)settings.width
|
||||||
height := i32(settings.height)
|
height := cast(i32)settings.height
|
||||||
target_monitor: glfw.MonitorHandle
|
target_monitor: glfw.MonitorHandle
|
||||||
|
|
||||||
mode := glfw.GetVideoMode(monitor)
|
mode := glfw.GetVideoMode(monitor)
|
||||||
@@ -86,7 +86,7 @@ os_set_monitor :: proc(settings: base.Settings) {
|
|||||||
glfw.SetWindowAttrib(
|
glfw.SetWindowAttrib(
|
||||||
state.os.window,
|
state.os.window,
|
||||||
glfw.DECORATED,
|
glfw.DECORATED,
|
||||||
i32(glfw.FALSE),
|
cast(i32)glfw.FALSE,
|
||||||
)
|
)
|
||||||
case .Windowed:
|
case .Windowed:
|
||||||
// Put window in the center of the monitor
|
// Put window in the center of the monitor
|
||||||
@@ -96,11 +96,12 @@ os_set_monitor :: proc(settings: base.Settings) {
|
|||||||
glfw.SetWindowAttrib(
|
glfw.SetWindowAttrib(
|
||||||
state.os.window,
|
state.os.window,
|
||||||
glfw.DECORATED,
|
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(
|
glfw.SetWindowMonitor(
|
||||||
state.os.window,
|
state.os.window,
|
||||||
@@ -123,7 +124,7 @@ os_should_close :: proc() -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
os_set_should_close :: proc(val: 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() {
|
os_poll_events :: proc() {
|
||||||
@@ -132,7 +133,7 @@ os_poll_events :: proc() {
|
|||||||
|
|
||||||
os_get_framebuffer_size :: proc() -> (width, height: u32) {
|
os_get_framebuffer_size :: proc() -> (width, height: u32) {
|
||||||
iw, ih := glfw.GetFramebufferSize(state.os.window)
|
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 {
|
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 {
|
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 {
|
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)
|
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
|
context = state.ctx
|
||||||
base.on_event(
|
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
|
} // GLFWcursorposfun
|
||||||
|
|
||||||
@@ -278,8 +290,8 @@ scroll_callback :: proc "c" (
|
|||||||
context = state.ctx
|
context = state.ctx
|
||||||
base.on_event(
|
base.on_event(
|
||||||
base.Mouse_Scroll_Event {
|
base.Mouse_Scroll_Event {
|
||||||
x_offset = f32(x_offset),
|
x_offset = cast(f32)x_offset,
|
||||||
y_offset = f32(y_offset),
|
y_offset = cast(f32)y_offset,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
} // GLFWscrollfun
|
} // GLFWscrollfun
|
||||||
|
|||||||
+40
-18
@@ -17,9 +17,18 @@ import "wgpu:wgpu"
|
|||||||
|
|
||||||
import "sindri:base"
|
import "sindri:base"
|
||||||
|
|
||||||
|
CAMERA_NEAR :: 0.1
|
||||||
|
CAMERA_FAR :: 100
|
||||||
|
|
||||||
// -----------------------------------------
|
// -----------------------------------------
|
||||||
// Types
|
// Types
|
||||||
|
|
||||||
|
// Main camera
|
||||||
|
Camera :: struct {
|
||||||
|
pos: base.Vec3,
|
||||||
|
rot: base.Vec3,
|
||||||
|
}
|
||||||
|
|
||||||
// Updated once per frame
|
// Updated once per frame
|
||||||
Camera_Uniform :: struct {
|
Camera_Uniform :: struct {
|
||||||
mvp: base.Mat4,
|
mvp: base.Mat4,
|
||||||
@@ -76,9 +85,11 @@ state: struct {
|
|||||||
depth_texture: wgpu.Texture,
|
depth_texture: wgpu.Texture,
|
||||||
depth_texture_view: wgpu.TextureView,
|
depth_texture_view: wgpu.TextureView,
|
||||||
// ----------------------------------------
|
// ----------------------------------------
|
||||||
|
camera: Camera,
|
||||||
camera_uniform_buffer: wgpu.Buffer,
|
camera_uniform_buffer: wgpu.Buffer,
|
||||||
camera_bind_group_layout: wgpu.BindGroupLayout,
|
camera_bind_group_layout: wgpu.BindGroupLayout,
|
||||||
camera_bind_group: wgpu.BindGroup,
|
camera_bind_group: wgpu.BindGroup,
|
||||||
|
camera_fov: f32,
|
||||||
// ----------------------------------------
|
// ----------------------------------------
|
||||||
material_bind_group_layout: wgpu.BindGroupLayout,
|
material_bind_group_layout: wgpu.BindGroupLayout,
|
||||||
materials: map[string]Material,
|
materials: map[string]Material,
|
||||||
@@ -127,6 +138,7 @@ instance_init :: proc(
|
|||||||
// Present modes
|
// Present modes
|
||||||
get_present_modes()
|
get_present_modes()
|
||||||
state.vsync = settings.vsync
|
state.vsync = settings.vsync
|
||||||
|
state.camera_fov = settings.fov
|
||||||
|
|
||||||
// Device
|
// Device
|
||||||
wgpu.AdapterRequestDevice(state.adapter, nil, {callback = on_device})
|
wgpu.AdapterRequestDevice(state.adapter, nil, {callback = on_device})
|
||||||
@@ -201,17 +213,17 @@ resize :: proc "c" () {
|
|||||||
wgpu.SurfaceConfigure(state.surface, &state.config)
|
wgpu.SurfaceConfigure(state.surface, &state.config)
|
||||||
|
|
||||||
create_depth_texture()
|
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(
|
upload_camera(
|
||||||
state.queue,
|
state.queue,
|
||||||
state.camera_uniform_buffer,
|
state.camera_uniform_buffer,
|
||||||
framebuffer_aspect(),
|
framebuffer_aspect(),
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
frame :: proc "c" () {
|
|
||||||
context = state.ctx
|
|
||||||
|
|
||||||
surface_texture := wgpu.SurfaceGetCurrentTexture(state.surface)
|
surface_texture := wgpu.SurfaceGetCurrentTexture(state.surface)
|
||||||
switch surface_texture.status {
|
switch surface_texture.status {
|
||||||
@@ -530,8 +542,8 @@ on_device :: proc "c" (
|
|||||||
// ----------------------------------------
|
// ----------------------------------------
|
||||||
// Compile shader
|
// Compile shader
|
||||||
|
|
||||||
uniforms_struct := to_wgsl_struct(Camera_Uniform, "Camera")
|
camera_struct := to_wgsl_struct(Camera_Uniform, "Camera")
|
||||||
defer delete(uniforms_struct)
|
defer delete(camera_struct)
|
||||||
vertex_input_struct := to_wgsl_struct(
|
vertex_input_struct := to_wgsl_struct(
|
||||||
base.Vertex,
|
base.Vertex,
|
||||||
"VertexInput",
|
"VertexInput",
|
||||||
@@ -581,7 +593,7 @@ on_device :: proc "c" (
|
|||||||
return textureSample(t_diffuse, s_diffuse, in.uv) * vec4<f32>(in.col, 1.0);
|
return textureSample(t_diffuse, s_diffuse, in.uv) * vec4<f32>(in.col, 1.0);
|
||||||
}}
|
}}
|
||||||
`,
|
`,
|
||||||
uniforms_struct,
|
camera_struct,
|
||||||
material_struct,
|
material_struct,
|
||||||
renderable_struct,
|
renderable_struct,
|
||||||
vertex_input_struct,
|
vertex_input_struct,
|
||||||
@@ -772,13 +784,6 @@ on_device :: proc "c" (
|
|||||||
entries = raw_data(bind_group_entries[:]),
|
entries = raw_data(bind_group_entries[:]),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
// Initial upload of the matrix
|
|
||||||
upload_camera(
|
|
||||||
state.queue,
|
|
||||||
state.camera_uniform_buffer,
|
|
||||||
framebuffer_aspect(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@(private = "file")
|
@(private = "file")
|
||||||
@@ -876,10 +881,27 @@ camera_perspective :: proc(fovy_degrees, aspect, near, far: f32) -> base.Mat4 {
|
|||||||
|
|
||||||
@(private = "file")
|
@(private = "file")
|
||||||
camera_mvp :: proc(aspect: f32) -> base.Mat4 {
|
camera_mvp :: proc(aspect: f32) -> base.Mat4 {
|
||||||
projection := camera_perspective(60, aspect, 0.1, 100)
|
projection := camera_perspective(
|
||||||
view := linalg.matrix4_look_at_f32({0, 2, 5}, {0, 0, 0}, {0, 1, 0})
|
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
|
// model = identity: the loader already centered the model at the origin
|
||||||
return projection * view
|
return projection * view // * model
|
||||||
}
|
}
|
||||||
|
|
||||||
@(private = "file")
|
@(private = "file")
|
||||||
|
|||||||
Reference in New Issue
Block a user