Merge branch 'main' of codeberg.org:Caznix/Zenyx
This commit is contained in:
commit
ec74b0f93b
7 changed files with 342 additions and 82 deletions
|
@ -5,6 +5,7 @@ use std::time::Instant;
|
|||
|
||||
use cgmath::{Deg, Matrix4, Point3, Rad, SquareMatrix, Vector3, perspective};
|
||||
use futures::executor::block_on;
|
||||
use thiserror::Error;
|
||||
use tracing::{debug, error, info, trace};
|
||||
use wgpu::TextureUsages;
|
||||
use wgpu::{Backends, InstanceDescriptor, util::DeviceExt};
|
||||
|
@ -223,7 +224,7 @@ struct FontState {
|
|||
impl<'window> Renderer<'window> {
|
||||
pub async fn new(window: Arc<Window>) -> Result<Self> {
|
||||
let instance = wgpu::Instance::new(&InstanceDescriptor {
|
||||
backends: Backends::from_comma_list("dx12,metal,opengl,webgpu"),
|
||||
backends: Backends::from_comma_list("dx12,metal,opengl,webgpu,vulkan"),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
|
@ -344,22 +345,31 @@ impl<'window> Renderer<'window> {
|
|||
let camera = Camera::new(&device, &camera_bind_group_layout, width, height);
|
||||
|
||||
let surface_caps = surface.get_capabilities(&adapter);
|
||||
let present_mode = [
|
||||
wgpu::PresentMode::Immediate,
|
||||
wgpu::PresentMode::Mailbox,
|
||||
wgpu::PresentMode::AutoNoVsync,
|
||||
]
|
||||
.iter()
|
||||
.copied()
|
||||
.find(|mode| surface_caps.present_modes.contains(mode))
|
||||
.unwrap_or(wgpu::PresentMode::Fifo);
|
||||
|
||||
debug!("Using {:#?} present mode.", present_mode);
|
||||
|
||||
let surface_config = wgpu::SurfaceConfiguration {
|
||||
width,
|
||||
height,
|
||||
format: surface_caps.formats[0],
|
||||
present_mode: wgpu::PresentMode::AutoNoVsync,
|
||||
present_mode,
|
||||
alpha_mode: wgpu::CompositeAlphaMode::Auto,
|
||||
view_formats: vec![],
|
||||
usage: TextureUsages::RENDER_ATTACHMENT,
|
||||
desired_maximum_frame_latency: 3,
|
||||
};
|
||||
surface.configure(&device, &surface_config);
|
||||
let (depth_texture, depth_texture_view) = create_depth_texture(
|
||||
&device,
|
||||
surface_config.width,
|
||||
surface_config.height,
|
||||
);
|
||||
let (depth_texture, depth_texture_view) =
|
||||
create_depth_texture(&device, surface_config.width, surface_config.height);
|
||||
|
||||
let font_bytes = include_bytes!("DejaVuSans.ttf");
|
||||
let font = FontRef::try_from_slice(font_bytes).map_err(|e| {
|
||||
|
@ -605,7 +615,6 @@ fn create_depth_texture(
|
|||
device: &wgpu::Device,
|
||||
width: u32,
|
||||
height: u32,
|
||||
// format: wgpu::TextureFormat,
|
||||
) -> (wgpu::Texture, wgpu::TextureView) {
|
||||
let size = wgpu::Extent3d {
|
||||
width,
|
||||
|
|
|
@ -7,12 +7,15 @@ use winit::dpi::Size;
|
|||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use tobj::Mesh;
|
||||
use tobj::{LoadOptions, Model};
|
||||
use tracing::{debug, error, info, trace, warn};
|
||||
use wgpu::rwh::HasWindowHandle;
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::event::{KeyEvent, WindowEvent};
|
||||
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
|
||||
use winit::monitor::MonitorHandle;
|
||||
use winit::window::Fullscreen;
|
||||
use winit::window::Window;
|
||||
use winit::window::WindowId;
|
||||
|
||||
|
@ -44,16 +47,16 @@ pub struct App<'window> {
|
|||
static CUBE_OBJ: &str = "
|
||||
# Blender 4.2.3 LTS
|
||||
# www.blender.org
|
||||
mtllib cube.mtl
|
||||
mtllib untitled.mtl
|
||||
o Cube
|
||||
v 1.000000 1.000000 -1.000000
|
||||
v 1.000000 -1.000000 -1.000000
|
||||
v 1.000000 1.000000 1.000000
|
||||
v 1.000000 -1.000000 1.000000
|
||||
v -1.000000 1.000000 -1.000000
|
||||
v -1.000000 -1.000000 -1.000000
|
||||
v -1.000000 1.000000 1.000000
|
||||
v -1.000000 -1.000000 1.000000
|
||||
v 0.645975 0.645975 -0.645975
|
||||
v 0.645975 -0.645975 -0.645975
|
||||
v 0.645975 0.645975 0.645975
|
||||
v 0.645975 -0.645975 0.645975
|
||||
v -0.645975 0.645975 -0.645975
|
||||
v -0.645975 -0.645975 -0.645975
|
||||
v -0.645975 0.645975 0.645975
|
||||
v -0.645975 -0.645975 0.645975
|
||||
vn -0.0000 1.0000 -0.0000
|
||||
vn -0.0000 -0.0000 1.0000
|
||||
vn -1.0000 -0.0000 -0.0000
|
||||
|
@ -82,6 +85,7 @@ f 8/8/3 7/9/3 5/10/3 6/11/3
|
|||
f 6/12/4 2/13/4 4/5/4 8/14/4
|
||||
f 2/13/5 1/1/5 3/4/5 4/5/5
|
||||
f 6/11/6 5/10/6 1/1/6 2/13/6
|
||||
|
||||
";
|
||||
|
||||
impl App<'_> {
|
||||
|
@ -103,10 +107,22 @@ impl App<'_> {
|
|||
) {
|
||||
Ok(obj) => obj,
|
||||
Err(e) => {
|
||||
error!("{e}");
|
||||
panic!()
|
||||
error!("Failed to load Pumpkin.obj: {e}");
|
||||
// Fallback to CUBE_OBJ
|
||||
let fallback_obj = CUBE_OBJ.to_string();
|
||||
tobj::load_obj_buf(
|
||||
&mut fallback_obj.as_bytes(),
|
||||
&LoadOptions {
|
||||
triangulate: true,
|
||||
single_index: true,
|
||||
..Default::default()
|
||||
},
|
||||
|_| Ok(Default::default()),
|
||||
)
|
||||
.expect("Failed to load fallback CUBE_OBJ")
|
||||
}
|
||||
};
|
||||
|
||||
let (combined_vertices, combined_indices) = parse_obj(&obj.0);
|
||||
|
||||
wgpu_ctx.add_model(&combined_vertices, &combined_indices);
|
||||
|
@ -121,10 +137,10 @@ impl App<'_> {
|
|||
);
|
||||
info!("Main window created: {:?}", window_id);
|
||||
}
|
||||
Err(e) => error!("Failed to create WGPU context: {:#}", e),
|
||||
Err(e) => error!("Failed to create WGPU context: {:}", e),
|
||||
}
|
||||
}
|
||||
Err(e) => error!("Failed to create main window: {:#}", e),
|
||||
Err(e) => error!("Failed to create main window: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -153,6 +169,7 @@ impl App<'_> {
|
|||
winit::keyboard::KeyCode::Escape => {
|
||||
self.spawn_child_window(event_loop);
|
||||
}
|
||||
winit::keyboard::KeyCode::F11 => self.toggle_fullscreen(window_id),
|
||||
other => error!("Unimplemented keycode: {:?}", other),
|
||||
},
|
||||
_ => debug!("Received a keyboard event with no physical key"),
|
||||
|
@ -182,6 +199,23 @@ impl App<'_> {
|
|||
warn!("No window context for toggling background: {:?}", window_id);
|
||||
}
|
||||
}
|
||||
fn toggle_fullscreen(&mut self, window_id: WindowId) {
|
||||
if let Some(ctx) = self.windows.get_mut(&window_id) {
|
||||
let is_fullscreen = ctx.window.fullscreen().is_some();
|
||||
let fullscreen_mode = if is_fullscreen {
|
||||
None
|
||||
} else {
|
||||
ctx.window
|
||||
.current_monitor()
|
||||
.map(|monitor| Fullscreen::Borderless(Some(monitor)))
|
||||
};
|
||||
|
||||
ctx.window.set_fullscreen(fullscreen_mode);
|
||||
debug!("Fullscreen toggled for window: {:?}", window_id);
|
||||
} else {
|
||||
warn!("No window found for fullscreen toggle: {:?}", window_id);
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_child_window(&mut self, event_loop: &ActiveEventLoop) {
|
||||
if let Some(main_ctx) = self.windows.values().find(|ctx| ctx.is_main_window()) {
|
||||
|
@ -213,7 +247,7 @@ impl App<'_> {
|
|||
let mut tmp_path: PathBuf = env::temp_dir();
|
||||
tmp_path.push("cube.obj");
|
||||
if let Err(e) = fs::write(&tmp_path, CUBE_OBJ) {
|
||||
error!("Failed to write cube OBJ to temp: {:?}", e);
|
||||
error!("Failed to write cube OBJ to temp: {}", e);
|
||||
}
|
||||
|
||||
let load_options = tobj::LoadOptions {
|
||||
|
@ -228,7 +262,7 @@ impl App<'_> {
|
|||
wgpu_ctx.add_model(&cube_vertices, &cube_indices);
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to load cube OBJ from temp file: {:?}", e)
|
||||
error!("Failed to load cube OBJ from temp file: {:#}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -242,10 +276,10 @@ impl App<'_> {
|
|||
);
|
||||
debug!("Spawned new child window: {:?}", window_id);
|
||||
}
|
||||
Err(e) => error!("Failed to create WGPU context for child window: {:?}", e),
|
||||
Err(e) => error!("Failed to create WGPU context for child window: {}", e),
|
||||
}
|
||||
}
|
||||
Err(e) => error!("Failed to create child window: {:?}", e),
|
||||
Err(e) => error!("Failed to create child window: {}", e),
|
||||
}
|
||||
} else {
|
||||
error!("No main window found. Cannot spawn a child window.");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue