Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples/axis.glb
Binary file not shown.
1 change: 1 addition & 0 deletions gui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ nalgebra-glm = "0.13.0"
pollster = "0.2.4"
wgpu = "0.8.1"
winit = "0.24.0"
gltf = "1.4.1"
13 changes: 7 additions & 6 deletions gui/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use nalgebra_glm as glm;
use glm::Vec2;
use gltf;
use winit::{
dpi::{PhysicalSize},
event::{ElementState, ModifiersState, WindowEvent, DeviceEvent, VirtualKeyCode, MouseScrollDelta},
};

use triangulate::mesh::Mesh;
//use triangulate::mesh::Mesh;
use crate::{backdrop::Backdrop, camera::Camera, model::Model};

pub struct App {
Expand All @@ -16,7 +17,7 @@ pub struct App {
swapchain_format: wgpu::TextureFormat,
swapchain: wgpu::SwapChain,

loader: Option<std::thread::JoinHandle<Mesh>>,
loader: Option<std::thread::JoinHandle<gltf::Gltf>>,
model: Option<Model>,
backdrop: Backdrop,
camera: Camera,
Expand All @@ -38,7 +39,7 @@ pub enum Reply {
impl App {
pub fn new(start_time: std::time::SystemTime, size: PhysicalSize<u32>,
adapter: wgpu::Adapter, surface: wgpu::Surface,
device: wgpu::Device, loader: std::thread::JoinHandle<Mesh>)
device: wgpu::Device, loader: std::thread::JoinHandle<gltf::Gltf>)
-> Self
{
let swapchain_format = adapter.get_swap_chain_preferred_format(&surface)
Expand Down Expand Up @@ -200,10 +201,10 @@ impl App {
.unwrap()
.join()
.expect("Failed to load mesh");
let model = Model::new(&self.device, self.swapchain_format,
&mesh.verts, &mesh.triangles);
let (model, vertices) = Model::new(
&self.device, self.swapchain_format,&mesh);
self.model = Some(model);
self.camera.fit_verts(&mesh.verts);
self.camera.fit_verts(&vertices);
self.first_frame = true;
} else {
self.first_frame = false;
Expand Down
10 changes: 5 additions & 5 deletions gui/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use nalgebra_glm as glm;
use glm::{Vec2, Vec3, Vec4, Mat4};
use winit::event::MouseButton;

use triangulate::mesh::Vertex;
use crate::model::GPUVertex;

#[derive(Copy, Clone, Debug)]
enum MouseState {
Expand Down Expand Up @@ -116,10 +116,10 @@ impl Camera {
}
}

pub fn fit_verts(&mut self, verts: &[Vertex]) {
let xb = verts.iter().map(|v| v.pos.x).minmax().into_option().unwrap();
let yb = verts.iter().map(|v| v.pos.y).minmax().into_option().unwrap();
let zb = verts.iter().map(|v| v.pos.z).minmax().into_option().unwrap();
pub fn fit_verts(&mut self, verts: &[GPUVertex]) {
let xb = verts.iter().map(|v| v.pos[0]).minmax().into_option().unwrap();
let yb = verts.iter().map(|v| v.pos[1]).minmax().into_option().unwrap();
let zb = verts.iter().map(|v| v.pos[2]).minmax().into_option().unwrap();
let dx = xb.1 - xb.0;
let dy = yb.1 - yb.0;
let dz = zb.1 - zb.0;
Expand Down
16 changes: 6 additions & 10 deletions gui/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use gltf::Gltf;
use std::time::SystemTime;
use winit::{
event::{Event},
Expand All @@ -11,10 +12,9 @@ pub(crate) mod camera;
pub(crate) mod model;

use crate::app::App;
use triangulate::mesh::Mesh;

async fn run(start: SystemTime, event_loop: EventLoop<()>, window: Window,
loader: std::thread::JoinHandle<Mesh>)
loader: std::thread::JoinHandle<Gltf>)
{
let size = window.inner_size();
let (surface, adapter) = {
Expand Down Expand Up @@ -85,14 +85,10 @@ fn main() {
// and triangulated in the background while we wait for a GPU context
let loader = std::thread::spawn(|| {
println!("Loading mesh!");
use step::step_file::StepFile;
use triangulate::triangulate::triangulate;

let data = std::fs::read(input).expect("Could not open file");
let flat = StepFile::strip_flatten(&data);
let step = StepFile::parse(&flat);
let (mesh, _stats) = triangulate(&step);
mesh
use std::{fs, io};
let file = fs::File::open(input).unwrap();
let reader = io::BufReader::new(file);
gltf::Gltf::from_reader(reader).unwrap()
});

let event_loop = EventLoop::new();
Expand Down
89 changes: 58 additions & 31 deletions gui/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,19 @@ use std::borrow::Cow;
use bytemuck::{Pod, Zeroable};
use nalgebra_glm as glm;
use glm::{Vec4, Mat4};
use gltf::Gltf;
use wgpu::util::DeviceExt;

use triangulate::mesh::{Vertex, Triangle};

use crate::camera::Camera;


#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
struct GPUVertex {
pos: [f32; 4],
norm: [f32; 4],
color: [f32; 4],
}

impl GPUVertex {
fn from_vertex(v: &Vertex) -> Self {
Self {
pos: [v.pos.x as f32, v.pos.y as f32, v.pos.z as f32, 1.0],
norm: [v.norm.x as f32, v.norm.y as f32, v.norm.z as f32, 1.0],
color: [v.color.x as f32, v.color.y as f32, v.color.z as f32, 1.0],
}
}
pub struct GPUVertex {
pub pos: [f32; 4],
pub norm: [f32; 4],
pub color: [f32; 4],
}

pub struct Model {
Expand All @@ -37,26 +28,62 @@ pub struct Model {
}

impl Model {
pub fn new(device: &wgpu::Device, swapchain_format: wgpu::TextureFormat,
verts: &[Vertex], tris: &[Triangle]) -> Self {
pub fn new(device: &wgpu::Device, swapchain_format: wgpu::TextureFormat, gltf: &Gltf
) -> (Self, Vec<GPUVertex>) {

// Load buffers
let mut buffer_data = Vec::new();
for buffer in gltf.buffers() {
let bin = match buffer.source() {
gltf::buffer::Source::Bin => {
if let Some(blob) = gltf.blob.clone() {
blob
} else {
panic!("Missing Blob");
}
}
_ => panic!("Only GLB/embedded buffers supported")
};

buffer_data.push(bin);
}

let vertex_data: Vec<GPUVertex> = verts.into_iter()
.map(GPUVertex::from_vertex)
.collect();
let index_data: Vec<u32> = tris.into_iter()
.flat_map(|t| t.verts.iter())
.copied()
.collect();
let mesh = gltf.meshes().next().unwrap();
let primitive = mesh.primitives().next().unwrap();

let reader = primitive.reader(|buffer| Some(&buffer_data[buffer.index()]));

let (positions, normals, colors) = (
reader.read_positions().unwrap(),
reader.read_normals().unwrap(),
reader.read_colors(0).unwrap().into_rgba_f32(),
);

let indices = reader.read_indices().map(|indices| indices.into_u32());
let indices = match indices {
Some(indices) => indices.collect::<Vec<_>>(),
None => (0..positions.len() as u32).collect(),
};

let vertices = positions
.zip(normals)
.zip(colors)
.map(|((pos, norm), color)| GPUVertex {
pos: [pos[0], pos[1], pos[2], 1.0],
norm: [norm[0], norm[1], norm[2], 1.0],
color,
})
.collect::<Vec<_>>();

let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Vertex buffer"),
contents: bytemuck::cast_slice(&vertex_data),
label: Some("Vertex Buffer"),
contents: bytemuck::cast_slice(&vertices),
usage: wgpu::BufferUsage::VERTEX,
});

let index_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Index buffer"),
contents: bytemuck::cast_slice(&index_data),
label: Some("Index Buffer"),
contents: bytemuck::cast_slice(&indices),
usage: wgpu::BufferUsage::INDEX,
});

Expand Down Expand Up @@ -170,14 +197,14 @@ impl Model {
multisample: wgpu::MultisampleState::default(),
});

Model {
(Model {
render_pipeline,
index_buf,
vertex_buf,
uniform_buf,
bind_group,
index_count: tris.len() as u32 * 3,
}
index_count: indices.len() as u32 // index_count: tris.len() as u32 * 3,
}, vertices)
}

pub fn draw(&self, camera: &Camera,
Expand Down