Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2aa64060b6 | |||
| 493563b7cc | |||
| b955d7f1b6 | |||
| 3a8c0148c8 | |||
| ddc135e09a | |||
| da0d5a26a3 | |||
| 9db44c212c | |||
| f6b0068ec2 | |||
| d71967565d | |||
| 11a16e092f | |||
| 73ac584ca4 | |||
| a66cd3ae64 | |||
| 2583de70b3 | |||
| f2a645730c | |||
| 381157c8e2 | |||
| f795c73ed8 | |||
| 8eb9ab2d63 |
@@ -13,9 +13,17 @@ gl = "*"
|
|||||||
gfx = "*"
|
gfx = "*"
|
||||||
gfx_device_gl = "*"
|
gfx_device_gl = "*"
|
||||||
image = "*"
|
image = "*"
|
||||||
|
lzw = "*"
|
||||||
nalgebra = "*"
|
nalgebra = "*"
|
||||||
num-traits = "*"
|
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 = "*"
|
||||||
|
|
||||||
|
# for pose-relay
|
||||||
|
byteorder = "*"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
lto = true
|
||||||
|
panic = "abort"
|
||||||
|
|||||||
@@ -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],
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ fn main() {
|
|||||||
let mut ega_vec = Vec::<u8>::new();
|
let mut ega_vec = Vec::<u8>::new();
|
||||||
|
|
||||||
file.read_to_end(&mut ega_vec).expect("Read EGA file");
|
file.read_to_end(&mut ega_vec).expect("Read EGA file");
|
||||||
let ega_page = ega::decode(&ega_vec, Compression::UNCOMPRESSED, Tiling::TILED(16));
|
let ega_page = ega::decode(&ega_vec, Compression::Uncompressed, Tiling::Tiled(16));
|
||||||
for (i, tilepixels) in ega_page.iter().enumerate() {
|
for (i, tilepixels) in ega_page.iter().enumerate() {
|
||||||
let out_name = format!("out/{}.png", i);
|
let out_name = format!("out/{}.png", i);
|
||||||
let out_file = std::fs::File::create(Path::new(&out_name)).expect("open out file");
|
let out_file = std::fs::File::create(Path::new(&out_name)).expect("open out file");
|
||||||
|
|||||||
@@ -1,38 +1,51 @@
|
|||||||
extern crate vrtue;
|
extern crate vrtue;
|
||||||
use vrtue::{scenes, view, vr};
|
use vrtue::{context, scenes, view, vr};
|
||||||
use vrtue::scene::{Event, Scene};
|
use vrtue::engine::{Event, Scene};
|
||||||
|
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
extern crate gfx_device_gl;
|
extern crate gfx_device_gl;
|
||||||
#[macro_use] extern crate log;
|
#[macro_use] extern crate log;
|
||||||
|
extern crate piston;
|
||||||
extern crate piston_window;
|
extern crate piston_window;
|
||||||
|
|
||||||
|
use self::piston::input::{Button, Input, Key};
|
||||||
use self::piston_window::{PistonWindow, Window, WindowSettings};
|
use self::piston_window::{PistonWindow, Window, WindowSettings};
|
||||||
|
use std::env;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
env_logger::init().expect("env logger");
|
env_logger::init().expect("env logger");
|
||||||
let mut vr = vr::VR::new().expect("VR init");
|
let mut vr = match env::var("NO_VR") {
|
||||||
|
Ok(ref no_vr) if no_vr != "0" => None,
|
||||||
|
_ => Some(vr::VR::new().expect("VR init"))
|
||||||
|
};
|
||||||
|
|
||||||
let mut window: PistonWindow =
|
let mut window: PistonWindow =
|
||||||
WindowSettings::new("Hello, Britannia!", [1024; 2])
|
WindowSettings::new("Hello, Britannia!", [1024; 2])
|
||||||
.exit_on_esc(true)
|
.exit_on_esc(true)
|
||||||
.vsync(false)
|
.vsync(vr.is_none()) // Let VR throttle framerate, if available
|
||||||
.build().expect("Building Window");
|
.build().expect("Building Window");
|
||||||
|
|
||||||
let mut scene = scenes::world::WorldScene::new(&mut window.factory, &mut window.encoder);
|
let mut aux_command = window.factory.create_command_buffer();
|
||||||
|
let mut scene = scenes::world::WorldScene::new(&mut window.device,
|
||||||
|
&mut window.factory,
|
||||||
|
&mut aux_command);
|
||||||
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);
|
||||||
|
|
||||||
|
let mut game = context::VrtueRootContext::new(&mut window.device,
|
||||||
|
&mut window.factory,
|
||||||
|
&mut aux_command);
|
||||||
'main:
|
'main:
|
||||||
//while let Some(_) = window.next() {
|
//while let Some(_) = window.next() {
|
||||||
loop {
|
loop {
|
||||||
scene.update(&mut vr, &mut window.encoder);
|
scene.update(&mut game, &mut vr, &mut window.encoder);
|
||||||
view.draw(&mut window, &mut vr, &scene);
|
view.draw(&mut game, &mut window, &mut vr, &scene);
|
||||||
|
|
||||||
// handle window events
|
// handle window events
|
||||||
while let Some(ev) = window.poll_event() {
|
while let Some(ev) = window.poll_event() {
|
||||||
match ev {
|
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)
|
_ => debug!("\t{:?}", ev)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +53,7 @@ pub fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// handle VR events
|
// handle VR events
|
||||||
while let Some(ev) = vr.poll_next_event() {
|
while let Some(ev) = vr.as_mut().and_then(|vr| vr.poll_next_event()) {
|
||||||
scene.event(Event::Vr(ev));
|
scene.event(Event::Vr(ev));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
77
src/context.rs
Normal file
77
src/context.rs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
use ega;
|
||||||
|
use engine;
|
||||||
|
|
||||||
|
use std::io::Read;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use gfx::{self, CommandBuffer};
|
||||||
|
use gfx::memory::Typed;
|
||||||
|
use gfx::texture;
|
||||||
|
|
||||||
|
const TILEDIM: u16 = 16;
|
||||||
|
|
||||||
|
pub struct VrtueRootContext<D, F, T>
|
||||||
|
where D: gfx::Device,
|
||||||
|
F: gfx::Factory<D::Resources>,
|
||||||
|
T: gfx::format::TextureFormat {
|
||||||
|
|
||||||
|
tiles: gfx::handle::ShaderResourceView<D::Resources, T::View>,
|
||||||
|
_factory: PhantomData<F>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D, F, T> engine::GameContext for VrtueRootContext<D, F, T>
|
||||||
|
where D: gfx::Device,
|
||||||
|
F: gfx::Factory<D::Resources>,
|
||||||
|
T: gfx::format::TextureFormat {}
|
||||||
|
|
||||||
|
impl<D, F, T> VrtueRootContext<D, F, T>
|
||||||
|
where D: gfx::Device,
|
||||||
|
F: gfx::Factory<D::Resources>,
|
||||||
|
T: gfx::format::TextureFormat,
|
||||||
|
T::View: Clone {
|
||||||
|
|
||||||
|
pub fn new(device: &mut D,
|
||||||
|
factory: &mut F,
|
||||||
|
command: &mut <D as gfx::Device>::CommandBuffer) -> VrtueRootContext<D, F, T> {
|
||||||
|
VrtueRootContext {
|
||||||
|
tiles: Self::make_tiles(device, factory, command),
|
||||||
|
_factory: PhantomData
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tiles(&self) -> gfx::handle::ShaderResourceView<D::Resources, T::View> {
|
||||||
|
self.tiles.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_tiles(device: &mut D,
|
||||||
|
factory: &mut F,
|
||||||
|
command: &mut <D as gfx::Device>::CommandBuffer)
|
||||||
|
-> gfx::handle::ShaderResourceView<D::Resources, T::View> {
|
||||||
|
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_immutable_u8::<T>(texture::Kind::D2Array(mipmap.dim as u16,
|
||||||
|
mipmap.dim as u16,
|
||||||
|
mipmap.len as u16,
|
||||||
|
texture::AaMode::Single),
|
||||||
|
&mipmap.slices())
|
||||||
|
.expect("create tile texture");
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut manager = gfx::handle::Manager::<D::Resources>::new();
|
||||||
|
// XXX: Find out if Textures need to be/can be fenced like Buffers,
|
||||||
|
// Seems like I should mark tex.1 as being read/written, but it's not a Buffer?
|
||||||
|
let access = gfx::pso::AccessInfo::new();
|
||||||
|
let view = manager.ref_srv(tex.1.raw());
|
||||||
|
command.generate_mipmap(*view);
|
||||||
|
device.submit(command, &access);
|
||||||
|
}
|
||||||
|
tex.1
|
||||||
|
}
|
||||||
|
}
|
||||||
273
src/ega.rs
273
src/ega.rs
@@ -1,39 +1,101 @@
|
|||||||
static EGA_PALETTE: [[u8; 4]; 16] = [[0x00, 0x00, 0x00, 0x00],
|
extern crate lzw;
|
||||||
[0x00, 0x00, 0xAA, 0x00],
|
|
||||||
[0x00, 0xAA, 0x00, 0x00],
|
use std::cell::RefCell;
|
||||||
[0x00, 0xAA, 0xAA, 0x00],
|
|
||||||
[0xAA, 0x00, 0x00, 0x00],
|
use self::lzw::BitReader;
|
||||||
[0xAA, 0x00, 0xAA, 0x00],
|
|
||||||
[0xAA, 0x55, 0x00, 0x00],
|
static EGA_PALETTE: [[u8; 4]; 16] = [[0x00, 0x00, 0x00, 0xFF],
|
||||||
[0xAA, 0xAA, 0xAA, 0x00],
|
[0x00, 0x00, 0xAA, 0xFF],
|
||||||
[0x55, 0x55, 0x55, 0x00],
|
[0x00, 0xAA, 0x00, 0xFF],
|
||||||
[0x55, 0x55, 0xFF, 0x00],
|
[0x00, 0xAA, 0xAA, 0xFF],
|
||||||
[0x55, 0xFF, 0x55, 0x00],
|
[0xAA, 0x00, 0x00, 0xFF],
|
||||||
[0x55, 0xFF, 0xFF, 0x00],
|
[0xAA, 0x00, 0xAA, 0xFF],
|
||||||
[0xFF, 0x55, 0x55, 0x00],
|
[0xAA, 0x55, 0x00, 0xFF],
|
||||||
[0xFF, 0x55, 0xFF, 0x00],
|
[0xAA, 0xAA, 0xAA, 0xFF],
|
||||||
[0xFF, 0xFF, 0x55, 0x00],
|
[0x55, 0x55, 0x55, 0xFF],
|
||||||
[0xFF, 0xFF, 0xFF, 0x00]];
|
[0x55, 0x55, 0xFF, 0xFF],
|
||||||
|
[0x55, 0xFF, 0x55, 0xFF],
|
||||||
|
[0x55, 0xFF, 0xFF, 0xFF],
|
||||||
|
[0xFF, 0x55, 0x55, 0xFF],
|
||||||
|
[0xFF, 0x55, 0xFF, 0xFF],
|
||||||
|
[0xFF, 0xFF, 0x55, 0xFF],
|
||||||
|
[0xFF, 0xFF, 0xFF, 0xFF]];
|
||||||
|
|
||||||
pub enum Compression {
|
pub enum Compression {
|
||||||
UNCOMPRESSED,
|
Uncompressed,
|
||||||
RLE,
|
Rle,
|
||||||
LZW
|
Lzw
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Tiling {
|
pub enum Tiling {
|
||||||
UNTILED,
|
Untiled,
|
||||||
TILED(u16)
|
Tiled(u16)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct EgaPage {
|
pub struct EgaPage {
|
||||||
pub data: Vec<u8>,
|
pub data: Vec<u8>,
|
||||||
pub tilesize: u16,
|
pub dim: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EgaPage {
|
impl EgaPage {
|
||||||
pub fn iter<'a>(&'a self) -> impl Iterator<Item=&'a [u8]> {
|
pub fn iter<'a>(&'a self) -> impl Iterator<Item=&'a [u8]> {
|
||||||
self.data.chunks(self.tilesize as usize)
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,20 +104,157 @@ pub fn decode<'a>(buf: &[u8], compression: Compression, tiling: Tiling)
|
|||||||
let out: Vec<u8>;
|
let out: Vec<u8>;
|
||||||
|
|
||||||
out = match compression {
|
out = match compression {
|
||||||
Compression::UNCOMPRESSED => buf.iter()
|
Compression::Uncompressed => decode_uncompressed(buf),
|
||||||
.flat_map(|tile_byte| {
|
Compression::Rle => {
|
||||||
EGA_PALETTE[(tile_byte >> 4u8 & 0xF) as usize]
|
decode_uncompressed(&decode_rle(buf))
|
||||||
.into_iter()
|
},
|
||||||
.chain(EGA_PALETTE[(tile_byte & 0xF) as usize]
|
Compression::Lzw => {
|
||||||
.into_iter())
|
let mut decoder = U4Lzw::new();
|
||||||
})
|
decode_uncompressed(&decoder.decode(buf))
|
||||||
.map(|x| *x)
|
},
|
||||||
.collect(),
|
|
||||||
_ => unimplemented!()
|
|
||||||
};
|
};
|
||||||
let tilesize = match tiling {
|
let dim = match tiling {
|
||||||
Tiling::TILED(tiledim) => 4 * tiledim * tiledim,
|
Tiling::Tiled(tiledim) => tiledim as usize,
|
||||||
Tiling::UNTILED => out.len() as u16
|
Tiling::Untiled => out.len()
|
||||||
};
|
};
|
||||||
EgaPage { data: out, tilesize: tilesize}
|
EgaPage { data: out, dim: dim}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode_uncompressed(buf: &[u8]) -> Vec<u8> {
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode_rle(bytes: &[u8]) -> Vec<u8> {
|
||||||
|
let mut out = Vec::new();
|
||||||
|
let mut iter = bytes.iter();
|
||||||
|
|
||||||
|
while let Some(cmd) = iter.next() {
|
||||||
|
match cmd {
|
||||||
|
&0x02 => {
|
||||||
|
let len = *iter.next().expect("rle missing run length") as usize;
|
||||||
|
out.extend(::std::iter::repeat(*iter.next().expect("rle missing run value")).take(len));
|
||||||
|
},
|
||||||
|
val => out.push(*val),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
struct U4Lzw {
|
||||||
|
table: Vec<RefCell<Vec<u8>>>,
|
||||||
|
load: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
const U4_CODE_SIZE: u8 = 12;
|
||||||
|
impl U4Lzw {
|
||||||
|
fn new() -> U4Lzw {
|
||||||
|
let mut out = U4Lzw {
|
||||||
|
table: vec![RefCell::new(Vec::new()); 1 << U4_CODE_SIZE],
|
||||||
|
load: 0
|
||||||
|
};
|
||||||
|
for (i, entry) in out.table.iter_mut().take(256).enumerate() {
|
||||||
|
entry.borrow_mut().push(i as u8)
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reset(&mut self) {
|
||||||
|
for i in 256..(1 << U4_CODE_SIZE) {
|
||||||
|
self.table[i].borrow_mut().clear();
|
||||||
|
}
|
||||||
|
self.load = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode(&mut self, bytes: &[u8]) -> Vec<u8> {
|
||||||
|
let mut out = Vec::new();
|
||||||
|
let mut iter = CodeIter::new(bytes);
|
||||||
|
|
||||||
|
let mut prev = iter.next().expect("empty lzw");
|
||||||
|
let mut ch = prev as u8;
|
||||||
|
out.push(ch);
|
||||||
|
|
||||||
|
for code in iter {
|
||||||
|
{
|
||||||
|
let mut new;
|
||||||
|
let mut seq = &*self.table[code as usize].borrow();
|
||||||
|
if seq.is_empty() {
|
||||||
|
new = self.table[prev as usize].borrow().clone();
|
||||||
|
new.push(ch);
|
||||||
|
seq = &new;
|
||||||
|
};
|
||||||
|
out.extend(seq.iter());
|
||||||
|
ch = seq[0];
|
||||||
|
}
|
||||||
|
let hash = self.hash(ch, prev);
|
||||||
|
{
|
||||||
|
let old = self.table[prev as usize].borrow();
|
||||||
|
let mut entry = self.table[hash].borrow_mut();
|
||||||
|
entry.extend(old.iter());
|
||||||
|
entry.push(ch);
|
||||||
|
}
|
||||||
|
prev = code;
|
||||||
|
|
||||||
|
self.load += 1;
|
||||||
|
if self.load > 0xccd { self.reset(); }
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash(&self, root: u8, code: u16) -> usize {
|
||||||
|
let hash = Self::hash_primary(root, code);
|
||||||
|
if self.table[hash].borrow().is_empty() { return hash; }
|
||||||
|
|
||||||
|
let mut hash = Self::hash_secondary(root, code);
|
||||||
|
while !self.table[hash].borrow().is_empty() {
|
||||||
|
hash = (hash + 509) & 0xfff
|
||||||
|
}
|
||||||
|
|
||||||
|
hash
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash_primary(root: u8, code: u16) -> usize {
|
||||||
|
((root as (u16) << 4) ^ code) as usize
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash_secondary(root: u8, code: u16) -> usize {
|
||||||
|
let base = (((root as (u16) << 1) + code) | 0x800) as u32;
|
||||||
|
let squared = base * base;
|
||||||
|
((squared & 0x0003ffc0) >> 6) as usize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct CodeIter<'a> {
|
||||||
|
pos: usize,
|
||||||
|
bytes: &'a [u8],
|
||||||
|
reader: lzw::MsbReader,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> CodeIter<'a> {
|
||||||
|
fn new(bytes: &[u8]) -> CodeIter {
|
||||||
|
CodeIter { pos: 0, bytes: bytes, reader: lzw::MsbReader::new() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Iterator for CodeIter<'a> {
|
||||||
|
type Item = u16;
|
||||||
|
fn next(&mut self) -> Option<u16> {
|
||||||
|
match self.reader.read_bits(&self.bytes[self.pos..], U4_CODE_SIZE) {
|
||||||
|
lzw::Bits::Some(used, code) => {
|
||||||
|
self.pos += used;
|
||||||
|
Some(code)
|
||||||
|
},
|
||||||
|
lzw::Bits::None(used) => {
|
||||||
|
self.pos += used;
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
30
src/engine.rs
Normal file
30
src/engine.rs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
use gfx;
|
||||||
|
use na;
|
||||||
|
use piston;
|
||||||
|
use view;
|
||||||
|
use vr;
|
||||||
|
|
||||||
|
pub trait GameContext {}
|
||||||
|
|
||||||
|
pub trait Scene<G: GameContext,
|
||||||
|
D: gfx::Device,
|
||||||
|
F: gfx::Factory<D::Resources>> {
|
||||||
|
fn event(&mut self, event: Event);
|
||||||
|
fn update(&mut self,
|
||||||
|
game: &mut G,
|
||||||
|
vr: &mut Option<vr::VR>, // TODO: abstract this out
|
||||||
|
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>);
|
||||||
|
fn render(&self,
|
||||||
|
game: &mut G,
|
||||||
|
trans: &gfx::handle::Buffer<D::Resources, view::Trans>,
|
||||||
|
target: &gfx::handle::RenderTargetView<D::Resources, view::ColorFormat>,
|
||||||
|
depth: &gfx::handle::DepthStencilView<D::Resources, view::DepthFormat>,
|
||||||
|
factory: &mut F,
|
||||||
|
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>);
|
||||||
|
fn origin(&self) -> na::Matrix4<f32>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Event {
|
||||||
|
Vr(vr::Event),
|
||||||
|
Piston(piston::input::Input),
|
||||||
|
}
|
||||||
@@ -2,10 +2,14 @@
|
|||||||
|
|
||||||
#[macro_use] extern crate gfx;
|
#[macro_use] extern crate gfx;
|
||||||
#[macro_use] extern crate log;
|
#[macro_use] extern crate log;
|
||||||
|
extern crate nalgebra as na;
|
||||||
|
extern crate num_traits;
|
||||||
|
extern crate piston;
|
||||||
|
|
||||||
pub mod arena;
|
pub mod arena;
|
||||||
|
pub mod context;
|
||||||
pub mod ega;
|
pub mod ega;
|
||||||
pub mod scene;
|
pub mod engine;
|
||||||
pub mod scenes;
|
pub mod scenes;
|
||||||
pub mod tile;
|
pub mod tile;
|
||||||
pub mod town;
|
pub mod town;
|
||||||
|
|||||||
12
src/scene.rs
12
src/scene.rs
@@ -1,16 +1,14 @@
|
|||||||
|
use gfx;
|
||||||
|
use na;
|
||||||
|
use piston;
|
||||||
use view;
|
use view;
|
||||||
use vr;
|
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,
|
pub trait Scene<D: gfx::Device,
|
||||||
F: gfx::Factory<D::Resources>> {
|
F: gfx::Factory<D::Resources>> {
|
||||||
fn event(&mut self, event: Event);
|
fn event(&mut self, event: Event);
|
||||||
fn update(&mut self,
|
fn update(&mut self,
|
||||||
vr: &mut vr::VR,
|
vr: &mut Option<vr::VR>, // TODO: abstract this out
|
||||||
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>);
|
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>);
|
||||||
fn render(&self,
|
fn render(&self,
|
||||||
factory: &mut F,
|
factory: &mut F,
|
||||||
@@ -24,5 +22,5 @@ pub trait Scene<D: gfx::Device,
|
|||||||
|
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
Vr(vr::Event),
|
Vr(vr::Event),
|
||||||
Piston(piston_window::Input),
|
Piston(piston::input::Input),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,21 @@
|
|||||||
#version 150
|
#version 150
|
||||||
|
|
||||||
#define MILLIS_PER_TILE 4000u
|
#define MILLIS_PER_TILE 3000u
|
||||||
|
|
||||||
in vec2 v_uv;
|
in vec2 v_uv;
|
||||||
flat in uint v_tileidx;
|
flat in uint v_tileidx;
|
||||||
|
in float v_fade;
|
||||||
out vec4 pixcolor;
|
out vec4 pixcolor;
|
||||||
uniform sampler2DArray t_tiles;
|
uniform sampler2DArray t_tiles;
|
||||||
uniform b_constants {
|
uniform b_constants {
|
||||||
uvec4 anim;
|
uvec4 anim;
|
||||||
float R1;
|
float R1;
|
||||||
float R2;
|
float R2;
|
||||||
|
float R3;
|
||||||
|
float haze;
|
||||||
|
vec4 hazecolor;
|
||||||
};
|
};
|
||||||
uniform b_locals {
|
uniform b_locals {
|
||||||
mat4 camera;
|
|
||||||
uint millis;
|
uint millis;
|
||||||
float treadmill_x;
|
float treadmill_x;
|
||||||
float treadmill_y;
|
float treadmill_y;
|
||||||
@@ -24,5 +27,6 @@ void main() {
|
|||||||
anim_uv = vec2(v_uv.x, v_uv.y + float(millis % MILLIS_PER_TILE) / MILLIS_PER_TILE);
|
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));
|
vec4 texcolor = texture(t_tiles, vec3(anim_uv.x, 1.0 - anim_uv.y, v_tileidx));
|
||||||
|
pixcolor = mix(texcolor, hazecolor, v_fade);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,23 +8,27 @@ in vec2 a_uv;
|
|||||||
in uint a_tileidx;
|
in uint a_tileidx;
|
||||||
out vec2 v_uv;
|
out vec2 v_uv;
|
||||||
flat out uint v_tileidx;
|
flat out uint v_tileidx;
|
||||||
|
out float v_fade;
|
||||||
uniform b_trans {
|
uniform b_trans {
|
||||||
|
mat4 u_viewmodel;
|
||||||
mat4 u_matrix;
|
mat4 u_matrix;
|
||||||
};
|
};
|
||||||
uniform b_constants {
|
uniform b_constants {
|
||||||
uvec4 anim;
|
uvec4 anim;
|
||||||
float R1;
|
float R1;
|
||||||
float R2;
|
float R2;
|
||||||
|
float R3;
|
||||||
|
float haze;
|
||||||
|
vec4 hazecolor;
|
||||||
};
|
};
|
||||||
uniform b_locals {
|
uniform b_locals {
|
||||||
mat4 camera;
|
|
||||||
uint millis;
|
uint millis;
|
||||||
float treadmill_x;
|
float treadmill_x;
|
||||||
float treadmill_y;
|
float treadmill_y;
|
||||||
};
|
};
|
||||||
|
|
||||||
vec3 toroid(vec2 src, float r1, float r2, float r3) {
|
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)) * cos(src.y),
|
||||||
(r1 + r2 * cos(src.x)) * sin(src.y));
|
(r1 + r2 * cos(src.x)) * sin(src.y));
|
||||||
}
|
}
|
||||||
@@ -35,8 +39,12 @@ void main() {
|
|||||||
|
|
||||||
vec2 thetaphi = vec2(TWO_PI_CIRC * (a_pos.x + treadmill_x),
|
vec2 thetaphi = vec2(TWO_PI_CIRC * (a_pos.x + treadmill_x),
|
||||||
TWO_PI_CIRC * (a_pos.y + treadmill_y));
|
TWO_PI_CIRC * (a_pos.y + treadmill_y));
|
||||||
float height = R1 * TWO_PI_CIRC;
|
float height = R2 * 4 * TWO_PI_CIRC;
|
||||||
vec3 normal = vec3(toroid(thetaphi, 0, height, height));
|
vec3 normal = vec3(toroid(thetaphi, 0, height, height)) *
|
||||||
gl_Position = u_matrix * camera *
|
vec3(R2 / R3, 1.0, 1.0);
|
||||||
vec4(toroid(thetaphi, R1, R2, R1) + a_pos.z * normal, 1.0);
|
vec4 model_pos = vec4(toroid(thetaphi, R1, R2, R3) + a_pos.z * normal, 1.0);
|
||||||
|
gl_Position = u_matrix * model_pos;
|
||||||
|
|
||||||
|
vec4 view_pos = u_viewmodel * model_pos;
|
||||||
|
v_fade = min(1.0, length(view_pos.xyz) / R1 / 2 * haze);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,31 +1,31 @@
|
|||||||
use scene;
|
use context::VrtueRootContext;
|
||||||
use tile;
|
use engine;
|
||||||
use view;
|
use view;
|
||||||
use vr;
|
use vr;
|
||||||
use world as model;
|
use world as model;
|
||||||
use world::HasMap;
|
use world::HasMap;
|
||||||
|
|
||||||
extern crate gfx;
|
extern crate memmap;
|
||||||
extern crate nalgebra as na;
|
|
||||||
extern crate num_traits;
|
|
||||||
extern crate openvr_sys;
|
|
||||||
extern crate piston_window;
|
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
|
|
||||||
use gfx::tex;
|
use gfx::{self, texture};
|
||||||
use gfx::traits::FactoryExt;
|
use gfx::traits::FactoryExt;
|
||||||
use self::na::ToHomogeneous;
|
use na::{self, ToHomogeneous};
|
||||||
use self::num_traits::identities::One;
|
use num_traits::identities::One;
|
||||||
|
use 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 R1: f32 = 256.0;
|
||||||
const R2: f32 = 64.0;
|
const R2: f32 = 64.0;
|
||||||
|
const R3: f32 = 128.0;
|
||||||
|
|
||||||
|
//const SKY_COLOR: [f32; 4] = [0.15, 0.15, 0.75, 1.0];
|
||||||
|
const SKY_COLOR: [f32; 4] = [0.005, 0.005, 0.01, 1.0];
|
||||||
|
|
||||||
gfx_defines! {
|
gfx_defines! {
|
||||||
vertex Vertex {
|
vertex Vertex {
|
||||||
@@ -38,10 +38,12 @@ gfx_defines! {
|
|||||||
anim: [u32; 4] = "anim",
|
anim: [u32; 4] = "anim",
|
||||||
r1: f32 = "R1",
|
r1: f32 = "R1",
|
||||||
r2: f32 = "R2",
|
r2: f32 = "R2",
|
||||||
|
r3: f32 = "R3",
|
||||||
|
haze: f32 = "haze",
|
||||||
|
hazecolor: [f32; 4] = "hazecolor",
|
||||||
}
|
}
|
||||||
|
|
||||||
constant Locals {
|
constant Locals {
|
||||||
camera: [[f32; 4]; 4] = "camera",
|
|
||||||
millis: u32 = "millis",
|
millis: u32 = "millis",
|
||||||
treadmill_x: f32 = "treadmill_x",
|
treadmill_x: f32 = "treadmill_x",
|
||||||
treadmill_y: f32 = "treadmill_y",
|
treadmill_y: f32 = "treadmill_y",
|
||||||
@@ -52,7 +54,7 @@ gfx_defines! {
|
|||||||
trans: gfx::ConstantBuffer<::view::Trans> = "b_trans",
|
trans: gfx::ConstantBuffer<::view::Trans> = "b_trans",
|
||||||
constants: gfx::ConstantBuffer<Constants> = "b_constants",
|
constants: gfx::ConstantBuffer<Constants> = "b_constants",
|
||||||
locals: gfx::ConstantBuffer<Locals> = "b_locals",
|
locals: gfx::ConstantBuffer<Locals> = "b_locals",
|
||||||
atlas: gfx::TextureSampler<[f32; 4]> = "t_tiles",
|
tiles: gfx::TextureSampler<[f32; 4]> = "t_tiles",
|
||||||
pixcolor: gfx::RenderTarget<::view::ColorFormat> = "pixcolor",
|
pixcolor: gfx::RenderTarget<::view::ColorFormat> = "pixcolor",
|
||||||
depth: gfx::DepthTarget<::view::DepthFormat> = gfx::preset::depth::LESS_EQUAL_WRITE,
|
depth: gfx::DepthTarget<::view::DepthFormat> = gfx::preset::depth::LESS_EQUAL_WRITE,
|
||||||
}
|
}
|
||||||
@@ -76,28 +78,27 @@ fn get_model(world: &model::World) -> (Vec<Vertex>, Vec<u32>) {
|
|||||||
10 | 11 | 12 => 1.0,
|
10 | 11 | 12 => 1.0,
|
||||||
_ => 0.0,
|
_ => 0.0,
|
||||||
};
|
};
|
||||||
let rf = (((r + 90) % 256) as i16 - 128) as f32;
|
let (rf, cf) = (r as f32, c as f32);
|
||||||
let cf = (((c + 144) % 256) as i16 - 128) as f32;
|
|
||||||
if alt == 0.0 {
|
if alt == 0.0 {
|
||||||
verticies.extend_from_slice(
|
verticies.extend_from_slice(
|
||||||
&[Vertex { pos: [ cf + 0., -rf - 1., 0. ], uv: [0., 0.], 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 + 1., 0. ], uv: [1., 0.], tileidx: tileidx },
|
||||||
Vertex { pos: [ cf + 1., -rf - 0., 0. ], uv: [1., 1.], 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., 0. ], uv: [0., 1.], tileidx: tileidx },]);
|
||||||
indicies.extend_from_slice(
|
indicies.extend_from_slice(
|
||||||
&[ v + 0, v + 1, v + 2,
|
&[ v + 0, v + 1, v + 2,
|
||||||
v + 2, v + 3, v + 0 ]);
|
v + 2, v + 3, v + 0 ]);
|
||||||
v += 4;
|
v += 4;
|
||||||
} else {
|
} else {
|
||||||
verticies.extend_from_slice(
|
verticies.extend_from_slice(
|
||||||
&[Vertex { pos: [ cf + 0., -rf - 1., 0. ], uv: [0., 0.], 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 + 1., 0. ], uv: [1., 0.], tileidx: tileidx },
|
||||||
Vertex { pos: [ cf + 1., -rf - 0., 0. ], uv: [1., 1.], 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., 0. ], uv: [0., 1.], tileidx: tileidx },
|
||||||
Vertex { pos: [ cf + 0., -rf, 0. ], uv: [0., 0.], 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, 0. ], uv: [1., 0.], tileidx: tileidx },
|
||||||
Vertex { pos: [ cf + 1., -rf, alt ], uv: [1., 1.], 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, alt ], uv: [0., 1.], tileidx: tileidx },]);
|
||||||
indicies.extend_from_slice(
|
indicies.extend_from_slice(
|
||||||
&[ v + 0, v + 1, v + 2,
|
&[ v + 0, v + 1, v + 2,
|
||||||
v + 2, v + 3, v + 0,
|
v + 2, v + 3, v + 0,
|
||||||
@@ -112,18 +113,18 @@ fn get_model(world: &model::World) -> (Vec<Vertex>, Vec<u32>) {
|
|||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||||
enum TrackMode {
|
enum TrackMode {
|
||||||
TOUCH,
|
Touch,
|
||||||
PRESS
|
Press
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WorldScene<D: gfx::Device,
|
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>,
|
constants: Constants,
|
||||||
|
constants_buffer: gfx::handle::Buffer<D::Resources, Constants>,
|
||||||
|
constants_dirty: bool,
|
||||||
locals: gfx::handle::Buffer<D::Resources, Locals>,
|
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>,
|
sampler: gfx::handle::Sampler<D::Resources>,
|
||||||
f: PhantomData<F>,
|
f: PhantomData<F>,
|
||||||
|
|
||||||
@@ -132,32 +133,37 @@ pub struct WorldScene<D: gfx::Device,
|
|||||||
|
|
||||||
start_time: SystemTime,
|
start_time: SystemTime,
|
||||||
treadmills: (f32, f32),
|
treadmills: (f32, f32),
|
||||||
pads: BTreeMap<u32, (TrackMode, Option<openvr_sys::VRControllerState_t>)>,
|
pads: BTreeMap<u32, (TrackMode, Option<vr::VRControllerState_t>)>,
|
||||||
|
|
||||||
|
_worldmap: model::World,
|
||||||
|
lat: u8,
|
||||||
|
lng: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D: gfx::Device, F: gfx::Factory<D::Resources>> WorldScene<D, F> {
|
impl<D: gfx::Device,
|
||||||
pub fn new(factory: &mut F,
|
F: gfx::Factory<D::Resources>> WorldScene<D, F> {
|
||||||
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>) -> WorldScene<D, F> {
|
pub fn new(_device: &mut D,
|
||||||
let (model, model_idx) = get_model(&get_data_model());
|
factory: &mut F,
|
||||||
|
_aux_command: &mut <D as gfx::Device>::CommandBuffer) -> WorldScene<D, F> {
|
||||||
|
let worldmap = get_data_model();
|
||||||
|
let (model, model_idx) = get_model(&worldmap);
|
||||||
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,
|
|
||||||
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,
|
constants: Constants { anim: ANIMDATA,
|
||||||
|
r1: R1, r2: R2, r3: R3,
|
||||||
|
haze: 1.0/2.0f32.sqrt(), hazecolor: SKY_COLOR },
|
||||||
|
constants_buffer: factory.create_constant_buffer(1),
|
||||||
|
constants_dirty: true,
|
||||||
locals: factory.create_constant_buffer(1),
|
locals: factory.create_constant_buffer(1),
|
||||||
atlas: tile::get_tiles::<_, _, view::ColorFormat>(factory),
|
sampler: factory.create_sampler(texture::SamplerInfo::new(texture::FilterMethod::Jrd,
|
||||||
sampler: factory.create_sampler(tex::SamplerInfo::new(tex::FilterMethod::Scale,
|
texture::WrapMode::Tile)),
|
||||||
tex::WrapMode::Tile)),
|
|
||||||
f: PhantomData,
|
f: PhantomData,
|
||||||
|
|
||||||
vbuf: vertex_buffer,
|
vbuf: vertex_buffer,
|
||||||
@@ -165,8 +171,21 @@ impl<D: gfx::Device, F: gfx::Factory<D::Resources>> WorldScene<D, F> {
|
|||||||
start_time: SystemTime::now(),
|
start_time: SystemTime::now(),
|
||||||
treadmills: (0.0, 0.0),
|
treadmills: (0.0, 0.0),
|
||||||
pads: BTreeMap::new(),
|
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] =
|
const ANIMDATA: [u32; 4] =
|
||||||
@@ -176,30 +195,89 @@ const ANIMDATA: [u32; 4] =
|
|||||||
0];
|
0];
|
||||||
|
|
||||||
impl<D: gfx::Device,
|
impl<D: gfx::Device,
|
||||||
F: gfx::Factory<D::Resources>> scene::Scene<D, F> for WorldScene<D, F> {
|
F: gfx::Factory<D::Resources>>
|
||||||
|
engine::Scene<VrtueRootContext<D, F, view::ColorFormat>, D, F>
|
||||||
|
for WorldScene<D, F> {
|
||||||
|
|
||||||
fn event(&mut self, event: scene::Event) {
|
fn event(&mut self, event: engine::Event) {
|
||||||
use scene::Event::*;
|
use engine::Event::*;
|
||||||
use vr::Event::*;
|
use vr::Event::*;
|
||||||
match event {
|
match event {
|
||||||
Vr(Touch { dev_idx, controller }) => {
|
// treadmill / camera movement registration
|
||||||
self.pads.insert(dev_idx, (TrackMode::TOUCH, None));
|
Vr(Touch { dev_idx, .. }) => {
|
||||||
|
self.pads.insert(dev_idx, (TrackMode::Touch, None));
|
||||||
},
|
},
|
||||||
Vr(Press { dev_idx, controller }) => {
|
Vr(Press { dev_idx, .. }) => {
|
||||||
self.pads.insert(dev_idx, (TrackMode::PRESS, None));
|
self.pads.insert(dev_idx, (TrackMode::Press, None));
|
||||||
},
|
},
|
||||||
Vr(Unpress { dev_idx, controller }) => {
|
Vr(Unpress { dev_idx, .. }) => {
|
||||||
self.pads.insert(dev_idx, (TrackMode::TOUCH, None));
|
self.pads.insert(dev_idx, (TrackMode::Touch, None));
|
||||||
},
|
},
|
||||||
Vr(Untouch { dev_idx, controller }) => {
|
Vr(Untouch { dev_idx, .. }) => {
|
||||||
self.pads.remove(&dev_idx);
|
self.pads.remove(&dev_idx);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// treadmill / camera reset
|
||||||
|
Piston(Input::Press(Button::Keyboard(Key::Backspace))) => {
|
||||||
|
self.treadmills = (0.0, 0.0);
|
||||||
|
},
|
||||||
|
Piston(Input::Press(Button::Keyboard(Key::D0))) => {
|
||||||
|
self.camera = na::Matrix4::one();
|
||||||
|
},
|
||||||
|
|
||||||
|
// player movement
|
||||||
|
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);
|
||||||
|
},
|
||||||
|
|
||||||
|
// scale adjustment
|
||||||
|
Piston(Input::Press(Button::Keyboard(Key::Q))) => {
|
||||||
|
self.constants = Constants { r1: R1 / 2.0, r2: R2 / 2.0, r3: R3 / 2.0, ..self.constants };
|
||||||
|
self.constants_dirty = true;
|
||||||
|
},
|
||||||
|
Piston(Input::Press(Button::Keyboard(Key::D1))) => {
|
||||||
|
self.constants = Constants { r1: R1, r2: R2, r3: R3, ..self.constants };
|
||||||
|
self.constants_dirty = true;
|
||||||
|
},
|
||||||
|
Piston(Input::Press(Button::Keyboard(Key::D2))) => {
|
||||||
|
self.constants = Constants { r1: R1 * 2.0, r2: R2 * 2.0, r3: R3 * 2.0, ..self.constants };
|
||||||
|
self.constants_dirty = true;
|
||||||
|
},
|
||||||
|
Piston(Input::Press(Button::Keyboard(Key::D3))) => {
|
||||||
|
self.constants = Constants { r1: R1 * 4.0, r2: R2 * 4.0, r3: R3 * 4.0, ..self.constants };
|
||||||
|
self.constants_dirty = true;
|
||||||
|
},
|
||||||
|
Piston(Input::Press(Button::Keyboard(Key::D4))) => {
|
||||||
|
self.constants = Constants { r1: R1 * 16.0, r2: R2 * 16.0, r3: R3 * 16.0, ..self.constants };
|
||||||
|
self.constants_dirty = true;
|
||||||
|
},
|
||||||
|
|
||||||
|
Piston(Input::Press(Button::Keyboard(Key::H))) => {
|
||||||
|
self.constants = Constants { haze: self.constants.haze * 2.0f32.sqrt().sqrt(), ..self.constants };
|
||||||
|
println!("haze: {}", self.constants.haze);
|
||||||
|
self.constants_dirty = true;
|
||||||
|
},
|
||||||
|
Piston(Input::Press(Button::Keyboard(Key::N))) => {
|
||||||
|
self.constants = Constants { haze: self.constants.haze / 2.0f32.sqrt().sqrt(), ..self.constants };
|
||||||
|
println!("haze: {}", self.constants.haze);
|
||||||
|
self.constants_dirty = true;
|
||||||
|
},
|
||||||
_ => ()
|
_ => ()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self,
|
fn update(&mut self,
|
||||||
vr: &mut vr::VR,
|
_game: &mut VrtueRootContext<D, F, view::ColorFormat>,
|
||||||
|
vr: &mut Option<vr::VR>,
|
||||||
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>) {
|
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>) {
|
||||||
const NANOS_PER_MILLI: u32 = 1_000_000;
|
const NANOS_PER_MILLI: u32 = 1_000_000;
|
||||||
const MILLIS_PER_SEC: u64 = 1_000;
|
const MILLIS_PER_SEC: u64 = 1_000;
|
||||||
@@ -208,10 +286,10 @@ impl<D: gfx::Device,
|
|||||||
|
|
||||||
for (pad, track) in self.pads.iter_mut() {
|
for (pad, track) in self.pads.iter_mut() {
|
||||||
let mode = track.0;
|
let mode = track.0;
|
||||||
if let Some(state) = vr.get_controller_state(*pad) {
|
if let Some(state) = vr.as_ref().and_then(|vr| vr.get_controller_state(*pad)) {
|
||||||
if let Some(old_state) = track.1 {
|
if let Some(old_state) = track.1 {
|
||||||
match mode {
|
match mode {
|
||||||
TrackMode::TOUCH => {
|
TrackMode::Touch => {
|
||||||
const THRESHOLD: f32 = 0.005;
|
const THRESHOLD: f32 = 0.005;
|
||||||
const SCALE: f32 = 32.0;
|
const SCALE: f32 = 32.0;
|
||||||
let xdiff = state.rAxis[0].x - old_state.rAxis[0].x;
|
let xdiff = state.rAxis[0].x - old_state.rAxis[0].x;
|
||||||
@@ -219,9 +297,9 @@ impl<D: gfx::Device,
|
|||||||
if xdiff.abs() > THRESHOLD { self.treadmills.0 += SCALE * xdiff; }
|
if xdiff.abs() > THRESHOLD { self.treadmills.0 += SCALE * xdiff; }
|
||||||
if ydiff.abs() > THRESHOLD { self.treadmills.1 += SCALE * ydiff; }
|
if ydiff.abs() > THRESHOLD { self.treadmills.1 += SCALE * ydiff; }
|
||||||
},
|
},
|
||||||
TrackMode::PRESS => {
|
TrackMode::Press => {
|
||||||
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;
|
let speed = R2 * 0.005;
|
||||||
if state.rAxis[0].x > 0.5 {
|
if state.rAxis[0].x > 0.5 {
|
||||||
self.camera = na::Similarity3::new(na::Vector3::new(-speed, 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;
|
||||||
@@ -246,25 +324,32 @@ impl<D: gfx::Device,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
encoder.update_constant_buffer(&self.locals, &Locals { camera: *(self.camera).as_ref(),
|
if self.constants_dirty {
|
||||||
millis: millis,
|
self.constants_dirty = false;
|
||||||
|
encoder.update_constant_buffer(&self.constants_buffer, &self.constants);
|
||||||
|
}
|
||||||
|
|
||||||
|
encoder.update_constant_buffer(&self.locals, &Locals { millis: millis,
|
||||||
treadmill_x: self.treadmills.0,
|
treadmill_x: self.treadmills.0,
|
||||||
treadmill_y: self.treadmills.1 });
|
treadmill_y: self.treadmills.1 });
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render(&self,
|
fn render(&self,
|
||||||
_factory: &mut F,
|
game: &mut VrtueRootContext<D, F, view::ColorFormat>,
|
||||||
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>,
|
|
||||||
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>,
|
||||||
|
_factory: &mut F,
|
||||||
|
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>) {
|
||||||
|
|
||||||
|
encoder.clear(&target, SKY_COLOR);
|
||||||
|
encoder.clear_depth(&depth, 1.0);
|
||||||
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(),
|
constants: self.constants_buffer.clone(),
|
||||||
locals: self.locals.clone(),
|
locals: self.locals.clone(),
|
||||||
atlas: (self.atlas.clone(), self.sampler.clone()),
|
tiles: (game.tiles(), self.sampler.clone()),
|
||||||
pixcolor: target.clone(),
|
pixcolor: target.clone(),
|
||||||
depth: depth.clone(),
|
depth: depth.clone(),
|
||||||
};
|
};
|
||||||
@@ -272,13 +357,18 @@ impl<D: gfx::Device,
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn origin(&self) -> na::Matrix4<f32> {
|
fn origin(&self) -> na::Matrix4<f32> {
|
||||||
na::Similarity3::new(na::Vector3::new(0.0, R1 - R2, 0.0),
|
let (r1, r2, r3) = (self.constants.r1, self.constants.r2, self.constants.r3);
|
||||||
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); // center of tile
|
||||||
|
let eye = Self::toroid((x, y), r1, r2, r3);
|
||||||
|
let looktgt = Self::toroid((x, y - 1.0), r1, r2, r3); // look ahead = north
|
||||||
|
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 {
|
fn get_data_model() -> model::World {
|
||||||
use self::memmap::{Mmap, Protection};
|
use self::memmap::{Mmap, Protection};
|
||||||
use std::mem::transmute;
|
use std::mem::transmute;
|
||||||
|
|||||||
30
src/tile.rs
30
src/tile.rs
@@ -1,39 +1,9 @@
|
|||||||
extern crate gfx;
|
|
||||||
|
|
||||||
use ega;
|
|
||||||
|
|
||||||
use ::std;
|
|
||||||
use std::io::Read;
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use self::gfx::tex;
|
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct Tile {
|
pub struct Tile {
|
||||||
pub val: u8,
|
pub val: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_tiles<R, F, T>(factory: &mut F) -> (//gfx::handle::Texture<R, T::Surface>,
|
|
||||||
gfx::handle::ShaderResourceView<R, T::View>)
|
|
||||||
where R: gfx::Resources,
|
|
||||||
F: gfx::Factory<R>,
|
|
||||||
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(16));
|
|
||||||
let tiles: Vec<&[u8]> = ega_page.iter().collect();
|
|
||||||
let tex = factory.create_texture_const_u8::<T>(tex::Kind::D2Array(16, 16, 256,
|
|
||||||
tex::AaMode::Single),
|
|
||||||
&tiles)
|
|
||||||
.expect("create tile texture");
|
|
||||||
tex.1
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
impl Tile {
|
impl Tile {
|
||||||
pub fn as_char(&self) -> char {
|
pub fn as_char(&self) -> char {
|
||||||
|
|||||||
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
128
src/view.rs
@@ -1,25 +1,24 @@
|
|||||||
use vr;
|
use engine::{GameContext, Scene};
|
||||||
use vr::{AsMatrix4, VR};
|
use vr::{self, AsMatrix4, VR};
|
||||||
|
|
||||||
extern crate gfx_device_gl;
|
extern crate gfx_device_gl;
|
||||||
extern crate nalgebra as na;
|
|
||||||
extern crate num_traits;
|
|
||||||
extern crate piston_window;
|
extern crate piston_window;
|
||||||
|
|
||||||
use gfx;
|
use gfx;
|
||||||
use gfx::Device;
|
use gfx::Device;
|
||||||
use gfx::traits::FactoryExt;
|
use gfx::traits::FactoryExt;
|
||||||
use self::na::Inverse;
|
use na::{self, Inverse, ToHomogeneous};
|
||||||
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 = 1000.0;
|
const FAR: f32 = 3072.0;
|
||||||
|
|
||||||
gfx_constant_struct! {
|
gfx_constant_struct! {
|
||||||
Trans {
|
Trans {
|
||||||
|
viewmodel: [[f32; 4]; 4] = "u_viewmodel",
|
||||||
matrix: [[f32; 4]; 4] = "u_matrix",
|
matrix: [[f32; 4]; 4] = "u_matrix",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,75 +28,102 @@ pub struct ViewRoot<Dev, T, D>
|
|||||||
T: gfx::format::RenderFormat + gfx::format::TextureFormat,
|
T: gfx::format::RenderFormat + gfx::format::TextureFormat,
|
||||||
D: gfx::format::DepthFormat + gfx::format::TextureFormat {
|
D: gfx::format::DepthFormat + gfx::format::TextureFormat {
|
||||||
|
|
||||||
left: vr::EyeBuffer<T, D>,
|
left: Option<vr::EyeBuffer<T, D>>,
|
||||||
right: vr::EyeBuffer<T, D>,
|
right: Option<vr::EyeBuffer<T, D>>,
|
||||||
trans: gfx::handle::Buffer<Dev::Resources, Trans>,
|
trans: gfx::handle::Buffer<Dev::Resources, Trans>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ViewRoot<gfx_device_gl::Device, ColorFormat, DepthFormat> {
|
impl ViewRoot<gfx_device_gl::Device, ColorFormat, DepthFormat> {
|
||||||
pub fn create_view(window: &mut PistonWindow, vr: &VR)
|
pub fn create_view(window: &mut PistonWindow, vr: &Option<VR>)
|
||||||
-> ViewRoot<gfx_device_gl::Device, ColorFormat, DepthFormat> {
|
-> 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.recommended_render_target_size();
|
let render_size = vr::Size { width: render_size.width * 100 / 100,
|
||||||
|
height: render_size.height * 100 / 100 };
|
||||||
|
|
||||||
let render_size = vr::Size { width: render_size.width * 220 / 100,
|
let left = vr::create_eyebuffer(&mut window.factory, render_size)
|
||||||
height: render_size.height * 220 / 100 };
|
.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);
|
||||||
|
|
||||||
let left = vr::create_eyebuffer(&mut window.factory, render_size)
|
window.window.swap_buffers(); // To contain setup calls to Frame 0 in apitrace
|
||||||
.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),
|
||||||
ViewRoot::<gfx_device_gl::Device, ColorFormat, DepthFormat> {
|
right: Some(right),
|
||||||
left: left,
|
trans: trans,
|
||||||
right: right,
|
}
|
||||||
trans: trans.clone(),
|
} 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,
|
pub fn draw<G: GameContext>(&self,
|
||||||
window: &mut PistonWindow,
|
game: &mut G,
|
||||||
vr: &mut vr::VR,
|
window: &mut PistonWindow,
|
||||||
scene: &::scene::Scene<gfx_device_gl::Device, gfx_device_gl::Factory>) {
|
vr: &mut Option<vr::VR>,
|
||||||
// Get the current sensor state
|
scene: &Scene<G, gfx_device_gl::Device, gfx_device_gl::Factory>) {
|
||||||
let poses = vr.poses();
|
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();
|
let mut hmd_mat = poses.poses[0].to_device.as_matrix4();
|
||||||
hmd_mat.inverse_mut();
|
hmd_mat.inverse_mut();
|
||||||
|
|
||||||
for &(eye, buffers) in [(vr::Eye::Left, &self.left),
|
for &(eye, buffers) in [(vr::Eye::Left, &self.left),
|
||||||
(vr::Eye::Right, &self.right)].into_iter() {
|
(vr::Eye::Right, &self.right)].into_iter() {
|
||||||
window.encoder.clear(&buffers.target, [0.005, 0.005, 0.01, 1.0]);
|
let target = &buffers.as_ref().expect("vr color buffer").target;
|
||||||
|
let depth = &buffers.as_ref().expect("vr depth buffer").depth;
|
||||||
|
|
||||||
window.encoder.clear_depth(&buffers.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 viewmodel_mat = eye_mat * hmd_mat * scene_mat;
|
||||||
|
let trans = Trans { viewmodel: *viewmodel_mat.as_ref(),
|
||||||
|
matrix: *(proj_mat * viewmodel_mat).as_ref() };
|
||||||
|
window.encoder.update_constant_buffer(&self.trans, &trans);
|
||||||
|
|
||||||
let proj_mat = vr.projection_matrix(eye, NEAR, FAR);
|
scene.render(game,
|
||||||
let eye_mat = vr.head_to_eye_transform(eye);
|
&self.trans,
|
||||||
|
&target,
|
||||||
|
&depth,
|
||||||
|
&mut window.factory,
|
||||||
|
&mut window.encoder);
|
||||||
|
}
|
||||||
|
} 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 scene_mat = scene.origin();
|
||||||
let trans = Trans { matrix: *(proj_mat * eye_mat * hmd_mat * scene_mat).as_ref() };
|
let viewmodel_mat = head_mat * scene_mat;
|
||||||
|
let trans = Trans { viewmodel: *viewmodel_mat.as_ref(),
|
||||||
|
matrix: *(proj_mat * viewmodel_mat).as_ref() };
|
||||||
window.encoder.update_constant_buffer(&self.trans, &trans);
|
window.encoder.update_constant_buffer(&self.trans, &trans);
|
||||||
|
|
||||||
scene.render(&mut window.factory,
|
|
||||||
&mut window.encoder,
|
|
||||||
&self.trans,
|
|
||||||
&buffers.target,
|
|
||||||
&buffers.depth);
|
|
||||||
}
|
}
|
||||||
// draw monitor window
|
// draw monitor window
|
||||||
window.encoder.clear(&window.output_color, [0.005, 0.005, 0.01, 1.0]);
|
scene.render(game,
|
||||||
window.encoder.clear_depth(&window.output_stencil, 1.0);
|
|
||||||
scene.render(&mut window.factory,
|
|
||||||
&mut window.encoder,
|
|
||||||
&self.trans,
|
&self.trans,
|
||||||
&window.output_color,
|
&window.output_color,
|
||||||
&window.output_stencil);
|
&window.output_stencil,
|
||||||
|
&mut window.factory,
|
||||||
|
&mut window.encoder);
|
||||||
|
|
||||||
window.encoder.flush(&mut window.device);
|
window.encoder.flush(&mut window.device);
|
||||||
vr.submit(vr::Eye::Left, &self.left.tex);
|
if let (&mut Some(ref mut vr),
|
||||||
vr.submit(vr::Eye::Right, &self.right.tex);
|
&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.window.swap_buffers();
|
||||||
window.device.cleanup();
|
window.device.cleanup();
|
||||||
}
|
}
|
||||||
|
|||||||
39
src/vr.rs
39
src/vr.rs
@@ -1,19 +1,18 @@
|
|||||||
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;
|
||||||
|
|
||||||
pub use self::vr::Eye;
|
pub use self::vr::Eye;
|
||||||
pub use self::vr::common::Size;
|
pub use self::vr::common::Size;
|
||||||
pub use self::vr::tracking::{TrackedDeviceClass, TrackedDevicePoses};
|
pub use self::openvr_sys::VRControllerState_t;
|
||||||
|
|
||||||
|
use gfx::{self, texture, Factory};
|
||||||
|
use gfx::memory::Typed;
|
||||||
|
use na::{self, Inverse};
|
||||||
|
use num_traits::identities::Zero;
|
||||||
|
use num_traits::identities::One;
|
||||||
|
|
||||||
use self::gfx::{tex, Factory, Typed};
|
|
||||||
use self::gfx_device_gl::Resources as GLResources;
|
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};
|
use self::openvr_sys::{VREvent_Controller_t, VREvent_t};
|
||||||
|
|
||||||
pub struct VR {
|
pub struct VR {
|
||||||
@@ -34,8 +33,8 @@ pub enum Event {
|
|||||||
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: vr::init()?,
|
||||||
compositor: try!(vr::compositor()),
|
compositor: vr::compositor()?,
|
||||||
gfx_handles: gfx::handle::Manager::new(),
|
gfx_handles: gfx::handle::Manager::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -154,14 +153,16 @@ pub fn create_eyebuffer<T, D>(factory: &mut gfx_device_gl::Factory,
|
|||||||
-> Result<EyeBuffer<T, D>, gfx::CombinedError>
|
-> Result<EyeBuffer<T, D>, gfx::CombinedError>
|
||||||
where T: gfx::format::RenderFormat + gfx::format::TextureFormat,
|
where T: gfx::format::RenderFormat + gfx::format::TextureFormat,
|
||||||
D: gfx::format::DepthFormat + gfx::format::TextureFormat {
|
D: gfx::format::DepthFormat + gfx::format::TextureFormat {
|
||||||
let tex = try!(factory.create_texture(
|
let tex = factory
|
||||||
tex::Kind::D2(size.width as tex::Size, size.height as tex::Size, tex::AaMode::Single),
|
.create_texture(texture::Kind::D2(size.width as texture::Size,
|
||||||
1, // levels
|
size.height as texture::Size,
|
||||||
gfx::RENDER_TARGET, // bind
|
texture::AaMode::Single),
|
||||||
gfx::Usage::GpuOnly, // Usage
|
1, // levels
|
||||||
Some(<T::Channel as gfx::format::ChannelTyped>::get_channel_type()))); // hint: format::ChannelType?
|
gfx::RENDER_TARGET, // bind
|
||||||
let tgt = try!(factory.view_texture_as_render_target(&tex, 0, None));
|
gfx::memory::Usage::GpuOnly, // Usage
|
||||||
let depth = try!(factory.create_depth_stencil_view_only(size.width as tex::Size,
|
Some(<T::Channel as gfx::format::ChannelTyped>::get_channel_type()))?; // hint: format::ChannelType?
|
||||||
size.height as tex::Size));
|
let tgt = factory.view_texture_as_render_target(&tex, 0, None)?;
|
||||||
|
let depth = factory.create_depth_stencil_view_only(size.width as texture::Size,
|
||||||
|
size.height as texture::Size)?;
|
||||||
Ok(EyeBuffer { tex: tex, target: tgt, depth: depth })
|
Ok(EyeBuffer { tex: tex, target: tgt, depth: depth })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
extern crate core;
|
|
||||||
extern crate itertools;
|
extern crate itertools;
|
||||||
|
|
||||||
use self::itertools::Itertools;
|
use self::itertools::Itertools;
|
||||||
|
|||||||
Reference in New Issue
Block a user