From e758b0d67cbb0fdb0bc316b9b4037511e492c0d5 Mon Sep 17 00:00:00 2001 From: Bernie Innocenti Date: Sat, 8 Jul 2017 14:04:21 -0400 Subject: [PATCH] Put text on the top faces of cubes. --- main.rs | 23 ++++++++++++++--------- mandelwow.rs | 13 ++++++------- text.rs | 18 ++++++++++-------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/main.rs b/main.rs index ef61ea8..bd75e42 100644 --- a/main.rs +++ b/main.rs @@ -79,7 +79,7 @@ fn main() { gl_info(&display); - let text = text::Text::new(&display); + let mut text = text::Text::new(&display, 'A'); let mandelwow_program = mandelwow::program(&display); let bounding_box_program = bounding_box::solid_fill_program(&display); let shaded_program = shaded_cube::shaded_program(&display); @@ -101,15 +101,16 @@ fn main() { let mandelwow_bbox = bounding_box::BoundingBox::new(&display, &bounds, &bounding_box_program); let shaded_cube = ShadedCube::new(&display, &shaded_program); - const SEA_XSIZE: usize = 24; - const SEA_ZSIZE: usize = 20; - let sea_xmin = -14.0f32; - let sea_xmax = 14.0f32; + const SEA_XSIZE: usize = 40; + const SEA_ZSIZE: usize = 25; + let sea_xmin = -20.0f32; + let sea_xmax = 20.0f32; let sea_y = -2.5; let sea_zmin = -2.0f32; - let sea_zmax = -26.0f32; + let sea_zmax = -27.0f32; let sea_xstep = (sea_xmax - sea_xmin) / (SEA_XSIZE as f32); let sea_zstep = (sea_zmax - sea_zmin) / (SEA_ZSIZE as f32); + println!("xstep={} ystep={:?}", sea_xstep, sea_zstep); let mut sea = [[Vector3::zero(); SEA_ZSIZE]; SEA_XSIZE]; for x in 0..SEA_XSIZE { @@ -168,24 +169,28 @@ fn main() { mandelwow_bbox.draw(&mut frame, &uniforms); } + let text_rot = Matrix4::from_angle_x(cgmath::Deg(-90.0f32)); + let text_pos = Matrix4::from_translation(Vector3 { x: 0.0, y: 0.501, z: 0.0f32}) * text_rot; for x in 0..SEA_XSIZE { for z in 0..SEA_ZSIZE { let wave = ((x as f32 / SEA_XSIZE as f32 * PI * 5.0 + t * 2.0).sin() + (z as f32 / SEA_ZSIZE as f32 * PI * 3.0 + t * 3.0).sin()) * 0.3; - let model = Matrix4::from_translation(sea[x][z] + Vector3 {x: 0., y: wave, z: 0.}); + let model = Matrix4::from_translation( + sea[x][z] + Vector3 {x: 0., y: wave, z: 0.}); let uniforms = uniform! { model: array4x4(model), perspview: perspview, col: [0., (1. - wave).abs() * 0.5, wave.abs()], }; shaded_cube.draw(&mut frame, &uniforms); + text.model = model * text_pos; + text.character = (x + z * SEA_XSIZE) as u8 as char; + text.draw(&mut frame, &perspview); } } mandelwow::draw(&display, &mut frame, &mandelwow_program, model, &camera, &bounds, wow); - text.draw(&mut frame, &perspview); - frame.finish().unwrap(); for ev in display.poll_events() { diff --git a/mandelwow.rs b/mandelwow.rs index 41f7e5d..bbb3671 100644 --- a/mandelwow.rs +++ b/mandelwow.rs @@ -3,10 +3,9 @@ use cube::Cube; use glium; use glium::index::PrimitiveType; -use glium::Surface; +use glium::{Display, Program, Surface}; use support; - /* fn mand(cx: f32, cy: f32) -> [f32; 3] { let maxiter = 64; @@ -28,7 +27,7 @@ fn mand(cx: f32, cy: f32) -> [f32; 3] { } */ -pub fn program(display: &glium::Display) -> glium::Program { +pub fn program(display: &Display) -> Program { Program::from_source( display, include_str!("shaders/mandelwow.vert"), @@ -36,9 +35,9 @@ pub fn program(display: &glium::Display) -> glium::Program { .unwrap() } -fn mandel(display: &glium::Display, +fn mandel(display: &Display, frame: &mut glium::Frame, - program: &glium::Program, + program: &Program, uniforms: &U, bounds: &Cube, z: [f32; 2]) where U: glium::uniforms::Uniforms { @@ -104,9 +103,9 @@ fn mandel(display: &glium::Display, frame.draw(&vb, &indices, program, uniforms, ¶ms).unwrap(); } -pub fn draw(display: &glium::Display, +pub fn draw(display: &Display, mut frame: &mut glium::Frame, - program: &glium::Program, + program: &Program, model: [[f32; 4]; 4], camera: &support::camera::CameraState, bounds: &Cube, diff --git a/text.rs b/text.rs index d1dc7c6..4e1fcdf 100644 --- a/text.rs +++ b/text.rs @@ -58,11 +58,12 @@ pub struct Text<'a> { index_buffer: glium::IndexBuffer, program: glium::Program, params: glium::DrawParameters<'a>, - model: Matrix4, + pub model: Matrix4, + pub character: char, } impl<'a> Text<'a> { - pub fn new(display: &Display) -> Text { + pub fn new(display: &Display, character: char) -> Text { let (w, h, pixels) = c64_font(); let image = glium::texture::RawImage2d { data: std::borrow::Cow::from(pixels), @@ -83,19 +84,19 @@ impl<'a> Text<'a> { display, &[ Vertex { - position: [-1.0, -1.0], + position: [-0.5, -0.5], tex_coords: [0.0, 1.0], }, Vertex { - position: [-1.0, 1.0], + position: [-0.5, 0.5], tex_coords: [0.0, 0.0], }, Vertex { - position: [1.0, 1.0], + position: [0.5, 0.5], tex_coords: [1.0, 0.0], }, Vertex { - position: [1.0, -1.0], + position: [0.5, -0.5], tex_coords: [1.0, 1.0], }, ], @@ -119,12 +120,13 @@ impl<'a> Text<'a> { }; Text { - model: Matrix4::from_translation(Vector3::unit_z() * (-1.0)), tex: tex, vertex_buffer: vertex_buffer, index_buffer: index_buffer, program: text_program(display), params: params, + model: Matrix4::from_translation(Vector3::unit_z() * (-1.0)), + character: character, } } @@ -135,7 +137,7 @@ impl<'a> Text<'a> { perspview: *perspview, tex: self.tex.sampled() .magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest), - index: 'C' as i32, + index: self.character 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 -- 2.25.1