1 use cgmath::conv::array4x4;
2 use cgmath::{Matrix4, Vector3};
4 use glium::{Surface, texture};
8 fn gamma<T>(x: T) -> f32
13 ((f32::from(x)) / 255.).powf(2.2)
16 fn srgb<T>(c: [T; 3]) -> [f32; 4]
21 [gamma(c[0]), gamma(c[1]), gamma(c[2]), 0.0]
24 #[derive(Copy, Clone)]
29 implement_vertex!(Vertex, position, tex_coords);
32 tex: texture::Texture2d,
33 vertex_buffer: glium::VertexBuffer<Vertex>,
34 index_buffer: glium::IndexBuffer<u16>,
35 program: glium::Program,
36 params: glium::DrawParameters<'a>,
41 pub fn new(display: &glium::Display) -> Text {
43 image::load_from_memory_with_format(&include_bytes!("c64-font.png")[..], image::PNG)
46 let (w, h) = image.dimensions();
47 let image = glium::texture::RawImage2d {
48 data: std::borrow::Cow::from(image.into_raw()),
51 format: glium::texture::ClientFormat::U8,
53 let tex = glium::texture::Texture2d::with_format(
56 glium::texture::UncompressedFloatFormat::U8,
57 glium::texture::MipmapsOption::NoMipmap,
60 // building the vertex buffer, which contains all the vertices that we will draw
62 glium::VertexBuffer::new(
66 position: [-1.0, -1.0],
67 tex_coords: [0.0, 1.0],
70 position: [-1.0, 1.0],
71 tex_coords: [0.0, 0.0],
75 tex_coords: [1.0, 0.0],
78 position: [1.0, -1.0],
79 tex_coords: [1.0, 1.0],
85 let index_buffer = glium::IndexBuffer::new(
87 glium::index::PrimitiveType::TriangleStrip,
91 // compiling shaders and linking them together
92 let program = program!(display,
98 uniform mat4 perspview;
104 out vec2 v_tex_coords;
108 gl_Position = perspview * model * vec4(position, 0.0, 1.0);
110 // Characters are arranged in a 16x16 square.
111 int xpos = index % 16;
112 int ypos = index / 16;
113 v_tex_coords = (tex_coords + vec2(xpos, ypos)) / 16.;
119 uniform sampler2D tex;
120 uniform vec4 bgcolor;
121 uniform vec4 fgcolor;
123 in vec2 v_tex_coords;
127 f_color = texture(tex, v_tex_coords).x == 0U ? bgcolor : fgcolor;
132 let params = glium::DrawParameters {
133 depth: glium::Depth {
134 test: glium::draw_parameters::DepthTest::IfLess,
143 model: Matrix4::from_translation(Vector3::unit_z() * (-1.0)),
145 vertex_buffer: vertex_buffer,
146 index_buffer: index_buffer,
152 pub fn draw(&self, frame: &mut glium::Frame, perspview: &[[f32; 4]; 4]) {
155 model: array4x4(self.model),
156 perspview: *perspview,
157 tex: self.tex.sampled()
158 .magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest),
160 // RGB values from http://unusedino.de/ec64/technical/misc/vic656x/colors/
161 bgcolor: srgb([ 64, 50, 133u8]), // 6 - blue
162 fgcolor: srgb([120, 106, 189u8]), // 14 - light blue