Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2aa64060b6 |
@@ -21,6 +21,9 @@ openvr_sys = "*"
|
|||||||
piston = "*"
|
piston = "*"
|
||||||
piston_window = "*"
|
piston_window = "*"
|
||||||
|
|
||||||
|
# for pose-relay
|
||||||
|
byteorder = "*"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = true
|
lto = true
|
||||||
panic = "abort"
|
panic = "abort"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
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;
|
||||||
@@ -12,7 +12,6 @@ use self::piston::input::{Button, Input, Key};
|
|||||||
use self::piston_window::{PistonWindow, Window, WindowSettings};
|
use self::piston_window::{PistonWindow, Window, WindowSettings};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
env_logger::init().expect("env logger");
|
env_logger::init().expect("env logger");
|
||||||
let mut vr = match env::var("NO_VR") {
|
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>
|
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() {
|
||||||
|
|||||||
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
|
||||||
|
}
|
||||||
|
}
|
||||||
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),
|
||||||
|
}
|
||||||
@@ -7,8 +7,9 @@ extern crate num_traits;
|
|||||||
extern crate piston;
|
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;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
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;
|
||||||
@@ -54,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,
|
||||||
}
|
}
|
||||||
@@ -125,8 +125,6 @@ pub struct WorldScene<D: gfx::Device,
|
|||||||
constants_buffer: gfx::handle::Buffer<D::Resources, Constants>,
|
constants_buffer: gfx::handle::Buffer<D::Resources, Constants>,
|
||||||
constants_dirty: bool,
|
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>,
|
||||||
|
|
||||||
@@ -142,10 +140,11 @@ pub struct WorldScene<D: gfx::Device,
|
|||||||
lng: u8,
|
lng: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D: gfx::Device, F: gfx::Factory<D::Resources>> WorldScene<D, F> {
|
impl<D: gfx::Device,
|
||||||
pub fn new(device: &mut D,
|
F: gfx::Factory<D::Resources>> WorldScene<D, F> {
|
||||||
|
pub fn new(_device: &mut D,
|
||||||
factory: &mut F,
|
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 worldmap = get_data_model();
|
||||||
let (model, model_idx) = get_model(&worldmap);
|
let (model, model_idx) = get_model(&worldmap);
|
||||||
let (vertex_buffer, slice) =
|
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_buffer: factory.create_constant_buffer(1),
|
||||||
constants_dirty: true,
|
constants_dirty: true,
|
||||||
locals: factory.create_constant_buffer(1),
|
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,
|
sampler: factory.create_sampler(texture::SamplerInfo::new(texture::FilterMethod::Jrd,
|
||||||
texture::WrapMode::Tile)),
|
texture::WrapMode::Tile)),
|
||||||
f: PhantomData,
|
f: PhantomData,
|
||||||
@@ -197,10 +195,12 @@ 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 {
|
||||||
// treadmill / camera movement registration
|
// treadmill / camera movement registration
|
||||||
@@ -276,6 +276,7 @@ impl<D: gfx::Device,
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self,
|
fn update(&mut self,
|
||||||
|
_game: &mut VrtueRootContext<D, F, view::ColorFormat>,
|
||||||
vr: &mut Option<vr::VR>,
|
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;
|
||||||
@@ -334,11 +335,12 @@ impl<D: gfx::Device,
|
|||||||
}
|
}
|
||||||
|
|
||||||
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(&target, SKY_COLOR);
|
||||||
encoder.clear_depth(&depth, 1.0);
|
encoder.clear_depth(&depth, 1.0);
|
||||||
@@ -347,7 +349,7 @@ impl<D: gfx::Device,
|
|||||||
trans: trans.clone(),
|
trans: trans.clone(),
|
||||||
constants: self.constants_buffer.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(),
|
||||||
};
|
};
|
||||||
|
|||||||
46
src/tile.rs
46
src/tile.rs
@@ -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)]
|
#[repr(C)]
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct Tile {
|
pub struct Tile {
|
||||||
pub 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_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 {
|
impl Tile {
|
||||||
pub fn as_char(&self) -> char {
|
pub fn as_char(&self) -> char {
|
||||||
|
|||||||
24
src/view.rs
24
src/view.rs
@@ -1,3 +1,4 @@
|
|||||||
|
use engine::{GameContext, Scene};
|
||||||
use vr::{self, AsMatrix4, VR};
|
use vr::{self, AsMatrix4, VR};
|
||||||
|
|
||||||
extern crate gfx_device_gl;
|
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,
|
||||||
window: &mut PistonWindow,
|
game: &mut G,
|
||||||
vr: &mut Option<vr::VR>,
|
window: &mut PistonWindow,
|
||||||
scene: &::scene::Scene<gfx_device_gl::Device, gfx_device_gl::Factory>) {
|
vr: &mut Option<vr::VR>,
|
||||||
|
scene: &Scene<G, gfx_device_gl::Device, gfx_device_gl::Factory>) {
|
||||||
if let &mut Some(ref mut vr) = vr {
|
if let &mut Some(ref mut vr) = vr {
|
||||||
// Get the current sensor state
|
// Get the current sensor state
|
||||||
let poses = vr.poses();
|
let poses = vr.poses();
|
||||||
@@ -88,11 +90,12 @@ impl ViewRoot<gfx_device_gl::Device, ColorFormat, DepthFormat> {
|
|||||||
matrix: *(proj_mat * 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,
|
scene.render(game,
|
||||||
&mut window.encoder,
|
|
||||||
&self.trans,
|
&self.trans,
|
||||||
&target,
|
&target,
|
||||||
&depth);
|
&depth,
|
||||||
|
&mut window.factory,
|
||||||
|
&mut window.encoder);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If running without VR, just draw from some default projection near the scene origin
|
// 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);
|
window.encoder.update_constant_buffer(&self.trans, &trans);
|
||||||
}
|
}
|
||||||
// draw monitor window
|
// draw monitor window
|
||||||
scene.render(&mut window.factory,
|
scene.render(game,
|
||||||
&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);
|
||||||
if let (&mut Some(ref mut vr),
|
if let (&mut Some(ref mut vr),
|
||||||
|
|||||||
Reference in New Issue
Block a user