1 use cgmath::conv::array4x4;
2 use cgmath::{Matrix4, Vector3};
4 use glium::{Display, Program, 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>) {
26 let image = image::load_from_memory_with_format(
27 &include_bytes!("textures/c64-font.png")[..],
31 let (w, h) = image.dimensions();
32 (w, h, image.into_raw())
35 #[cfg(not(feature = "image"))]
36 fn c64_font() -> (u32, u32, Vec<u8>) {
37 let pixels = &include_bytes!("textures/c64-font.gray")[..];
38 (128, 128, Vec::from(pixels))
41 pub fn text_program(display: &Display) -> Program {
42 //load_program(display, "shaders/text.vert", "shaders/text.frag");
43 let vertex_shader_src = include_str!("shaders/text.vert");
44 let fragment_shader_src = include_str!("shaders/text.frag");
45 Program::from_source(display, vertex_shader_src, fragment_shader_src, None).unwrap()
48 #[derive(Copy, Clone)]
53 implement_vertex!(Vertex, position, tex_coords);
56 tex: texture::Texture2d,
57 vertex_buffer: glium::VertexBuffer<Vertex>,
58 index_buffer: glium::IndexBuffer<u16>,
59 program: glium::Program,
60 params: glium::DrawParameters<'a>,
65 pub fn new(display: &Display) -> Text {
66 let (w, h, pixels) = c64_font();
67 let image = glium::texture::RawImage2d {
68 data: std::borrow::Cow::from(pixels),
71 format: glium::texture::ClientFormat::U8,
73 let tex = glium::texture::Texture2d::with_format(
76 glium::texture::UncompressedFloatFormat::U8,
77 glium::texture::MipmapsOption::NoMipmap,
80 // building the vertex buffer, which contains all the vertices that we will draw
82 glium::VertexBuffer::new(
86 position: [-1.0, -1.0],
87 tex_coords: [0.0, 1.0],
90 position: [-1.0, 1.0],
91 tex_coords: [0.0, 0.0],
95 tex_coords: [1.0, 0.0],
98 position: [1.0, -1.0],
99 tex_coords: [1.0, 1.0],
105 let index_buffer = glium::IndexBuffer::new(
107 glium::index::PrimitiveType::TriangleStrip,
108 &[1 as u16, 2, 0, 3],
111 let params = glium::DrawParameters {
112 depth: glium::Depth {
113 test: glium::draw_parameters::DepthTest::IfLess,
122 model: Matrix4::from_translation(Vector3::unit_z() * (-1.0)),
124 vertex_buffer: vertex_buffer,
125 index_buffer: index_buffer,
126 program: text_program(display),
131 pub fn draw(&self, frame: &mut glium::Frame, perspview: &[[f32; 4]; 4]) {
134 model: array4x4(self.model),
135 perspview: *perspview,
136 tex: self.tex.sampled()
137 .magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest),
139 // RGB values from http://unusedino.de/ec64/technical/misc/vic656x/colors/
140 bgcolor: srgb([ 64, 50, 133u8]), // 6 - blue
141 fgcolor: srgb([120, 106, 189u8]), // 14 - light blue