1 Commits
wip3 ... zzz

Author SHA1 Message Date
2aa64060b6 wip 2017-02-17 17:02:14 -08:00
12 changed files with 154 additions and 254 deletions

View File

@@ -14,13 +14,16 @@ gfx = "*"
gfx_device_gl = "*"
image = "*"
lzw = "*"
nalgebra = "0.10"
nalgebra = "*"
num-traits = "*"
openvr = { git = "https://github.com/rust-openvr/rust-openvr" }
openvr_sys = "*"
piston = "*"
piston_window = "*"
# for pose-relay
byteorder = "*"
[profile.release]
lto = true
panic = "abort"

View File

@@ -1,15 +0,0 @@
extern crate openvr_sys;
use openvr_sys::EVRInitError::*;
use openvr_sys::EVRApplicationType::*;
fn main() {
unsafe {
let mut err_arr = [EVRInitError_VRInitError_None; 16];
let err = std::mem::transmute(&mut err_arr);
let app_type = EVRApplicationType_VRApplication_Scene;
openvr_sys::VR_InitInternal(err, app_type);
};
}

View File

@@ -1,135 +0,0 @@
extern crate vrtue;
use vrtue::vr;
extern crate byteorder;
extern crate env_logger;
extern crate gfx;
#[macro_use] extern crate log;
extern crate openvr;
extern crate openvr_sys;
extern crate piston_window;
use std::env;
use std::mem::size_of;
use std::net::UdpSocket;
use std::io::Write;
use self::byteorder::{LittleEndian, WriteBytesExt};
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 args: Vec<String> = env::args().collect();
if args.len() != 2 {
println!("usage: {} <socketaddr>", args[0]);
return;
}
let udp_socket = UdpSocket::bind("0.0.0.0:0").expect("binding UDP socket");
let mut window: PistonWindow =
WindowSettings::new("Controller Pose Relay", [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();
let mut iteration = 0u64;
let mut udp_buffer = Vec::<u8>::with_capacity(size_of::<u64>() + size_of::<[[f32; 4]; 3]>());
'main: loop {
let poses = vr.poses();
vr.submit(vr::Eye::Left, &left.tex);
vr.submit(vr::Eye::Right, &right.tex);
for pose in poses.poses.iter() {
match pose.device_class() {
openvr::tracking::TrackedDeviceClass::Controller => {
udp_buffer.write_u64::<LittleEndian>(iteration).expect("buffering iter number");
for outer in pose.to_device.iter() {
for inner in outer {
udp_buffer.write_f32::<LittleEndian>(*inner).expect("buffering pose matrix element");
}
}
udp_buffer.flush().expect("flushing buffer");
udp_socket.send_to(&udp_buffer, &args[1][..]).expect("sending data");
if iteration % 90 == 0 {
println!("\nPOSE: {:?}", pose.to_device);
println!(" BUF: {:?}", udp_buffer);
}
break;
},
_ => continue
}
}
iteration += 1;
udp_buffer.clear();
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)
}
}
}
}

View File

@@ -1,6 +1,6 @@
extern crate vrtue;
use vrtue::{scenes, view, vr};
use vrtue::scene::{Event, Scene};
use vrtue::{context, scenes, view, vr};
use vrtue::engine::{Event, Scene};
extern crate env_logger;
extern crate gfx_device_gl;
@@ -12,7 +12,6 @@ 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 = match env::var("NO_VR") {
@@ -33,11 +32,14 @@ pub fn main() {
let view = view::ViewRoot::<gfx_device_gl::Device, view::ColorFormat, view::DepthFormat>
::create_view(&mut window, &mut vr);
let mut game = context::VrtueRootContext::new(&mut window.device,
&mut window.factory,
&mut aux_command);
'main:
//while let Some(_) = window.next() {
loop {
scene.update(&mut vr, &mut window.encoder);
view.draw(&mut window, &mut vr, &scene);
scene.update(&mut game, &mut vr, &mut window.encoder);
view.draw(&mut game, &mut window, &mut vr, &scene);
// handle window events
while let Some(ev) = window.poll_event() {

77
src/context.rs Normal file
View 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
}
}

30
src/engine.rs Normal file
View 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),
}

View File

@@ -1,14 +1,15 @@
#![feature(conservative_impl_trait)]
#[macro_use] extern crate gfx;
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 context;
pub mod ega;
pub mod scene;
pub mod engine;
pub mod scenes;
pub mod tile;
pub mod town;

View File

@@ -1,23 +0,0 @@
use std::time::SystemTime;
struct Message {
text: String,
end_time: SystemTime,
}
impl Message {
pub fn new() -> Message {
Message {
text: String::new(),
end_time: SystemTime::now(),
}
}
pub fn display(&mut self, text: String) {
self.text = text;
self.end_time = SystemTime::now() + 5000;
}
pub fn render(&self) {
}
}

View File

@@ -1,5 +1,5 @@
use scene;
use tile;
use context::VrtueRootContext;
use engine;
use view;
use vr;
use world as model;
@@ -54,7 +54,7 @@ gfx_defines! {
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",
tiles: gfx::TextureSampler<[f32; 4]> = "t_tiles",
pixcolor: gfx::RenderTarget<::view::ColorFormat> = "pixcolor",
depth: gfx::DepthTarget<::view::DepthFormat> = gfx::preset::depth::LESS_EQUAL_WRITE,
}
@@ -125,8 +125,6 @@ pub struct WorldScene<D: gfx::Device,
constants_buffer: gfx::handle::Buffer<D::Resources, Constants>,
constants_dirty: bool,
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>,
@@ -142,10 +140,11 @@ pub struct WorldScene<D: gfx::Device,
lng: u8,
}
impl<D: gfx::Device, F: gfx::Factory<D::Resources>> WorldScene<D, F> {
pub fn new(device: &mut D,
impl<D: gfx::Device,
F: gfx::Factory<D::Resources>> WorldScene<D, F> {
pub fn new(_device: &mut D,
factory: &mut F,
aux_command: &mut <D as gfx::Device>::CommandBuffer) -> WorldScene<D, 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) =
@@ -163,7 +162,6 @@ impl<D: gfx::Device, F: gfx::Factory<D::Resources>> WorldScene<D, F> {
constants_buffer: factory.create_constant_buffer(1),
constants_dirty: true,
locals: factory.create_constant_buffer(1),
atlas: tile::get_tiles::<_, _, view::ColorFormat>(device, factory, aux_command),
sampler: factory.create_sampler(texture::SamplerInfo::new(texture::FilterMethod::Jrd,
texture::WrapMode::Tile)),
f: PhantomData,
@@ -197,10 +195,12 @@ const ANIMDATA: [u32; 4] =
0];
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) {
use scene::Event::*;
fn event(&mut self, event: engine::Event) {
use engine::Event::*;
use vr::Event::*;
match event {
// treadmill / camera movement registration
@@ -276,6 +276,7 @@ impl<D: gfx::Device,
}
fn update(&mut self,
_game: &mut VrtueRootContext<D, F, view::ColorFormat>,
vr: &mut Option<vr::VR>,
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>) {
const NANOS_PER_MILLI: u32 = 1_000_000;
@@ -334,11 +335,12 @@ impl<D: gfx::Device,
}
fn render(&self,
_factory: &mut F,
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>,
game: &mut VrtueRootContext<D, F, view::ColorFormat>,
trans: &gfx::handle::Buffer<D::Resources, view::Trans>,
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);
@@ -347,7 +349,7 @@ impl<D: gfx::Device,
trans: trans.clone(),
constants: self.constants_buffer.clone(),
locals: self.locals.clone(),
atlas: (self.atlas.clone(), self.sampler.clone()),
tiles: (game.tiles(), self.sampler.clone()),
pixcolor: target.clone(),
depth: depth.clone(),
};

View File

@@ -1,55 +1,9 @@
use ega;
use ::std;
use std::io::Read;
use std::path::Path;
use gfx::{self, texture, CommandBuffer};
use gfx::memory::Typed;
const TILEDIM: u16 = 16;
#[repr(C)]
#[derive(Clone, Copy)]
pub struct Tile {
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_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
}
impl Tile {
pub fn as_char(&self) -> char {

View File

@@ -1,3 +1,4 @@
use engine::{GameContext, Scene};
use vr::{self, AsMatrix4, VR};
extern crate gfx_device_gl;
@@ -64,10 +65,11 @@ impl ViewRoot<gfx_device_gl::Device, ColorFormat, DepthFormat> {
}
}
pub fn draw(&self,
pub fn draw<G: GameContext>(&self,
game: &mut G,
window: &mut PistonWindow,
vr: &mut Option<vr::VR>,
scene: &::scene::Scene<gfx_device_gl::Device, gfx_device_gl::Factory>) {
scene: &Scene<G, 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();
@@ -88,11 +90,12 @@ impl ViewRoot<gfx_device_gl::Device, ColorFormat, DepthFormat> {
matrix: *(proj_mat * viewmodel_mat).as_ref() };
window.encoder.update_constant_buffer(&self.trans, &trans);
scene.render(&mut window.factory,
&mut window.encoder,
scene.render(game,
&self.trans,
&target,
&depth);
&depth,
&mut window.factory,
&mut window.encoder);
}
} else {
// If running without VR, just draw from some default projection near the scene origin
@@ -107,11 +110,12 @@ impl ViewRoot<gfx_device_gl::Device, ColorFormat, DepthFormat> {
window.encoder.update_constant_buffer(&self.trans, &trans);
}
// draw monitor window
scene.render(&mut window.factory,
&mut window.encoder,
scene.render(game,
&self.trans,
&window.output_color,
&window.output_stencil);
&window.output_stencil,
&mut window.factory,
&mut window.encoder);
window.encoder.flush(&mut window.device);
if let (&mut Some(ref mut vr),

View File

@@ -159,7 +159,7 @@ pub fn create_eyebuffer<T, D>(factory: &mut gfx_device_gl::Factory,
texture::AaMode::Single),
1, // levels
gfx::RENDER_TARGET, // bind
gfx::memory::Usage::Data, // Usage
gfx::memory::Usage::GpuOnly, // Usage
Some(<T::Channel as gfx::format::ChannelTyped>::get_channel_type()))?; // hint: format::ChannelType?
let tgt = factory.view_texture_as_render_target(&tex, 0, None)?;
let depth = factory.create_depth_stencil_view_only(size.width as texture::Size,