Put text on the top faces of cubes.
authorBernie Innocenti <bernie@codewiz.org>
Sat, 8 Jul 2017 18:04:21 +0000 (14:04 -0400)
committerBernie Innocenti <bernie@codewiz.org>
Sat, 8 Jul 2017 18:04:21 +0000 (14:04 -0400)
main.rs
mandelwow.rs
text.rs

diff --git a/main.rs b/main.rs
index ef61ea816e0bb89f8468ae6a0078f3178c49db5d..bd75e428df4b225cef9576fe13f51043f2ab7a59 100644 (file)
--- 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() {
index 41f7e5d8cabf569f25e401253ae96f1b87e5e50d..bbb36712b6e08c2295ee24e820f8053b7d919cdc 100644 (file)
@@ -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<U>(display: &glium::Display,
+fn mandel<U>(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<U>(display: &glium::Display,
     frame.draw(&vb, &indices, program, uniforms, &params).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 d1dc7c65f17bb47938474065cfd97b1d7e226bc6..4e1fcdfc10579f3ed5813005795bc11bb3a7bfc4 100644 (file)
--- a/text.rs
+++ b/text.rs
@@ -58,11 +58,12 @@ pub struct Text<'a> {
     index_buffer: glium::IndexBuffer<u16>,
     program: glium::Program,
     params: glium::DrawParameters<'a>,
-    model: Matrix4<f32>,
+    pub model: Matrix4<f32>,
+    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