-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmain.rs
104 lines (86 loc) · 2.83 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
use glium::backend::glutin::SimpleWindowBuilder;
use glium::backend::Facade;
use glium::uniform;
use glium::winit::event::{Event, WindowEvent};
use glium::winit::event_loop::{ControlFlow, EventLoop};
use glium::Program;
use obj::{load_obj, Obj};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let event_loop = EventLoop::new()?;
// building the display, ie. the main object
let (_window, display) = SimpleWindowBuilder::new()
.with_inner_size(500, 400)
.with_title("obj-rs")
.build(&event_loop);
let input = include_bytes!("../../obj-rs/tests/fixtures/normal-cone.obj");
let obj: Obj = load_obj(&input[..])?;
let vb = obj.vertex_buffer(display.get_context())?;
let ib = obj.index_buffer(display.get_context())?;
let program = Program::from_source(
&display,
r#"
#version 410
uniform mat4 matrix;
in vec3 position;
in vec3 normal;
smooth out vec3 _normal;
void main() {
gl_Position = matrix * vec4(position, 1.0);
_normal = normalize(normal);
}
"#,
r#"
#version 410
uniform vec3 light;
smooth in vec3 _normal;
out vec4 result;
void main() {
result = vec4(clamp(dot(_normal, -light), 0.0f, 1.0f) * vec3(1.0f, 0.93f, 0.56f), 1.0f);
}
"#,
None,
)?;
// drawing a frame
let uniforms = uniform! {
matrix: [
[ 2.356724, 0.000000, -0.217148, -0.216930],
[ 0.000000, 2.414214, 0.000000, 0.000000],
[-0.523716, 0.000000, -0.977164, -0.976187],
[ 0.000000, 0.000000, 9.128673, 9.219544f32]
],
light: (-1.0, -1.0, -1.0f32)
};
let params = glium::DrawParameters {
depth: glium::Depth {
test: glium::DepthTest::IfLess,
write: true,
..Default::default()
},
..Default::default()
};
// Main loop
#[allow(deprecated, reason = "TODO: Migrate this into `.run_app()` later")]
event_loop.run(move |event, active_event_loop| {
active_event_loop.set_control_flow(ControlFlow::Wait);
match event {
Event::LoopExiting => {}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => active_event_loop.exit(),
Event::WindowEvent {
event: WindowEvent::RedrawRequested,
..
} => {
// draw
use glium::Surface;
let mut target = display.draw();
target.clear_color_and_depth((0.0, 0.0, 0.0, 0.0), 1.0);
target.draw(&vb, &ib, &program, &uniforms, ¶ms).unwrap();
target.finish().unwrap();
}
_ => {}
}
})?;
Ok(())
}