Skip to content

Commit 3f85a92

Browse files
committed
Next step: The results
1 parent 9a3671f commit 3f85a92

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
I am following [sotrh's learn-wgpu tutorial](https://sotrh.github.io/learn-wgpu/).
33

44
## Status
5-
I am just about to start: [A change to the VERTICES](https://sotrh.github.io/learn-wgpu/beginner/tutorial5-textures/#a-change-to-the-vertices).
5+
I am just about to start: [The results](https://sotrh.github.io/learn-wgpu/beginner/tutorial5-textures/#the-results).
66

77
## Glossary
88
Word | Definition

src/main.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn main() {
6262
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
6363
struct Vertex {
6464
position: [f32; 3],
65-
color: [f32; 3],
65+
tex_coords: [f32; 2],
6666
}
6767

6868
impl Vertex {
@@ -79,21 +79,22 @@ impl Vertex {
7979
wgpu::VertexAttribute {
8080
offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
8181
shader_location: 1,
82-
format: wgpu::VertexFormat::Float32x3,
82+
format: wgpu::VertexFormat::Float32x2,
8383
}
8484
]
8585
}
8686
}
8787
}
8888

8989
const VERTICES: &[Vertex] = &[
90-
Vertex { position: [-0.0868241, 0.49240386, 0.0], color: [0.5, 0.0, 0.5] }, // A
91-
Vertex { position: [-0.49513406, 0.06958647, 0.0], color: [0.5, 0.0, 0.5] }, // B
92-
Vertex { position: [-0.21918549, -0.44939706, 0.0], color: [0.5, 0.0, 0.5] }, // C
93-
Vertex { position: [0.35966998, -0.3473291, 0.0], color: [0.5, 0.0, 0.5] }, // D
94-
Vertex { position: [0.44147372, 0.2347359, 0.0], color: [0.5, 0.0, 0.5] }, // E
90+
Vertex { position: [-0.0868241, 0.49240386, 0.0], tex_coords: [0.4131759, 0.99240386], }, // A
91+
Vertex { position: [-0.49513406, 0.06958647, 0.0], tex_coords: [0.0048659444, 0.56958646], }, // B
92+
Vertex { position: [-0.21918549, -0.44939706, 0.0], tex_coords: [0.28081453, 0.050602943], }, // C
93+
Vertex { position: [0.35966998, -0.3473291, 0.0], tex_coords: [0.85967, 0.15267089], }, // D
94+
Vertex { position: [0.44147372, 0.2347359, 0.0], tex_coords: [0.9414737, 0.7347359], }, // E
9595
];
9696

97+
9798
const INDICES: &[u16] = &[
9899
0, 1, 4,
99100
1, 2, 4,
@@ -153,7 +154,7 @@ impl State {
153154

154155
let diffuse_bytes = include_bytes!("happy-tree.png");
155156
let diffuse_image = image::load_from_memory(diffuse_bytes).unwrap();
156-
let diffuse_rgba = diffuse_image.as_bgra8().unwrap();
157+
let diffuse_rgba = diffuse_image.as_rgba8().unwrap();
157158

158159
use image::GenericImageView;
159160
let dimensions = diffuse_image.dimensions();

src/shader.wgsl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
//Vertex shader
22
struct VertexInput {
33
[[location(0)]] position: vec3<f32>;
4-
[[location(1)]] color: vec3<f32>;
4+
[[location(1)]] tex_coords: vec2<f32>;
55
};
66

77
struct VertexOutput {
88
[[builtin(position)]] clip_position: vec4<f32>;
9-
[[location(0)]] color: vec3<f32>;
9+
[[location(0)]] tex_coords: vec2<f32>;
1010
};
1111

1212
[[stage(vertex)]]
1313
fn main(model: VertexInput) -> VertexOutput {
1414
var out: VertexOutput;
15-
out.color = model.color;
15+
out.tex_coords = model.tex_coords;
1616
out.clip_position = vec4<f32>(model.position, 1.0);
1717
return out;
1818
}
1919

2020
//Fragment shader
21+
[[group(0), binding(0)]] //group corresponds to the first parameter in set_bind_group()
22+
var t_diffuse: texture_2d<f32>;
23+
[[group(0), binding(1)]] //binding relates to the binding specified when creating BindGroup/Layout
24+
var s_diffuse: sampler;
2125

2226
[[stage(fragment)]]
2327
fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
24-
return vec4<f32>(in.color, 1.0);
28+
return textureSample(t_diffuse, s_diffuse, in.tex_coords);
2529
}

0 commit comments

Comments
 (0)