-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtutorial_02.rs
More file actions
89 lines (72 loc) · 2.33 KB
/
tutorial_02.rs
File metadata and controls
89 lines (72 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#[macro_use]
extern crate glium;
use glium::{DisplayBuild, Surface, VertexBuffer, Program};
use glium::glutin::{Event, WindowBuilder};
use glium::index::{NoIndices, PrimitiveType};
use glium::uniforms::EmptyUniforms;
use glium::backend::glutin_backend::GlutinFacade;
// Represent a 3D vertex
#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 3]
}
// Let glium implement Vertex for us
implement_vertex!(Vertex, position);
// Some constants can be re-used
const EMPTY_UNIFORMS: EmptyUniforms = EmptyUniforms;
fn create_vertex_buffer(display: &GlutinFacade) -> VertexBuffer<Vertex> {
let vertex = Vertex { position: [0.0, 0.0, 0.0] };
let point = vec![vertex];
let vertex_buffer = VertexBuffer::new(display, &point).unwrap();
vertex_buffer
}
fn create_shaders(display: &GlutinFacade) -> Program {
let vertex_shader_src = r#"
#version 140
in vec3 position;
void main() {
gl_Position = vec4(position, 1.0);
}
"#;
let fragment_shader_src = r#"
#version 140
out vec4 color;
void main() {
color = vec4(1.0, 0.0, 0.0, 1.0);
}
"#;
Program::from_source(display,
vertex_shader_src, fragment_shader_src, None).unwrap()
}
fn render_scene(display: &GlutinFacade, vertex_buffer: &VertexBuffer<Vertex>, program: &Program) {
let mut frame = display.draw();
frame.clear_color(0.0, 0.0, 0.0, 0.0);
frame.draw(vertex_buffer, &NoIndices(PrimitiveType::Points), program,
&EMPTY_UNIFORMS, &Default::default()).unwrap();
frame.finish().unwrap();
}
fn main() {
// Set up and create a window
let display = WindowBuilder::new()
.with_dimensions(1024, 768)
.with_srgb(Some(true))
.with_title("Tutorial 02")
.build_glium()
.unwrap();
// Create a vertex buffer
let vertex_buffer = create_vertex_buffer(&display);
// Create a shader program
let program = create_shaders(&display);
// Main loop
loop {
// Render
render_scene(&display, &vertex_buffer, &program);
// Handle events
for event in display.poll_events() {
match event {
Event::Closed => return,
_ => ()
}
}
}
}