Finished winit upgrade and new rendering implementation

This commit is contained in:
Verox001 2025-01-16 18:11:32 +01:00
parent 05c733dc25
commit fae154a666
4 changed files with 385 additions and 111 deletions

346
Cargo.lock generated
View File

@ -48,9 +48,9 @@ dependencies = [
[[package]]
name = "android-activity"
version = "0.5.2"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee91c0c2905bae44f84bfa4e044536541df26b7703fd0888deeb9060fcc44289"
checksum = "ef6978589202a00cd7e118380c448a08b6ed394c3a8df3a430d0898e3a42d046"
dependencies = [
"android-properties",
"bitflags 2.7.0",
@ -62,7 +62,7 @@ dependencies = [
"log",
"ndk",
"ndk-context",
"ndk-sys",
"ndk-sys 0.6.0+11769913",
"num_enum",
"thiserror",
]
@ -169,22 +169,12 @@ version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a"
[[package]]
name = "block-sys"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae85a0696e7ea3b835a453750bf002770776609115e6d25c6d2ff28a8200f7e7"
dependencies = [
"objc-sys",
]
[[package]]
name = "block2"
version = "0.3.0"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15b55663a85f33501257357e6421bb33e769d5c9ffb5ba0921c975a123e35e68"
checksum = "2c132eebf10f5cad5289222520a4a058514204aed6d791f1cf4fe8088b82d15f"
dependencies = [
"block-sys",
"objc2",
]
@ -228,9 +218,9 @@ checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b"
[[package]]
name = "calloop"
version = "0.12.4"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fba7adb4dd5aa98e5553510223000e7148f621165ec5f9acd7113f6ca4995298"
checksum = "b99da2f8558ca23c71f4fd15dc57c906239752dd27ff3c00a1d56b685b7cbfec"
dependencies = [
"bitflags 2.7.0",
"log",
@ -242,9 +232,9 @@ dependencies = [
[[package]]
name = "calloop-wayland-source"
version = "0.2.0"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f0ea9b9476c7fad82841a8dbb380e2eae480c21910feba80725b46931ed8f02"
checksum = "95a66a987056935f7efce4ab5668920b5d0dac4a7c99991a67395f13702ddd20"
dependencies = [
"calloop",
"rustix",
@ -281,6 +271,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e"
[[package]]
name = "cfg_aliases"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "cgmath"
version = "0.18.0"
@ -459,6 +455,12 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2"
[[package]]
name = "dpi"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f25c0e292a7ca6d6498557ff1df68f32c99850012b6ea401cf8daf771f22ff53"
[[package]]
name = "env_logger"
version = "0.10.2"
@ -699,17 +701,6 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "icrate"
version = "0.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d3aaff8a54577104bafdf686ff18565c3b6903ca5782a2026ef06e2c7aa319"
dependencies = [
"block2",
"dispatch",
"objc2",
]
[[package]]
name = "image"
version = "0.24.9"
@ -931,7 +922,7 @@ dependencies = [
"arrayvec",
"bit-set",
"bitflags 2.7.0",
"cfg_aliases",
"cfg_aliases 0.1.1",
"codespan-reporting",
"hexf-parse",
"indexmap",
@ -945,14 +936,14 @@ dependencies = [
[[package]]
name = "ndk"
version = "0.8.0"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2076a31b7010b17a38c01907c45b945e8f11495ee4dd588309718901b1f7a5b7"
checksum = "c3f42e7bbe13d351b6bead8286a43aac9534b82bd3cc43e47037f012ebfd62d4"
dependencies = [
"bitflags 2.7.0",
"jni-sys",
"log",
"ndk-sys",
"ndk-sys 0.6.0+11769913",
"num_enum",
"raw-window-handle 0.5.2",
"raw-window-handle 0.6.2",
@ -974,6 +965,15 @@ dependencies = [
"jni-sys",
]
[[package]]
name = "ndk-sys"
version = "0.6.0+11769913"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee6cda3051665f1fb8d9e08fc35c96d5a244fb1be711a03b71118828afc9a873"
dependencies = [
"jni-sys",
]
[[package]]
name = "num-traits"
version = "0.2.19"
@ -1021,19 +1021,200 @@ checksum = "cdb91bdd390c7ce1a8607f35f3ca7151b65afc0ff5ff3b34fa350f7d7c7e4310"
[[package]]
name = "objc2"
version = "0.4.1"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "559c5a40fdd30eb5e344fbceacf7595a81e242529fb4e21cf5f43fb4f11ff98d"
checksum = "46a785d4eeff09c14c487497c162e92766fbb3e4059a71840cecc03d9a50b804"
dependencies = [
"objc-sys",
"objc2-encode",
]
[[package]]
name = "objc2-encode"
version = "3.0.0"
name = "objc2-app-kit"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d079845b37af429bfe5dfa76e6d087d788031045b25cfc6fd898486fd9847666"
checksum = "e4e89ad9e3d7d297152b17d39ed92cd50ca8063a89a9fa569046d41568891eff"
dependencies = [
"bitflags 2.7.0",
"block2",
"libc",
"objc2",
"objc2-core-data",
"objc2-core-image",
"objc2-foundation",
"objc2-quartz-core",
]
[[package]]
name = "objc2-cloud-kit"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74dd3b56391c7a0596a295029734d3c1c5e7e510a4cb30245f8221ccea96b009"
dependencies = [
"bitflags 2.7.0",
"block2",
"objc2",
"objc2-core-location",
"objc2-foundation",
]
[[package]]
name = "objc2-contacts"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5ff520e9c33812fd374d8deecef01d4a840e7b41862d849513de77e44aa4889"
dependencies = [
"block2",
"objc2",
"objc2-foundation",
]
[[package]]
name = "objc2-core-data"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "617fbf49e071c178c0b24c080767db52958f716d9eabdf0890523aeae54773ef"
dependencies = [
"bitflags 2.7.0",
"block2",
"objc2",
"objc2-foundation",
]
[[package]]
name = "objc2-core-image"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55260963a527c99f1819c4f8e3b47fe04f9650694ef348ffd2227e8196d34c80"
dependencies = [
"block2",
"objc2",
"objc2-foundation",
"objc2-metal",
]
[[package]]
name = "objc2-core-location"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "000cfee34e683244f284252ee206a27953279d370e309649dc3ee317b37e5781"
dependencies = [
"block2",
"objc2",
"objc2-contacts",
"objc2-foundation",
]
[[package]]
name = "objc2-encode"
version = "4.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7891e71393cd1f227313c9379a26a584ff3d7e6e7159e988851f0934c993f0f8"
[[package]]
name = "objc2-foundation"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ee638a5da3799329310ad4cfa62fbf045d5f56e3ef5ba4149e7452dcf89d5a8"
dependencies = [
"bitflags 2.7.0",
"block2",
"dispatch",
"libc",
"objc2",
]
[[package]]
name = "objc2-link-presentation"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1a1ae721c5e35be65f01a03b6d2ac13a54cb4fa70d8a5da293d7b0020261398"
dependencies = [
"block2",
"objc2",
"objc2-app-kit",
"objc2-foundation",
]
[[package]]
name = "objc2-metal"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd0cba1276f6023976a406a14ffa85e1fdd19df6b0f737b063b95f6c8c7aadd6"
dependencies = [
"bitflags 2.7.0",
"block2",
"objc2",
"objc2-foundation",
]
[[package]]
name = "objc2-quartz-core"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e42bee7bff906b14b167da2bac5efe6b6a07e6f7c0a21a7308d40c960242dc7a"
dependencies = [
"bitflags 2.7.0",
"block2",
"objc2",
"objc2-foundation",
"objc2-metal",
]
[[package]]
name = "objc2-symbols"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a684efe3dec1b305badae1a28f6555f6ddd3bb2c2267896782858d5a78404dc"
dependencies = [
"objc2",
"objc2-foundation",
]
[[package]]
name = "objc2-ui-kit"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8bb46798b20cd6b91cbd113524c490f1686f4c4e8f49502431415f3512e2b6f"
dependencies = [
"bitflags 2.7.0",
"block2",
"objc2",
"objc2-cloud-kit",
"objc2-core-data",
"objc2-core-image",
"objc2-core-location",
"objc2-foundation",
"objc2-link-presentation",
"objc2-quartz-core",
"objc2-symbols",
"objc2-uniform-type-identifiers",
"objc2-user-notifications",
]
[[package]]
name = "objc2-uniform-type-identifiers"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44fa5f9748dbfe1ca6c0b79ad20725a11eca7c2218bceb4b005cb1be26273bfe"
dependencies = [
"block2",
"objc2",
"objc2-foundation",
]
[[package]]
name = "objc2-user-notifications"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76cfcbf642358e8689af64cee815d139339f3ed8ad05103ed5eaf73db8d84cb3"
dependencies = [
"bitflags 2.7.0",
"block2",
"objc2",
"objc2-core-location",
"objc2-foundation",
]
[[package]]
name = "once_cell"
@ -1114,6 +1295,26 @@ version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
name = "pin-project"
version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e2ec53ad785f4d35dac0adea7f7dc6f1bb277ad84a680c7afefeae05d1f5916"
dependencies = [
"pin-project-internal",
]
[[package]]
name = "pin-project-internal"
version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d56a66c0c55993aa927429d0f8a0abfd74f084e4d9c192cffed01e418d83eefb"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.96",
]
[[package]]
name = "pin-project-lite"
version = "0.2.16"
@ -1228,9 +1429,9 @@ checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539"
[[package]]
name = "redox_syscall"
version = "0.3.5"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29"
checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa"
dependencies = [
"bitflags 1.3.2",
]
@ -1327,9 +1528,9 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "sctk-adwaita"
version = "0.8.3"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70b31447ca297092c5a9916fc3b955203157b37c19ca8edde4f52e9843e602c7"
checksum = "b6277f0217056f77f1d8f49f2950ac6c278c0d607c45f5ee99328d792ede24ec"
dependencies = [
"ab_glyph",
"log",
@ -1396,9 +1597,9 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "smithay-client-toolkit"
version = "0.18.1"
version = "0.19.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "922fd3eeab3bd820d76537ce8f582b1cf951eceb5475c28500c7457d9d17f53a"
checksum = "3457dea1f0eb631b4034d61d4d8c32074caa6cd1ab2d59f2327bd8461e2c0016"
dependencies = [
"bitflags 2.7.0",
"calloop",
@ -1737,9 +1938,9 @@ dependencies = [
[[package]]
name = "wayland-protocols"
version = "0.31.2"
version = "0.32.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f81f365b8b4a97f422ac0e8737c438024b5951734506b0e1d775c73030561f4"
checksum = "7cd0ade57c4e6e9a8952741325c30bf82f4246885dca8bf561898b86d0c1f58e"
dependencies = [
"bitflags 2.7.0",
"wayland-backend",
@ -1749,9 +1950,9 @@ dependencies = [
[[package]]
name = "wayland-protocols-plasma"
version = "0.2.0"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23803551115ff9ea9bce586860c5c5a971e360825a0309264102a9495a5ff479"
checksum = "9b31cab548ee68c7eb155517f2212049dc151f7cd7910c2b66abfd31c3ee12bd"
dependencies = [
"bitflags 2.7.0",
"wayland-backend",
@ -1762,9 +1963,9 @@ dependencies = [
[[package]]
name = "wayland-protocols-wlr"
version = "0.2.0"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad1f61b76b6c2d8742e10f9ba5c3737f6530b4c243132c2a2ccc8aa96fe25cd6"
checksum = "782e12f6cd923c3c316130d56205ebab53f55d6666b7faddfad36cecaeeb4022"
dependencies = [
"bitflags 2.7.0",
"wayland-backend",
@ -1808,9 +2009,9 @@ dependencies = [
[[package]]
name = "web-time"
version = "0.2.4"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa30049b1c872b72c89866d458eae9f20380ab280ffd1b1e18df2d3e2d98cfe0"
checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb"
dependencies = [
"js-sys",
"wasm-bindgen",
@ -1823,7 +2024,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1d1c4ba43f80542cf63a0a6ed3134629ae73e8ab51e4b765a67f3aa062eb433"
dependencies = [
"arrayvec",
"cfg_aliases",
"cfg_aliases 0.1.1",
"document-features",
"js-sys",
"log",
@ -1850,7 +2051,7 @@ dependencies = [
"arrayvec",
"bit-vec",
"bitflags 2.7.0",
"cfg_aliases",
"cfg_aliases 0.1.1",
"document-features",
"indexmap",
"log",
@ -1878,7 +2079,7 @@ dependencies = [
"bit-set",
"bitflags 2.7.0",
"block",
"cfg_aliases",
"cfg_aliases 0.1.1",
"core-graphics-types",
"d3d12",
"glow",
@ -1894,7 +2095,7 @@ dependencies = [
"log",
"metal",
"naga",
"ndk-sys",
"ndk-sys 0.5.0+25.2.9519653",
"objc",
"once_cell",
"parking_lot",
@ -1987,15 +2188,6 @@ dependencies = [
"windows-targets 0.42.2",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
@ -2194,38 +2386,42 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "winit"
version = "0.29.15"
version = "0.30.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d59ad965a635657faf09c8f062badd885748428933dad8e8bdd64064d92e5ca"
checksum = "f5d74280aabb958072864bff6cfbcf9025cf8bfacdde5e32b5e12920ef703b0f"
dependencies = [
"ahash",
"android-activity",
"atomic-waker",
"bitflags 2.7.0",
"block2",
"bytemuck",
"calloop",
"cfg_aliases",
"cfg_aliases 0.2.1",
"concurrent-queue",
"core-foundation",
"core-graphics",
"cursor-icon",
"icrate",
"dpi",
"js-sys",
"libc",
"log",
"memmap2",
"ndk",
"ndk-sys",
"objc2",
"once_cell",
"objc2-app-kit",
"objc2-foundation",
"objc2-ui-kit",
"orbclient",
"percent-encoding",
"pin-project",
"raw-window-handle 0.5.2",
"raw-window-handle 0.6.2",
"redox_syscall 0.3.5",
"redox_syscall 0.4.1",
"rustix",
"sctk-adwaita",
"smithay-client-toolkit",
"smol_str",
"tracing",
"unicode-segmentation",
"wasm-bindgen",
"wasm-bindgen-futures",
@ -2235,7 +2431,7 @@ dependencies = [
"wayland-protocols-plasma",
"web-sys",
"web-time",
"windows-sys 0.48.0",
"windows-sys 0.52.0",
"x11-dl",
"x11rb",
"xkbcommon-dl",

View File

@ -13,7 +13,7 @@ pollster = "0.3"
log = "0.4"
tobj = { version = "3.2", default-features = false, features = ["async"]}
wgpu = { version = "22.0"}
winit = { version = "0.29", features = ["rwh_05"] }
winit = { version = "0.30.8", features = ["rwh_05"] }
instant = "0.1"
[dependencies.image]

View File

@ -121,11 +121,11 @@ impl CameraController {
self.amount_right = amount;
true
}
KeyCode::Space => {
KeyCode::ShiftLeft => {
self.amount_up = amount;
true
}
KeyCode::ShiftLeft => {
KeyCode::ControlLeft => {
self.amount_down = amount;
true
}
@ -134,15 +134,14 @@ impl CameraController {
}
pub fn process_mouse(&mut self, mouse_dx: f64, mouse_dy: f64) {
self.rotate_horizontal = mouse_dx as f32;
self.rotate_vertical = mouse_dy as f32;
self.rotate_horizontal += mouse_dx as f32 * self.sensitivity;
self.rotate_vertical += mouse_dy as f32 * self.sensitivity;
}
pub fn process_scroll(&mut self, delta: &MouseScrollDelta) {
self.scroll = match delta {
// I'm assuming a line is about 100 pixels
MouseScrollDelta::LineDelta(_, scroll) => -scroll * 0.5,
MouseScrollDelta::PixelDelta(PhysicalPosition { y: scroll, .. }) => -*scroll as f32,
MouseScrollDelta::LineDelta(_, scroll) => scroll * 4.0 * self.speed,
MouseScrollDelta::PixelDelta(PhysicalPosition { y: scroll, .. }) => *scroll as f32 * self.speed,
};
}

View File

@ -1,5 +1,5 @@
use std::{f32::consts::PI, iter};
use std::sync::Arc;
use cgmath::prelude::*;
use wgpu::util::DeviceExt;
use winit::{
@ -8,6 +8,10 @@ use winit::{
keyboard::{KeyCode, PhysicalKey},
window::Window,
};
use winit::application::ApplicationHandler;
use winit::dpi::{PhysicalPosition, PhysicalSize};
use winit::event_loop::ActiveEventLoop;
use winit::window::WindowId;
mod camera;
mod hdr;
@ -26,10 +30,10 @@ const NUM_INSTANCES_PER_ROW: u32 = 10;
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct CameraUniform {
view_position: [f32; 4],
view: [[f32; 4]; 4], // NEW!
view: [[f32; 4]; 4],
view_proj: [[f32; 4]; 4],
inv_proj: [[f32; 4]; 4], // NEW!
inv_view: [[f32; 4]; 4], // NEW!
inv_proj: [[f32; 4]; 4],
inv_view: [[f32; 4]; 4],
}
impl CameraUniform {
@ -43,7 +47,6 @@ impl CameraUniform {
}
}
// UPDATED!
fn update_view_proj(&mut self, camera: &camera::Camera, projection: &camera::Projection) {
self.view_position = camera.position.to_homogeneous().into();
let proj = projection.calc_matrix();
@ -144,8 +147,63 @@ struct LightUniform {
_padding2: u32,
}
struct StateApplication<'a> {
state: Option<State<'a>>,
last_render_time: instant::Instant,
}
impl<'a> StateApplication<'a> {
pub fn new() -> Self {
Self {
state: None,
last_render_time: instant::Instant::now(),
}
}
}
impl<'a> ApplicationHandler for StateApplication<'a>{
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let window = event_loop
.create_window(Window::default_attributes().with_title(env!("CARGO_PKG_NAME")))
.unwrap();
self.state = Some(pollster::block_on(State::new(window)));
}
fn window_event(&mut self, event_loop: &ActiveEventLoop, window_id: WindowId, event: WindowEvent) {
let window = self.state.as_ref().unwrap().window();
if window.id() == window_id {
if !self.state.as_mut().unwrap().input(&event) {
match event {
WindowEvent::CloseRequested => {
event_loop.exit();
}
WindowEvent::Resized(physical_size) => {
self.state.as_mut().unwrap().resize(physical_size);
}
WindowEvent::RedrawRequested => {
let now = instant::Instant::now();
let dt = now - self.last_render_time;
self.last_render_time = now;
self.state.as_mut().unwrap().update(dt);
self.state.as_mut().unwrap().render().unwrap();
}
_ => {},
}
}
}
}
fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
let window = self.state.as_ref().unwrap().window();
window.request_redraw();
}
}
struct State<'a> {
window: &'a Window,
window: Arc<Window>,
size: PhysicalSize<u32>,
surface: wgpu::Surface<'a>,
device: wgpu::Device,
queue: wgpu::Queue,
@ -158,11 +216,11 @@ struct State<'a> {
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
last_mouse_position: PhysicalPosition<f64>,
instances: Vec<Instance>,
#[allow(dead_code)]
instance_buffer: wgpu::Buffer,
depth_texture: texture::Texture,
size: winit::dpi::PhysicalSize<u32>,
light_uniform: LightUniform,
light_buffer: wgpu::Buffer,
light_bind_group: wgpu::BindGroup,
@ -170,7 +228,6 @@ struct State<'a> {
#[allow(dead_code)]
debug_material: model::Material,
mouse_pressed: bool,
// NEW!
hdr: hdr::HdrPipeline,
environment_bind_group: wgpu::BindGroup,
sky_pipeline: wgpu::RenderPipeline,
@ -240,8 +297,9 @@ fn create_render_pipeline(
}
impl<'a> State<'a> {
async fn new(window: &'a Window) -> anyhow::Result<State<'a>> {
let size = window.inner_size();
async fn new(window: Window) -> Self {
let window_arc = Arc::new(window);
let size = window_arc.inner_size();
// The instance is a handle to our GPU
// BackendBit::PRIMARY => Vulkan + Metal + DX12 + Browser WebGPU
@ -250,7 +308,7 @@ impl<'a> State<'a> {
..Default::default()
});
let surface = instance.create_surface(window).unwrap();
let surface = instance.create_surface(window_arc.clone()).unwrap();
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
@ -453,14 +511,14 @@ impl<'a> State<'a> {
let hdr = hdr::HdrPipeline::new(&device, &config);
let hdr_loader = resources::HdrLoader::new(&device);
let sky_bytes = resources::load_binary("pure-sky.hdr").await?;
let sky_bytes = resources::load_binary("pure-sky.hdr").await.unwrap();
let sky_texture = hdr_loader.from_equirectangular_bytes(
&device,
&queue,
&sky_bytes,
1080,
Some("Sky Texture"),
)?;
).unwrap();
let environment_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
@ -601,8 +659,9 @@ impl<'a> State<'a> {
#[cfg(feature = "debug")]
let debug = debug::Debug::new(&device, &camera_bind_group_layout, surface_format);
Ok(Self {
window,
Self {
window: window_arc,
size,
surface,
device,
queue,
@ -614,11 +673,11 @@ impl<'a> State<'a> {
camera_controller,
camera_buffer,
camera_bind_group,
last_mouse_position: PhysicalPosition::new(0.0, 0.0),
camera_uniform,
instances,
instance_buffer,
depth_texture,
size,
light_uniform,
light_buffer,
light_bind_group,
@ -626,14 +685,13 @@ impl<'a> State<'a> {
#[allow(dead_code)]
debug_material,
mouse_pressed: false,
// NEW!
hdr,
environment_bind_group,
sky_pipeline,
#[cfg(feature = "debug")]
debug,
})
}
}
pub fn window(&self) -> &Window {
@ -643,12 +701,18 @@ impl<'a> State<'a> {
fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 {
println!("Resizing to {:?}", new_size);
self.projection.resize(new_size.width, new_size.height);
self.hdr
.resize(&self.device, new_size.width, new_size.height);
self.size = new_size;
self.config.width = new_size.width;
self.config.height = new_size.height;
// Begrenzen der Dimensionen auf GPU-Limits
let max_texture_dimension = self.device.limits().max_texture_dimension_2d as u32;
let width = new_size.width.min(max_texture_dimension);
let height = new_size.height.min(max_texture_dimension);
self.projection.resize(width, height);
self.hdr.resize(&self.device, width, height);
self.size = PhysicalSize { width, height }; // Aktualisieren auf die begrenzten Dimensionen
self.config.width = width;
self.config.height = height;
self.surface.configure(&self.device, &self.config);
self.depth_texture =
texture::Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
@ -656,6 +720,7 @@ impl<'a> State<'a> {
}
fn input(&mut self, event: &WindowEvent) -> bool {
println!("Handle event {:?}", event);
match event {
WindowEvent::KeyboardInput {
event:
@ -678,6 +743,15 @@ impl<'a> State<'a> {
self.mouse_pressed = *state == ElementState::Pressed;
true
}
WindowEvent::CursorMoved { position, .. } => {
if self.mouse_pressed {
let delta_x = position.x as f64 - self.last_mouse_position.x as f64;
let delta_y = position.y as f64 - self.last_mouse_position.y as f64;
self.camera_controller.process_mouse(delta_x, delta_y);
}
self.last_mouse_position = *position;
true
}
_ => false,
}
}
@ -804,13 +878,18 @@ pub async fn run() {
env_logger::init();
let event_loop = EventLoop::new().unwrap();
let title = env!("CARGO_PKG_NAME");
/*let title = env!("CARGO_PKG_NAME");
let window = winit::window::WindowBuilder::new()
.with_title(title)
// Limit dimensions to 1080p
.with_inner_size(winit::dpi::PhysicalSize::new(1920, 1080))
.build(&event_loop)
.unwrap();
.unwrap();*/
let mut state = State::new(&window).await.unwrap();
let mut window_state = StateApplication::new();
let _ = event_loop.run_app(&mut window_state);
/*let mut state = State::new(&window).await.unwrap();
let mut last_render_time = instant::Instant::now();
event_loop.run(move |event, control_flow| {
match event {
@ -860,5 +939,5 @@ pub async fn run() {
}
_ => {}
}
}).unwrap();
}).unwrap();*/
}