Migrate into mesh.rs

This commit is contained in:
marceline-cramer 2022-01-31 11:15:07 -07:00
parent 5d68d3f71d
commit 55264bfceb
2 changed files with 32 additions and 29 deletions

View File

@ -8,8 +8,10 @@ use winit::{
};
mod camera;
mod mesh;
use camera::*;
use mesh::*;
struct Renderer {
pub device: wgpu::Device,
@ -20,6 +22,7 @@ struct Renderer {
config: wgpu::SurfaceConfiguration,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
meshes_buffer: wgpu::Buffer,
meshes_bind_group: wgpu::BindGroup,
@ -233,6 +236,7 @@ impl Renderer {
transform_ranges.push((*group_id, start_idx..end_idx));
}
// TODO persistent staging buffer (write_buffer creates a new one per call)
self.queue.write_buffer(
&self.meshes_buffer,
0,
@ -354,33 +358,6 @@ impl CameraUniform {
}
}
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Vertex {
position: [f32; 3],
}
impl Vertex {
pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[wgpu::VertexAttribute {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float32x3,
}],
}
}
}
type Index = u32;
struct MeshData {
vertices: Vec<Vertex>,
indices: Vec<Index>,
}
#[repr(C)]
#[derive(Copy, Clone, Eq, Hash, PartialEq)]
struct MeshHandle {
@ -400,8 +377,8 @@ type MeshCommands = Vec<MeshInstance>;
fn load_model() -> MeshData {
use tobj::*;
let mut model_data = include_bytes!("viking_room.obj").to_vec();
let mut model_data = &mut model_data.as_slice();
let model_data = include_bytes!("viking_room.obj").to_vec();
let model_data = &mut model_data.as_slice();
let load_options = LoadOptions {
triangulate: true,

26
src/mesh.rs Normal file
View File

@ -0,0 +1,26 @@
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Vertex {
pub position: [f32; 3],
}
impl Vertex {
pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[wgpu::VertexAttribute {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float32x3,
}],
}
}
}
pub type Index = u32;
pub struct MeshData {
pub vertices: Vec<Vertex>,
pub indices: Vec<Index>,
}