-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtutorial_09.rs
More file actions
122 lines (95 loc) · 3.19 KB
/
tutorial_09.rs
File metadata and controls
122 lines (95 loc) · 3.19 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#[macro_use]
extern crate glium;
extern crate cgmath;
use glium::{DisplayBuild, Surface, VertexBuffer, Program};
use glium::glutin::{Event, WindowBuilder};
use glium::index::{NoIndices, PrimitiveType};
use glium::backend::glutin_backend::GlutinFacade;
use cgmath::{Matrix, Matrix4};
// Represent a 3D vertex
#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 3]
}
// Let glium implement Vertex for us
implement_vertex!(Vertex, position);
fn create_vertex_buffer(display: &GlutinFacade) -> VertexBuffer<Vertex> {
let vertices = vec![
Vertex { position: [-1.0, -1.0, 0.0] },
Vertex { position: [1.0, -1.0, 0.0] },
Vertex { position: [0.0, 1.0, 0.0] }
];
let vertex_buffer = VertexBuffer::new(display, &vertices).unwrap();
vertex_buffer
}
fn create_shaders(display: &GlutinFacade) -> Program {
let vertex_shader_src = r#"
#version 330
layout (location = 0) in vec3 position;
uniform mat4 gWorld;
out vec4 color;
void main() {
gl_Position = gWorld * vec4(position, 1.0);
color = vec4(clamp(position, 0.0, 1.0), 1.0);
}
"#;
let fragment_shader_src = r#"
#version 330
in vec4 color;
out vec4 fragColor;
void main() {
fragColor = color;
}
"#;
Program::from_source(display,
vertex_shader_src, fragment_shader_src, None).unwrap()
}
fn render_scene(display: &GlutinFacade, vertex_buffer: &VertexBuffer<Vertex>, program: &Program,
scale: f32) {
// Build the transform matrix
// Note that the matrix is in column-major order
// so you need to transpose it for OpenGL
let sin_scale = scale.sin();
let world: [[f32; 4]; 4] = Matrix4::new(
sin_scale, 0.0, 0.0, 0.0,
0.0, sin_scale, 0.0, 0.0,
0.0, 0.0, sin_scale, 0.0,
0.0, 0.0, 0.0, 1.0
).transpose().into();
let uniform = uniform!{ gWorld: world };
let mut frame = display.draw();
frame.clear_color(0.0, 0.0, 0.0, 0.0);
frame.draw(vertex_buffer, &NoIndices(PrimitiveType::TrianglesList), program,
&uniform, &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 09")
.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
let mut scale: f32 = 0.0;
loop {
// Change the scale
// (I use a smaller factor than the one used in the original source code
// since the original factor is too large in my case)
scale += 0.0001;
// Render
render_scene(&display, &vertex_buffer, &program, scale);
// Handle events
for event in display.poll_events() {
match event {
Event::Closed => return,
_ => ()
}
}
}
}