-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
282 lines (250 loc) · 11.6 KB
/
main.rs
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use glfw::{Action, Key, Modifiers, MouseButton, WindowEvent};
use gloam::{
app,
camera::{Camera, FreeCamera},
context::ClearMask,
error::Result,
shader::{program::Linker, Shader, ShaderType},
texture::{TextureBuilder, TextureFilterParam, TextureWrapParam},
uniform::Uniform,
vertex::{Primitive, Usage, VOBInit, VertexObjectBuilder},
};
use nalgebra_glm as glm;
use std::{collections::HashSet, f32::consts::PI, path::PathBuf, thread};
fn main() -> Result<()> {
let cube_positions: [glm::TVec3<f32>; 10] = [
glm::vec3(0.0, 0.0, 0.0),
glm::vec3(2.0, 5.0, -15.0),
glm::vec3(-1.5, -2.2, -2.5),
glm::vec3(-3.8, -2.0, -12.3),
glm::vec3(2.4, -0.4, -3.5),
glm::vec3(-1.7, 3.0, -7.5),
glm::vec3(1.3, -2.0, -2.5),
glm::vec3(1.5, 2.0, -2.5),
glm::vec3(1.5, 0.2, -1.5),
glm::vec3(-1.3, 1.0, -1.5),
];
let (mut window, mut ctx) = app::init_default_opengl_3_3("HelloFreeCamera").unwrap();
window.set_key_polling(true);
window.set_mouse_button_polling(true);
window.set_cursor_pos_polling(true);
window.set_framebuffer_size_polling(true);
let aspect_ratio = {
let (width, height) = window.get_size();
width as f32 / height as f32
};
ctx.try_enable_depth_test(None).unwrap();
let vertex_shader_src = PathBuf::new()
.join("examples")
.join("hello_camera")
.join("hello_camera_vertex.glsl");
let fragment_shader_src = PathBuf::new()
.join("examples")
.join("hello_camera")
.join("hello_camera_fragment.glsl");
let vertex_shader = Shader::new(vertex_shader_src, ShaderType::Vertex)?;
let fragment_shader = Shader::new(fragment_shader_src, ShaderType::Fragment)?;
let program = Linker::new()
.attach_shader(vertex_shader)
.attach_shader(fragment_shader)
.link(&mut ctx)?;
let texture_metal_src = PathBuf::new()
.join("examples")
.join("hello_camera")
.join("hello_camera_metal.jpg");
let texture_sift_src = PathBuf::new()
.join("examples")
.join("hello_camera")
.join("hello_camera_sift.png");
let t = std::time::Instant::now();
let raw_textures = thread::scope(|s| {
let metal = s.spawn(|| TextureBuilder::new_2d_rgba8(texture_metal_src));
let sift = s.spawn(|| TextureBuilder::new_2d_rgba8(texture_sift_src));
[metal, sift]
.into_iter()
.map(|j| j.join().unwrap())
.collect::<Vec<_>>()
});
println!(
"time to load textures: {}ms",
std::time::Instant::now().duration_since(t).as_millis()
);
let [raw_texture_metal, raw_texture_sift]: [Result<TextureBuilder>; 2] =
raw_textures.try_into().unwrap();
let texture_metal = raw_texture_metal
.map(|b| b.s_wrap(TextureWrapParam::Repeat))
.map(|b| b.t_wrap(TextureWrapParam::Repeat))
.map(|b| b.min_filter(TextureFilterParam::LinearMipmapLinear))
.map(|b| b.mag_filter(TextureFilterParam::Linear))
.and_then(|b| b.build(&mut ctx))?;
let texture_sift = raw_texture_sift
.map(|b| b.s_wrap(TextureWrapParam::Repeat))
.map(|b| b.t_wrap(TextureWrapParam::Repeat))
.map(|b| b.min_filter(TextureFilterParam::LinearMipmapLinear))
.map(|b| b.mag_filter(TextureFilterParam::Linear))
.and_then(|b| b.build(&mut ctx))?;
let surface = VertexObjectBuilder::<VOBInit>::new(Primitive::Triangles, Usage::Static)
.attribute("pos_attr", 3, &POSITION_ATTR)
.and_then(|b| b.attribute("tex_attr", 2, &TEXTURE_ATTR))
.and_then(|b| b.build(&mut ctx, program))?;
ctx.try_use_program(program)?;
ctx.try_bind_vertex_object(surface)?;
let texture_unit_metal = ctx.activate_texture(texture_metal, true)?;
let texture_unit_sift = ctx.activate_texture(texture_sift, true)?;
ctx.try_set_uniform(&Uniform::new_1i("metal", texture_unit_metal))?;
ctx.try_set_uniform(&Uniform::new_1i("sift", texture_unit_sift))?;
let mut camera = FreeCamera::new(
glm::vec3(0.0, 0.0, 5.0),
glm::vec3(0.0, 0.0, 0.0),
glm::vec3(0.0, 1.0, 0.0),
10.0,
0.2,
);
ctx.try_set_uniform(&Uniform::new_mat4fv(
"view",
camera.get_view_matrix(),
false,
))?;
let projection_matrix = glm::perspective(PI / 4.0, aspect_ratio, 0.1, 100.0);
ctx.try_set_uniform(&Uniform::new_mat4fv("projection", projection_matrix, false))?;
let identity_matrix = glm::identity::<f32, 4>();
let mut time_last_draw = window.get_time() as f32;
let (mut cursor_x, mut cursor_y) = window.get_cursor_pos();
let mut mouse_down = false;
let mut active_dir_keys = HashSet::<Key>::new();
window.run_event_loop(|win, event| {
let time = win.get_time() as f32;
let dtime = time - time_last_draw;
match event {
None => (),
Some(win_event) => match win_event {
WindowEvent::Key(key, code, action, modifier) => {
match (key, code, action, modifier) {
(Key::W, _, _, Modifiers::Super) | (Key::Escape, _, _, _) => {
win.set_should_close(true);
}
(Key::W, _, Action::Press | Action::Repeat, _) => {
active_dir_keys.insert(key);
if active_dir_keys.contains(&Key::D) {
camera.move_forward_right(dtime);
} else if active_dir_keys.contains(&Key::A) {
camera.move_forward_left(dtime);
} else {
camera.move_forward(dtime);
}
ctx.try_set_uniform(&Uniform::new_mat4fv(
"view",
camera.get_view_matrix(),
false,
))?;
}
(Key::S, _, Action::Press | Action::Repeat, _) => {
active_dir_keys.insert(key);
if active_dir_keys.contains(&Key::D) {
camera.move_backward_right(dtime);
} else if active_dir_keys.contains(&Key::A) {
camera.move_backward_left(dtime);
} else {
camera.move_backward(dtime);
}
ctx.try_set_uniform(&Uniform::new_mat4fv(
"view",
camera.get_view_matrix(),
false,
))?;
}
(Key::D, _, Action::Press | Action::Repeat, _) => {
active_dir_keys.insert(key);
if active_dir_keys.contains(&Key::W) {
camera.move_forward_right(dtime);
} else if active_dir_keys.contains(&Key::S) {
camera.move_backward_right(dtime);
} else {
camera.move_right(dtime);
}
ctx.try_set_uniform(&Uniform::new_mat4fv(
"view",
camera.get_view_matrix(),
false,
))?;
}
(Key::A, _, Action::Press | Action::Repeat, _) => {
active_dir_keys.insert(key);
if active_dir_keys.contains(&Key::W) {
camera.move_forward_left(dtime);
} else if active_dir_keys.contains(&Key::S) {
camera.move_backward_left(dtime);
} else {
camera.move_left(dtime);
}
ctx.try_set_uniform(&Uniform::new_mat4fv(
"view",
camera.get_view_matrix(),
false,
))?;
}
(Key::W, _, Action::Release, _)
| (Key::A, _, Action::Release, _)
| (Key::S, _, Action::Release, _)
| (Key::D, _, Action::Release, _) => {
active_dir_keys.remove(&key);
}
_ => (),
}
}
WindowEvent::MouseButton(button, action, modifier) => {
match (button, action, modifier) {
(MouseButton::Button1, Action::Press, _) => {
mouse_down = true;
(cursor_x, cursor_y) = win.get_cursor_pos();
}
(MouseButton::Button1, Action::Release, _) => mouse_down = false,
_ => (),
}
}
WindowEvent::CursorPos(new_cursor_x, new_cursor_y) if mouse_down => {
let x_delta = (new_cursor_x - cursor_x) as f32;
let y_delta = (new_cursor_y - cursor_y) as f32;
let direction = glm::vec3(x_delta, y_delta, 0.0);
cursor_x = new_cursor_x;
cursor_y = new_cursor_y;
camera.rotate_to_direction(direction, dtime);
ctx.try_set_uniform(&Uniform::new_mat4fv(
"view",
camera.get_view_matrix(),
false,
))?;
}
WindowEvent::FramebufferSize(width, height) => ctx.viewport(0, 0, width, height),
_ => (),
},
}
ctx.clear(&[ClearMask::DepthBuffer, ClearMask::Color(0.2, 0.3, 0.3, 1.0)]);
for (i, position) in cube_positions.iter().enumerate() {
let angle = time * 20.0 * (i as f32 + 1.0);
let mut model_matrix = glm::translate(&identity_matrix, position);
model_matrix =
glm::rotate(&model_matrix, PI * angle / 180.0, &glm::vec3(1.0, 0.3, 0.5));
ctx.try_set_uniform(&Uniform::new_mat4fv("model", model_matrix, false))?;
ctx.try_render()?;
}
win.draw();
time_last_draw = time;
Ok(())
})
}
const POSITION_ATTR: [f32; 108] = [
-0.5, -0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5,
-0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5,
-0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5,
0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, -0.5, -0.5, 0.5, -0.5, -0.5, 0.5,
-0.5, 0.5, 0.5, 0.5, 0.5, -0.5, -0.5, -0.5, 0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5,
-0.5, -0.5, 0.5, -0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5,
];
const TEXTURE_ATTR: [f32; 72] = [
0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0,
1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
];