-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.rs
150 lines (137 loc) · 4.35 KB
/
text.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
use cgmath::conv::array4x4;
use cgmath::Matrix4;
use glium;
use glium::{Display, Program, Surface, implement_vertex, texture, uniform};
use std;
fn gamma<T>(x: T) -> f32
where
f32: From<T>,
T: Copy,
{
((f32::from(x)) / 255.).powf(2.2)
}
fn srgb<T>(c: [T; 3]) -> [f32; 4]
where
f32: From<T>,
T: Copy,
{
[gamma(c[0]), gamma(c[1]), gamma(c[2]), 0.0]
}
#[cfg(feature = "image")]
fn c64_font() -> (u32, u32, Vec<u8>) {
let image = image::load_from_memory_with_format(
&include_bytes!("textures/c64-font.png")[..],
image::PNG,
).unwrap()
.to_luma();
let (w, h) = image.dimensions();
(w, h, image.into_raw())
}
#[cfg(not(feature = "image"))]
fn c64_font() -> (u32, u32, Vec<u8>) {
let pixels = &include_bytes!("textures/c64-font.gray")[..];
(128, 128, Vec::from(pixels))
}
pub fn text_program(display: &Display) -> Program {
//load_program(display, "shaders/text.vert", "shaders/text.frag");
let vertex_shader_src = include_str!("shaders/text.vert");
let fragment_shader_src = include_str!("shaders/text.frag");
Program::from_source(display, vertex_shader_src, fragment_shader_src, None).unwrap()
}
#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 2],
tex_coords: [f32; 2],
}
implement_vertex!(Vertex, position, tex_coords);
pub struct Text {
tex: texture::Texture2d,
vertex_buffer: glium::VertexBuffer<Vertex>,
index_buffer: glium::IndexBuffer<u16>,
program: glium::Program,
params: glium::DrawParameters<'static>,
}
impl Text {
pub fn new(display: &Display) -> Text {
let (w, h, pixels) = c64_font();
let image = glium::texture::RawImage2d {
data: std::borrow::Cow::from(pixels),
width: w,
height: h,
format: glium::texture::ClientFormat::U8,
};
let tex = glium::texture::Texture2d::with_format(
display,
image,
glium::texture::UncompressedFloatFormat::U8,
glium::texture::MipmapsOption::NoMipmap,
).unwrap();
// building the vertex buffer, which contains all the vertices that we will draw
let vertex_buffer = {
glium::VertexBuffer::new(
display,
&[
Vertex {
position: [-0.5, -0.5],
tex_coords: [0.0, 1.0],
},
Vertex {
position: [-0.5, 0.5],
tex_coords: [0.0, 0.0],
},
Vertex {
position: [0.5, 0.5],
tex_coords: [1.0, 0.0],
},
Vertex {
position: [0.5, -0.5],
tex_coords: [1.0, 1.0],
},
],
).unwrap()
};
let index_buffer = glium::IndexBuffer::new(
display,
glium::index::PrimitiveType::TriangleStrip,
&[1 as u16, 2, 0, 3],
).unwrap();
let params = glium::DrawParameters {
depth: glium::Depth {
test: glium::draw_parameters::DepthTest::IfLess,
write: true,
..Default::default()
},
multisampling: true,
..Default::default()
};
Text {
tex: tex,
vertex_buffer: vertex_buffer,
index_buffer: index_buffer,
program: text_program(display),
params: params,
}
}
pub fn draw(&self, frame: &mut glium::Frame, c: char, model: &Matrix4<f32>, perspview: &[[f32; 4]; 4]) {
let uniforms =
uniform! {
model: array4x4(*model),
perspview: *perspview,
tex: self.tex.sampled()
.magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest),
index: c as i32,
// RGB values from http://unusedino.de/ec64/technical/misc/vic656x/colors/
bgcolor: srgb([ 64, 50, 133u8]), // 6 - blue
fgcolor: srgb([120, 106, 189u8]), // 14 - light blue
};
frame
.draw(
&self.vertex_buffer,
&self.index_buffer,
&self.program,
&uniforms,
&self.params,
)
.unwrap();
}
}