forked from gtk-rs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglarea.rs
221 lines (175 loc) · 6.02 KB
/
glarea.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
//! # GLArea Sample
//!
//! This sample demonstrates how to use GLAreas and OpenGL
#[cfg(feature = "opengl")]
#[macro_use]
extern crate glium;
// make moving clones into closures more convenient
macro_rules! clone {
($($n:ident),+; || $body:block) => (
{
$( let $n = $n.clone(); )+
move || { $body }
}
);
($($n:ident),+; |$($p:ident),+| $body:block) => (
{
$( let $n = $n.clone(); )+
move |$($p),+| { $body }
}
);
}
#[cfg(feature = "opengl")]
mod example {
extern crate gtk;
extern crate libc;
extern crate epoxy;
extern crate shared_library;
use std::ptr;
use std::cell::RefCell;
use std::rc::Rc;
use self::gtk::traits::*;
use self::gtk::signal::Inhibit;
use self::gtk::{GLArea, Window};
use glium;
use glium::Surface;
use self::shared_library::dynamic_library::DynamicLibrary;
pub fn main() {
if gtk::init().is_err() {
println!("Failed to initialize GTK.");
return;
}
let window = Window::new(gtk::WindowType::Toplevel).unwrap();
let glarea = GLArea::new().unwrap();
window.connect_delete_event(|_, _| {
gtk::main_quit();
Inhibit(false)
});
epoxy::load_with(|s| {
unsafe {
match DynamicLibrary::open(None).unwrap().symbol(s) {
Ok(v) => v,
Err(_) => ptr::null(),
}
}
});
struct Backend {
glarea: GLArea,
}
unsafe impl glium::backend::Backend for Backend {
fn swap_buffers(&self) -> Result<(), glium::SwapBuffersError> {
Ok(())
}
unsafe fn get_proc_address(&self, symbol: &str) -> *const libc::c_void {
epoxy::get_proc_addr(symbol)
}
fn get_framebuffer_dimensions(&self) -> (u32, u32) {
(self.glarea.get_allocated_width() as u32, self.glarea.get_allocated_height() as u32)
}
fn is_current(&self) -> bool {
unsafe { self.make_current() };
true
}
unsafe fn make_current(&self) {
if self.glarea.get_realized() {
self.glarea.make_current();
}
}
}
struct Facade {
context: Rc<glium::backend::Context>,
}
impl glium::backend::Facade for Facade {
fn get_context(&self) -> &Rc<glium::backend::Context> {
&self.context
}
}
impl Facade {
fn draw(&self) -> glium::Frame {
glium::Frame::new(self.context.clone(), self.context.get_framebuffer_dimensions())
}
}
#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 2],
color: [f32; 3]
}
implement_vertex!(Vertex, position, color);
struct State {
display: Facade,
vertex_buffer: glium::VertexBuffer<Vertex>,
indices: glium::index::NoIndices,
program: glium::program::Program,
}
let state: Rc<RefCell<Option<State>>> = Rc::new(RefCell::new(None));
glarea.connect_realize(clone!(glarea, state; |_widget| {
let mut state = state.borrow_mut();
let display = Facade {
context: unsafe {
glium::backend::Context::new::<_, ()>(
Backend {
glarea: glarea.clone(),
}, true, Default::default())
}.unwrap(),
};
let vertices = vec![
Vertex{ position: [0.0, 0.5], color: [1.0, 0.0, 0.0] },
Vertex{ position: [0.5, -0.5], color: [0.0, 1.0, 0.0] },
Vertex{ position: [-0.5, -0.5], color: [0.0, 0.0, 1.0] },
];
let vertex_buffer = glium::VertexBuffer::new(&display, &vertices).unwrap();
let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);
let vert_shader_src = r#"
#version 140
in vec2 position;
in vec3 color;
out vec3 vertex_color;
void main() {
vertex_color = color;
gl_Position = vec4(position, 0.0, 1.0);
}"#;
let frag_shader_src = r#"
#version 140
in vec3 vertex_color;
out vec4 color;
void main() {
color = vec4(vertex_color, 1.0);
}"#;
let program = glium::Program::from_source(&display, vert_shader_src,
frag_shader_src, None).unwrap();
*state = Some(State {
display: display,
vertex_buffer: vertex_buffer,
indices: indices,
program: program,
});
}));
glarea.connect_unrealize(clone!(state; |_widget| {
let mut state = state.borrow_mut();
*state = None;
}));
glarea.connect_render(clone!(state; |_glarea, _glctx| {
let state = state.borrow();
let state = state.as_ref().unwrap();
let mut target = state.display.draw();
target.clear_color(0.3, 0.3, 0.3, 1.0);
target.draw(&state.vertex_buffer, &state.indices, &state.program,
&glium::uniforms::EmptyUniforms, &Default::default()).unwrap();
target.finish().unwrap();
Inhibit(false)
}));
window.set_title("GLArea Example");
window.set_default_size(400, 400);
window.add(&glarea);
window.show_all();
gtk::main();
}
}
#[cfg(feature = "opengl")]
fn main() {
example::main()
}
#[cfg(not(feature = "opengl"))]
fn main() {
println!("Did you forget to build with `--features opengl`?");
}