Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Melchizedek6809 committed May 4, 2023
1 parent c0753ae commit 5ca9edc
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 2 deletions.
3 changes: 2 additions & 1 deletion book/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- [Opening a window](tuto-01-getting-started.md)
- [Drawing a triangle](tuto-02-triangle.md)
- [Event loop]()
- [Uniforms](tuto-03-animated-triangle.md)
- [Matrices](tuto-04-matrices.md)
- [Adding colors](tuto-05-colors.md)
Expand All @@ -21,5 +22,5 @@
- [Shadow mapping]()
- [Antialiasing]()
- [Drawing lots of objects with instancing]()
- [Performances](perf-intro.md)
- [Performance](perf-intro.md)
- [Synchronization](perf-sync.md)
2 changes: 1 addition & 1 deletion book/perf-intro.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Performances
# Performance

Here is the estimated cost of various operations:

Expand Down
39 changes: 39 additions & 0 deletions book/tuto-03-event-loop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# The event loop

Before we continue animating things we need to make a short detour to make animation possible.
To simplify things we were only ever rendering a single frame during startup.
To change this we need to alter the structure a little bit, but first we should put the code that creates the context into a separate function. We'll use the following function in the following tutorials:

```rust
fn create_window_and_display(title: &str) -> (window: winit::Window, display: glium::Display<WindowSurface>) {
// First we start by opening a new Window
let window_builder = WindowBuilder::new().with_title(title);
let display_builder = DisplayBuilder::new().with_window_builder(Some(window_builder));
let config_template_builder = ConfigTemplateBuilder::new();
let (window, gl_config) = display_builder
.build(&event_loop, config_template_builder, |mut configs| {
// Just use the first configuration since we don't have any special preferences right now
configs.next().unwrap()
})
.unwrap();
let window = window.unwrap();

// Now we get the window size to use as the initial size of the Surface
let (width, height): (u32, u32) = window.inner_size().into();
let attrs = glutin::surface::SurfaceAttributesBuilder::<WindowSurface>::new().build(
window.raw_window_handle(),
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
);

// Finally we can create a Surface, use it to make a PossiblyCurrentContext and create the glium Display
let surface = unsafe { gl_config.display().create_window_surface(&gl_config, &attrs).unwrap() };
let context_attributes = glutin::context::ContextAttributesBuilder::new().build(Some(window.raw_window_handle()));
let current_context = Some(unsafe {
gl_config.display().create_context(&gl_config, &context_attributes).expect("failed to create context")
}).unwrap().make_current(&surface).unwrap();
let display = glium::Display::from_context_surface(current_context, surface).unwrap();

(window, display)
}
```
105 changes: 105 additions & 0 deletions examples/tutorial-03-new.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#[macro_use]
extern crate glium;

use winit::event_loop::EventLoopBuilder;
use winit::window::WindowBuilder;
use glutin_winit::DisplayBuilder;
use glutin::config::ConfigTemplateBuilder;
use glutin::prelude::*;
use glutin::display::GetGlDisplay;
use glutin::surface::WindowSurface;
use raw_window_handle::HasRawWindowHandle;
use std::num::NonZeroU32;
use glium::Surface;

fn main() {
// We start by creating the EventLoop, this can only be done once per process.
// This also needs to happen on the main thread to make the program portable.
let event_loop = EventLoopBuilder::new().build();
let (_window, display) = create_window_and_display(&event_loop, "Glium tutorial #3");

#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 2],
}
implement_vertex!(Vertex, position);

let vertex1 = Vertex { position: [-0.5, -0.5] };
let vertex2 = Vertex { position: [ 0.0, 0.5] };
let vertex3 = Vertex { position: [ 0.5, -0.25] };
let shape = vec![vertex1, vertex2, vertex3];
let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();
let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);

let vertex_shader_src = r#"
#version 140
in vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}
"#;

let fragment_shader_src = r#"
#version 140
out vec4 color;
void main() {
color = vec4(1.0, 0.0, 0.0, 1.0);
}
"#;

let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).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();

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

fn create_window_and_display(event_loop: &winit::event_loop::EventLoop<()>, title: &str) -> (winit::window::Window, glium::Display<WindowSurface>) {
// First we start by opening a new Window
let window_builder = WindowBuilder::new().with_title(title);
let display_builder = DisplayBuilder::new().with_window_builder(Some(window_builder));
let config_template_builder = ConfigTemplateBuilder::new();
let (window, gl_config) = display_builder
.build(event_loop, config_template_builder, |mut configs| {
// Just use the first configuration since we don't have any special preferences right now
configs.next().unwrap()
})
.unwrap();
let window = window.unwrap();

// Now we get the window size to use as the initial size of the Surface
let (width, height): (u32, u32) = window.inner_size().into();
let attrs = glutin::surface::SurfaceAttributesBuilder::<WindowSurface>::new().build(
window.raw_window_handle(),
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
);

// Finally we can create a Surface, use it to make a PossiblyCurrentContext and create the glium Display
let surface = unsafe { gl_config.display().create_window_surface(&gl_config, &attrs).unwrap() };
let context_attributes = glutin::context::ContextAttributesBuilder::new().build(Some(window.raw_window_handle()));
let current_context = Some(unsafe {
gl_config.display().create_context(&gl_config, &context_attributes).expect("failed to create context")
}).unwrap().make_current(&surface).unwrap();
let display = glium::Display::from_context_surface(current_context, surface).unwrap();

(window, display)
}

0 comments on commit 5ca9edc

Please sign in to comment.