Add sample rendering #2

Open
noteuclid wants to merge 5 commits from sample into master
Showing only changes of commit 2bb28c240b - Show all commits

View File

@ -39,23 +39,27 @@ pub struct Core {
surface: wgpu::Surface<'static>, surface: wgpu::Surface<'static>,
mesh_pipeline: render::mesh::Pipeline, mesh_pipeline: render::mesh::Pipeline,
mesh1: render::mesh::GpuMeshIndexed, mesh_color: render::mesh::GpuMeshIndexed,
mesh_gray: render::mesh::GpuMeshIndexed,
depth: wgpu::Texture, depth: wgpu::Texture,
} }
fn cube() -> render::mesh::CpuMeshIndexed { fn cube(a: f32, b: f32) -> render::mesh::CpuMeshIndexed {
let gray_level = (b + a) / 2.;
let gray = Vec3::splat(gray_level);
let color_level = (b - a) / 2.;
let mut mesh = render::mesh::CpuMeshIndexed::default(); let mut mesh = render::mesh::CpuMeshIndexed::default();
for (u, v, w) in [ for (u, v, w) in [
(Vec3::X, Vec3::Y, Vec3::Z), (Vec3::X, Vec3::Y, Vec3::Z),
(Vec3::Y, Vec3::Z, Vec3::X), (Vec3::Y, Vec3::Z, Vec3::X),
(Vec3::Z, Vec3::X, Vec3::Y), (Vec3::Z, Vec3::X, Vec3::Y),
] { ] {
for (m, color) in [(1., w), (-1., 1. - w)] { for m in [1., -1.] {
let base = mesh.vertices.len() as u16; let base = mesh.vertices.len() as u16;
for offset in [m * v + u, -m * v + u, -m * v - u, m * v - u] { for offset in [m * v + u, -m * v + u, -m * v - u, m * v - u] {
let vertex = render::mesh::Vertex { let vertex = render::mesh::Vertex {
position: (m * w + offset), position: (m * w + offset),
color, color: gray + color_level * m * w,
normal: m * w, normal: m * w,
}; };
mesh.vertices.push(vertex); mesh.vertices.push(vertex);
@ -77,8 +81,8 @@ impl Core {
let depth = Self::create_depth_buffer(&device, pixel_size); let depth = Self::create_depth_buffer(&device, pixel_size);
let mesh_pipeline = render::mesh::Pipeline::new(&device); let mesh_pipeline = render::mesh::Pipeline::new(&device);
let mesh1 = cube(); let mesh_color = render::mesh::GpuMeshIndexed::new(&device, &cube(0.2, 0.8));
let mesh1 = render::mesh::GpuMeshIndexed::new(&device, &mesh1); let mesh_gray = render::mesh::GpuMeshIndexed::new(&device, &cube(0.7, 0.7));
Self::configure_surface(&surface, &device, pixel_size); Self::configure_surface(&surface, &device, pixel_size);
@ -87,7 +91,8 @@ impl Core {
queue, queue,
surface, surface,
mesh_pipeline, mesh_pipeline,
mesh1, mesh_color,
mesh_gray,
depth, depth,
} }
} }
@ -109,7 +114,7 @@ impl Core {
&self.queue, &self.queue,
render::mesh::Params { render::mesh::Params {
mvp: proj * view, mvp: proj * view,
light: vec3(1., 0.5, 0.5).normalize(), light: vec3(0.7, 0.5, 1.0).normalize(),
_zero: 0., _zero: 0.,
}, },
); );
@ -147,7 +152,7 @@ impl Core {
}), }),
..Default::default() ..Default::default()
}); });
self.mesh1.render(&self.mesh_pipeline, &mut pass); self.mesh_gray.render(&self.mesh_pipeline, &mut pass);
drop(pass); drop(pass);
self.queue.submit(std::iter::once(encoder.finish())); self.queue.submit(std::iter::once(encoder.finish()));
} }