From 1c4edfbcd1c5a17a8b802707479332f730f24eb3 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 15 Mar 2020 18:44:05 +0000 Subject: [PATCH] Text rendering works, though it's still ugly --- src/bin/pumpkin.rs | 76 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 22 deletions(-) diff --git a/src/bin/pumpkin.rs b/src/bin/pumpkin.rs index 2b38c2a..b481d1a 100644 --- a/src/bin/pumpkin.rs +++ b/src/bin/pumpkin.rs @@ -561,7 +561,7 @@ impl GameGraphics { let texture_sky = Texture::from_file ("sky.png"); let texture_grass = Texture::from_file ("grass.png"); - let texture_font = Texture::from_file ("checkerboard.png"); + let texture_font = Texture::from_file ("font.png"); let (pitch_colors, grass_index) = { let silver = (255.0, 255.0, 255.0); @@ -818,7 +818,7 @@ impl GameGraphics { ]; let v: Vec = (0u32..1024).map (|i| { - quad.iter ().map (move |j| 6 * i + j) + quad.iter ().map (move |j| 4 * i + j) }).flatten ().collect (); gpu_buffers::IndexBuffer::from_slice_u32 (&v) } @@ -1119,11 +1119,46 @@ impl GameGraphics { Mat4::from_scale ((2.0 / screen_size.0, 2.0 / screen_size.1, 1.0).into ()); glezz::uniform_matrix_4fv (unis [&MVP], &mvp); + let stride_floats = 3 + 2; + let stride_bytes = stride_floats * 4; + + let mut vert_buffer = Vec::with_capacity (stride_floats * 1024); + + // X and Y are in screen pixels, c is ASCII + let mut add_char = |x: f32, y: f32, c: char| { + let c = c as u32 - 1; + + assert! (c >= 0); + assert! (c < 128); + + let u = (c % 32) as f32; + let v = (c / 32) as f32; + + for vert in &[ + (0.0, 0.0), + (1.0, 0.0), + (1.0, 1.0), + (0.0, 1.0), + ] { + vert_buffer.push (x + vert.0 * 8.0); + vert_buffer.push (y + vert.1 * 18.0); + + vert_buffer.push ((u + vert.0) * 8.0 / 256.0); + vert_buffer.push ((v + 1.0 - vert.1) * 18.0 / 256.0); + } + }; + + let s = "Pumpkin"; + + for (i, c) in s.chars ().enumerate () { + add_char (30.0 + i as f32 * 8.0, 30.0, c); + } + let pos: Vec = vec! [ - 0.0, 0.0, 0.0, - 256.0, 0.0, 0.0, - 256.0, 256.0, 0.0, - 0.0, 256.0, 0.0, + 0.0, 0.0, + 256.0, 0.0, + 256.0, 256.0, + 0.0, 256.0, ]; use std::convert::TryInto; @@ -1144,24 +1179,21 @@ impl GameGraphics { use renderable_model::attributes::*; use std::ffi::c_void; - gl::BufferSubData ( - gl::ARRAY_BUFFER, - 0.try_into ().unwrap (), - (4 * 4 * 3).try_into ().unwrap (), - &pos [0] as *const f32 as *const c_void - ); + if true { + gl::BufferSubData ( + gl::ARRAY_BUFFER, + 0.try_into ().unwrap (), + (s.len () * 4 * 4 * 4).try_into ().unwrap (), + &vert_buffer [0] as *const f32 as *const c_void + ); + + gl::VertexAttribPointer (attrs [POS].unwrap (), 2, gl::FLOAT, 0u8, 4 * 4, 0 as *const f32 as *const c_void); + gl::VertexAttribPointer (attrs [UV].unwrap (), 2, gl::FLOAT, 0u8, 4 * 4, (4 * 2) as *const f32 as *const c_void); + } - gl::BufferSubData ( - gl::ARRAY_BUFFER, - (4 * 4 * 3).try_into ().unwrap (), - (4 * 4 * 2).try_into ().unwrap (), - &uv [0] as *const f32 as *const c_void - ); + let num_indices = s.len () as u32 * 6; - gl::VertexAttribPointer (attrs [POS].unwrap (), 3, gl::FLOAT, 0u8, 4 * 3, 0 as *const f32 as *const c_void); - gl::VertexAttribPointer (attrs [UV].unwrap (), 2, gl::FLOAT, 0u8, 4 * 2, (4 * 4 * 3) as *const f32 as *const c_void); - - gl::DrawRangeElements (gl::TRIANGLES, 0, 6, 6, gl::UNSIGNED_INT, 0 as *const u32 as *const c_void); + gl::DrawRangeElements (gl::TRIANGLES, 0, num_indices, num_indices as i32, gl::UNSIGNED_INT, 0 as *const u32 as *const c_void); } }); }