From 2583de70b3c7992798bc834c60ec654fcfdf5475 Mon Sep 17 00:00:00 2001 From: Jared Roberts Date: Wed, 19 Oct 2016 15:14:57 -0700 Subject: [PATCH] walk around reorienting world torus --- src/bin/vrtue.rs | 6 +- src/scenes/shader/tile_frag.glsl | 2 +- src/scenes/shader/torus_vertex.glsl | 12 ++-- src/scenes/world.rs | 90 ++++++++++++++++++++--------- src/view.rs | 2 +- 5 files changed, 76 insertions(+), 36 deletions(-) diff --git a/src/bin/vrtue.rs b/src/bin/vrtue.rs index 6b907f9..233e88f 100644 --- a/src/bin/vrtue.rs +++ b/src/bin/vrtue.rs @@ -5,11 +5,14 @@ use vrtue::scene::{Event, Scene}; extern crate env_logger; extern crate gfx_device_gl; #[macro_use] extern crate log; +extern crate piston; extern crate piston_window; +use self::piston::input::{Button, Input, Key}; use self::piston_window::{PistonWindow, Window, WindowSettings}; use std::env; + pub fn main() { env_logger::init().expect("env logger"); let mut vr = if env::var("NO_VR").is_ok() { @@ -41,7 +44,8 @@ pub fn main() { // handle window events while let Some(ev) = window.poll_event() { match ev { - piston_window::Input::Text(_) => break 'main, + Input::Press(Button::Keyboard(Key::Space)) | + Input::Press(Button::Keyboard(Key::Escape)) => break 'main, _ => debug!("\t{:?}", ev) } diff --git a/src/scenes/shader/tile_frag.glsl b/src/scenes/shader/tile_frag.glsl index 650fa52..66aef4c 100644 --- a/src/scenes/shader/tile_frag.glsl +++ b/src/scenes/shader/tile_frag.glsl @@ -10,9 +10,9 @@ uniform b_constants { uvec4 anim; float R1; float R2; + float R3; }; uniform b_locals { - mat4 camera; uint millis; float treadmill_x; float treadmill_y; diff --git a/src/scenes/shader/torus_vertex.glsl b/src/scenes/shader/torus_vertex.glsl index 2c40e20..b9998f2 100644 --- a/src/scenes/shader/torus_vertex.glsl +++ b/src/scenes/shader/torus_vertex.glsl @@ -15,16 +15,16 @@ uniform b_constants { uvec4 anim; float R1; float R2; + float R3; }; uniform b_locals { - mat4 camera; uint millis; float treadmill_x; float treadmill_y; }; vec3 toroid(vec2 src, float r1, float r2, float r3) { - return vec3(r3 * -1.0 * sin(src.x), // use r3 instead of r2 for "deflated" torus + return vec3(r3 * sin(src.x), // use r3 instead of r2 for "deflated" torus (r1 + r2 * cos(src.x)) * cos(src.y), (r1 + r2 * cos(src.x)) * sin(src.y)); } @@ -35,8 +35,8 @@ void main() { vec2 thetaphi = vec2(TWO_PI_CIRC * (a_pos.x + treadmill_x), TWO_PI_CIRC * (a_pos.y + treadmill_y)); - float height = R1 * TWO_PI_CIRC; - vec3 normal = vec3(toroid(thetaphi, 0, height, height)); - gl_Position = u_matrix * camera * - vec4(toroid(thetaphi, R1, R2, R1) + a_pos.z * normal, 1.0); + float height = R2 * 4 * TWO_PI_CIRC; + vec3 normal = vec3(toroid(thetaphi, 0, height, height)) * + vec3(R2 / R3, 1.0, 1.0); + gl_Position = u_matrix * vec4(toroid(thetaphi, R1, R2, R3) + a_pos.z * normal, 1.0); } diff --git a/src/scenes/world.rs b/src/scenes/world.rs index 44132d3..8d96068 100644 --- a/src/scenes/world.rs +++ b/src/scenes/world.rs @@ -9,6 +9,7 @@ extern crate gfx; extern crate nalgebra as na; extern crate num_traits; extern crate openvr_sys; +extern crate piston; extern crate piston_window; use std::collections::BTreeMap; @@ -19,13 +20,14 @@ use gfx::tex; use gfx::traits::FactoryExt; use self::na::ToHomogeneous; use self::num_traits::identities::One; +use self::piston::input::{Button, Input, Key}; + +const PI: f32 = ::std::f32::consts::PI; +const TWO_PI_CIRC: f32 = 2.0 * PI / 256.0; -//const R1: f32 = 4096.0; -//const R2: f32 = 1024.0; -//const R1: f32 = 4.0; -//const R2: f32 = 1.0; const R1: f32 = 256.0; const R2: f32 = 64.0; +const R3: f32 = 128.0; gfx_defines! { vertex Vertex { @@ -38,10 +40,10 @@ gfx_defines! { anim: [u32; 4] = "anim", r1: f32 = "R1", r2: f32 = "R2", + r3: f32 = "R3", } constant Locals { - camera: [[f32; 4]; 4] = "camera", millis: u32 = "millis", treadmill_x: f32 = "treadmill_x", treadmill_y: f32 = "treadmill_y", @@ -76,28 +78,27 @@ fn get_model(world: &model::World) -> (Vec, Vec) { 10 | 11 | 12 => 1.0, _ => 0.0, }; - let rf = (((r + 90) % 256) as i16 - 128) as f32; - let cf = (((c + 144) % 256) as i16 - 128) as f32; + let (rf, cf) = (r as f32, c as f32); if alt == 0.0 { verticies.extend_from_slice( - &[Vertex { pos: [ cf + 0., -rf - 1., 0. ], uv: [0., 0.], tileidx: tileidx }, - Vertex { pos: [ cf + 1., -rf - 1., 0. ], uv: [1., 0.], tileidx: tileidx }, - Vertex { pos: [ cf + 1., -rf - 0., 0. ], uv: [1., 1.], tileidx: tileidx }, - Vertex { pos: [ cf + 0., -rf - 0., 0. ], uv: [0., 1.], tileidx: tileidx },]); + &[Vertex { pos: [ cf + 0., rf + 1., 0. ], uv: [0., 0.], tileidx: tileidx }, + Vertex { pos: [ cf + 1., rf + 1., 0. ], uv: [1., 0.], tileidx: tileidx }, + Vertex { pos: [ cf + 1., rf + 0., 0. ], uv: [1., 1.], tileidx: tileidx }, + Vertex { pos: [ cf + 0., rf + 0., 0. ], uv: [0., 1.], tileidx: tileidx },]); indicies.extend_from_slice( &[ v + 0, v + 1, v + 2, v + 2, v + 3, v + 0 ]); v += 4; } else { verticies.extend_from_slice( - &[Vertex { pos: [ cf + 0., -rf - 1., 0. ], uv: [0., 0.], tileidx: tileidx }, - Vertex { pos: [ cf + 1., -rf - 1., 0. ], uv: [1., 0.], tileidx: tileidx }, - Vertex { pos: [ cf + 1., -rf - 0., 0. ], uv: [1., 1.], tileidx: tileidx }, - Vertex { pos: [ cf + 0., -rf - 0., 0. ], uv: [0., 1.], tileidx: tileidx }, - Vertex { pos: [ cf + 0., -rf, 0. ], uv: [0., 0.], tileidx: tileidx }, - Vertex { pos: [ cf + 1., -rf, 0. ], uv: [1., 0.], tileidx: tileidx }, - Vertex { pos: [ cf + 1., -rf, alt ], uv: [1., 1.], tileidx: tileidx }, - Vertex { pos: [ cf + 0., -rf, alt ], uv: [0., 1.], tileidx: tileidx },]); + &[Vertex { pos: [ cf + 0., rf + 1., 0. ], uv: [0., 0.], tileidx: tileidx }, + Vertex { pos: [ cf + 1., rf + 1., 0. ], uv: [1., 0.], tileidx: tileidx }, + Vertex { pos: [ cf + 1., rf + 0., 0. ], uv: [1., 1.], tileidx: tileidx }, + Vertex { pos: [ cf + 0., rf + 0., 0. ], uv: [0., 1.], tileidx: tileidx }, + Vertex { pos: [ cf + 0., rf, 0. ], uv: [0., 0.], tileidx: tileidx }, + Vertex { pos: [ cf + 1., rf, 0. ], uv: [1., 0.], tileidx: tileidx }, + Vertex { pos: [ cf + 1., rf, alt ], uv: [1., 1.], tileidx: tileidx }, + Vertex { pos: [ cf + 0., rf, alt ], uv: [0., 1.], tileidx: tileidx },]); indicies.extend_from_slice( &[ v + 0, v + 1, v + 2, v + 2, v + 3, v + 0, @@ -133,6 +134,10 @@ pub struct WorldScene)>, + + _worldmap: model::World, + lat: u8, + lng: u8, } impl> WorldScene { @@ -140,14 +145,16 @@ impl> WorldScene { factory: &mut F, encoder: &mut gfx::Encoder, aux_command: &mut ::CommandBuffer) -> WorldScene { - let (model, model_idx) = get_model(&get_data_model()); + let worldmap = get_data_model(); + let (model, model_idx) = get_model(&worldmap); let (vertex_buffer, slice) = factory.create_vertex_buffer_with_slice(&model, &model_idx[..]); let constants = factory.create_constant_buffer(1); encoder.update_constant_buffer(&constants, &Constants { anim: ANIMDATA, r1: R1, - r2: R2 }); + r2: R2, + r3: R3}); WorldScene { pso: factory.create_pipeline_simple(VERTEX_SHADER_SRC, @@ -167,8 +174,21 @@ impl> WorldScene { start_time: SystemTime::now(), treadmills: (0.0, 0.0), pads: BTreeMap::new(), + + _worldmap: worldmap, + lat: 144, + lng: 90, } } + + fn toroid((x, y): (f32, f32), r1: f32, r2: f32, r3: f32) -> na::Vector3 + { + let x: f32 = TWO_PI_CIRC * x as f32; + let y: f32 = TWO_PI_CIRC * y as f32; + na::Vector3::::new(r3 * x.sin(), // use r3 instead of r2 for "deflated" torus + (r1 + r2 * x.cos()) * y.cos(), + (r1 + r2 * x.cos()) * y.sin()) + } } const ANIMDATA: [u32; 4] = @@ -196,6 +216,18 @@ impl { self.pads.remove(&dev_idx); }, + Piston(Input::Press(Button::Keyboard(Key::Up))) => { + self.lat = self.lat.wrapping_sub(1); + }, + Piston(Input::Press(Button::Keyboard(Key::Down))) => { + self.lat = self.lat.wrapping_add(1); + }, + Piston(Input::Press(Button::Keyboard(Key::Left))) => { + self.lng = self.lng.wrapping_sub(1); + }, + Piston(Input::Press(Button::Keyboard(Key::Right))) => { + self.lng = self.lng.wrapping_add(1); + }, _ => () } } @@ -223,7 +255,7 @@ impl { let rot = na::Vector3::new(0.0, 0.0, 0.0); - let speed = R2 * 0.01; + let speed = R2 * 0.005; if state.rAxis[0].x > 0.5 { self.camera = na::Similarity3::new(na::Vector3::new(-speed, 0.0, 0.0), rot, 1.0).to_homogeneous() * self.camera; @@ -248,8 +280,7 @@ impl na::Matrix4 { - na::Similarity3::new(na::Vector3::new(0.0, R1 - R2, 0.0), - na::Vector3::new(0.0, 0.0, 0.0), 1.0).to_homogeneous() - + let (y, x) = (self.lat as f32 + 0.5, self.lng as f32 + 0.5); + let eye = Self::toroid((x, y), R1, R2, R3); + let looktgt = Self::toroid((x, y - 1.0), R1, R2, R3); + let normal = Self::toroid((x, y), 0.0, R2, R2) * na::Vector3::new(R2 / R3, 1.0, 1.0); + self.camera * na::Isometry3::look_at_rh(eye.as_point(), + looktgt.as_point(), + &normal, + ).to_homogeneous() } } diff --git a/src/view.rs b/src/view.rs index 7d52523..a16bda4 100644 --- a/src/view.rs +++ b/src/view.rs @@ -98,7 +98,7 @@ impl ViewRoot { } } else { // If running without VR, just draw from some default projection near the scene origin - let head_mat = na::Similarity3::new(na::Vector3::new(0.0, -1.0, 0.0), + let head_mat = na::Similarity3::new(na::Vector3::new(0.0, -1.5, 0.0), na::Vector3::new(0.0, 0.0, 0.0), 1.0).to_homogeneous(); let proj_mat = na::PerspectiveMatrix3::new(1.0, 90.0, NEAR, FAR).to_matrix();