-
Notifications
You must be signed in to change notification settings - Fork 4
/
cube.rs
227 lines (208 loc) · 7.25 KB
/
cube.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
#[macro_use]
extern crate glium;
extern crate arcball;
extern crate cgmath;
use arcball::ArcballCamera;
use cgmath::{Vector2, Vector3};
use glium::index::PrimitiveType;
use glium::{glutin, Surface};
#[derive(Copy, Clone)]
struct Vertex {
pos: [f32; 3],
color: [f32; 3],
}
implement_vertex!(Vertex, pos, color);
// NOTE: This is still provided as an example of how to hook up mouse events
// to the camera, however it should eventually be replaced by one using
// device events (I think) since window events don't seem to fire often
fn main() {
let window = glutin::window::WindowBuilder::new().with_title("Arcball Camera Cube Example");
let context = glutin::ContextBuilder::new()
.with_vsync(true);
let event_loop = glutin::event_loop::EventLoop::new();
let display =
glium::Display::new(window, context, &event_loop).expect("failed to create display");
// Hard-coded cube triangle strip
let vertex_buffer = glium::VertexBuffer::new(
&display,
&[
Vertex {
pos: [1.0, 1.0, -1.0],
color: [1.0, 0.0, 0.0],
},
Vertex {
pos: [-1.0, 1.0, -1.0],
color: [1.0, 0.0, 0.0],
},
Vertex {
pos: [1.0, 1.0, 1.0],
color: [1.0, 0.0, 0.0],
},
Vertex {
pos: [-1.0, 1.0, 1.0],
color: [0.0, 1.0, 0.0],
},
Vertex {
pos: [-1.0, -1.0, 1.0],
color: [0.0, 1.0, 0.0],
},
Vertex {
pos: [-1.0, 1.0, -1.0],
color: [0.0, 1.0, 0.0],
},
Vertex {
pos: [-1.0, -1.0, -1.0],
color: [0.0, 0.0, 1.0],
},
Vertex {
pos: [1.0, 1.0, -1.0],
color: [0.0, 0.0, 1.0],
},
Vertex {
pos: [1.0, -1.0, -1.0],
color: [0.0, 0.0, 1.0],
},
Vertex {
pos: [1.0, 1.0, 1.0],
color: [1.0, 1.0, 0.0],
},
Vertex {
pos: [1.0, -1.0, 1.0],
color: [1.0, 1.0, 0.0],
},
Vertex {
pos: [-1.0, -1.0, 1.0],
color: [1.0, 1.0, 0.0],
},
Vertex {
pos: [1.0, -1.0, -1.0],
color: [1.0, 0.0, 1.0],
},
Vertex {
pos: [-1.0, -1.0, -1.0],
color: [1.0, 0.0, 1.0],
},
],
)
.unwrap();
let index_buffer = glium::index::NoIndices(PrimitiveType::TriangleStrip);
let program = program!(&display,
140 => {
vertex: "
#version 140
uniform mat4 proj_view;
in vec3 pos;
in vec3 color;
out vec3 vcolor;
void main(void) {
gl_Position = proj_view * vec4(pos, 1.0);
vcolor = color;
}
",
fragment: "
#version 140
in vec3 vcolor;
out vec4 color;
void main(void) {
color = vec4(vcolor, 1.0);
}
"
},
)
.unwrap();
let display_dims = display.get_framebuffer_dimensions();
let persp_proj = cgmath::perspective(
cgmath::Deg(65.0),
display_dims.0 as f32 / display_dims.1 as f32,
1.0,
200.0,
);
let mut arcball_camera = ArcballCamera::new(
Vector3::new(0.0, 0.0, 0.0),
1.0,
[display_dims.0 as f32, display_dims.1 as f32],
);
// Track if left/right mouse is down
let mut mouse_pressed = [false, false];
let mut prev_mouse = None;
event_loop.run(move |event, _, control_flow| {
let mut should_quit = false;
println!("running");
match event {
glutin::event::Event::WindowEvent { event, .. } => match event {
glutin::event::WindowEvent::CloseRequested => should_quit = true,
glutin::event::WindowEvent::KeyboardInput { input, .. } => match input.virtual_keycode {
Some(glutin::event::VirtualKeyCode::Escape) => should_quit = true,
_ => {}
},
glutin::event::WindowEvent::CursorMoved { position, .. } if prev_mouse.is_none() => {
prev_mouse = Some(position);
println!("init mouse = {:?}", prev_mouse);
}
glutin::event::WindowEvent::CursorMoved { position, .. } => {
let prev = prev_mouse.unwrap();
if mouse_pressed[0] {
arcball_camera.rotate(
Vector2::new(prev.x as f32, prev.y as f32),
Vector2::new(position.x as f32, position.y as f32),
);
} else if mouse_pressed[1] {
let mouse_delta = Vector2::new(
(position.x - prev.x) as f32,
(position.y - prev.y) as f32,
);
arcball_camera.pan(mouse_delta);
}
prev_mouse = Some(position);
println!("prev = {:?}", prev_mouse);
}
glutin::event::WindowEvent::MouseInput { state, button, .. } => {
if button == glutin::event::MouseButton::Left {
mouse_pressed[0] = state == glutin::event::ElementState::Pressed;
} else if button == glutin::event::MouseButton::Right {
mouse_pressed[1] = state == glutin::event::ElementState::Pressed;
}
}
glutin::event::WindowEvent::MouseWheel { delta, .. } => {
let y = match delta {
glutin::event::MouseScrollDelta::LineDelta(_, y) => y,
glutin::event::MouseScrollDelta::PixelDelta(p) => p.y as f32,
};
arcball_camera.zoom(y, 0.16);
}
_ => {}
},
_ => {}
}
*control_flow = if should_quit {
glutin::event_loop::ControlFlow::Exit
} else {
glutin::event_loop::ControlFlow::Poll
};
let proj_view: [[f32; 4]; 4] = (persp_proj * arcball_camera.get_mat4()).into();
let uniforms = uniform! {
proj_view: proj_view,
};
let draw_params = glium::DrawParameters {
depth: glium::Depth {
test: glium::draw_parameters::DepthTest::IfLess,
write: true,
..Default::default()
},
..Default::default()
};
let mut target = display.draw();
target.clear_color(0.1, 0.1, 0.1, 0.0);
target.clear_depth(1.0);
target
.draw(
&vertex_buffer,
&index_buffer,
&program,
&uniforms,
&draw_params,
)
.unwrap();
target.finish().unwrap();
});
}