-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.rs
89 lines (81 loc) · 2.34 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
use glu_sys::*;
use fltk::{
prelude::*,
*,
};
use std::cell::RefCell;
use std::rc::Rc;
const W: i32 = 600;
const H: i32 = 400;
pub fn main() {
let app = app::App::default();
let mut wind = window::GlWindow::new(100, 100, W, H, "Rotate me!");
wind.make_resizable(true);
wind.end();
wind.show();
let rotangle = Rc::from(RefCell::from(0.0));
let rotangle_rc = rotangle.clone();
wind.draw(move |_| draw_triangle(&rotangle_rc.borrow()));
let (s, r) = app::channel::<(i32, i32)>();
wind.handle(move |_, ev| match ev {
enums::Event::Drag => {
s.send(app::event_coords());
true
}
_ => false,
});
while app.wait() {
match r.recv() {
Some(coords) => {
let rand: f32 = ((coords.0 - W / 2) * (coords.1 - H / 2) / 360) as f32;
*rotangle.borrow_mut() += rand;
wind.redraw();
}
None => (),
}
}
}
fn draw_triangle(rotangle: &f32) {
unsafe {
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS);
glDepthFunc(GL_LEQUAL);
glDepthRange(0.0, 1.0);
glDepthMask(1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, W, H);
gluPerspective(45.0, (W as f32 / H as f32).into(), 1.0, 10.0);
glTranslatef(0.0, 0.0, -5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(*rotangle, 1.0, 1.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glEnd();
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(0.0, -1.0, -1.0);
glVertex3f(1.0, -1.0, 1.0);
glEnd();
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(0.0, -1.0, -1.0);
glEnd();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(0.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);
glEnd();
glLoadIdentity();
glRasterPos2f(-3.0, -2.0);
}
}