properly parameterize world torus radii

This commit is contained in:
2016-10-01 00:29:00 -07:00
parent 8faa60b921
commit d69223ef13
4 changed files with 58 additions and 30 deletions

View File

@@ -19,7 +19,7 @@ pub fn main() {
.vsync(false) .vsync(false)
.build().expect("Building Window"); .build().expect("Building Window");
let mut scene = scenes::world::WorldScene::new(&mut window.factory); let mut scene = scenes::world::WorldScene::new(&mut window.factory, &mut window.encoder);
let view = view::ViewRoot::<gfx_device_gl::Device, view::ColorFormat, view::DepthFormat> let view = view::ViewRoot::<gfx_device_gl::Device, view::ColorFormat, view::DepthFormat>
::create_view(&mut window, &mut vr); ::create_view(&mut window, &mut vr);

View File

@@ -3,6 +3,7 @@ use vr;
extern crate gfx; extern crate gfx;
extern crate gfx_device_gl; extern crate gfx_device_gl;
extern crate nalgebra as na;
extern crate piston_window; extern crate piston_window;
pub trait Scene<D: gfx::Device, pub trait Scene<D: gfx::Device,
@@ -17,6 +18,8 @@ pub trait Scene<D: gfx::Device,
trans: &gfx::handle::Buffer<D::Resources, view::Trans>, trans: &gfx::handle::Buffer<D::Resources, view::Trans>,
target: &gfx::handle::RenderTargetView<D::Resources, view::ColorFormat>, target: &gfx::handle::RenderTargetView<D::Resources, view::ColorFormat>,
depth: &gfx::handle::DepthStencilView<D::Resources, view::DepthFormat>); depth: &gfx::handle::DepthStencilView<D::Resources, view::DepthFormat>);
fn origin(&self) -> na::Matrix4<f32>;
} }
pub enum Event { pub enum Event {

View File

@@ -17,8 +17,12 @@ use std::time::SystemTime;
use gfx::tex; use gfx::tex;
use gfx::traits::FactoryExt; use gfx::traits::FactoryExt;
use self::na::ToHomogeneous;
use self::num_traits::identities::One; use self::num_traits::identities::One;
const R1: f32 = 4096.0;
const R2: f32 = 1024.0;
gfx_defines! { gfx_defines! {
vertex Vertex { vertex Vertex {
pos: [f32; 3] = "a_pos", pos: [f32; 3] = "a_pos",
@@ -26,10 +30,15 @@ gfx_defines! {
tileidx: u32 = "a_tileidx", tileidx: u32 = "a_tileidx",
} }
constant Constants {
anim: u32 = "anim",
r1: f32 = "R1",
r2: f32 = "R2",
}
constant Locals { constant Locals {
camera: [[f32; 4]; 4] = "camera", camera: [[f32; 4]; 4] = "camera",
millis: u32 = "millis", millis: u32 = "millis",
anim: u32 = "anim",
treadmill_x: f32 = "treadmill_x", treadmill_x: f32 = "treadmill_x",
treadmill_y: f32 = "treadmill_y", treadmill_y: f32 = "treadmill_y",
} }
@@ -37,6 +46,7 @@ gfx_defines! {
pipeline pipe { pipeline pipe {
vbuf: gfx::VertexBuffer<Vertex> = (), vbuf: gfx::VertexBuffer<Vertex> = (),
trans: gfx::ConstantBuffer<::view::Trans> = "b_trans", trans: gfx::ConstantBuffer<::view::Trans> = "b_trans",
constants: gfx::ConstantBuffer<Constants> = "b_constants",
locals: gfx::ConstantBuffer<Locals> = "b_locals", locals: gfx::ConstantBuffer<Locals> = "b_locals",
atlas: gfx::TextureSampler<[f32; 4]> = "t_atlas", atlas: gfx::TextureSampler<[f32; 4]> = "t_atlas",
pixcolor: gfx::RenderTarget<::view::ColorFormat> = "pixcolor", pixcolor: gfx::RenderTarget<::view::ColorFormat> = "pixcolor",
@@ -79,6 +89,7 @@ pub struct WorldScene<D: gfx::Device,
F: gfx::Factory<D::Resources>> { F: gfx::Factory<D::Resources>> {
pso: gfx::PipelineState<D::Resources, pipe::Meta>, pso: gfx::PipelineState<D::Resources, pipe::Meta>,
camera: na::Matrix4<f32>, camera: na::Matrix4<f32>,
constants: gfx::handle::Buffer<D::Resources, Constants>,
locals: gfx::handle::Buffer<D::Resources, Locals>, locals: gfx::handle::Buffer<D::Resources, Locals>,
atlas: gfx::handle::ShaderResourceView<D::Resources, atlas: gfx::handle::ShaderResourceView<D::Resources,
<view::ColorFormat as gfx::format::Formatted>::View>, <view::ColorFormat as gfx::format::Formatted>::View>,
@@ -94,17 +105,24 @@ pub struct WorldScene<D: gfx::Device,
} }
impl<D: gfx::Device, F: gfx::Factory<D::Resources>> WorldScene<D, F> { impl<D: gfx::Device, F: gfx::Factory<D::Resources>> WorldScene<D, F> {
pub fn new(factory: &mut F) -> WorldScene<D, F> { pub fn new(factory: &mut F,
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>) -> WorldScene<D, F> {
let (model, model_idx) = get_model(&get_data_model()); let (model, model_idx) = get_model(&get_data_model());
let (vertex_buffer, slice) = let (vertex_buffer, slice) =
factory.create_vertex_buffer_with_slice(&model, &model_idx[..]); factory.create_vertex_buffer_with_slice(&model, &model_idx[..]);
let constants = factory.create_constant_buffer(1);
encoder.update_constant_buffer(&constants, &Constants { anim: ANIMDATA[0],
r1: R1,
r2: R2 });
WorldScene { WorldScene {
pso: factory.create_pipeline_simple(VERTEX_SHADER_SRC, pso: factory.create_pipeline_simple(VERTEX_SHADER_SRC,
FRAGMENT_SHADER_SRC, FRAGMENT_SHADER_SRC,
pipe::new()) pipe::new())
.expect("create pipeline"), .expect("create pipeline"),
camera: na::Matrix4::one(), camera: na::Matrix4::one(),
constants: constants,
locals: factory.create_constant_buffer(1), locals: factory.create_constant_buffer(1),
atlas: tile::get_tiles::<_, _, view::ColorFormat>(factory), atlas: tile::get_tiles::<_, _, view::ColorFormat>(factory),
sampler: factory.create_sampler(tex::SamplerInfo::new(tex::FilterMethod::Scale, sampler: factory.create_sampler(tex::SamplerInfo::new(tex::FilterMethod::Scale,
@@ -171,19 +189,19 @@ impl<D: gfx::Device,
if ydiff.abs() > THRESHOLD { self.treadmills.1 += SCALE * ydiff; } if ydiff.abs() > THRESHOLD { self.treadmills.1 += SCALE * ydiff; }
}, },
TrackMode::PRESS => { TrackMode::PRESS => {
use self::na::ToHomogeneous;
let rot = na::Vector3::new(0.0, 0.0, 0.0); let rot = na::Vector3::new(0.0, 0.0, 0.0);
let speed = R2 * 0.01;
if state.rAxis[0].x > 0.5 { if state.rAxis[0].x > 0.5 {
self.camera = na::Similarity3::new(na::Vector3::new(-0.5, 0.0, 0.0), self.camera = na::Similarity3::new(na::Vector3::new(-speed, 0.0, 0.0),
rot, 1.0).to_homogeneous() * self.camera; rot, 1.0).to_homogeneous() * self.camera;
} if state.rAxis[0].x < -0.5 { } if state.rAxis[0].x < -0.5 {
self.camera = na::Similarity3::new(na::Vector3::new( 0.5, 0.0, 0.0), self.camera = na::Similarity3::new(na::Vector3::new( speed, 0.0, 0.0),
rot, 1.0).to_homogeneous() * self.camera; rot, 1.0).to_homogeneous() * self.camera;
} if state.rAxis[0].y > 0.5 { } if state.rAxis[0].y > 0.5 {
self.camera = na::Similarity3::new(na::Vector3::new( 0.0, -5.0, 0.0), self.camera = na::Similarity3::new(na::Vector3::new( 0.0, -speed, 0.0),
rot, 1.0).to_homogeneous() * self.camera; rot, 1.0).to_homogeneous() * self.camera;
} if state.rAxis[0].y < -0.5 { } if state.rAxis[0].y < -0.5 {
self.camera = na::Similarity3::new(na::Vector3::new( 0.0, 5.0, 0.0), self.camera = na::Similarity3::new(na::Vector3::new( 0.0, speed, 0.0),
rot, 1.0).to_homogeneous() * self.camera; rot, 1.0).to_homogeneous() * self.camera;
} }
}, },
@@ -199,7 +217,6 @@ impl<D: gfx::Device,
encoder.update_constant_buffer(&self.locals, &Locals { camera: *(self.camera).as_ref(), encoder.update_constant_buffer(&self.locals, &Locals { camera: *(self.camera).as_ref(),
millis: millis, millis: millis,
anim: ANIMDATA[0],
treadmill_x: self.treadmills.0, treadmill_x: self.treadmills.0,
treadmill_y: self.treadmills.1 }); treadmill_y: self.treadmills.1 });
} }
@@ -214,6 +231,7 @@ impl<D: gfx::Device,
let pipe = pipe::Data { let pipe = pipe::Data {
vbuf: self.vbuf.clone(), vbuf: self.vbuf.clone(),
trans: trans.clone(), trans: trans.clone(),
constants: self.constants.clone(),
locals: self.locals.clone(), locals: self.locals.clone(),
atlas: (self.atlas.clone(), self.sampler.clone()), atlas: (self.atlas.clone(), self.sampler.clone()),
pixcolor: target.clone(), pixcolor: target.clone(),
@@ -221,6 +239,12 @@ impl<D: gfx::Device,
}; };
encoder.draw(&self.slice, &self.pso, &pipe); encoder.draw(&self.slice, &self.pso, &pipe);
} }
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()
}
} }
extern crate memmap; extern crate memmap;
@@ -252,10 +276,15 @@ const VERTEX_SHADER_SRC: &'static [u8] = br#"
uniform b_trans { uniform b_trans {
mat4 u_matrix; mat4 u_matrix;
}; };
uniform b_constants {
//uvec4 animdata;
uint animdata;
float R1;
float R2;
};
uniform b_locals { uniform b_locals {
mat4 camera; mat4 camera;
uint millis; uint millis;
uint animdata;
float treadmill_x; float treadmill_x;
float treadmill_y; float treadmill_y;
}; };
@@ -264,21 +293,19 @@ const VERTEX_SHADER_SRC: &'static [u8] = br#"
v_uv = a_uv; v_uv = a_uv;
v_tileidx = a_tileidx; v_tileidx = a_tileidx;
float TWO_PI_CIRC = 2 * PI / 256.0; float TWO_PI_CIRC = 2 * PI / 256.0;
float R = 1024.0;
float R2 = 128.0;
gl_Position = u_matrix * camera * vec4(R2 * -1.0 * sin(TWO_PI_CIRC * (a_pos.x + treadmill_x)), gl_Position = u_matrix * camera * vec4(R2 * -1.0 * sin(TWO_PI_CIRC * (a_pos.x + treadmill_x)),
(R + R2 * cos(TWO_PI_CIRC * (a_pos.x + treadmill_x))) * cos(TWO_PI_CIRC * (a_pos.z + treadmill_y)), (R1 + R2 * cos(TWO_PI_CIRC * (a_pos.x + treadmill_x))) * cos(TWO_PI_CIRC * (a_pos.z + treadmill_y)),
(R + R2 * cos(TWO_PI_CIRC * (a_pos.x + treadmill_x))) * sin(TWO_PI_CIRC * (a_pos.z + treadmill_y)), (R1 + R2 * cos(TWO_PI_CIRC * (a_pos.x + treadmill_x))) * sin(TWO_PI_CIRC * (a_pos.z + treadmill_y)),
1.0); 1.0);
/* /*
float which = (1.0 + sin(float(millis) / 5000.)) / 2.0; float which = (1.0 + sin(float(millis) / 5000.)) / 2.0;
gl_Position = u_matrix * vec4(a_pos.x, gl_Position = u_matrix * camera * vec4(a_pos.x,
R * cos(TWO_PI_CIRC * a_pos.z), R1 * cos(TWO_PI_CIRC * a_pos.z),
which * a_pos.z + which * a_pos.z +
((1.0-which) * R * sin(TWO_PI_CIRC * a_pos.z)), ((1.0-which) * R1 * sin(TWO_PI_CIRC * a_pos.z)),
1.0); 1.0);
*/ */
} }
"#; "#;
@@ -290,11 +317,15 @@ const FRAGMENT_SHADER_SRC: &'static [u8] = br#"
flat in uint v_tileidx; flat in uint v_tileidx;
out vec4 pixcolor; out vec4 pixcolor;
uniform sampler2D t_atlas; uniform sampler2D t_atlas;
uniform b_constants {
//uvec4 animdata;
uint animdata;
float R1;
float R2;
};
uniform b_locals { uniform b_locals {
mat4 camera; mat4 camera;
uint millis; uint millis;
//uvec4 animdata;
uint animdata;
float treadmill_x; float treadmill_x;
float treadmill_y; float treadmill_y;
}; };

View File

@@ -10,14 +10,13 @@ use gfx;
use gfx::Device; use gfx::Device;
use gfx::traits::FactoryExt; use gfx::traits::FactoryExt;
use self::na::Inverse; use self::na::Inverse;
use self::num_traits::identities::One;
use self::piston_window::{PistonWindow, Window}; use self::piston_window::{PistonWindow, Window};
pub type ColorFormat = gfx::format::Srgba8; pub type ColorFormat = gfx::format::Srgba8;
pub type DepthFormat = gfx::format::DepthStencil; pub type DepthFormat = gfx::format::DepthStencil;
const NEAR: f32 = 0.01; const NEAR: f32 = 0.01;
const FAR: f32 = 4000.0; const FAR: f32 = 10000.0;
gfx_constant_struct! { gfx_constant_struct! {
Trans { Trans {
@@ -75,15 +74,10 @@ impl ViewRoot<gfx_device_gl::Device, ColorFormat, DepthFormat> {
window.encoder.clear_depth(&buffers.depth, 1.0); window.encoder.clear_depth(&buffers.depth, 1.0);
// Submit eye textures
let proj_mat = vr.projection_matrix(eye, NEAR, FAR); let proj_mat = vr.projection_matrix(eye, NEAR, FAR);
let eye_mat = vr.head_to_eye_transform(eye); let eye_mat = vr.head_to_eye_transform(eye);
let scene_mat = scene.origin();
use self::na::ToHomogeneous; let trans = Trans { matrix: *(proj_mat * eye_mat * hmd_mat * scene_mat).as_ref() };
let view_mat = na::Similarity3::new(na::Vector3::new(0.0, 1024.0 - 128.0, 0.0),
na::Vector3::new(0.0, 0.0, 0.0), 1.0).to_homogeneous();
let trans = Trans { matrix: *(proj_mat * eye_mat * hmd_mat * view_mat).as_ref() };
window.encoder.update_constant_buffer(&self.trans, &trans); window.encoder.update_constant_buffer(&self.trans, &trans);
scene.render(&mut window.factory, scene.render(&mut window.factory,