1 use cgmath::conv::array4x4;
2 use cgmath::{Matrix4, Vector3};
4 use glium::{Surface, texture};
7 fn gamma<T>(x: T) -> f32
12 ((f32::from(x)) / 255.).powf(2.2)
15 fn srgb<T>(c: [T; 3]) -> [f32; 4]
20 [gamma(c[0]), gamma(c[1]), gamma(c[2]), 0.0]
23 #[cfg(feature = "image")]
24 fn c64_font() -> (u32, u32, Vec<u8>) {
27 image::load_from_memory_with_format(&include_bytes!("c64-font.png")[..], image::PNG)
30 let (w, h) = image.dimensions();
31 (w, h, image.into_raw())
34 #[cfg(not(feature = "image"))]
35 fn c64_font() -> (u32, u32, Vec<u8>) {
36 let pixels = &include_bytes!("c64-font.gray")[..];
37 (128, 128, Vec::from(pixels))
40 #[derive(Copy, Clone)]
45 implement_vertex!(Vertex, position, tex_coords);
48 tex: texture::Texture2d,
49 vertex_buffer: glium::VertexBuffer<Vertex>,
50 index_buffer: glium::IndexBuffer<u16>,
51 program: glium::Program,
52 params: glium::DrawParameters<'a>,
57 pub fn new(display: &glium::Display) -> Text {
58 let (w, h, pixels) = c64_font();
59 let image = glium::texture::RawImage2d {
60 data: std::borrow::Cow::from(pixels),
63 format: glium::texture::ClientFormat::U8,
65 let tex = glium::texture::Texture2d::with_format(
68 glium::texture::UncompressedFloatFormat::U8,
69 glium::texture::MipmapsOption::NoMipmap,
72 // building the vertex buffer, which contains all the vertices that we will draw
74 glium::VertexBuffer::new(
78 position: [-1.0, -1.0],
79 tex_coords: [0.0, 1.0],
82 position: [-1.0, 1.0],
83 tex_coords: [0.0, 0.0],
87 tex_coords: [1.0, 0.0],
90 position: [1.0, -1.0],
91 tex_coords: [1.0, 1.0],
97 let index_buffer = glium::IndexBuffer::new(
99 glium::index::PrimitiveType::TriangleStrip,
100 &[1 as u16, 2, 0, 3],
103 // compiling shaders and linking them together
104 let program = program!(display,
110 uniform mat4 perspview;
116 out vec2 v_tex_coords;
120 gl_Position = perspview * model * vec4(position, 0.0, 1.0);
122 // Characters are arranged in a 16x16 square.
123 int xpos = index % 16;
124 int ypos = index / 16;
125 v_tex_coords = (tex_coords + vec2(xpos, ypos)) / 16.;
131 uniform sampler2D tex;
132 uniform vec4 bgcolor;
133 uniform vec4 fgcolor;
135 in vec2 v_tex_coords;
139 f_color = texture(tex, v_tex_coords).x == 0U ? bgcolor : fgcolor;
144 let params = glium::DrawParameters {
145 depth: glium::Depth {
146 test: glium::draw_parameters::DepthTest::IfLess,
155 model: Matrix4::from_translation(Vector3::unit_z() * (-1.0)),
157 vertex_buffer: vertex_buffer,
158 index_buffer: index_buffer,
164 pub fn draw(&self, frame: &mut glium::Frame, perspview: &[[f32; 4]; 4]) {
167 model: array4x4(self.model),
168 perspview: *perspview,
169 tex: self.tex.sampled()
170 .magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest),
172 // RGB values from http://unusedino.de/ec64/technical/misc/vic656x/colors/
173 bgcolor: srgb([ 64, 50, 133u8]), // 6 - blue
174 fgcolor: srgb([120, 106, 189u8]), // 14 - light blue