Skip to content

Commit

Permalink
Merge pull request #2097 from justincredible/intro-fixes
Browse files Browse the repository at this point in the history
Minor fixes to introductory material
  • Loading branch information
est31 committed Feb 1, 2024
2 parents af52f0a + da7dc5e commit a352c66
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 75 deletions.
22 changes: 10 additions & 12 deletions book/tuto-01-getting-started.md
Expand Up @@ -40,24 +40,22 @@ This will open a new window, register it with the given event_loop and create a

```rust
fn main() {
let event_loop = winit::event_loop::EventLoopBuilder::new().build();
let event_loop = winit::event_loop::EventLoopBuilder::new().build().expect("event loop building");
let (_window, display) = glium::backend::glutin::SimpleWindowBuilder::new().build(&event_loop);
}
```

If you try to run this example with `cargo run` you'll encounter a problem: as soon as the window has been created, our main function exits and the window is closed. To prevent this, we need to wait until we receive a `CloseRequested` event. We do this by calling `event_loop.run`:

```rust
event_loop.run(move |ev, _, control_flow| {
match ev {
let _ = event_loop.run(move |event, window_target| {
match event {
winit::event::Event::WindowEvent { event, .. } => match event {
winit::event::WindowEvent::CloseRequested => {
*control_flow = winit::event_loop::ControlFlow::Exit;
},
_ => (),
winit::event::WindowEvent::CloseRequested => window_target.exit(),
_ => (),
},
_ => (),
}
};
});
```

Expand Down Expand Up @@ -102,18 +100,18 @@ Here is our full program:
use glium::Surface;

fn main() {
let event_loop = winit::event_loop::EventLoopBuilder::new().build();
let event_loop = winit::event_loop::EventLoopBuilder::new().build().expect("event loop building");
let (_window, display) = glium::backend::glutin::SimpleWindowBuilder::new().build(&event_loop);

let mut frame = display.draw();
frame.clear_color(0.0, 0.0, 1.0, 1.0);
frame.finish().unwrap();

event_loop.run(move |event, _, control_flow| {
let _ = event_loop.run(move |event, window_target| {
match event {
winit::event::Event::WindowEvent { event, .. } => match event {
winit::event::WindowEvent::CloseRequested => control_flow.set_exit(),
_ => (),
winit::event::WindowEvent::CloseRequested => window_target.exit(),
_ => (),
},
_ => (),
};
Expand Down
4 changes: 2 additions & 2 deletions book/tuto-02-triangle.md
Expand Up @@ -78,7 +78,7 @@ The tricky part is that *we* need to write the vertex and fragment shaders. To d

```rust
let vertex_shader_src = r#"
##version 140
#version 140
in vec2 position;
Expand All @@ -98,7 +98,7 @@ The second shader is called the fragment shader (sometimes also named *pixel sha

```rust
let fragment_shader_src = r#"
##version 140
#version 140
out vec4 color;
Expand Down
110 changes: 55 additions & 55 deletions book/tuto-03-animated-triangle.md
Expand Up @@ -5,15 +5,15 @@ Now that we have a triangle, we are going to animate it. Remember that OpenGL is
So far we have only ever rendered a single frame and then waited for the program to exit. For an animation to show we need to change the way we draw our triangle. Instead of drawing a frame and then waiting in our event_loop for the window to close, we first draw our triangle when requested by the operating system:

```rust
event_loop.run(move |event, _, control_flow| {
let _ = event_loop.run(move |event, window_target| {
match event {
winit::event::Event::WindowEvent { event, .. } => match event {
winit::event::WindowEvent::CloseRequested => control_flow.set_exit(),
winit::event::WindowEvent::CloseRequested => window_target.exit(),
winit::event::WindowEvent::RedrawRequested => {
// Move the draw code here!
},
_ => (),
},
winit::event::Event::RedrawRequested(_) => {
// Move the draw code here!
},
_ => (),
};
});
Expand All @@ -22,18 +22,18 @@ event_loop.run(move |event, _, control_flow| {
What exactly triggers this event is platform specific, but in order to draw our triangle over and over again we can request a redraw ourselves once we've finished rendering, to do that we'll respond to yet another event:

```rust
event_loop.run(move |event, _, control_flow| {
let _ = event_loop.run(move |event, window_target| {
match event {
winit::event::Event::WindowEvent { event, .. } => match event {
winit::event::WindowEvent::CloseRequested => control_flow.set_exit(),
winit::event::WindowEvent::CloseRequested => window_target.exit(),
winit::event::WindowEvent::RedrawRequested => {
// Move the draw code here!
},
_ => (),
},
winit::event::Event::RedrawEventsCleared => {
winit::event::Event::AboutToWait => {
window.request_redraw();
},
winit::event::Event::RedrawRequested(_) => {
// Move the draw code here!
},
_ => (),
};
});
Expand All @@ -45,21 +45,21 @@ While we are working on our event_loop there is one more event that we should ha

```rust
let mut t: f32 = 0.0;
event_loop.run(move |event, _, control_flow| {
let _ = event_loop.run(move |event, window_target| {
match event {
winit::event::Event::WindowEvent { event, .. } => match event {
winit::event::WindowEvent::CloseRequested => control_flow.set_exit(),
winit::event::WindowEvent::CloseRequested => window_target.exit(),
winit::event::WindowEvent::Resized(window_size) => {
display.resize(window_size.into());
},
winit::event::WindowEvent::RedrawRequested => {
// Move the draw code here!
},
_ => (),
},
winit::event::Event::RedrawEventsCleared => {
winit::event::Event::AboutToWait => {
window.request_redraw();
},
winit::event::Event::RedrawRequested(_) => {
// Move the draw code here!
},
_ => (),
};
});
Expand All @@ -73,37 +73,37 @@ Our first approach will be to create a variable named `t` which represents the s

```rust
let mut t: f32 = 0.0;
event_loop.run(move |event, _, control_flow| {
let _ = event_loop.run(move |event, window_target| {
match event {
winit::event::Event::WindowEvent { event, .. } => match event {
winit::event::WindowEvent::CloseRequested => control_flow.set_exit(),
winit::event::WindowEvent::CloseRequested => window_target.exit(),
winit::event::WindowEvent::Resized(window_size) => {
display.resize(window_size.into());
},
winit::event::WindowEvent::RedrawRequested => {
// We update `t`
t += 0.02;
// We use the sine of t as an offset, this way we get a nice smooth animation
let x_off = t.sin() * 0.5;

let shape = vec![
Vertex { position: [-0.5 + x_off, -0.5] },
Vertex { position: [ 0.0 + x_off, 0.5] },
Vertex { position: [ 0.5 + x_off, -0.25] }
];
let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();

let mut target = display.draw();
target.clear_color(0.0, 0.0, 1.0, 1.0);
target.draw(&vertex_buffer, &indices, &program, &glium::uniforms::EmptyUniforms,
&Default::default()).unwrap();
target.finish().unwrap();
},
_ => (),
},
winit::event::Event::RedrawEventsCleared => {
winit::event::Event::AboutToWait => {
window.request_redraw();
},
winit::event::Event::RedrawRequested(_) => {
// We update `t`
t += 0.02;
// We use the sine of t as an offset, this way we get a nice smooth animation
let x_off = t.sin() * 0.5;

let shape = vec![
Vertex { position: [-0.5 + x_off, -0.5] },
Vertex { position: [ 0.0 + x_off, 0.5] },
Vertex { position: [ 0.5 + x_off, -0.25] }
];
let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();

let mut target = display.draw();
target.clear_color(0.0, 0.0, 1.0, 1.0);
target.draw(&vertex_buffer, &indices, &program, &glium::uniforms::EmptyUniforms,
&Default::default()).unwrap();
target.finish().unwrap();
},
_ => (),
};
});
Expand All @@ -125,30 +125,30 @@ Let's remove the two `let`'s that redefine our shape and vertex_buffer from our

```rust
let mut t: f32 = 0.0;
event_loop.run(move |event, _, control_flow| {
let _ = event_loop.run(move |event, window_target| {
match event {
winit::event::Event::WindowEvent { event, .. } => match event {
winit::event::WindowEvent::CloseRequested => control_flow.set_exit(),
winit::event::WindowEvent::CloseRequested => window_target.exit(),
winit::event::WindowEvent::Resized(window_size) => {
display.resize(window_size.into());
},
winit::event::WindowEvent::RedrawRequested => {
// We update `t`
t += 0.02;
// We use the sine of t as an offset, this way we get a nice smooth animation
let x_off = t.sin() * 0.5;

let mut target = display.draw();
target.clear_color(0.0, 0.0, 1.0, 1.0);
target.draw(&vertex_buffer, &indices, &program, &glium::uniforms::EmptyUniforms,
&Default::default()).unwrap();
target.finish().unwrap();
},
_ => (),
},
winit::event::Event::RedrawEventsCleared => {
winit::event::Event::AboutToWait => {
window.request_redraw();
},
winit::event::Event::RedrawRequested(_) => {
// We update `t`
t += 0.02;
// We use the sine of t as an offset, this way we get a nice smooth animation
let x = t.sin() * 0.5;

let mut target = display.draw();
target.clear_color(0.0, 0.0, 1.0, 1.0);
target.draw(&vertex_buffer, &indices, &program, &glium::uniforms::EmptyUniforms,
&Default::default()).unwrap();
target.finish().unwrap();
},
_ => (),
};
});
Expand All @@ -175,7 +175,7 @@ let vertex_shader_src = r#"
You may notice that this is exactly the operation that we've been doing above, except that this time it is done on the GPU side. We have added a variable `t` in our shader, which is declared as a **uniform**. A uniform is a global variable whose value is set when we draw by passing its value to the `draw` function. The easiest way to do so is to use the `uniform!` macro:

```rust
target.draw(&vertex_buffer, &indices, &program, &uniform! { x: x },
target.draw(&vertex_buffer, &indices, &program, &uniform! { x: x_off },
&Default::default()).unwrap();
```

Expand Down
2 changes: 1 addition & 1 deletion book/tuto-04-matrices.md
Expand Up @@ -17,7 +17,7 @@ Let's get back to our moving triangle. We are going to change the vertex shader

```rust
let vertex_shader_src = r#"
##version 140
#version 140
in vec2 position;
Expand Down
4 changes: 2 additions & 2 deletions book/tuto-06-texture.md
Expand Up @@ -22,7 +22,7 @@ let image = glium::texture::RawImage2d::from_raw_rgba_reversed(&image.into_raw()
And in order to upload the image as a texture, it's as simple as:

```rust
let texture = glium::texture::SrgbTexture2d::new(&display, image).unwrap();
let texture = glium::texture::Texture2d::new(&display, image).unwrap();
```

# Using the texture
Expand Down Expand Up @@ -97,7 +97,7 @@ let uniforms = uniform! {
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[ t , 0.0, 0.0, 1.0f32],
[ x , 0.0, 0.0, 1.0f32],
],
tex: &texture,
};
Expand Down
2 changes: 1 addition & 1 deletion book/tuto-14-wall.md
Expand Up @@ -54,7 +54,7 @@ let image = image::load(Cursor::new(&include_bytes!("../book/tuto-14-diffuse.jpg
image::JPEG).unwrap().to_rgba8();
let image_dimensions = image.dimensions();
let image = glium::texture::RawImage2d::from_raw_rgba_reversed(&image.into_raw(), image_dimensions);
let diffuse_texture = glium::texture::SrgbTexture2d::new(&display, image).unwrap();
let diffuse_texture = glium::texture::Texture2d::new(&display, image).unwrap();
```

Adding the texture coordinates is also very easy:
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial-14.rs
Expand Up @@ -31,7 +31,7 @@ fn main() {
image::ImageFormat::Jpeg).unwrap().to_rgba8();
let image_dimensions = image.dimensions();
let image = glium::texture::RawImage2d::from_raw_rgba_reversed(&image.into_raw(), image_dimensions);
let diffuse_texture = glium::texture::SrgbTexture2d::new(&display, image).unwrap();
let diffuse_texture = glium::texture::Texture2d::new(&display, image).unwrap();

let image = image::load(std::io::Cursor::new(&include_bytes!("../book/resources/tuto-14-normal.png")),
image::ImageFormat::Png).unwrap().to_rgba8();
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -16,7 +16,7 @@ extern crate winit;
fn main() {
// 1. The **winit::EventLoop** for handling events.
let event_loop = winit::event_loop::EventLoopBuilder::new().build();
let event_loop = winit::event_loop::EventLoopBuilder::new().build().unwrap();
// 2. Create a glutin context and glium Display
let (window, display) = glium::backend::glutin::SimpleWindowBuilder::new().build(&event_loop);
}
Expand Down

0 comments on commit a352c66

Please sign in to comment.