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
use graphics::{ self, DrawState, Graphics };
use glium::{
Surface, Program, VertexBuffer,
};
use glium::index::{ NoIndices, PrimitiveType };
use glium::backend::Facade;
use shader_version::{ Shaders, OpenGL };
use shader_version::glsl::GLSL;
use Texture;
use draw_state;
#[derive(Copy, Clone)]
struct PlainVertex {
pos: [f32; 2],
}
implement_vertex!(PlainVertex, pos);
#[derive(Copy, Clone)]
struct TexturedVertex {
pos: [f32; 2],
uv: [f32; 2],
}
implement_vertex!(TexturedVertex, pos, uv);
pub struct Glium2d {
plain_buffer: VertexBuffer<PlainVertex>,
textured_buffer: VertexBuffer<TexturedVertex>,
shader_texture: Program,
shader_color: Program,
}
impl Glium2d {
pub fn new<W>(opengl: OpenGL, window: &W) -> Glium2d where W: Facade {
use shaders::{ colored, textured };
let src = |bytes| { unsafe { ::std::str::from_utf8_unchecked(bytes) } };
let glsl = opengl.to_glsl();
Glium2d {
plain_buffer: VertexBuffer::empty_dynamic(window, graphics::BACK_END_MAX_VERTEX_COUNT).unwrap(),
textured_buffer: VertexBuffer::empty_dynamic(window, graphics::BACK_END_MAX_VERTEX_COUNT).unwrap(),
shader_texture:
Program::from_source(window,
Shaders::new().set(GLSL::V1_20, src(textured::VERTEX_GLSL_120))
.set(GLSL::V1_50, src(textured::VERTEX_GLSL_150_CORE))
.get(glsl).unwrap(),
Shaders::new().set(GLSL::V1_20, src(textured::FRAGMENT_GLSL_120))
.set(GLSL::V1_50, src(textured::FRAGMENT_GLSL_150_CORE))
.get(glsl).unwrap(),
None).ok().expect("failed to initialize textured shader"),
shader_color:
Program::from_source(window,
Shaders::new().set(GLSL::V1_20, src(colored::VERTEX_GLSL_120))
.set(GLSL::V1_50, src(colored::VERTEX_GLSL_150_CORE))
.get(glsl).unwrap(),
Shaders::new().set(GLSL::V1_20, src(colored::FRAGMENT_GLSL_120))
.set(GLSL::V1_50, src(colored::FRAGMENT_GLSL_150_CORE))
.get(glsl).unwrap(),
None).ok().expect("failed to initialize colored shader"),
}
}
}
pub struct GliumGraphics<'d, 's, S: 's> {
system: &'d mut Glium2d,
surface: &'s mut S,
}
impl<'d, 's, S> GliumGraphics<'d, 's, S> {
pub fn new(system: &'d mut Glium2d, surface: &'s mut S)
-> GliumGraphics<'d, 's, S> {
GliumGraphics {
system: system,
surface: surface,
}
}
}
impl<'d, 's, S: Surface> Graphics for GliumGraphics<'d, 's, S> {
type Texture = Texture;
fn clear_color(&mut self, color: [f32; 4]) {
let (r, g, b, a) = (color[0], color[1], color[2], color[3]);
self.surface.clear_color(r, g, b, a);
}
fn clear_stencil(&mut self, value: u8) {
self.surface.clear_stencil(value as i32);
}
fn tri_list<F>(
&mut self,
draw_state: &DrawState,
color: &[f32; 4],
mut f: F
)
where F: FnMut(&mut FnMut(&[f32]))
{
f(&mut |vertices: &[f32]| {
self.system.plain_buffer.invalidate();
let slice = self.system.plain_buffer.slice(0..vertices.len() / 2).unwrap();
slice.write({
&(0 .. vertices.len() / 2)
.map(|i| PlainVertex {
pos: [vertices[2 * i], vertices[2 * i + 1]]
})
.collect::<Vec<_>>()
});
self.surface.draw(
slice,
&NoIndices(PrimitiveType::TrianglesList),
&self.system.shader_color,
&uniform! { color: *color },
&draw_state::convert_draw_state(draw_state),
)
.ok()
.expect("failed to draw triangle list");
})
}
fn tri_list_uv<F>(
&mut self,
draw_state: &DrawState,
color: &[f32; 4],
texture: &Texture,
mut f: F
)
where F: FnMut(&mut FnMut(&[f32], &[f32]))
{
use std::cmp::min;
f(&mut |vertices: &[f32], texture_coords: &[f32]| {
let len = min(vertices.len(), texture_coords.len()) / 2;
self.system.textured_buffer.invalidate();
let slice = self.system.textured_buffer.slice(0..len).unwrap();
slice.write({
&(0 .. len)
.map(|i| TexturedVertex {
pos: [vertices[2 * i], vertices[2 * i + 1]],
uv: [texture_coords[2 * i], 1.0 - texture_coords[2 * i + 1]]
})
.collect::<Vec<_>>()
});
let ref texture = texture.0;
self.surface.draw(
slice,
&NoIndices(PrimitiveType::TrianglesList),
&self.system.shader_texture,
&uniform! {
color: *color,
s_texture: texture
},
&draw_state::convert_draw_state(draw_state),
)
.ok()
.expect("failed to draw triangle list");
})
}
}