walk around reorienting world torus
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<Vertex>, Vec<u32>) {
|
||||
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<D: gfx::Device,
|
||||
start_time: SystemTime,
|
||||
treadmills: (f32, f32),
|
||||
pads: BTreeMap<u32, (TrackMode, Option<openvr_sys::VRControllerState_t>)>,
|
||||
|
||||
_worldmap: model::World,
|
||||
lat: u8,
|
||||
lng: u8,
|
||||
}
|
||||
|
||||
impl<D: gfx::Device, F: gfx::Factory<D::Resources>> WorldScene<D, F> {
|
||||
@@ -140,14 +145,16 @@ impl<D: gfx::Device, F: gfx::Factory<D::Resources>> WorldScene<D, F> {
|
||||
factory: &mut F,
|
||||
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>,
|
||||
aux_command: &mut <D as gfx::Device>::CommandBuffer) -> WorldScene<D, F> {
|
||||
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<D: gfx::Device, F: gfx::Factory<D::Resources>> WorldScene<D, F> {
|
||||
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<f32>
|
||||
{
|
||||
let x: f32 = TWO_PI_CIRC * x as f32;
|
||||
let y: f32 = TWO_PI_CIRC * y as f32;
|
||||
na::Vector3::<f32>::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<D: gfx::Device,
|
||||
Vr(Untouch { dev_idx, .. }) => {
|
||||
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<D: gfx::Device,
|
||||
},
|
||||
TrackMode::Press => {
|
||||
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<D: gfx::Device,
|
||||
}
|
||||
}
|
||||
|
||||
encoder.update_constant_buffer(&self.locals, &Locals { camera: *(self.camera).as_ref(),
|
||||
millis: millis,
|
||||
encoder.update_constant_buffer(&self.locals, &Locals { millis: millis,
|
||||
treadmill_x: self.treadmills.0,
|
||||
treadmill_y: self.treadmills.1 });
|
||||
}
|
||||
@@ -274,9 +305,14 @@ impl<D: gfx::Device,
|
||||
}
|
||||
|
||||
fn origin(&self) -> na::Matrix4<f32> {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -98,7 +98,7 @@ impl ViewRoot<gfx_device_gl::Device, ColorFormat, DepthFormat> {
|
||||
}
|
||||
} 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();
|
||||
|
||||
Reference in New Issue
Block a user