From: Bernie Innocenti Date: Sat, 8 Jul 2017 16:09:00 +0000 (-0400) Subject: Move text rendering shaders out of line. X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=45391f3ccaa74be2be93e4b31507f45939afa4b0;p=mandelwow.git Move text rendering shaders out of line. --- diff --git a/release.sh b/release.sh new file mode 100755 index 0000000..c2a5883 --- /dev/null +++ b/release.sh @@ -0,0 +1,4 @@ +set -e +cargo build --release --no-default-features +strip target/release/mandelwow +upx -9 target/release/mandelwow diff --git a/shaders/text.frag b/shaders/text.frag new file mode 100644 index 0000000..ab43bfe --- /dev/null +++ b/shaders/text.frag @@ -0,0 +1,11 @@ +#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).x == 0U ? bgcolor : fgcolor; +} diff --git a/shaders/text.vert b/shaders/text.vert new file mode 100644 index 0000000..e3544f7 --- /dev/null +++ b/shaders/text.vert @@ -0,0 +1,20 @@ +#version 140 + +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. + int xpos = index % 16; + int ypos = index / 16; + v_tex_coords = (tex_coords + vec2(xpos, ypos)) / 16.; +} diff --git a/text.rs b/text.rs index 14b2633..d1dc7c6 100644 --- a/text.rs +++ b/text.rs @@ -1,7 +1,7 @@ use cgmath::conv::array4x4; use cgmath::{Matrix4, Vector3}; use glium; -use glium::{Surface, texture}; +use glium::{Display, Program, Surface, texture}; use std; fn gamma(x: T) -> f32 @@ -23,20 +23,28 @@ where #[cfg(feature = "image")] fn c64_font() -> (u32, u32, Vec) { use image; - let image = - image::load_from_memory_with_format(&include_bytes!("c64-font.png")[..], image::PNG) - .unwrap() - .to_luma(); + let image = image::load_from_memory_with_format( + &include_bytes!("textures/c64-font.png")[..], + image::PNG, + ).unwrap() + .to_luma(); let (w, h) = image.dimensions(); (w, h, image.into_raw()) } #[cfg(not(feature = "image"))] fn c64_font() -> (u32, u32, Vec) { - let pixels = &include_bytes!("c64-font.gray")[..]; + let pixels = &include_bytes!("textures/c64-font.gray")[..]; (128, 128, Vec::from(pixels)) } +pub fn text_program(display: &Display) -> Program { + //load_program(display, "shaders/text.vert", "shaders/text.frag"); + let vertex_shader_src = include_str!("shaders/text.vert"); + let fragment_shader_src = include_str!("shaders/text.frag"); + Program::from_source(display, vertex_shader_src, fragment_shader_src, None).unwrap() +} + #[derive(Copy, Clone)] struct Vertex { position: [f32; 2], @@ -54,7 +62,7 @@ pub struct Text<'a> { } impl<'a> Text<'a> { - pub fn new(display: &glium::Display) -> Text { + pub fn new(display: &Display) -> Text { let (w, h, pixels) = c64_font(); let image = glium::texture::RawImage2d { data: std::borrow::Cow::from(pixels), @@ -100,47 +108,6 @@ impl<'a> Text<'a> { &[1 as u16, 2, 0, 3], ).unwrap(); - // compiling shaders and linking them together - let program = program!(display, - 140 => { - vertex: " - #version 140 - - 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. - 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).x == 0U ? bgcolor : fgcolor; - } - " - }).unwrap(); - let params = glium::DrawParameters { depth: glium::Depth { test: glium::draw_parameters::DepthTest::IfLess, @@ -156,7 +123,7 @@ impl<'a> Text<'a> { tex: tex, vertex_buffer: vertex_buffer, index_buffer: index_buffer, - program: program, + program: text_program(display), params: params, } }