load arbitrary model data
This commit is contained in:
parent
358dd3a6bc
commit
8097441fee
11 changed files with 3022 additions and 314 deletions
|
@ -2,7 +2,8 @@ use std::collections::HashMap;
|
|||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
|
||||
use ctx::WgpuCtx;
|
||||
use ctx::{Renderer, Vertex};
|
||||
use tobj::LoadOptions;
|
||||
use tracing::{debug, error, info, trace, warn};
|
||||
use wgpu::rwh::HasWindowHandle;
|
||||
use winit::application::ApplicationHandler;
|
||||
|
@ -15,7 +16,7 @@ pub mod ctx;
|
|||
|
||||
struct WindowContext<'window> {
|
||||
window: Arc<Window>,
|
||||
ctx: WgpuCtx<'window>,
|
||||
ctx: Renderer<'window>,
|
||||
main_window: bool,
|
||||
}
|
||||
impl Deref for WindowContext<'_> {
|
||||
|
@ -35,6 +36,161 @@ impl WindowContext<'_> {
|
|||
pub struct App<'window> {
|
||||
windows: HashMap<WindowId, WindowContext<'window>>,
|
||||
}
|
||||
static CUBE_VERTICES: &[Vertex] = &[
|
||||
Vertex {
|
||||
position: [-0.5, -0.5, 0.5],
|
||||
normal: [0.0, 0.0, 1.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, -0.5, 0.5],
|
||||
normal: [0.0, 0.0, 1.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, 0.5, 0.5],
|
||||
normal: [0.0, 0.0, 1.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, 0.5, 0.5],
|
||||
normal: [0.0, 0.0, 1.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, 0.5, 0.5],
|
||||
normal: [0.0, 0.0, 1.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, -0.5, 0.5],
|
||||
normal: [0.0, 0.0, 1.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, -0.5, -0.5],
|
||||
normal: [0.0, 0.0, -1.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, -0.5, -0.5],
|
||||
normal: [0.0, 0.0, -1.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, 0.5, -0.5],
|
||||
normal: [0.0, 0.0, -1.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, 0.5, -0.5],
|
||||
normal: [0.0, 0.0, -1.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, 0.5, -0.5],
|
||||
normal: [0.0, 0.0, -1.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, -0.5, -0.5],
|
||||
normal: [0.0, 0.0, -1.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, -0.5, 0.5],
|
||||
normal: [1.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, -0.5, -0.5],
|
||||
normal: [1.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, 0.5, -0.5],
|
||||
normal: [1.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, 0.5, -0.5],
|
||||
normal: [1.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, 0.5, 0.5],
|
||||
normal: [1.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, -0.5, 0.5],
|
||||
normal: [1.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, -0.5, -0.5],
|
||||
normal: [-1.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, -0.5, 0.5],
|
||||
normal: [-1.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, 0.5, 0.5],
|
||||
normal: [-1.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, 0.5, 0.5],
|
||||
normal: [-1.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, 0.5, -0.5],
|
||||
normal: [-1.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, -0.5, -0.5],
|
||||
normal: [-1.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, 0.5, 0.5],
|
||||
normal: [0.0, 1.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, 0.5, 0.5],
|
||||
normal: [0.0, 1.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, 0.5, -0.5],
|
||||
normal: [0.0, 1.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, 0.5, -0.5],
|
||||
normal: [0.0, 1.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, 0.5, -0.5],
|
||||
normal: [0.0, 1.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, 0.5, 0.5],
|
||||
normal: [0.0, 1.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, -0.5, -0.5],
|
||||
normal: [0.0, -1.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, -0.5, -0.5],
|
||||
normal: [0.0, -1.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, -0.5, 0.5],
|
||||
normal: [0.0, -1.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [0.5, -0.5, 0.5],
|
||||
normal: [0.0, -1.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, -0.5, 0.5],
|
||||
normal: [0.0, -1.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-0.5, -0.5, -0.5],
|
||||
normal: [0.0, -1.0, 0.0],
|
||||
},
|
||||
];
|
||||
const CUBE_INDICES: &[u32] = &[
|
||||
0, 1, 2, 2, 3, 0,
|
||||
4, 5, 6, 6, 7, 4,
|
||||
8, 9, 10, 10, 11, 8,
|
||||
12, 13, 14, 14, 15, 12,
|
||||
16, 17, 18, 18, 19, 16,
|
||||
20, 21, 22, 22, 23, 20,
|
||||
];
|
||||
|
||||
|
||||
impl App<'_> {
|
||||
fn create_main_window(&mut self, event_loop: &ActiveEventLoop) {
|
||||
|
@ -43,8 +199,45 @@ impl App<'_> {
|
|||
Ok(window) => {
|
||||
let window = Arc::new(window);
|
||||
let window_id = window.id();
|
||||
match WgpuCtx::new_blocking(window.clone()) {
|
||||
Ok(wgpu_ctx) => {
|
||||
match Renderer::new_blocking(window.clone()) {
|
||||
Ok(mut wgpu_ctx) => {
|
||||
let obj = match tobj::load_obj(
|
||||
"test.obj",
|
||||
&LoadOptions {
|
||||
triangulate: true,
|
||||
single_index: true,
|
||||
..Default::default()
|
||||
},
|
||||
) {
|
||||
Ok(obj) => obj,
|
||||
Err(e) => {
|
||||
error!("{e}");
|
||||
panic!()
|
||||
}
|
||||
};
|
||||
let mesh = obj.0.get(0).unwrap().mesh.clone();
|
||||
|
||||
let vertices: Vec<Vertex> = (0..mesh.positions.len() / 3)
|
||||
.map(|i| Vertex {
|
||||
position: [
|
||||
mesh.positions[i * 3],
|
||||
mesh.positions[i * 3 + 1],
|
||||
mesh.positions[i * 3 + 2],
|
||||
],
|
||||
normal: if !mesh.normals.is_empty() {
|
||||
[
|
||||
mesh.normals[i * 3],
|
||||
mesh.normals[i * 3 + 1],
|
||||
mesh.normals[i * 3 + 2],
|
||||
]
|
||||
} else {
|
||||
[0.0; 3]
|
||||
},
|
||||
})
|
||||
.collect();
|
||||
|
||||
wgpu_ctx.add_model(&vertices, &mesh.indices);
|
||||
// wgpu_ctx.add_model(CUBE_VERTICES,CUBE_INDICES);
|
||||
self.windows.insert(
|
||||
window_id,
|
||||
WindowContext {
|
||||
|
@ -101,7 +294,7 @@ impl App<'_> {
|
|||
wgpu::Color::BLACK => wgpu::Color::WHITE,
|
||||
_ => wgpu::Color::WHITE,
|
||||
};
|
||||
window_context.ctx.change_bg_color(new_color);
|
||||
window_context.ctx.set_bg_color(new_color);
|
||||
debug!("Toggled background color for window {:?}", window_id);
|
||||
} else {
|
||||
warn!("No window context for toggling background: {:?}", window_id);
|
||||
|
@ -111,7 +304,7 @@ impl App<'_> {
|
|||
fn spawn_child_window(&mut self, event_loop: &ActiveEventLoop) {
|
||||
if let Some(main_ctx) = self.windows.values().find(|ctx| ctx.is_main_window()) {
|
||||
let title = format!("Zenyx - New Window {}", self.windows.len());
|
||||
//TODO: Verify that this is safe instead of matching on it
|
||||
// TODO: Verify that this is safe instead of matching on it
|
||||
let win_attr = unsafe {
|
||||
let base = Window::default_attributes().with_title(title);
|
||||
match main_ctx.window_handle() {
|
||||
|
@ -123,8 +316,9 @@ impl App<'_> {
|
|||
Ok(window) => {
|
||||
let window = Arc::new(window);
|
||||
let window_id = window.id();
|
||||
match WgpuCtx::new_blocking(window.clone()) {
|
||||
Ok(wgpu_ctx) => {
|
||||
match Renderer::new_blocking(window.clone()) {
|
||||
Ok(mut wgpu_ctx) => {
|
||||
wgpu_ctx.add_model(CUBE_VERTICES,CUBE_INDICES);
|
||||
self.windows.insert(
|
||||
window_id,
|
||||
WindowContext {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue