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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
extern crate fontconfig;
extern crate freetype;
extern crate libc;
extern crate glutin;
extern crate gl;
extern crate cgmath;
extern crate euclid;
mod list_fonts;
mod text;
use std::ffi::CString;
use std::mem::size_of;
use std::ptr;
use euclid::{Rect, Size2D, Point2D};
use libc::c_void;
use gl::types::*;
use cgmath::Matrix;
use text::RasterizedGlyph;
static TEXT_SHADER_V: &'static str = include_str!("../res/text.v.glsl");
static TEXT_SHADER_F: &'static str = include_str!("../res/text.f.glsl");
fn main() {
let window = glutin::Window::new().unwrap();
let (width, height) = window.get_inner_size_pixels().unwrap();
unsafe {
window.make_current()
};
unsafe {
gl::load_with(|symbol| window.get_proc_address(symbol) as *const _);
gl::Viewport(0, 0, width as i32, height as i32);
}
let rasterizer = text::Rasterizer::new();
let glyph_j = rasterizer.get_glyph(180., 'J');
let tex = AlphaTexture::new(
glyph_j.width as i32,
glyph_j.height as i32,
glyph_j.buf.as_ptr() as *const _
);
unsafe {
gl::Enable(gl::BLEND);
gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
}
let mut vao: GLuint = 0;
let mut vbo: GLuint = 0;
let mut ebo: GLuint = 0;
unsafe {
gl::GenVertexArrays(1, &mut vao);
gl::GenBuffers(1, &mut vbo);
gl::GenBuffers(1, &mut ebo);
gl::BindVertexArray(vao);
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
gl::BufferData(
gl::ARRAY_BUFFER,
(size_of::<f32>() * 4 * 4) as GLsizeiptr,
ptr::null(),
gl::DYNAMIC_DRAW
);
let indices: [u32; 6] = [0, 1, 3,
1, 2, 3];
gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, ebo);
gl::BufferData(gl::ELEMENT_ARRAY_BUFFER,
6 * size_of::<u32>() as isize,
indices.as_ptr() as *const _,
gl::STATIC_DRAW);
gl::EnableVertexAttribArray(0);
gl::VertexAttribPointer(0, 4, gl::FLOAT, gl::FALSE, 4 * size_of::<f32>() as i32,
ptr::null());
gl::BindBuffer(gl::ARRAY_BUFFER, 0);
gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, 0);
gl::BindVertexArray(0);
}
let program = ShaderProgram::new(width, height);
for event in window.wait_events() {
unsafe {
gl::ClearColor(0.08, 0.08, 0.08, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT);
}
render(&program, &glyph_j, &tex, vbo, vao, ebo);
window.swap_buffers();
match event {
glutin::Event::Closed => break,
_ => ()
}
}
}
fn get_rect(glyph: &RasterizedGlyph, x: f32, y: f32) -> Rect<f32> {
Rect::new(
Point2D::new(x, y),
Size2D::new(glyph.width as f32, glyph.height as f32)
)
}
/// Render a character
fn render(program: &ShaderProgram, glyph: &RasterizedGlyph, tex: &AlphaTexture, vbo: GLuint,
vao: GLuint, ebo: GLuint)
{
program.activate();
unsafe {
// set color
gl::Uniform3f(program.u_color, 1., 1., 0.5);
}
let rect = get_rect(glyph, 10.0, 10.0);
// top right of character
let vertices: [[f32; 4]; 4] = [
[rect.max_x(), rect.max_y(), 1., 0.], // top-right
[rect.max_x(), rect.min_y(), 1., 1.], // bottom-right
[rect.min_x(), rect.min_y(), 0., 1.], // bottom-left
[rect.min_x(), rect.max_y(), 0., 0.], // top-left
];
unsafe {
bind_mask_texture(tex.id);
gl::BindVertexArray(vao);
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
gl::BufferSubData(
gl::ARRAY_BUFFER,
0,
(4 * 4 * size_of::<f32>()) as isize,
vertices.as_ptr() as *const _
);
gl::BindBuffer(gl::ARRAY_BUFFER, 0);
gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, ebo);
gl::DrawElements(gl::TRIANGLES, 6, gl::UNSIGNED_INT, ptr::null());
gl::BindVertexArray(0);
gl::BindTexture(gl::TEXTURE_2D, 0);
}
program.deactivate();
}
fn bind_mask_texture(id: u32) {
unsafe {
gl::ActiveTexture(gl::TEXTURE0);
gl::BindTexture(gl::TEXTURE_2D, id);
}
}
pub struct ShaderProgram {
id: GLuint,
/// projection matrix uniform
u_projection: GLint,
/// color uniform
u_color: GLint,
}
impl ShaderProgram {
pub fn activate(&self) {
unsafe {
gl::UseProgram(self.id);
}
}
pub fn deactivate(&self) {
unsafe {
gl::UseProgram(0);
}
}
pub fn new(width: u32, height: u32) -> ShaderProgram {
let vertex_shader = ShaderProgram::create_vertex_shader();
let fragment_shader = ShaderProgram::create_fragment_shader();
let program = ShaderProgram::create_program(vertex_shader, fragment_shader);
unsafe {
gl::DeleteShader(vertex_shader);
gl::DeleteShader(fragment_shader);
}
// get uniform locations
let projection_str = CString::new("projection").unwrap();
let color_str = CString::new("textColor").unwrap();
let (projection, color) = unsafe {
(
gl::GetUniformLocation(program, projection_str.as_ptr()),
gl::GetUniformLocation(program, color_str.as_ptr())
)
};
assert!(projection != gl::INVALID_VALUE as i32);
assert!(projection != gl::INVALID_OPERATION as i32);
assert!(color != gl::INVALID_VALUE as i32);
assert!(color != gl::INVALID_OPERATION as i32);
let shader = ShaderProgram {
id: program,
u_projection: projection,
u_color: color,
};
// set projection uniform
let ortho = cgmath::ortho(0., width as f32, 0., height as f32, -1., 1.);
let projection: [[f32; 4]; 4] = ortho.into();
println!("width: {}, height: {}", width, height);
shader.activate();
unsafe {
gl::UniformMatrix4fv(shader.u_projection,
1, gl::FALSE, projection.as_ptr() as *const _);
}
shader.deactivate();
shader
}
fn create_program(vertex: GLuint, fragment: GLuint) -> GLuint {
unsafe {
let program = gl::CreateProgram();
gl::AttachShader(program, vertex);
gl::AttachShader(program, fragment);
gl::LinkProgram(program);
let mut success: GLint = 0;
gl::GetProgramiv(program, gl::LINK_STATUS, &mut success);
if success != (gl::TRUE as GLint) {
panic!("failed to link shader program");
}
program
}
}
fn create_fragment_shader() -> GLuint {
unsafe {
let fragment_shader = gl::CreateShader(gl::FRAGMENT_SHADER);
let fragment_source = CString::new(TEXT_SHADER_F).unwrap();
gl::ShaderSource(fragment_shader, 1, &fragment_source.as_ptr(), ptr::null());
gl::CompileShader(fragment_shader);
let mut success: GLint = 0;
gl::GetShaderiv(fragment_shader, gl::COMPILE_STATUS, &mut success);
if success != (gl::TRUE as GLint) {
panic!("failed to compiler fragment shader");
}
fragment_shader
}
}
fn create_vertex_shader() -> GLuint {
unsafe {
let vertex_shader = gl::CreateShader(gl::VERTEX_SHADER);
let vertex_source = CString::new(TEXT_SHADER_V).unwrap();
gl::ShaderSource(vertex_shader, 1, &vertex_source.as_ptr(), ptr::null());
gl::CompileShader(vertex_shader);
let mut success: GLint = 0;
gl::GetShaderiv(vertex_shader, gl::COMPILE_STATUS, &mut success);
if success != (gl::TRUE as GLint) {
panic!("failed to compiler vertex shader");
}
vertex_shader
}
}
}
struct AlphaTexture {
pub id: GLuint,
pub width: i32,
pub height: i32,
}
impl AlphaTexture {
pub fn new(width: i32, height: i32, bytes: *const ::std::os::raw::c_void) -> AlphaTexture {
let mut id: GLuint = 0;
unsafe {
gl::PixelStorei(gl::UNPACK_ALIGNMENT, 1);
gl::GenTextures(1, &mut id);
gl::BindTexture(gl::TEXTURE_2D, id);
gl::TexImage2D(
gl::TEXTURE_2D,
0,
gl::RED as i32,
width as i32,
height as i32,
0,
gl::RED,
gl::UNSIGNED_BYTE,
bytes
);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as i32);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as i32);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR as i32);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::LINEAR as i32);
gl::BindTexture(gl::TEXTURE_2D, 0);
}
AlphaTexture {
id: id,
width: width,
height: height,
}
}
}
|