X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=text.rs;h=abcd9b40f17b3235fcf230883f1e57f8c686b28e;hb=e909401ee19ae946d8888dc3aa75d269f408fb92;hp=c483846c0afe2ed05135234136d4c384f12228c8;hpb=8c4dafcdc1b1edc686f02df6e9a53b10e1ca1bf0;p=mandelwow.git diff --git a/text.rs b/text.rs index c483846..abcd9b4 100644 --- a/text.rs +++ b/text.rs @@ -1,9 +1,25 @@ use cgmath::conv::array4x4; -use cgmath::{Matrix4, One}; +use cgmath::{Matrix4, Vector3}; use glium; use glium::{Surface, texture}; use image; -use std::io::Cursor; +use std; + +fn gamma(x: T) -> f32 +where + f32: From, + T: Copy, +{ + ((f32::from(x)) / 255.).powf(2.2) +} + +fn srgb(c: [T; 3]) -> [f32; 4] +where + f32: From, + T: Copy, +{ + [gamma(c[0]), gamma(c[1]), gamma(c[2]), 0.0] +} #[derive(Copy, Clone)] struct Vertex { @@ -13,7 +29,7 @@ struct Vertex { implement_vertex!(Vertex, position, tex_coords); pub struct Text<'a> { - tex: texture::CompressedSrgbTexture2d, + tex: texture::Texture2d, vertex_buffer: glium::VertexBuffer, index_buffer: glium::IndexBuffer, program: glium::Program, @@ -23,13 +39,23 @@ pub struct Text<'a> { impl<'a> Text<'a> { pub fn new(display: &glium::Display) -> Text { - let image = image::load(Cursor::new(&include_bytes!("c64-font.png")[..]), image::PNG) - .unwrap() - .to_rgba(); - let dimensions = image.dimensions(); let image = - glium::texture::RawImage2d::from_raw_rgba_reversed(image.into_raw(), dimensions); - let tex = glium::texture::CompressedSrgbTexture2d::new(display, image).unwrap(); + image::load_from_memory_with_format(&include_bytes!("c64-font.png")[..], image::PNG) + .unwrap() + .to_luma(); + let (w, h) = image.dimensions(); + let image = glium::texture::RawImage2d { + data: std::borrow::Cow::from(image.into_raw()), + 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 = { @@ -38,19 +64,19 @@ impl<'a> Text<'a> { &[ Vertex { position: [-1.0, -1.0], - tex_coords: [0.0, 0.0], + tex_coords: [0.0, 1.0], }, Vertex { position: [-1.0, 1.0], - tex_coords: [0.0, 1.0], + tex_coords: [0.0, 0.0], }, Vertex { position: [1.0, 1.0], - tex_coords: [1.0, 1.0], + tex_coords: [1.0, 0.0], }, Vertex { position: [1.0, -1.0], - tex_coords: [1.0, 0.0], + tex_coords: [1.0, 1.0], }, ], ).unwrap() @@ -70,28 +96,35 @@ impl<'a> Text<'a> { uniform mat4 model; uniform mat4 perspview; + uniform int index; in vec2 position; in vec2 tex_coords; out vec2 v_tex_coords; + out ivec4 v_fgcolor; void main() { gl_Position = perspview * model * vec4(position, 0.0, 1.0); + // Characters are arranged in a 16x16 square. - // Texture oordinates originate in the bottom-left corner. - v_tex_coords = (tex_coords) / 16.0 + vec2(0. / 16., 15. / 16.); + int xpos = index % 16; + int ypos = index / 16; + v_tex_coords = (tex_coords + vec2(xpos, ypos)) / 16.; } ", fragment: " #version 140 uniform sampler2D tex; + uniform vec4 bgcolor; + uniform vec4 fgcolor; + in vec2 v_tex_coords; out vec4 f_color; void main() { - f_color = texture(tex, v_tex_coords); + f_color = texture(tex, v_tex_coords).x == 0U ? bgcolor : fgcolor; } " }).unwrap(); @@ -107,7 +140,7 @@ impl<'a> Text<'a> { }; Text { - model: Matrix4::one(), + model: Matrix4::from_translation(Vector3::unit_z() * (-1.0)), tex: tex, vertex_buffer: vertex_buffer, index_buffer: index_buffer, @@ -117,12 +150,16 @@ impl<'a> Text<'a> { } pub fn draw(&self, frame: &mut glium::Frame, perspview: &[[f32; 4]; 4]) { - let uniforms = uniform! { + let uniforms = + uniform! { model: array4x4(self.model), perspview: *perspview, tex: self.tex.sampled() - //.minify_filter(glium::uniforms::MinifySamplerFilter::Nearest) .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(