This commit is contained in:
2017-02-17 17:02:14 -08:00
parent 493563b7cc
commit 2aa64060b6
8 changed files with 151 additions and 78 deletions

View File

@@ -21,6 +21,9 @@ openvr_sys = "*"
piston = "*"
piston_window = "*"
# for pose-relay
byteorder = "*"
[profile.release]
lto = true
panic = "abort"

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

@@ -7,8 +7,9 @@ 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,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,
window: &mut PistonWindow,
vr: &mut Option<vr::VR>,
scene: &::scene::Scene<gfx_device_gl::Device, gfx_device_gl::Factory>) {
pub fn draw<G: GameContext>(&self,
game: &mut G,
window: &mut PistonWindow,
vr: &mut Option<vr::VR>,
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),