Texture coordinates

This commit is contained in:
marceline-cramer 2022-01-31 12:23:03 -07:00
parent 5b318134c5
commit d6639bd2c8
3 changed files with 23 additions and 8 deletions

View File

@ -367,6 +367,10 @@ fn load_model() -> MeshData {
m.mesh.positions[t + 2], m.mesh.positions[t + 2],
-m.mesh.positions[t + 1], -m.mesh.positions[t + 1],
], ],
tex_coords: [
m.mesh.texcoords[i * 2],
m.mesh.texcoords[i * 2 + 1],
],
}); });
} }
@ -479,8 +483,8 @@ fn main() {
let mut camera = Flycam::new(10.0, 0.002); let mut camera = Flycam::new(10.0, 0.002);
let mut is_grabbed = false; let mut is_grabbed = false;
let mut ren = pollster::block_on(Renderer::new(&window)); let mut ren = pollster::block_on(Renderer::new(&window));
let mut state: Box<dyn WorldState> = Box::new(Planets::new(&mut ren)); // let mut state: Box<dyn WorldState> = Box::new(Planets::new(&mut ren));
// let mut state: Box<dyn WorldState> = Box::new(Grid::new(&mut ren)); let mut state: Box<dyn WorldState> = Box::new(Grid::new(&mut ren));
event_loop.run(move |event, _, control_flow| match event { event_loop.run(move |event, _, control_flow| match event {
Event::RedrawRequested(_) => match ren.render(&camera, &state.render()) { Event::RedrawRequested(_) => match ren.render(&camera, &state.render()) {

View File

@ -2,6 +2,7 @@
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] #[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Vertex { pub struct Vertex {
pub position: [f32; 3], pub position: [f32; 3],
pub tex_coords: [f32; 2],
} }
impl Vertex { impl Vertex {
@ -9,11 +10,18 @@ impl Vertex {
wgpu::VertexBufferLayout { wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress, array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex, step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[wgpu::VertexAttribute { attributes: &[
offset: 0, wgpu::VertexAttribute {
shader_location: 0, offset: 0,
format: wgpu::VertexFormat::Float32x3, shader_location: 0,
}], format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
shader_location: 1,
format: wgpu::VertexFormat::Float32x2,
},
],
} }
} }
} }

View File

@ -12,10 +12,12 @@ struct MeshData {
struct VertexInput { struct VertexInput {
[[location(0)]] position: vec3<f32>; [[location(0)]] position: vec3<f32>;
[[location(1)]] tex_coords: vec2<f32>;
}; };
struct VertexOutput { struct VertexOutput {
[[builtin(position)]] clip_position: vec4<f32>; [[builtin(position)]] clip_position: vec4<f32>;
[[location(0)]] color: vec3<f32>;
}; };
[[group(0), binding(0)]] [[group(0), binding(0)]]
@ -32,6 +34,7 @@ fn vs_main(
let transform = meshes.instances[mesh_idx].transform; let transform = meshes.instances[mesh_idx].transform;
var out: VertexOutput; var out: VertexOutput;
out.clip_position = camera.vp * transform * vec4<f32>(vertex.position, 1.0); out.clip_position = camera.vp * transform * vec4<f32>(vertex.position, 1.0);
out.color = vec3<f32>(vertex.tex_coords, 0.0);
return out; return out;
} }
@ -39,5 +42,5 @@ fn vs_main(
fn fs_main( fn fs_main(
frag: VertexOutput, frag: VertexOutput,
) -> [[location(0)]] vec4<f32> { ) -> [[location(0)]] vec4<f32> {
return vec4<f32>(1.0); return vec4<f32>(frag.color, 1.0);
} }