Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5e0a514aa5 | |||
| f2a645730c | |||
| 381157c8e2 | |||
| f795c73ed8 | |||
| 8eb9ab2d63 | |||
| 0275d01b96 | |||
| 931bc1b0a8 | |||
| 31ab07f615 | |||
| 708e0b4a16 | |||
| 193e807b6a | |||
| d69223ef13 | |||
| 8faa60b921 | |||
| 708af6720c | |||
| 8211912b44 | |||
| 9323380d5c | |||
| 8a68c1780e | |||
| 50ac1960b0 | |||
| ad647f2e99 | |||
| 0b8db8d9b4 | |||
| 235b5d58bd | |||
| 7a9f65115c | |||
| b8b5bf8ab7 | |||
| d851f934e0 | |||
| ebefec08ab | |||
| 067e8906be | |||
| fd9d1cca13 | |||
| 9f7246efba | |||
| a7562b449c | |||
| 251ace63a7 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,5 +2,6 @@ Cargo.lock
|
|||||||
data/
|
data/
|
||||||
openvr/
|
openvr/
|
||||||
openvr32/
|
openvr32/
|
||||||
|
out/
|
||||||
target/
|
target/
|
||||||
*.bat
|
*.bat
|
||||||
|
|||||||
@@ -12,10 +12,10 @@ memmap = "~0.2"
|
|||||||
gl = "*"
|
gl = "*"
|
||||||
gfx = "*"
|
gfx = "*"
|
||||||
gfx_device_gl = "*"
|
gfx_device_gl = "*"
|
||||||
|
image = "*"
|
||||||
|
nalgebra = "*"
|
||||||
|
num-traits = "*"
|
||||||
openvr = { git = "https://github.com/rust-openvr/rust-openvr" }
|
openvr = { git = "https://github.com/rust-openvr/rust-openvr" }
|
||||||
openvr_sys = "*"
|
openvr_sys = "*"
|
||||||
piston = "*"
|
piston = "*"
|
||||||
piston_window = "*"
|
piston_window = "*"
|
||||||
|
|
||||||
sdl2 = "0.22"
|
|
||||||
sdl2_image = "0.22"
|
|
||||||
|
|||||||
@@ -11,10 +11,10 @@ use tile::Tile;
|
|||||||
// 0x40 121 11x11 map matrix
|
// 0x40 121 11x11 map matrix
|
||||||
// 0xB9 7 ???
|
// 0xB9 7 ???
|
||||||
pub struct Arena {
|
pub struct Arena {
|
||||||
monster_x: [u8; 16],
|
_monster_x: [u8; 16],
|
||||||
monster_y: [u8; 16],
|
_monster_y: [u8; 16],
|
||||||
party_x: [u8; 8],
|
_party_x: [u8; 8],
|
||||||
party_y: [u8; 8],
|
_party_y: [u8; 8],
|
||||||
_unknown1: [u8; 16],
|
_unknown1: [u8; 16],
|
||||||
map: Field,
|
map: Field,
|
||||||
_unknown2: [u8; 7],
|
_unknown2: [u8; 7],
|
||||||
|
|||||||
94
src/bin/click.rs
Normal file
94
src/bin/click.rs
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
extern crate vrtue;
|
||||||
|
use vrtue::vr;
|
||||||
|
|
||||||
|
extern crate env_logger;
|
||||||
|
extern crate gfx;
|
||||||
|
#[macro_use] extern crate log;
|
||||||
|
extern crate openvr_sys;
|
||||||
|
extern crate piston_window;
|
||||||
|
|
||||||
|
use self::piston_window::{PistonWindow, Window, WindowSettings};
|
||||||
|
|
||||||
|
pub type ColorFormat = gfx::format::Srgba8;
|
||||||
|
pub type DepthFormat = gfx::format::DepthStencil;
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
let mut vr = vr::VR::new().expect("VR init");
|
||||||
|
|
||||||
|
let mut window: PistonWindow =
|
||||||
|
WindowSettings::new("Click Test", [512; 2])
|
||||||
|
.exit_on_esc(true)
|
||||||
|
.vsync(false)
|
||||||
|
.build().expect("Building Window");
|
||||||
|
|
||||||
|
let render_size = vr.recommended_render_target_size();
|
||||||
|
let left: vr::EyeBuffer<ColorFormat, DepthFormat> = vr::create_eyebuffer(&mut window.factory, render_size)
|
||||||
|
.expect("create left renderbuffer");
|
||||||
|
let right: vr::EyeBuffer<ColorFormat, DepthFormat> = vr::create_eyebuffer(&mut window.factory, render_size)
|
||||||
|
.expect("create right renderbuffer");
|
||||||
|
window.encoder.clear(&left.target, [1.0, 0.0, 0.0, 1.0]);
|
||||||
|
window.encoder.clear_depth(&left.depth, 1.0);
|
||||||
|
window.encoder.clear(&right.target, [0.0, 1.0, 0.0, 1.0]);
|
||||||
|
window.encoder.clear_depth(&right.depth, 1.0);
|
||||||
|
window.encoder.flush(&mut window.device);
|
||||||
|
|
||||||
|
let mut pads = ::std::collections::BTreeMap::<_, Option<openvr_sys::VRControllerState_t>>::new();
|
||||||
|
'main: loop {
|
||||||
|
let _poses = vr.poses();
|
||||||
|
vr.submit(vr::Eye::Left, &left.tex);
|
||||||
|
vr.submit(vr::Eye::Right, &right.tex);
|
||||||
|
|
||||||
|
while let Some(ev) = vr.poll_next_event() {
|
||||||
|
match ev {
|
||||||
|
vr::Event::Press { dev_idx, controller } => {
|
||||||
|
println!("Press event on #{}: {:?}", dev_idx, controller);
|
||||||
|
},
|
||||||
|
vr::Event::Unpress { dev_idx, controller } => {
|
||||||
|
println!("Unpress event on #{}: {:?}", dev_idx, controller);
|
||||||
|
},
|
||||||
|
vr::Event::Touch { dev_idx, controller } => {
|
||||||
|
if controller.button == openvr_sys::EVRButtonId_k_EButton_SteamVR_Touchpad as u32 {
|
||||||
|
pads.insert(dev_idx, None);
|
||||||
|
}
|
||||||
|
println!("Touch event on #{}: {:?}", dev_idx, controller);
|
||||||
|
},
|
||||||
|
vr::Event::Untouch { dev_idx, controller } => {
|
||||||
|
if controller.button == openvr_sys::EVRButtonId_k_EButton_SteamVR_Touchpad as u32 {
|
||||||
|
pads.remove(&dev_idx);
|
||||||
|
}
|
||||||
|
println!("Untouch event on #{}: {:?}", dev_idx, controller);
|
||||||
|
},
|
||||||
|
/*
|
||||||
|
t if t == openvr_sys::EVREventType::EVREventType_VREvent_TouchPadMove as u32 => {
|
||||||
|
let touch;
|
||||||
|
unsafe {
|
||||||
|
touch = *ev.data.touchPadMove();
|
||||||
|
}
|
||||||
|
println!("TouchPadMove event on #{}: {:?}", ev.trackedDeviceIndex, touch);
|
||||||
|
},
|
||||||
|
*/
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (pad, old) in pads.iter_mut() {
|
||||||
|
if let Some(state) = vr.get_controller_state(*pad) {
|
||||||
|
if let Some(old_state) = *old {
|
||||||
|
if state.unPacketNum == old_state.unPacketNum {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*old = Some(state);
|
||||||
|
println!("state for {}: {:?}", *pad, state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle window events
|
||||||
|
while let Some(ev) = window.poll_event() {
|
||||||
|
match ev {
|
||||||
|
piston_window::Input::Text(_) => break 'main,
|
||||||
|
_ => debug!("\t{:?}", ev)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
172
src/bin/gl.rs
172
src/bin/gl.rs
@@ -1,172 +0,0 @@
|
|||||||
extern crate vrtue;
|
|
||||||
use vrtue::*;
|
|
||||||
|
|
||||||
extern crate env_logger;
|
|
||||||
#[macro_use] extern crate gfx;
|
|
||||||
extern crate gl;
|
|
||||||
#[macro_use] extern crate log;
|
|
||||||
extern crate piston_window;
|
|
||||||
|
|
||||||
use gfx::Device;
|
|
||||||
use gfx::traits::FactoryExt;
|
|
||||||
use piston_window::{PistonWindow, Window, WindowSettings};
|
|
||||||
|
|
||||||
pub type ColorFormat = gfx::format::Srgba8;
|
|
||||||
//pub type DepthFormat = gfx::format::DepthStencil;
|
|
||||||
|
|
||||||
gfx_defines!{
|
|
||||||
vertex Vertex {
|
|
||||||
pos: [f32; 2] = "a_pos",
|
|
||||||
color: [f32; 3] = "a_color",
|
|
||||||
}
|
|
||||||
|
|
||||||
pipeline pipe {
|
|
||||||
vbuf: gfx::VertexBuffer<Vertex> = (),
|
|
||||||
pixcolor: gfx::RenderTarget<ColorFormat> = "pixcolor",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const TRIANGLE: [Vertex; 3] = [
|
|
||||||
Vertex { pos: [ -0.5, -0.5 ], color: [1.0, 0.0, 0.0] },
|
|
||||||
Vertex { pos: [ 0.5, -0.5 ], color: [0.0, 1.0, 0.0] },
|
|
||||||
Vertex { pos: [ 0.0, 0.5 ], color: [0.0, 0.0, 1.0] }
|
|
||||||
];
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
env_logger::init().expect("env logger");
|
|
||||||
let mut vr = vr::VR::new().expect("VR init");
|
|
||||||
let render_size = vr.recommended_render_target_size();
|
|
||||||
|
|
||||||
let mut window: PistonWindow =
|
|
||||||
WindowSettings::new("Hello Virtual World!", [512; 2])
|
|
||||||
.exit_on_esc(true)
|
|
||||||
.vsync(false)
|
|
||||||
//.vsync(true)
|
|
||||||
.build().expect("Building Window");
|
|
||||||
|
|
||||||
/*
|
|
||||||
let _sysleft = system.projection_matrix(vr::Eye::Left, 0.01, 1000.0);
|
|
||||||
let _eyeleft = system.eye_to_head_transform(vr::Eye::Left);
|
|
||||||
let _sysright = system.projection_matrix(vr::Eye::Right, 0.01, 1000.0);
|
|
||||||
let _eyeright = system.eye_to_head_transform(vr::Eye::Right);
|
|
||||||
*/
|
|
||||||
let pso = window.factory.create_pipeline_simple(VERTEX_SHADER_SRC,
|
|
||||||
FRAGMENT_SHADER_SRC,
|
|
||||||
pipe::new())
|
|
||||||
.expect("create pipeline");
|
|
||||||
|
|
||||||
let (tex_left, tgt_left) = vr::create_eyebuffer(&mut window.factory, render_size)
|
|
||||||
.expect("create left renderbuffer");
|
|
||||||
let (tex_right, tgt_right) = vr::create_eyebuffer(&mut window.factory, render_size)
|
|
||||||
.expect("create right renderbuffer");
|
|
||||||
let (vertex_buffer, slice) = window.factory.create_vertex_buffer_with_slice(&TRIANGLE, ());
|
|
||||||
|
|
||||||
let pipe_monitor = pipe::Data {
|
|
||||||
vbuf: vertex_buffer.clone(),
|
|
||||||
pixcolor: window.output_color.clone(),
|
|
||||||
};
|
|
||||||
let pipe_left = pipe::Data {
|
|
||||||
vbuf: vertex_buffer.clone(),
|
|
||||||
pixcolor: tgt_left,
|
|
||||||
};
|
|
||||||
let pipe_right = pipe::Data {
|
|
||||||
vbuf: vertex_buffer.clone(),
|
|
||||||
pixcolor: tgt_right,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut frame = 0;
|
|
||||||
window.window.swap_buffers(); frame += 1; // To contain setup calls to Frame 0 in apitrace
|
|
||||||
'main:
|
|
||||||
//while let Some(_) = window.next() {
|
|
||||||
loop {
|
|
||||||
info!("Frame #{}", frame);
|
|
||||||
let _now = std::time::SystemTime::now();
|
|
||||||
|
|
||||||
// Get the current sensor state
|
|
||||||
let _poses = vr.poses();
|
|
||||||
trace!("\t{:?} got pose", _now.elapsed());
|
|
||||||
if frame % 90 == 0 {
|
|
||||||
warn!("\t#{}: poses: {:?}\n", frame, _poses.poses[0]);
|
|
||||||
}
|
|
||||||
frame += 1;
|
|
||||||
|
|
||||||
for pass in [(Some((vr::Eye::Left, &tex_left)), &pipe_left),
|
|
||||||
(Some((vr::Eye::Right, &tex_right)), &pipe_right),
|
|
||||||
(None, &pipe_monitor),]
|
|
||||||
.into_iter() {
|
|
||||||
info!("\tpass for eye: {:?}", pass.0);
|
|
||||||
window.encoder.clear(&pass.1.pixcolor, [0.1, 0.5, 0.1, 1.0]);
|
|
||||||
window.encoder.draw(&slice, &pso, pass.1);
|
|
||||||
window.encoder.flush(&mut window.device);
|
|
||||||
|
|
||||||
// Submit eye textures
|
|
||||||
if let Some((eye, tex)) = pass.0 {
|
|
||||||
vr.submit(eye, tex);
|
|
||||||
trace!("\t\t{:?} submit {:?}", _now.elapsed(), eye);
|
|
||||||
} else {
|
|
||||||
window.window.swap_buffers();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
window.device.cleanup();
|
|
||||||
|
|
||||||
// handle window events
|
|
||||||
while let Some(ev) = window.poll_event() {
|
|
||||||
match ev {
|
|
||||||
piston_window::Input::Text(_) => break 'main,
|
|
||||||
_ => debug!("\t{:?}", ev)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
info!("shutting down");
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
fn gl_debug(device: &mut gfx_device_gl::Device, msg: &'static [u8; 6]) {
|
|
||||||
unsafe {
|
|
||||||
device.with_gl_naked(|gl| {
|
|
||||||
gl.DebugMessageInsert(gl::DEBUG_SOURCE_APPLICATION,
|
|
||||||
gl::DEBUG_TYPE_OTHER,
|
|
||||||
0,
|
|
||||||
gl::DEBUG_SEVERITY_LOW,
|
|
||||||
msg.len() as i32,
|
|
||||||
::std::mem::transmute(msg));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_err(device: &mut gfx_device_gl::Device) {
|
|
||||||
unsafe {
|
|
||||||
device.with_gl_naked(|gl| {
|
|
||||||
let err: gl::types::GLenum = gl.GetError();
|
|
||||||
if err != gl::NO_ERROR {
|
|
||||||
panic!("GL Error! {:?}", err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
const VERTEX_SHADER_SRC: &'static [u8] = br#"
|
|
||||||
#version 140
|
|
||||||
|
|
||||||
in vec2 a_pos;
|
|
||||||
in vec3 a_color;
|
|
||||||
out vec3 v_color;
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
v_color = a_color;
|
|
||||||
gl_Position = vec4(a_pos, 0.0, 1.0);
|
|
||||||
}
|
|
||||||
"#;
|
|
||||||
|
|
||||||
const FRAGMENT_SHADER_SRC: &'static [u8] = br#"
|
|
||||||
#version 140
|
|
||||||
|
|
||||||
in vec3 v_color;
|
|
||||||
out vec4 pixcolor;
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
pixcolor = vec4(v_color, 1.0);
|
|
||||||
}
|
|
||||||
"#;
|
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
extern crate vrtue;
|
||||||
|
use vrtue::{arena, tile, town, world};
|
||||||
|
|
||||||
extern crate itertools;
|
extern crate itertools;
|
||||||
extern crate memmap;
|
extern crate memmap;
|
||||||
|
|
||||||
@@ -7,11 +10,6 @@ use std::env;
|
|||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use memmap::{Mmap, Protection};
|
use memmap::{Mmap, Protection};
|
||||||
|
|
||||||
mod arena;
|
|
||||||
mod tile;
|
|
||||||
mod town;
|
|
||||||
mod transpose;
|
|
||||||
mod world;
|
|
||||||
|
|
||||||
fn mmap_to_rows<'a, M: world::HasMap>(mmap: &memmap::Mmap) -> &'a world::HasMap
|
fn mmap_to_rows<'a, M: world::HasMap>(mmap: &memmap::Mmap) -> &'a world::HasMap
|
||||||
where M: Copy + 'a
|
where M: Copy + 'a
|
||||||
@@ -1,36 +1,14 @@
|
|||||||
extern crate itertools;
|
extern crate vrtue;
|
||||||
extern crate sdl2;
|
use vrtue::ega;
|
||||||
extern crate sdl2_image;
|
use vrtue::ega::{Compression, Tiling};
|
||||||
|
|
||||||
|
extern crate image;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use itertools::Itertools;
|
fn main() {
|
||||||
use sdl2::surface::Surface;
|
|
||||||
use sdl2::pixels::PixelFormatEnum;
|
|
||||||
use sdl2_image::SaveSurface;
|
|
||||||
|
|
||||||
static EGA_PALETTE: [[u8; 4]; 16] = [[0x00u8, 0x00, 0x00, 0x00],
|
|
||||||
[0x00, 0xAA, 0x00, 0x00],
|
|
||||||
[0x00, 0x00, 0xAA, 0x00],
|
|
||||||
[0x00, 0xAA, 0xAA, 0x00],
|
|
||||||
[0x00, 0x00, 0x00, 0xAA],
|
|
||||||
[0x00, 0xAA, 0x00, 0xAA],
|
|
||||||
[0x00, 0x00, 0x55, 0xAA],
|
|
||||||
[0x00, 0xAA, 0xAA, 0xAA],
|
|
||||||
[0x00, 0x55, 0x55, 0x55],
|
|
||||||
[0x00, 0xFF, 0x55, 0x55],
|
|
||||||
[0x00, 0x55, 0xFF, 0x55],
|
|
||||||
[0x00, 0xFF, 0xFF, 0x55],
|
|
||||||
[0x00, 0x55, 0x55, 0xFF],
|
|
||||||
[0x00, 0xFF, 0x55, 0xFF],
|
|
||||||
[0x00, 0x55, 0xFF, 0xFF],
|
|
||||||
[0x00, 0xFF, 0xFF, 0xFF]];
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub fn main() {
|
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
let filename;
|
let filename;
|
||||||
if args.len() > 1 {
|
if args.len() > 1 {
|
||||||
@@ -39,28 +17,16 @@ pub fn main() {
|
|||||||
filename = "data/SHAPES.EGA";
|
filename = "data/SHAPES.EGA";
|
||||||
}
|
}
|
||||||
|
|
||||||
let _sdl_context = ::sdl2::init().unwrap();
|
let mut file = std::fs::File::open(Path::new(filename))
|
||||||
let _image_context = ::sdl2_image::init(::sdl2_image::INIT_PNG).unwrap();
|
.expect(&format!("failed opening EGA file: {}", filename));
|
||||||
|
let mut ega_vec = Vec::<u8>::new();
|
||||||
|
|
||||||
let mut file = std::fs::File::open(Path::new(filename)).unwrap();
|
file.read_to_end(&mut ega_vec).expect("Read EGA file");
|
||||||
let mut tile_buf = [0u8; 128];
|
let ega_page = ega::decode(&ega_vec, Compression::Uncompressed, Tiling::Tiled(16));
|
||||||
let mut surface = Surface::new(16, 16, PixelFormatEnum::RGBX8888).unwrap();
|
for (i, tilepixels) in ega_page.iter().enumerate() {
|
||||||
let mut i = 0;
|
|
||||||
while file.read_exact(&mut tile_buf).is_ok() {
|
|
||||||
surface.with_lock_mut(|pixel_bytes| {
|
|
||||||
pixel_bytes.iter_mut().set_from(tile_buf.iter()
|
|
||||||
.flat_map(|tile_byte| {
|
|
||||||
EGA_PALETTE[(tile_byte >> 4u8 & 0xF) as usize]
|
|
||||||
.into_iter()
|
|
||||||
.chain(EGA_PALETTE[(tile_byte & 0xF) as usize]
|
|
||||||
.into_iter())
|
|
||||||
})
|
|
||||||
.map(|x| *x));
|
|
||||||
|
|
||||||
|
|
||||||
});
|
|
||||||
let out_name = format!("out/{}.png", i);
|
let out_name = format!("out/{}.png", i);
|
||||||
surface.save(Path::new(&out_name)).ok();
|
let out_file = std::fs::File::create(Path::new(&out_name)).expect("open out file");
|
||||||
i += 1;
|
let enc = image::png::PNGEncoder::new(out_file);
|
||||||
|
enc.encode(&tilepixels, 16, 16, image::ColorType::RGBA(8)).expect("write png");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
62
src/bin/vrtue.rs
Normal file
62
src/bin/vrtue.rs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
extern crate vrtue;
|
||||||
|
use vrtue::{scenes, view, vr};
|
||||||
|
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() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(vr::VR::new().expect("VR init"))
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut window: PistonWindow =
|
||||||
|
WindowSettings::new("Hello, Britannia!", [1024; 2])
|
||||||
|
.exit_on_esc(true)
|
||||||
|
.vsync(vr.is_none()) // Let VR throttle framerate, if available
|
||||||
|
.build().expect("Building Window");
|
||||||
|
|
||||||
|
let mut aux_command = window.factory.create_command_buffer();
|
||||||
|
let mut scene = scenes::world::WorldScene::new(&mut window.device,
|
||||||
|
&mut window.factory,
|
||||||
|
&mut window.encoder,
|
||||||
|
&mut aux_command);
|
||||||
|
let view = view::ViewRoot::<gfx_device_gl::Device, view::ColorFormat, view::DepthFormat>
|
||||||
|
::create_view(&mut window, &mut vr);
|
||||||
|
|
||||||
|
'main:
|
||||||
|
//while let Some(_) = window.next() {
|
||||||
|
loop {
|
||||||
|
scene.update(&mut vr, &mut window.encoder);
|
||||||
|
view.draw(&mut window, &mut vr, &scene);
|
||||||
|
|
||||||
|
// handle window events
|
||||||
|
while let Some(ev) = window.poll_event() {
|
||||||
|
match ev {
|
||||||
|
Input::Press(Button::Keyboard(Key::Space)) |
|
||||||
|
Input::Press(Button::Keyboard(Key::Escape)) => break'main,
|
||||||
|
_ => debug!("\t{:?}", ev)
|
||||||
|
}
|
||||||
|
|
||||||
|
scene.event(Event::Piston(ev));
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle VR events
|
||||||
|
while let Some(ev) = vr.as_mut().and_then(|vr| vr.poll_next_event()) {
|
||||||
|
scene.event(Event::Vr(ev));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!("shutting down");
|
||||||
|
}
|
||||||
117
src/ega.rs
Normal file
117
src/ega.rs
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
static EGA_PALETTE: [[u8; 4]; 16] = [[0x00, 0x00, 0x00, 0x00],
|
||||||
|
[0x00, 0x00, 0xAA, 0x00],
|
||||||
|
[0x00, 0xAA, 0x00, 0x00],
|
||||||
|
[0x00, 0xAA, 0xAA, 0x00],
|
||||||
|
[0xAA, 0x00, 0x00, 0x00],
|
||||||
|
[0xAA, 0x00, 0xAA, 0x00],
|
||||||
|
[0xAA, 0x55, 0x00, 0x00],
|
||||||
|
[0xAA, 0xAA, 0xAA, 0x00],
|
||||||
|
[0x55, 0x55, 0x55, 0x00],
|
||||||
|
[0x55, 0x55, 0xFF, 0x00],
|
||||||
|
[0x55, 0xFF, 0x55, 0x00],
|
||||||
|
[0x55, 0xFF, 0xFF, 0x00],
|
||||||
|
[0xFF, 0x55, 0x55, 0x00],
|
||||||
|
[0xFF, 0x55, 0xFF, 0x00],
|
||||||
|
[0xFF, 0xFF, 0x55, 0x00],
|
||||||
|
[0xFF, 0xFF, 0xFF, 0x00]];
|
||||||
|
|
||||||
|
pub enum Compression {
|
||||||
|
Uncompressed,
|
||||||
|
Rle,
|
||||||
|
Lzw
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Tiling {
|
||||||
|
Untiled,
|
||||||
|
Tiled(u16)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EgaPage {
|
||||||
|
pub data: Vec<u8>,
|
||||||
|
pub dim: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EgaPage {
|
||||||
|
pub fn iter<'a>(&'a self) -> impl Iterator<Item=&'a [u8]> {
|
||||||
|
self.data.chunks(4 * self.dim * self.dim)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn mipmap(&self, scale_levels: u8) -> Mipmap {
|
||||||
|
let scale = 1 << scale_levels;
|
||||||
|
let scaled: Vec<Vec<u8>> = self.iter().map(|tile| {
|
||||||
|
let mut tilevec = Vec::new();
|
||||||
|
for row in tile.chunks(4 * self.dim) {
|
||||||
|
for _ in 0..scale {
|
||||||
|
for px in row.chunks(4) {
|
||||||
|
for subpx in px.iter().cycle().take(4 * scale) {
|
||||||
|
tilevec.push(*subpx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tilevec
|
||||||
|
}).collect();
|
||||||
|
|
||||||
|
// dummy buffers just to provide proper sizing on texture for mips
|
||||||
|
let dim = self.dim << scale_levels;
|
||||||
|
let mut dummies = Vec::new();
|
||||||
|
|
||||||
|
let mut level = 1;
|
||||||
|
loop {
|
||||||
|
let mipdim = dim >> level;
|
||||||
|
if mipdim == 0 { break; }
|
||||||
|
dummies.push(vec![0; 4 * mipdim * mipdim]);
|
||||||
|
level += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Mipmap {
|
||||||
|
len: self.data.len() / (4 * self.dim * self.dim),
|
||||||
|
dim: dim,
|
||||||
|
backing: scaled,
|
||||||
|
dummies: dummies,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Mipmap {
|
||||||
|
pub len: usize,
|
||||||
|
pub dim: usize,
|
||||||
|
backing: Vec<Vec<u8>>,
|
||||||
|
dummies: Vec<Vec<u8>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mipmap {
|
||||||
|
pub fn slices(&self) -> Vec<&[u8]> {
|
||||||
|
let mut slices: Vec<&[u8]> = Vec::with_capacity((1 + self.dummies.len()) * self.len);
|
||||||
|
for tile in self.backing.iter() {
|
||||||
|
slices.push(tile);
|
||||||
|
for dummy in self.dummies.iter() {
|
||||||
|
slices.push(&dummy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
slices
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decode<'a>(buf: &[u8], compression: Compression, tiling: Tiling)
|
||||||
|
-> EgaPage {
|
||||||
|
let out: Vec<u8>;
|
||||||
|
|
||||||
|
out = match compression {
|
||||||
|
Compression::Uncompressed => buf.iter()
|
||||||
|
.flat_map(|tile_byte| {
|
||||||
|
EGA_PALETTE[(tile_byte >> 4u8 & 0xF) as usize]
|
||||||
|
.into_iter()
|
||||||
|
.chain(EGA_PALETTE[(tile_byte & 0xF) as usize]
|
||||||
|
.into_iter())
|
||||||
|
})
|
||||||
|
.map(|x| *x)
|
||||||
|
.collect(),
|
||||||
|
_ => unimplemented!()
|
||||||
|
};
|
||||||
|
let dim = match tiling {
|
||||||
|
Tiling::Tiled(tiledim) => tiledim as usize,
|
||||||
|
Tiling::Untiled => out.len()
|
||||||
|
};
|
||||||
|
EgaPage { data: out, dim: dim}
|
||||||
|
}
|
||||||
13
src/lib.rs
13
src/lib.rs
@@ -1,3 +1,16 @@
|
|||||||
|
#![feature(conservative_impl_trait)]
|
||||||
|
|
||||||
|
#[macro_use] extern crate gfx;
|
||||||
#[macro_use] extern crate log;
|
#[macro_use] extern crate log;
|
||||||
|
|
||||||
|
pub mod arena;
|
||||||
|
pub mod ega;
|
||||||
|
pub mod scene;
|
||||||
|
pub mod scenes;
|
||||||
|
pub mod tile;
|
||||||
|
pub mod town;
|
||||||
|
pub mod view;
|
||||||
pub mod vr;
|
pub mod vr;
|
||||||
|
pub mod world;
|
||||||
|
|
||||||
|
mod transpose;
|
||||||
|
|||||||
28
src/scene.rs
Normal file
28
src/scene.rs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
use view;
|
||||||
|
use vr;
|
||||||
|
|
||||||
|
extern crate gfx;
|
||||||
|
extern crate gfx_device_gl;
|
||||||
|
extern crate nalgebra as na;
|
||||||
|
extern crate piston_window;
|
||||||
|
|
||||||
|
pub trait Scene<D: gfx::Device,
|
||||||
|
F: gfx::Factory<D::Resources>> {
|
||||||
|
fn event(&mut self, event: Event);
|
||||||
|
fn update(&mut self,
|
||||||
|
vr: &mut Option<vr::VR>, // TODO: abstract this out
|
||||||
|
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>);
|
||||||
|
fn render(&self,
|
||||||
|
factory: &mut F,
|
||||||
|
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>,
|
||||||
|
trans: &gfx::handle::Buffer<D::Resources, view::Trans>,
|
||||||
|
target: &gfx::handle::RenderTargetView<D::Resources, view::ColorFormat>,
|
||||||
|
depth: &gfx::handle::DepthStencilView<D::Resources, view::DepthFormat>);
|
||||||
|
|
||||||
|
fn origin(&self) -> na::Matrix4<f32>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Event {
|
||||||
|
Vr(vr::Event),
|
||||||
|
Piston(piston_window::Input),
|
||||||
|
}
|
||||||
1
src/scenes/mod.rs
Normal file
1
src/scenes/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod world;
|
||||||
28
src/scenes/shader/tile_frag.glsl
Normal file
28
src/scenes/shader/tile_frag.glsl
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#version 150
|
||||||
|
|
||||||
|
#define MILLIS_PER_TILE 4000u
|
||||||
|
|
||||||
|
in vec2 v_uv;
|
||||||
|
flat in uint v_tileidx;
|
||||||
|
out vec4 pixcolor;
|
||||||
|
uniform sampler2DArray t_tiles;
|
||||||
|
uniform b_constants {
|
||||||
|
uvec4 anim;
|
||||||
|
float R1;
|
||||||
|
float R2;
|
||||||
|
float R3;
|
||||||
|
};
|
||||||
|
uniform b_locals {
|
||||||
|
uint millis;
|
||||||
|
float treadmill_x;
|
||||||
|
float treadmill_y;
|
||||||
|
};
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
vec2 anim_uv = v_uv;
|
||||||
|
if (v_tileidx < 128u && bool(anim[v_tileidx / 32u] & 1u << v_tileidx % 32u)) {
|
||||||
|
anim_uv = vec2(v_uv.x, v_uv.y + float(millis % MILLIS_PER_TILE) / MILLIS_PER_TILE);
|
||||||
|
}
|
||||||
|
|
||||||
|
pixcolor = texture(t_tiles, vec3(anim_uv.x, 1.0 - anim_uv.y, v_tileidx));
|
||||||
|
}
|
||||||
42
src/scenes/shader/torus_vertex.glsl
Normal file
42
src/scenes/shader/torus_vertex.glsl
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#version 150
|
||||||
|
#define PI 3.1415926538
|
||||||
|
#define PI_CIRC (PI / 256.0)
|
||||||
|
#define TWO_PI_CIRC (2.0 * PI / 256.0)
|
||||||
|
|
||||||
|
in vec3 a_pos;
|
||||||
|
in vec2 a_uv;
|
||||||
|
in uint a_tileidx;
|
||||||
|
out vec2 v_uv;
|
||||||
|
flat out uint v_tileidx;
|
||||||
|
uniform b_trans {
|
||||||
|
mat4 u_matrix;
|
||||||
|
};
|
||||||
|
uniform b_constants {
|
||||||
|
uvec4 anim;
|
||||||
|
float R1;
|
||||||
|
float R2;
|
||||||
|
float R3;
|
||||||
|
};
|
||||||
|
uniform b_locals {
|
||||||
|
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
|
||||||
|
(r1 + r2 * cos(src.x)) * cos(src.y),
|
||||||
|
(r1 + r2 * cos(src.x)) * sin(src.y));
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
v_uv = a_uv;
|
||||||
|
v_tileidx = a_tileidx;
|
||||||
|
|
||||||
|
vec2 thetaphi = vec2(TWO_PI_CIRC * (a_pos.x + treadmill_x),
|
||||||
|
TWO_PI_CIRC * (a_pos.y + treadmill_y));
|
||||||
|
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);
|
||||||
|
}
|
||||||
336
src/scenes/world.rs
Normal file
336
src/scenes/world.rs
Normal file
@@ -0,0 +1,336 @@
|
|||||||
|
use scene;
|
||||||
|
use tile;
|
||||||
|
use view;
|
||||||
|
use vr;
|
||||||
|
use world as model;
|
||||||
|
use world::HasMap;
|
||||||
|
|
||||||
|
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;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::time::SystemTime;
|
||||||
|
|
||||||
|
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 R1: f32 = 256.0;
|
||||||
|
const R2: f32 = 64.0;
|
||||||
|
const R3: f32 = 128.0;
|
||||||
|
const PI: f32 = ::std::f32::consts::PI;
|
||||||
|
const TWO_PI: f32 = 2.0 * PI;
|
||||||
|
const TWO_PI_CIRC: f32 = TWO_PI / 256.0;
|
||||||
|
|
||||||
|
gfx_defines! {
|
||||||
|
vertex Vertex {
|
||||||
|
pos: [f32; 3] = "a_pos",
|
||||||
|
uv: [f32; 2] = "a_uv",
|
||||||
|
tileidx: u32 = "a_tileidx",
|
||||||
|
}
|
||||||
|
|
||||||
|
constant Constants {
|
||||||
|
anim: [u32; 4] = "anim",
|
||||||
|
r1: f32 = "R1",
|
||||||
|
r2: f32 = "R2",
|
||||||
|
r3: f32 = "R3",
|
||||||
|
}
|
||||||
|
|
||||||
|
constant Locals {
|
||||||
|
millis: u32 = "millis",
|
||||||
|
treadmill_x: f32 = "treadmill_x",
|
||||||
|
treadmill_y: f32 = "treadmill_y",
|
||||||
|
}
|
||||||
|
|
||||||
|
pipeline pipe {
|
||||||
|
vbuf: gfx::VertexBuffer<Vertex> = (),
|
||||||
|
trans: gfx::ConstantBuffer<::view::Trans> = "b_trans",
|
||||||
|
constants: gfx::ConstantBuffer<Constants> = "b_constants",
|
||||||
|
locals: gfx::ConstantBuffer<Locals> = "b_locals",
|
||||||
|
atlas: gfx::TextureSampler<[f32; 4]> = "t_tiles",
|
||||||
|
pixcolor: gfx::RenderTarget<::view::ColorFormat> = "pixcolor",
|
||||||
|
depth: gfx::DepthTarget<::view::DepthFormat> = gfx::preset::depth::LESS_EQUAL_WRITE,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn get_model(world: &model::World) -> (Vec<Vertex>, Vec<u32>) {
|
||||||
|
let mut verticies = Vec::new();
|
||||||
|
let mut indicies = Vec::new();
|
||||||
|
let mut v = 0;
|
||||||
|
|
||||||
|
for (r, row) in world.map().rows().enumerate() {
|
||||||
|
for (c, tile) in row.into_iter().enumerate() {
|
||||||
|
let tileidx = tile.val as u32;
|
||||||
|
let alt = match tileidx {
|
||||||
|
5 => 0.1,
|
||||||
|
6 => 0.8,
|
||||||
|
7 => 0.2,
|
||||||
|
8 => 1.5,
|
||||||
|
9 => 1.0,
|
||||||
|
10 | 11 | 12 => 1.0,
|
||||||
|
_ => 0.0,
|
||||||
|
};
|
||||||
|
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 },]);
|
||||||
|
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 },]);
|
||||||
|
indicies.extend_from_slice(
|
||||||
|
&[ v + 0, v + 1, v + 2,
|
||||||
|
v + 2, v + 3, v + 0,
|
||||||
|
v + 4, v + 5, v + 6,
|
||||||
|
v + 6, v + 7, v + 4 ]);
|
||||||
|
v += 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(verticies, indicies)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||||
|
enum TrackMode {
|
||||||
|
Touch,
|
||||||
|
Press
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct WorldScene<D: gfx::Device,
|
||||||
|
F: gfx::Factory<D::Resources>> {
|
||||||
|
pso: gfx::PipelineState<D::Resources, pipe::Meta>,
|
||||||
|
camera: na::Matrix4<f32>,
|
||||||
|
constants: gfx::handle::Buffer<D::Resources, Constants>,
|
||||||
|
locals: gfx::handle::Buffer<D::Resources, Locals>,
|
||||||
|
atlas: gfx::handle::ShaderResourceView<D::Resources,
|
||||||
|
<view::ColorFormat as gfx::format::Formatted>::View>,
|
||||||
|
sampler: gfx::handle::Sampler<D::Resources>,
|
||||||
|
f: PhantomData<F>,
|
||||||
|
|
||||||
|
vbuf: gfx::handle::Buffer<D::Resources, Vertex>,
|
||||||
|
slice: gfx::Slice<D::Resources>,
|
||||||
|
|
||||||
|
start_time: SystemTime,
|
||||||
|
treadmills: (f32, f32),
|
||||||
|
pads: BTreeMap<u32, (TrackMode, Option<openvr_sys::VRControllerState_t>)>,
|
||||||
|
|
||||||
|
pos: (u8, u8),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D: gfx::Device, F: gfx::Factory<D::Resources>> WorldScene<D, F> {
|
||||||
|
pub fn new(device: &mut D,
|
||||||
|
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 (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,
|
||||||
|
r3: R3});
|
||||||
|
|
||||||
|
WorldScene {
|
||||||
|
pso: factory.create_pipeline_simple(VERTEX_SHADER_SRC,
|
||||||
|
FRAGMENT_SHADER_SRC,
|
||||||
|
pipe::new())
|
||||||
|
.expect("create pipeline"),
|
||||||
|
camera: na::Matrix4::one(),
|
||||||
|
constants: constants,
|
||||||
|
locals: factory.create_constant_buffer(1),
|
||||||
|
atlas: tile::get_tiles::<_, _, view::ColorFormat>(device, factory, aux_command),
|
||||||
|
sampler: factory.create_sampler(tex::SamplerInfo::new(tex::FilterMethod::Jrd,
|
||||||
|
tex::WrapMode::Tile)),
|
||||||
|
f: PhantomData,
|
||||||
|
|
||||||
|
vbuf: vertex_buffer,
|
||||||
|
slice: slice,
|
||||||
|
start_time: SystemTime::now(),
|
||||||
|
treadmills: (0.0, 0.0),
|
||||||
|
pads: BTreeMap::new(),
|
||||||
|
|
||||||
|
pos: (90, 144),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn toroid((x, y): (u8, u8), 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 * -1.0f32 * 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] =
|
||||||
|
[1 << 0 | 1 << 1 | 1 << 2,
|
||||||
|
0,
|
||||||
|
1 << (68 % 32) | 1 << (69 % 32) | 1 << (70 % 32) | 1 << (71 % 32) | 1 << (76 % 32),
|
||||||
|
0];
|
||||||
|
|
||||||
|
impl<D: gfx::Device,
|
||||||
|
F: gfx::Factory<D::Resources>> scene::Scene<D, F> for WorldScene<D, F> {
|
||||||
|
|
||||||
|
fn event(&mut self, event: scene::Event) {
|
||||||
|
use scene::Event::*;
|
||||||
|
use vr::Event::*;
|
||||||
|
match event {
|
||||||
|
Vr(Touch { dev_idx, .. }) => {
|
||||||
|
self.pads.insert(dev_idx, (TrackMode::Touch, None));
|
||||||
|
},
|
||||||
|
Vr(Press { dev_idx, .. }) => {
|
||||||
|
self.pads.insert(dev_idx, (TrackMode::Press, None));
|
||||||
|
},
|
||||||
|
Vr(Unpress { dev_idx, .. }) => {
|
||||||
|
self.pads.insert(dev_idx, (TrackMode::Touch, None));
|
||||||
|
},
|
||||||
|
Vr(Untouch { dev_idx, .. }) => {
|
||||||
|
self.pads.remove(&dev_idx);
|
||||||
|
},
|
||||||
|
Piston(Input::Press(Button::Keyboard(Key::Left))) => {
|
||||||
|
self.pos = (self.pos.0.wrapping_sub(1), self.pos.1);
|
||||||
|
println!("x: {}, y: {}", self.pos.0, self.pos.1);
|
||||||
|
},
|
||||||
|
Piston(Input::Press(Button::Keyboard(Key::Right))) => {
|
||||||
|
self.pos = (self.pos.0.wrapping_add(1), self.pos.1);
|
||||||
|
println!("x: {}, y: {}", self.pos.0, self.pos.1);
|
||||||
|
},
|
||||||
|
Piston(Input::Press(Button::Keyboard(Key::Up))) => {
|
||||||
|
self.pos = (self.pos.0, self.pos.1.wrapping_sub(1));
|
||||||
|
println!("x: {}, y: {}", self.pos.0, self.pos.1);
|
||||||
|
},
|
||||||
|
Piston(Input::Press(Button::Keyboard(Key::Down))) => {
|
||||||
|
self.pos = (self.pos.0, self.pos.1.wrapping_add(1));
|
||||||
|
println!("x: {}, y: {}", self.pos.0, self.pos.1);
|
||||||
|
},
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&mut self,
|
||||||
|
vr: &mut Option<vr::VR>,
|
||||||
|
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>) {
|
||||||
|
const NANOS_PER_MILLI: u32 = 1_000_000;
|
||||||
|
const MILLIS_PER_SEC: u64 = 1_000;
|
||||||
|
let elapsed = self.start_time.elapsed().expect("scene timer");
|
||||||
|
let millis = elapsed.subsec_nanos() / NANOS_PER_MILLI + (elapsed.as_secs() * MILLIS_PER_SEC) as u32;
|
||||||
|
|
||||||
|
for (pad, track) in self.pads.iter_mut() {
|
||||||
|
let mode = track.0;
|
||||||
|
if let Some(state) = vr.as_ref().and_then(|vr| vr.get_controller_state(*pad)) {
|
||||||
|
if let Some(old_state) = track.1 {
|
||||||
|
match mode {
|
||||||
|
TrackMode::Touch => {
|
||||||
|
const THRESHOLD: f32 = 0.005;
|
||||||
|
const SCALE: f32 = 32.0;
|
||||||
|
let xdiff = state.rAxis[0].x - old_state.rAxis[0].x;
|
||||||
|
let ydiff = state.rAxis[0].y - old_state.rAxis[0].y;
|
||||||
|
if xdiff.abs() > THRESHOLD { self.treadmills.0 += SCALE * xdiff; }
|
||||||
|
if ydiff.abs() > THRESHOLD { self.treadmills.1 += SCALE * ydiff; }
|
||||||
|
},
|
||||||
|
TrackMode::Press => {
|
||||||
|
let rot = na::Vector3::new(0.0, 0.0, 0.0);
|
||||||
|
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;
|
||||||
|
} 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;
|
||||||
|
} if state.rAxis[0].y > 0.5 {
|
||||||
|
self.camera = na::Similarity3::new(na::Vector3::new( 0.0, -speed, 0.0),
|
||||||
|
rot, 1.0).to_homogeneous() * self.camera;
|
||||||
|
} if state.rAxis[0].y < -0.5 {
|
||||||
|
self.camera = na::Similarity3::new(na::Vector3::new( 0.0, speed, 0.0),
|
||||||
|
rot, 1.0).to_homogeneous() * self.camera;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if state.unPacketNum == old_state.unPacketNum {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*track = (mode, Some(state));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
encoder.update_constant_buffer(&self.locals, &Locals { millis: millis,
|
||||||
|
treadmill_x: self.treadmills.0,
|
||||||
|
treadmill_y: self.treadmills.1 });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render(&self,
|
||||||
|
_factory: &mut F,
|
||||||
|
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>,
|
||||||
|
trans: &gfx::handle::Buffer<D::Resources, view::Trans>,
|
||||||
|
target: &gfx::handle::RenderTargetView<D::Resources, view::ColorFormat>,
|
||||||
|
depth: &gfx::handle::DepthStencilView<D::Resources, view::DepthFormat>) {
|
||||||
|
|
||||||
|
let pipe = pipe::Data {
|
||||||
|
vbuf: self.vbuf.clone(),
|
||||||
|
trans: trans.clone(),
|
||||||
|
constants: self.constants.clone(),
|
||||||
|
locals: self.locals.clone(),
|
||||||
|
atlas: (self.atlas.clone(), self.sampler.clone()),
|
||||||
|
pixcolor: target.clone(),
|
||||||
|
depth: depth.clone(),
|
||||||
|
};
|
||||||
|
encoder.draw(&self.slice, &self.pso, &pipe);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn origin(&self) -> na::Matrix4<f32> {
|
||||||
|
let (x, y) = (self.pos.0, self.pos.1);
|
||||||
|
let eye = Self::toroid((x, y), R1, R2, R3);
|
||||||
|
let looktgt = Self::toroid((x, y.wrapping_add(1)), 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extern crate memmap;
|
||||||
|
fn get_data_model() -> model::World {
|
||||||
|
use self::memmap::{Mmap, Protection};
|
||||||
|
use std::mem::transmute;
|
||||||
|
|
||||||
|
fn mmap_to_rows<'a, M: model::HasMap>(mmap: &memmap::Mmap) -> &'a M
|
||||||
|
where M: Copy + 'a
|
||||||
|
{
|
||||||
|
assert_eq!(::std::mem::size_of::<M>(), mmap.len());
|
||||||
|
unsafe { transmute::<*const u8, &M>(mmap.ptr()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
let filename = "data/WORLD.MAP";
|
||||||
|
let file_mmap = Mmap::open_path(filename, Protection::Read).unwrap();
|
||||||
|
mmap_to_rows::<model::World>(&file_mmap).clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
const VERTEX_SHADER_SRC: &'static [u8] = include_bytes!("shader/torus_vertex.glsl");
|
||||||
|
const FRAGMENT_SHADER_SRC: &'static [u8] = include_bytes!("shader/tile_frag.glsl");
|
||||||
49
src/tile.rs
49
src/tile.rs
@@ -1,8 +1,55 @@
|
|||||||
|
extern crate gfx;
|
||||||
|
|
||||||
|
use ega;
|
||||||
|
|
||||||
|
use ::std;
|
||||||
|
use std::io::Read;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use self::gfx::{CommandBuffer, Typed};
|
||||||
|
use self::gfx::tex;
|
||||||
|
|
||||||
|
const TILEDIM: u16 = 16;
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct Tile {
|
pub struct Tile {
|
||||||
val: u8,
|
pub val: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_tiles<D, F, T>(device: &mut D,
|
||||||
|
factory: &mut F,
|
||||||
|
command: &mut <D as gfx::Device>::CommandBuffer)
|
||||||
|
-> gfx::handle::ShaderResourceView<D::Resources, T::View>
|
||||||
|
where D: gfx::Device,
|
||||||
|
F: gfx::Factory<D::Resources>,
|
||||||
|
T: gfx::format::TextureFormat {
|
||||||
|
let filename = "data/SHAPES.EGA";
|
||||||
|
let mut file = std::fs::File::open(Path::new(filename))
|
||||||
|
.expect(&format!("failed opening tiles file: {}", filename));
|
||||||
|
let mut ega_bytes = Vec::new();
|
||||||
|
file.read_to_end(&mut ega_bytes).expect("Read tiles file");
|
||||||
|
let ega_page = ega::decode(&ega_bytes, ega::Compression::Uncompressed, ega::Tiling::Tiled(TILEDIM));
|
||||||
|
let mipmap = ega_page.mipmap(2);
|
||||||
|
|
||||||
|
let tex = factory.create_texture_const_u8::<T>(tex::Kind::D2Array(mipmap.dim as u16,
|
||||||
|
mipmap.dim as u16,
|
||||||
|
mipmap.len as u16,
|
||||||
|
tex::AaMode::Single),
|
||||||
|
&mipmap.slices())
|
||||||
|
.expect("create tile texture");
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut manager = gfx::handle::Manager::<D::Resources>::new();
|
||||||
|
let view = manager.ref_srv(tex.1.raw());
|
||||||
|
command.generate_mipmap(*view);
|
||||||
|
device.submit(command);
|
||||||
|
}
|
||||||
|
tex.1
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
impl Tile {
|
impl Tile {
|
||||||
pub fn as_char(&self) -> char {
|
pub fn as_char(&self) -> char {
|
||||||
match self.val {
|
match self.val {
|
||||||
|
|||||||
24
src/town.rs
24
src/town.rs
@@ -6,23 +6,23 @@ use tile::Tile;
|
|||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
enum Behavior {
|
enum Behavior {
|
||||||
FIXED = 0x00,
|
Fixed = 0x00,
|
||||||
WANDER = 0x01,
|
Wander = 0x01,
|
||||||
FOLLOW = 0x80,
|
Follow = 0x80,
|
||||||
ATTACK = 0xFF,
|
Attack = 0xFF,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct Town {
|
pub struct Town {
|
||||||
map: Chunk,
|
map: Chunk,
|
||||||
npc_tile1: [Tile; 32],
|
_npc_tile1: [Tile; 32],
|
||||||
npc_x1: [u8; 32],
|
_npc_x1: [u8; 32],
|
||||||
npc_y1: [u8; 32],
|
_npc_y1: [u8; 32],
|
||||||
npc_tile2: [Tile; 32],
|
_npc_tile2: [Tile; 32],
|
||||||
npc_x2: [u8; 32],
|
_npc_x2: [u8; 32],
|
||||||
npc_y2: [u8; 32],
|
_npc_y2: [u8; 32],
|
||||||
npc_behavior: [Behavior; 32],
|
_npc_behavior: [Behavior; 32],
|
||||||
npc_talk_idx: [u8; 32],
|
_npc_talk_idx: [u8; 32],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HasMap for Town {
|
impl HasMap for Town {
|
||||||
|
|||||||
128
src/view.rs
Normal file
128
src/view.rs
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
use vr;
|
||||||
|
use vr::{AsMatrix4, VR};
|
||||||
|
|
||||||
|
extern crate gfx_device_gl;
|
||||||
|
extern crate nalgebra as na;
|
||||||
|
extern crate num_traits;
|
||||||
|
extern crate piston_window;
|
||||||
|
|
||||||
|
use gfx;
|
||||||
|
use gfx::Device;
|
||||||
|
use gfx::traits::FactoryExt;
|
||||||
|
use self::na::{Inverse, ToHomogeneous};
|
||||||
|
use self::piston_window::{PistonWindow, Window};
|
||||||
|
|
||||||
|
pub type ColorFormat = gfx::format::Srgba8;
|
||||||
|
pub type DepthFormat = gfx::format::DepthStencil;
|
||||||
|
|
||||||
|
const NEAR: f32 = 0.01;
|
||||||
|
const FAR: f32 = 1000.0;
|
||||||
|
|
||||||
|
gfx_constant_struct! {
|
||||||
|
Trans {
|
||||||
|
matrix: [[f32; 4]; 4] = "u_matrix",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ViewRoot<Dev, T, D>
|
||||||
|
where Dev: gfx::Device,
|
||||||
|
T: gfx::format::RenderFormat + gfx::format::TextureFormat,
|
||||||
|
D: gfx::format::DepthFormat + gfx::format::TextureFormat {
|
||||||
|
|
||||||
|
left: Option<vr::EyeBuffer<T, D>>,
|
||||||
|
right: Option<vr::EyeBuffer<T, D>>,
|
||||||
|
trans: gfx::handle::Buffer<Dev::Resources, Trans>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ViewRoot<gfx_device_gl::Device, ColorFormat, DepthFormat> {
|
||||||
|
pub fn create_view(window: &mut PistonWindow, vr: &Option<VR>)
|
||||||
|
-> ViewRoot<gfx_device_gl::Device, ColorFormat, DepthFormat> {
|
||||||
|
if let &Some(ref vr) = vr {
|
||||||
|
let render_size = vr.recommended_render_target_size();
|
||||||
|
|
||||||
|
let render_size = vr::Size { width: render_size.width * 220 / 100,
|
||||||
|
height: render_size.height * 220 / 100 };
|
||||||
|
|
||||||
|
let left = vr::create_eyebuffer(&mut window.factory, render_size)
|
||||||
|
.expect("create left renderbuffer");
|
||||||
|
let right = vr::create_eyebuffer(&mut window.factory, render_size)
|
||||||
|
.expect("create right renderbuffer");
|
||||||
|
let trans = window.factory.create_constant_buffer(1);
|
||||||
|
|
||||||
|
window.window.swap_buffers(); // To contain setup calls to Frame 0 in apitrace
|
||||||
|
|
||||||
|
ViewRoot::<gfx_device_gl::Device, ColorFormat, DepthFormat> {
|
||||||
|
left: Some(left),
|
||||||
|
right: Some(right),
|
||||||
|
trans: trans,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let trans = window.factory.create_constant_buffer(1);
|
||||||
|
ViewRoot::<gfx_device_gl::Device, ColorFormat, DepthFormat> {
|
||||||
|
left: None,
|
||||||
|
right: None,
|
||||||
|
trans: trans,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw(&self,
|
||||||
|
window: &mut PistonWindow,
|
||||||
|
vr: &mut Option<vr::VR>,
|
||||||
|
scene: &::scene::Scene<gfx_device_gl::Device, gfx_device_gl::Factory>) {
|
||||||
|
if let &mut Some(ref mut vr) = vr {
|
||||||
|
// Get the current sensor state
|
||||||
|
let poses = vr.poses();
|
||||||
|
|
||||||
|
let mut hmd_mat = poses.poses[0].to_device.as_matrix4();
|
||||||
|
hmd_mat.inverse_mut();
|
||||||
|
|
||||||
|
for &(eye, buffers) in [(vr::Eye::Left, &self.left),
|
||||||
|
(vr::Eye::Right, &self.right)].into_iter() {
|
||||||
|
let target = &buffers.as_ref().expect("vr color buffer").target;
|
||||||
|
let depth = &buffers.as_ref().expect("vr depth buffer").depth;
|
||||||
|
window.encoder.clear(target, [0.005, 0.005, 0.01, 1.0]);
|
||||||
|
window.encoder.clear_depth(depth, 1.0);
|
||||||
|
|
||||||
|
let proj_mat = vr.projection_matrix(eye, NEAR, FAR);
|
||||||
|
let eye_mat = vr.head_to_eye_transform(eye);
|
||||||
|
let scene_mat = scene.origin();
|
||||||
|
let trans = Trans { matrix: *(proj_mat * eye_mat * hmd_mat * scene_mat).as_ref() };
|
||||||
|
window.encoder.update_constant_buffer(&self.trans, &trans);
|
||||||
|
|
||||||
|
scene.render(&mut window.factory,
|
||||||
|
&mut window.encoder,
|
||||||
|
&self.trans,
|
||||||
|
&target,
|
||||||
|
&depth);
|
||||||
|
}
|
||||||
|
} 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.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();
|
||||||
|
let scene_mat = scene.origin();
|
||||||
|
let trans = Trans { matrix: *(proj_mat * head_mat * scene_mat).as_ref() };
|
||||||
|
window.encoder.update_constant_buffer(&self.trans, &trans);
|
||||||
|
}
|
||||||
|
// draw monitor window
|
||||||
|
window.encoder.clear(&window.output_color, [0.005, 0.005, 0.01, 1.0]);
|
||||||
|
window.encoder.clear_depth(&window.output_stencil, 1.0);
|
||||||
|
scene.render(&mut window.factory,
|
||||||
|
&mut window.encoder,
|
||||||
|
&self.trans,
|
||||||
|
&window.output_color,
|
||||||
|
&window.output_stencil);
|
||||||
|
|
||||||
|
window.encoder.flush(&mut window.device);
|
||||||
|
if let (&mut Some(ref mut vr),
|
||||||
|
&Some(ref left),
|
||||||
|
&Some(ref right)) = (vr, &self.left, &self.right) {
|
||||||
|
vr.submit(vr::Eye::Left, &left.tex);
|
||||||
|
vr.submit(vr::Eye::Right, &right.tex);
|
||||||
|
}
|
||||||
|
window.window.swap_buffers();
|
||||||
|
window.device.cleanup();
|
||||||
|
}
|
||||||
|
}
|
||||||
192
src/vr.rs
192
src/vr.rs
@@ -1,67 +1,167 @@
|
|||||||
extern crate gfx;
|
extern crate gfx;
|
||||||
extern crate gfx_device_gl;
|
extern crate gfx_device_gl;
|
||||||
|
extern crate nalgebra as na;
|
||||||
|
extern crate num_traits;
|
||||||
extern crate openvr as vr;
|
extern crate openvr as vr;
|
||||||
extern crate openvr_sys;
|
extern crate openvr_sys;
|
||||||
|
|
||||||
use self::gfx::{tex, Factory, Typed};
|
|
||||||
|
|
||||||
pub use self::vr::Eye;
|
pub use self::vr::Eye;
|
||||||
|
pub use self::vr::common::Size;
|
||||||
|
pub use self::vr::tracking::{TrackedDeviceClass, TrackedDevicePoses};
|
||||||
|
|
||||||
|
use self::gfx::{tex, Factory, Typed};
|
||||||
|
use self::gfx_device_gl::Resources as GLResources;
|
||||||
|
use self::na::Inverse;
|
||||||
|
use self::num_traits::identities::Zero;
|
||||||
|
use self::num_traits::identities::One;
|
||||||
|
use self::openvr_sys::{VREvent_Controller_t, VREvent_t};
|
||||||
|
|
||||||
pub struct VR {
|
pub struct VR {
|
||||||
system: vr::IVRSystem,
|
system: vr::IVRSystem,
|
||||||
compositor: vr::IVRCompositor,
|
compositor: vr::IVRCompositor,
|
||||||
gfx_handles: gfx::handle::Manager<gfx_device_gl::Resources>,
|
gfx_handles: gfx::handle::Manager<GLResources>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Event {
|
||||||
|
Touch { dev_idx: u32, controller: VREvent_Controller_t },
|
||||||
|
Press { dev_idx: u32, controller: VREvent_Controller_t },
|
||||||
|
Unpress { dev_idx: u32, controller: VREvent_Controller_t },
|
||||||
|
Untouch { dev_idx: u32, controller: VREvent_Controller_t },
|
||||||
|
Other(VREvent_t),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VR {
|
impl VR {
|
||||||
pub fn new() -> Result<VR, vr::Error<openvr_sys::EVRInitError>> {
|
pub fn new() -> Result<VR, vr::Error<openvr_sys::EVRInitError>> {
|
||||||
Ok(VR {
|
Ok(VR {
|
||||||
system: try!(vr::init()),
|
system: try!(vr::init()),
|
||||||
compositor: try!(vr::compositor()),
|
compositor: try!(vr::compositor()),
|
||||||
gfx_handles: gfx::handle::Manager::new(),
|
gfx_handles: gfx::handle::Manager::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn poses(&mut self) -> vr::tracking::TrackedDevicePoses {
|
pub fn poses(&mut self) -> vr::tracking::TrackedDevicePoses {
|
||||||
self.gfx_handles.clear();
|
self.gfx_handles.clear();
|
||||||
self.compositor.wait_get_poses()
|
self.compositor.wait_get_poses()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn submit<T>(&mut self, eye: Eye, tex: &gfx::handle::Texture<gfx_device_gl::Resources, T>) {
|
pub fn submit<T>(&mut self, eye: Eye, tex: &gfx::handle::Texture<GLResources, T>) {
|
||||||
let tex_id = match self.gfx_handles.ref_texture(tex.raw()) {
|
let tex_id = match self.gfx_handles.ref_texture(tex.raw()) {
|
||||||
&gfx_device_gl::NewTexture::Surface(id) => id,
|
&gfx_device_gl::NewTexture::Surface(id) => id,
|
||||||
_ => panic!("Not a surface")
|
_ => panic!("Not a surface")
|
||||||
};
|
};
|
||||||
self.compositor.submit(eye,
|
self.compositor.submit(eye,
|
||||||
tex_id as usize,
|
tex_id as usize,
|
||||||
vr::common::TextureBounds::new((0.0, 1.0), (0.0, 1.0)));
|
vr::common::TextureBounds::new((0.0, 1.0), (0.0, 1.0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn recommended_render_target_size(&self) -> vr::common::Size {
|
pub fn recommended_render_target_size(&self) -> Size {
|
||||||
self.system.recommended_render_target_size()
|
self.system.recommended_render_target_size()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn projection_matrix(self: &Self, eye: Eye, near: f32, far: f32) -> na::Matrix4<f32> {
|
||||||
|
self.system.projection_matrix(eye, near, far).as_matrix4()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn head_to_eye_transform(self: &Self, eye: Eye) -> na::Matrix4<f32> {
|
||||||
|
let mut mat = self.system.eye_to_head_transform(eye).as_matrix4();
|
||||||
|
assert!(mat.inverse_mut(), "inverse eye matrix");
|
||||||
|
mat
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn poll_next_event(&mut self) -> Option<Event> {
|
||||||
|
use self::openvr_sys::EVREventType as EvType;
|
||||||
|
unsafe {
|
||||||
|
let system = * { self.system.0 as *mut openvr_sys::VR_IVRSystem_FnTable };
|
||||||
|
let mut event: openvr_sys::VREvent_t = ::std::mem::zeroed();
|
||||||
|
|
||||||
|
if system.PollNextEvent.unwrap()(&mut event,
|
||||||
|
::std::mem::size_of::<openvr_sys::VREvent_t>() as u32
|
||||||
|
) == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let dev_idx = event.trackedDeviceIndex;
|
||||||
|
Some(match ::std::mem::transmute(event.eventType) {
|
||||||
|
EvType::EVREventType_VREvent_ButtonTouch =>
|
||||||
|
Event::Touch { dev_idx: dev_idx as u32, controller: *event.data.controller() },
|
||||||
|
EvType::EVREventType_VREvent_ButtonPress =>
|
||||||
|
Event::Press { dev_idx: dev_idx as u32, controller: *event.data.controller() },
|
||||||
|
EvType::EVREventType_VREvent_ButtonUnpress =>
|
||||||
|
Event::Unpress { dev_idx: dev_idx as u32, controller: *event.data.controller() },
|
||||||
|
EvType::EVREventType_VREvent_ButtonUntouch =>
|
||||||
|
Event::Untouch { dev_idx: dev_idx as u32, controller: *event.data.controller() },
|
||||||
|
_ => Event::Other(event),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_controller_state(&self, index: u32) -> Option<openvr_sys::VRControllerState_t> {
|
||||||
|
unsafe {
|
||||||
|
let system = * { self.system.0 as *const openvr_sys::VR_IVRSystem_FnTable };
|
||||||
|
let mut state: openvr_sys::VRControllerState_t = ::std::mem::zeroed();
|
||||||
|
|
||||||
|
match system.GetControllerState.unwrap()(
|
||||||
|
index,
|
||||||
|
&mut state,
|
||||||
|
) {
|
||||||
|
0 => None,
|
||||||
|
_ => Some(state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for VR {
|
impl Drop for VR {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
vr::shutdown()
|
vr::shutdown()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_eyebuffer<T>(factory: &mut gfx_device_gl::Factory,
|
pub trait AsMatrix4<N> {
|
||||||
size: vr::common::Size)
|
fn as_matrix4(self) -> na::Matrix4<N>;
|
||||||
-> Result<(gfx::handle::Texture<gfx_device_gl::Resources,
|
}
|
||||||
T::Surface>,
|
impl<N: Copy + Zero + One> AsMatrix4<N> for [[N; 4]; 3] {
|
||||||
gfx::handle::RenderTargetView<gfx_device_gl::Resources,
|
#[inline]
|
||||||
T>),
|
fn as_matrix4(self) -> na::Matrix4<N> {
|
||||||
gfx::CombinedError>
|
na::Matrix4::new(self[0][0], self[0][1], self[0][2], self[0][3],
|
||||||
where T: gfx::format::RenderFormat + gfx::format::TextureFormat {
|
self[1][0], self[1][1], self[1][2], self[1][3],
|
||||||
let tex = try!(factory.create_texture(
|
self[2][0], self[2][1], self[2][2], self[2][3],
|
||||||
tex::Kind::D2(size.width as tex::Size, size.height as tex::Size, tex::AaMode::Single),
|
N::zero(), N::zero(), N::zero(), N::one())
|
||||||
1, // levels
|
}
|
||||||
gfx::RENDER_TARGET, // bind
|
}
|
||||||
gfx::Usage::GpuOnly, // Usage
|
impl<N: Copy> AsMatrix4<N> for [[N; 4]; 4] {
|
||||||
Some(<T::Channel as gfx::format::ChannelTyped>::get_channel_type()))); // hint: format::ChannelType?
|
#[inline]
|
||||||
let tgt = try!(factory.view_texture_as_render_target(&tex, 0, None));
|
fn as_matrix4(self) -> na::Matrix4<N> {
|
||||||
Ok((tex, tgt))
|
na::Matrix4::new(self[0][0], self[0][1], self[0][2], self[0][3],
|
||||||
|
self[1][0], self[1][1], self[1][2], self[1][3],
|
||||||
|
self[2][0], self[2][1], self[2][2], self[2][3],
|
||||||
|
self[3][0], self[3][1], self[3][2], self[3][3])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EyeBuffer<T, D>
|
||||||
|
where T: gfx::format::RenderFormat + gfx::format::TextureFormat,
|
||||||
|
D: gfx::format::DepthFormat + gfx::format::TextureFormat {
|
||||||
|
|
||||||
|
pub tex: gfx::handle::Texture<GLResources, T::Surface>,
|
||||||
|
pub target: gfx::handle::RenderTargetView<GLResources, T>,
|
||||||
|
pub depth: gfx::handle::DepthStencilView<GLResources, D>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_eyebuffer<T, D>(factory: &mut gfx_device_gl::Factory,
|
||||||
|
size: Size)
|
||||||
|
-> Result<EyeBuffer<T, D>, gfx::CombinedError>
|
||||||
|
where T: gfx::format::RenderFormat + gfx::format::TextureFormat,
|
||||||
|
D: gfx::format::DepthFormat + gfx::format::TextureFormat {
|
||||||
|
let tex = try!(factory.create_texture(
|
||||||
|
tex::Kind::D2(size.width as tex::Size, size.height as tex::Size, tex::AaMode::Single),
|
||||||
|
1, // levels
|
||||||
|
gfx::RENDER_TARGET, // bind
|
||||||
|
gfx::Usage::GpuOnly, // Usage
|
||||||
|
Some(<T::Channel as gfx::format::ChannelTyped>::get_channel_type()))); // hint: format::ChannelType?
|
||||||
|
let tgt = try!(factory.view_texture_as_render_target(&tex, 0, None));
|
||||||
|
let depth = try!(factory.create_depth_stencil_view_only(size.width as tex::Size,
|
||||||
|
size.height as tex::Size));
|
||||||
|
Ok(EyeBuffer { tex: tex, target: tgt, depth: depth })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ pub trait HasMap {
|
|||||||
const CHUNKDIM: usize = 32;
|
const CHUNKDIM: usize = 32;
|
||||||
pub type ChunkRow = [Tile; CHUNKDIM];
|
pub type ChunkRow = [Tile; CHUNKDIM];
|
||||||
pub type ChunkRect = [ChunkRow; CHUNKDIM];
|
pub type ChunkRect = [ChunkRow; CHUNKDIM];
|
||||||
|
#[repr(C)]
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct Chunk {
|
pub struct Chunk {
|
||||||
pub rect: ChunkRect,
|
pub rect: ChunkRect,
|
||||||
@@ -53,6 +54,7 @@ impl<'a> IntoIterator for &'a Chunk {
|
|||||||
const WORLDDIM: usize = 8;
|
const WORLDDIM: usize = 8;
|
||||||
pub type WorldRow = [Chunk; WORLDDIM];
|
pub type WorldRow = [Chunk; WORLDDIM];
|
||||||
pub type WorldRect = [WorldRow; WORLDDIM];
|
pub type WorldRect = [WorldRow; WORLDDIM];
|
||||||
|
#[repr(C)]
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct World {
|
pub struct World {
|
||||||
pub rect: WorldRect,
|
pub rect: WorldRect,
|
||||||
|
|||||||
Reference in New Issue
Block a user