I'm trying to draw a Minecraft-like textured cube in WGPU. I have a vertex buffer and an index buffer. I'm using Repeat
for adress_mode
on the texture sampler. I use texture coordinates greater or smaller than 0 to repeat the dirt texture. The front, right and left faces render properly. However, the texture coordinates of the back and top faces are messed up. I didn't add the bottom face yet as I wanted to get these working first. The back texture gets drawn twice and inside out and the top one is completely messed up.
Here's the code:
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Vertex {
pub position: [f32; 3],
pub tex_coords: [f32; 2]
}
/// We arrange the vertices in counter-clockwise order: top, bottom left, bottom right.
pub const VERTICES: &[Vertex] = &[
// FRONT
Vertex {position: [-1.0, -1.0, 0.0], tex_coords: [0.0, 1.0]}, // 0
Vertex {position: [1.0, -1.0, 0.0], tex_coords: [1.0, 1.0]}, // 1
Vertex {position: [1.0, 1.0, 0.0], tex_coords: [1.0, 0.0]}, // 2
Vertex {position: [-1.0, 1.0, 0.0], tex_coords: [0.0, 0.0]}, // 3
// RIGHT
Vertex {position: [1.0, -1.0, -2.0], tex_coords: [2.0, 1.0]}, // 4
Vertex {position: [1.0, 1.0, -2.0], tex_coords: [2.0, 0.0]}, // 5
// LEFT
Vertex {position: [-1.0, -1.0, -2.0], tex_coords: [-1.0, 1.0]},
Vertex {position: [-1.0, 1.0, -2.0], tex_coords: [-1.0, 0.0]},
];
pub const INDICES: &[u16] = &[
// FRONT
0, 1, 2,
2, 3, 0,
// RIGHT
1, 4, 5,
5, 2, 1,
// LEFT
6, 0, 3,
3, 7, 6,
// BACK
6, 4, 5,
5, 7, 6,
// TOP
3, 2, 5,
5, 7, 3
];
My texture sampler:
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
address_mode_u: wgpu::AddressMode::Repeat,
address_mode_v: wgpu::AddressMode::Repeat,
address_mode_w: wgpu::AddressMode::Repeat,
mag_filter: wgpu::FilterMode::Nearest,
min_filter: wgpu::FilterMode::Nearest,
mipmap_filter: wgpu::FilterMode::Nearest,
..Default::default()
});