-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
63 lines (55 loc) · 1.99 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
use glfw::{Key, Modifiers, WindowEvent};
use gloam::{
app,
context::ClearMask,
shader::{program::Linker, Shader, ShaderType},
vertex::{Primitive, Usage, VOBInit, VertexObjectBuilder},
Result,
};
use std::path::PathBuf;
fn main() -> Result<()> {
let (mut window, mut ctx) = app::init_default_opengl_3_3("HelloTriangle")?;
window.set_key_polling(true);
window.set_framebuffer_size_polling(true);
let vs_src = PathBuf::new()
.join("examples")
.join("hello_triangle")
.join("hello_triangle_vertex.glsl");
let vertex_shader = Shader::new(vs_src, ShaderType::Vertex)?;
let fs_src = PathBuf::new()
.join("examples")
.join("hello_triangle")
.join("hello_triangle_fragment.glsl");
let fragment_shader = Shader::new(fs_src, ShaderType::Fragment)?;
let program = Linker::new()
.attach_shader(vertex_shader)
.attach_shader(fragment_shader)
.link(&mut ctx)?;
let triangle = VertexObjectBuilder::<VOBInit>::new(Primitive::Triangles, Usage::Static)
.attribute(
"aPosition",
3,
&[-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.0, 0.5, 0.0],
)?
.attribute("aColor", 3, &[1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0])?
.build(&mut ctx, program)?;
ctx.try_use_program(program)?;
ctx.try_bind_vertex_object(triangle)?;
window.run_event_loop(|win, event| {
match event {
None => (),
Some(win_event) => match win_event {
WindowEvent::Key(key, _, _, modifier) => match (modifier, key) {
(Modifiers::Super, Key::W) | (_, Key::Escape) => win.set_should_close(true),
_ => (),
},
WindowEvent::FramebufferSize(width, height) => ctx.viewport(0, 0, width, height),
_ => (),
},
}
ctx.clear(&[ClearMask::Color(0.2, 0.2, 0.2, 0.0)]);
ctx.try_render()?;
win.draw();
Ok(())
})
}