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>,
61 pub model: Matrix4<f32>,
66 pub fn new(display: &Display, character: char) -> Text {
67 let (w, h, pixels) = c64_font();
68 let image = glium::texture::RawImage2d {
69 data: std::borrow::Cow::from(pixels),
72 format: glium::texture::ClientFormat::U8,
74 let tex = glium::texture::Texture2d::with_format(
77 glium::texture::UncompressedFloatFormat::U8,
78 glium::texture::MipmapsOption::NoMipmap,
81 // building the vertex buffer, which contains all the vertices that we will draw
83 glium::VertexBuffer::new(
87 position: [-0.5, -0.5],
88 tex_coords: [0.0, 1.0],
91 position: [-0.5, 0.5],
92 tex_coords: [0.0, 0.0],
96 tex_coords: [1.0, 0.0],
99 position: [0.5, -0.5],
100 tex_coords: [1.0, 1.0],
106 let index_buffer = glium::IndexBuffer::new(
108 glium::index::PrimitiveType::TriangleStrip,
109 &[1 as u16, 2, 0, 3],
112 let params = glium::DrawParameters {
113 depth: glium::Depth {
114 test: glium::draw_parameters::DepthTest::IfLess,
124 vertex_buffer: vertex_buffer,
125 index_buffer: index_buffer,
126 program: text_program(display),
128 model: Matrix4::from_translation(Vector3::unit_z() * (-1.0)),
129 character: character,
133 pub fn draw(&self, frame: &mut glium::Frame, perspview: &[[f32; 4]; 4]) {
136 model: array4x4(self.model),
137 perspview: *perspview,
138 tex: self.tex.sampled()
139 .magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest),
140 index: self.character as i32,
141 // RGB values from http://unusedino.de/ec64/technical/misc/vic656x/colors/
142 bgcolor: srgb([ 64, 50, 133u8]), // 6 - blue
143 fgcolor: srgb([120, 106, 189u8]), // 14 - light blue