diff --git a/textium/src/renderer/gl_renderer_140.frag b/textium/src/renderer/gl_renderer_140.frag index fb00b9d..395f7a0 100644 --- a/textium/src/renderer/gl_renderer_140.frag +++ b/textium/src/renderer/gl_renderer_140.frag @@ -1,14 +1,16 @@ #version 140 in vec2 v_tex_coord; +in vec4 v_color; out vec4 frag_color; uniform sampler2D tex; void main() { - vec4 c = vec4(0, 0, 0, texture(tex, v_tex_coord)); - + frag_color = vec4(texture(tex, v_tex_coord).a, 0, 0, 1); + vec4 c = v_color; + c.a = texture(tex, v_tex_coord).r; if (c.a <= 0.01) { discard; } else { diff --git a/textium/src/renderer/gl_renderer_140.vert b/textium/src/renderer/gl_renderer_140.vert index f97643d..9e1e93b 100644 --- a/textium/src/renderer/gl_renderer_140.vert +++ b/textium/src/renderer/gl_renderer_140.vert @@ -1,12 +1,17 @@ #version 140 in vec2 position; -in vec2 tex_coord; +in vec2 tex_coords; +in vec4 color; out vec2 v_tex_coord; +out vec4 v_color; + +// uniform mat4 matrix; void main() { gl_Position = vec4(position, 0.0, 1.0); - v_tex_coord = tex_coord; + v_tex_coord = tex_coords; + v_color = color; } diff --git a/textium/src/renderer/glium_renderer.rs b/textium/src/renderer/glium_renderer.rs index 6f65e7e..ba4ff31 100644 --- a/textium/src/renderer/glium_renderer.rs +++ b/textium/src/renderer/glium_renderer.rs @@ -1,19 +1,29 @@ +use std::error::Error; + +use ::{GlyphMetadata, FontAtlas}; +use ::geometry::{Rect, Vec2}; +use ::texture::Buffer2d; +use ::cache::{CachedFaceData}; + +use rusttype::PositionedGlyph; use glium::Program; use glium::backend::Facade; + #[derive(Copy, Clone, Debug)] -struct Vertex { - position: [u32; 2], - tex_coords: [u32; 2], +pub struct Vertex { + pub position: [f32; 2], + pub tex_coords: [f32; 2], + pub color: [f32; 4], } -implement_vertex!(Vertex, position, tex_coords); +implement_vertex!(Vertex, position, tex_coords, color); static SHADER_FRAG_140: &'static str = include_str!("gl_renderer_140.frag"); static SHADER_VERT_140: &'static str = include_str!("gl_renderer_140.vert"); pub struct TextRenderer { - gl_program: Program, + pub gl_program: Program, } impl TextRenderer { @@ -29,6 +39,72 @@ impl TextRenderer { gl_program: program, } } +} + + +pub fn buffers_append_glyph(face: &CachedFaceData, vbo_data: &mut Vec, ibo_data: &mut Vec, + screen_rect: Rect, metadata: GlyphMetadata, color: [f32; 4]) + where T: Buffer2d +{ + println!("mbb {:?}", metadata.bounding_box); + let texture_bbox: Rect = metadata.bounding_box.cast(); + let atlas_dimensions: Vec2 = Vec2::from(face.buffer.dimensions()).cast(); + // TODO: ugly as shit, fix + let texture_bbox_gl = Rect { + x: texture_bbox.x / atlas_dimensions.x, + y: texture_bbox.y / atlas_dimensions.y, + w: texture_bbox.w / atlas_dimensions.x, + h: texture_bbox.h / atlas_dimensions.y, + }; + + println!("texture_bbox: {:?}", texture_bbox); + println!("atlas_dimensions: {:?}", atlas_dimensions); + println!("texture_bbox_gl: {:?}", texture_bbox_gl); + + + let vert_offset = vbo_data.len() as u16; + ibo_data.push(vert_offset); + ibo_data.push(vert_offset + 1); + ibo_data.push(vert_offset + 2); + ibo_data.push(vert_offset + 2); + ibo_data.push(vert_offset + 1); + ibo_data.push(vert_offset + 3); + + vbo_data.append(&mut rects_to_verts(&screen_rect, &texture_bbox_gl, color)); +} + +pub fn rects_to_verts(screen_rect: &Rect, texture_rect: &Rect, + color: [f32; 4]) -> Vec +{ + let mut vbo_dat = vec![]; + + // upper-left + vbo_dat.push(Vertex { + position: [screen_rect.left(), screen_rect.top()], + tex_coords: [texture_rect.left(), texture_rect.top()], + color: color + }); + + // upper-right + vbo_dat.push(Vertex { + position: [screen_rect.right(), screen_rect.top()], + tex_coords: [texture_rect.right(), texture_rect.top()], + color: color + }); + + // lower-left + vbo_dat.push(Vertex { + position: [screen_rect.left(), screen_rect.gl_bottom()], + tex_coords: [texture_rect.left(), texture_rect.bottom()], + color: color + }); + + // lower-right + vbo_dat.push(Vertex { + position: [screen_rect.right(), screen_rect.gl_bottom()], + tex_coords: [texture_rect.right(), texture_rect.bottom()], + color: color + }); - // pub fn draw(text: RenderableText) + vbo_dat }