-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtutorial_11.rs
More file actions
132 lines (106 loc) · 3.64 KB
/
tutorial_11.rs
File metadata and controls
132 lines (106 loc) · 3.64 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
123
124
125
126
127
128
129
130
131
132
#[macro_use]
extern crate glium;
extern crate cgmath;
extern crate ogldev;
use glium::{DisplayBuild, Surface, VertexBuffer, Program};
use glium::glutin::{Event, WindowBuilder};
use glium::index::{IndexBuffer, PrimitiveType};
use glium::backend::glutin_backend::GlutinFacade;
use ogldev::Pipeline;
// 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: [0.0, -1.0, 1.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_index_buffer(display: &GlutinFacade) -> IndexBuffer<u32> {
let indcies = vec![
0, 3, 1,
1, 3, 2,
2, 3, 0,
0, 1, 2
];
let index_buffer = IndexBuffer::new(display, PrimitiveType::TrianglesList, &indcies).unwrap();
index_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>,
index_buffer: &IndexBuffer<u32>, program: &Program, scale: f32) {
// Create a Pipeline
let mut pipeline = Pipeline::new();
pipeline.scale((scale * 0.1).sin(), (scale * 0.1).sin(), (scale * 0.1).sin());
pipeline.world_pos(scale.sin(), 0.0, 0.0);
pipeline.rotate(scale.sin() * 90.0, scale.sin() * 90.0, scale.sin() * 90.0);
// Set the uniform matrix
let world: [[f32; 4]; 4] = pipeline.get_world_trans().into();
let uniform = uniform!{ gWorld: world };
// Drawing
let mut frame = display.draw();
frame.clear_color(0.0, 0.0, 0.0, 0.0);
frame.draw(vertex_buffer, index_buffer, 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 11")
.build_glium()
.unwrap();
// Create a vertex buffer and indices
let vertex_buffer = create_vertex_buffer(&display);
let index_buffer = create_index_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, &index_buffer, &program, scale);
// Handle events
for event in display.poll_events() {
match event {
Event::Closed => return,
_ => ()
}
}
}
}