-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtutorial_16.rs
More file actions
177 lines (144 loc) · 5.68 KB
/
tutorial_16.rs
File metadata and controls
177 lines (144 loc) · 5.68 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#[macro_use]
extern crate glium;
extern crate ogldev;
extern crate image;
use std::path::Path;
use glium::{DisplayBuild, Surface, VertexBuffer, Program, DrawParameters};
use glium::glutin::{Event, WindowBuilder, VirtualKeyCode};
use glium::index::{IndexBuffer, PrimitiveType};
use glium::backend::glutin_backend::GlutinFacade;
use glium::texture::{RawImage2d, Texture2d};
use glium::draw_parameters::BackfaceCullingMode;
use ogldev::{Camera, Pipeline};
const WINDOW_WIDTH: u32 = 1280;
const WINDOW_HEIGHT: u32 = 1024;
// Represent a 3D vertex
#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 3],
tex_coords: [f32; 2]
}
// Let glium implement Vertex for us
implement_vertex!(Vertex, position, tex_coords);
fn create_vertex_buffer(display: &GlutinFacade) -> VertexBuffer<Vertex> {
let vertices = vec![
Vertex { position: [-1.0, -1.0, 0.5773], tex_coords: [0.0, 0.0] },
Vertex { position: [0.0, -1.0, -1.15475], tex_coords: [0.5, 0.0] },
Vertex { position: [1.0, -1.0, 0.5773], tex_coords: [1.0, 0.0] },
Vertex { position: [0.0, 1.0, 0.0], tex_coords: [0.5, 1.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;
layout (location = 1) in vec2 tex_coords;
uniform mat4 gWVP;
out vec2 texCoord0;
void main() {
gl_Position = gWVP * vec4(position, 1.0);
texCoord0 = tex_coords;
}
"#;
let fragment_shader_src = r#"
#version 330
in vec2 texCoord0;
out vec4 fragColor;
uniform sampler2D gSampler;
void main() {
fragColor = texture2D(gSampler, texCoord0.xy);
}
"#;
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, camera: &mut Camera, scale: f32,
texture: &Texture2d, params: &DrawParameters) {
// Notify the camera
camera.on_render();
// Create a Pipeline
let mut pipeline = Pipeline::new();
pipeline.rotate(0.0, scale, 0.0);
pipeline.world_pos(0.0, 0.0, 3.0);
pipeline.set_camera(camera.get_pos(), camera.get_target(), camera.get_up());
pipeline.set_perspective_proj(60.0, WINDOW_WIDTH as f32, WINDOW_HEIGHT as f32, 1.0, 100.0);
// Set the uniform matrix
let wvp: [[f32; 4]; 4] = pipeline.get_wvp_trans().into();
let uniform = uniform!{ gWVP: wvp, gSampler: texture };
// 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, params).unwrap();
frame.finish().unwrap();
}
fn main() {
// Set up and create a window
let display = WindowBuilder::new()
.with_dimensions(WINDOW_WIDTH, WINDOW_HEIGHT)
.with_srgb(Some(true))
.with_title("Tutorial 16")
.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);
// Create a camera
let mut camera = Camera::default(WINDOW_WIDTH, WINDOW_HEIGHT);
// Setup culling backface
// NOTE: Here is a little bit different from the original tutorial. The tutorial says that you
// need to specify the front face is drawn clockwisely or counterclockwisely. However, in glium
// , to cull back faces, you have to specify the way of identifying BACK FACES. That's why I
// give `BackfaceCullingMode::CullCounterClockwise` here, instead of
// `BackfaceCullingMode::CullClockwise`. You can try the effect of both parameters.
let params = DrawParameters {
backface_culling: BackfaceCullingMode::CullCounterClockwise,
.. Default::default()
};
// Load a texture
let image = image::open(&Path::new("content/test.png")).unwrap().to_rgba();
let image_dim = image.dimensions();
let image = RawImage2d::from_raw_rgba_reversed(image.into_raw(), image_dim);
let texture = Texture2d::new(&display, image).unwrap();
// 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.01;
// Render
render_scene(&display, &vertex_buffer, &index_buffer, &program, &mut camera, scale, &texture, ¶ms);
// Handle events
for event in display.poll_events() {
match event {
Event::Closed => return,
Event::KeyboardInput(_, _, Some(VirtualKeyCode::Q)) => {
std::process::exit(0);
},
Event::KeyboardInput(_, _, Some(key)) => {
camera.on_key_board(key);
},
Event::MouseMoved(x, y) => {
camera.on_mouse(x, y);
},
_ => ()
}
}
}
}