obj models

This commit is contained in:
Lauri Räsänen 2022-10-02 22:03:50 +03:00
parent 4ab10fe790
commit 3838b5d7c6
14 changed files with 1337 additions and 109 deletions

View file

@ -6,39 +6,11 @@ use winit::{event::*, window::Window};
use super::camera::{Camera, CameraController, CameraUniform};
use super::instance::{Instance, InstanceRaw};
use super::model::{DrawModel, Model, ModelVertex, Vertex};
use super::resources;
use super::texture::Texture;
use super::vertex::Vertex;
// test data
const VERTICES: &[Vertex] = &[
Vertex {
position: [-0.0868241, 0.49240386, 0.0],
tex_coords: [0.4131759, 0.99240386],
}, // A
Vertex {
position: [-0.49513406, 0.06958647, 0.0],
tex_coords: [0.0048659444, 0.56958647],
}, // B
Vertex {
position: [-0.21918549, -0.44939706, 0.0],
tex_coords: [0.28081453, 0.05060294],
}, // C
Vertex {
position: [0.35966998, -0.3473291, 0.0],
tex_coords: [0.85967, 0.1526709],
}, // D
Vertex {
position: [0.44147372, 0.2347359, 0.0],
tex_coords: [0.9414737, 0.7347359],
}, // E
];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];
const NUM_INSTANCES_PER_ROW: u32 = 10;
const INSTANCE_DISPLACEMENT: cgmath::Vector3<f32> = cgmath::Vector3::new(
NUM_INSTANCES_PER_ROW as f32 * 0.5,
0.0,
NUM_INSTANCES_PER_ROW as f32 * 0.5,
);
pub struct State {
pub size: winit::dpi::PhysicalSize<u32>,
@ -48,10 +20,6 @@ pub struct State {
queue: wgpu::Queue,
config: wgpu::SurfaceConfiguration,
render_pipeline: wgpu::RenderPipeline,
vertex_buffer: wgpu::Buffer,
index_buffer: wgpu::Buffer,
num_indices: u32,
diffuse_bind_group: wgpu::BindGroup,
camera: Camera,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
@ -59,6 +27,8 @@ pub struct State {
camera_controller: CameraController,
instances: Vec<Instance>,
instance_buffer: wgpu::Buffer,
depth_texture: Texture,
obj_model: Model,
}
impl State {
@ -141,11 +111,7 @@ impl State {
let camera_controller = CameraController::new(1.0, 2.0);
// Test image
surface.configure(&device, &config);
let diffuse_bytes = include_bytes!("../../assets/test.png");
let diffuse_texture =
Texture::from_bytes(&device, &queue, diffuse_bytes, "../../assets/test.png").unwrap();
let texture_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
@ -171,33 +137,22 @@ impl State {
],
label: Some("texture_bind_group_layout"),
});
let diffuse_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &texture_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&diffuse_texture.view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(&diffuse_texture.sampler),
},
],
label: Some("diffuse_bind_group"),
});
let obj_model =
resources::load_model("cube.obj", &device, &queue, &texture_bind_group_layout)
.await
.unwrap();
const SPACE_BETWEEN: f32 = 3.0;
let instances = (0..NUM_INSTANCES_PER_ROW)
.flat_map(|z| {
(0..NUM_INSTANCES_PER_ROW).map(move |x| {
let position = cgmath::Vector3 {
x: x as f32,
y: 0.0,
z: z as f32,
} - INSTANCE_DISPLACEMENT;
let x = SPACE_BETWEEN * (x as f32 - NUM_INSTANCES_PER_ROW as f32 / 2.0);
let z = SPACE_BETWEEN * (z as f32 - NUM_INSTANCES_PER_ROW as f32 / 2.0);
let position = cgmath::Vector3 { x, y: 0.0, z };
let rotation = if position.is_zero() {
// this is needed so an object at (0, 0, 0) won't get scaled to zero
// as Quaternions can effect scale if they're not created correctly
cgmath::Quaternion::from_axis_angle(
cgmath::Vector3::unit_z(),
cgmath::Deg(0.0),
@ -210,6 +165,7 @@ impl State {
})
})
.collect::<Vec<_>>();
let instance_data = instances.iter().map(Instance::to_raw).collect::<Vec<_>>();
let instance_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Instance Buffer"),
@ -217,6 +173,8 @@ impl State {
usage: wgpu::BufferUsages::VERTEX,
});
let depth_texture = Texture::create_depth_texture(&device, &config, "depth_texture");
// let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
// label: Some("Shader"),
// source: wgpu::ShaderSource::Wgsl(include_str!("../shaders/test.wgsl").into()),
@ -236,10 +194,9 @@ impl State {
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
buffers: &[Vertex::desc(), InstanceRaw::desc()],
buffers: &[ModelVertex::desc(), InstanceRaw::desc()],
},
fragment: Some(wgpu::FragmentState {
// 3.
module: &shader,
entry_point: "fs_main",
targets: &[Some(wgpu::ColorTargetState {
@ -260,7 +217,13 @@ impl State {
// Requires Features::CONSERVATIVE_RASTERIZATION
conservative: false,
},
depth_stencil: None,
depth_stencil: Some(wgpu::DepthStencilState {
format: Texture::DEPTH_FORMAT,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
}),
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
@ -269,19 +232,6 @@ impl State {
multiview: None,
});
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Vertex Buffer"),
contents: bytemuck::cast_slice(VERTICES),
usage: wgpu::BufferUsages::VERTEX,
});
let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Index Buffer"),
contents: bytemuck::cast_slice(INDICES),
usage: wgpu::BufferUsages::INDEX,
});
let num_indices = INDICES.len() as u32;
return Self {
size,
surface,
@ -289,10 +239,6 @@ impl State {
queue,
config,
render_pipeline,
vertex_buffer,
index_buffer,
num_indices,
diffuse_bind_group,
camera,
camera_uniform,
camera_buffer,
@ -300,6 +246,8 @@ impl State {
camera_controller,
instances,
instance_buffer,
depth_texture,
obj_model,
};
}
@ -312,6 +260,8 @@ impl State {
self.camera
.projection
.resize(new_size.width, new_size.height);
self.depth_texture =
Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
}
}
@ -357,28 +307,34 @@ impl State {
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: 0.1,
g: 0.2,
b: 0.3,
r: 0.0,
g: 0.5,
b: 0.0,
a: 1.0,
}),
store: true,
},
}),
],
depth_stencil_attachment: None,
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.depth_texture.view,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(1.0),
store: true,
}),
stencil_ops: None,
}),
});
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
render_pass.set_bind_group(1, &self.camera_bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
render_pass.draw_indexed(0..self.num_indices, 0, 0..self.instances.len() as _);
render_pass.draw_model_instanced(
&self.obj_model,
0..self.instances.len() as u32,
&self.camera_bind_group,
);
}
// submit will accept anything that implements IntoIter