view doesn't own scene

This commit is contained in:
2016-09-30 13:37:44 -07:00
parent 7a9f65115c
commit 235b5d58bd
4 changed files with 23 additions and 18 deletions

View File

@@ -1,5 +1,6 @@
extern crate vrtue;
use vrtue::{view, vr};
use vrtue::{scenes, view, vr};
use vrtue::scene::Scene;
extern crate env_logger;
extern crate gfx_device_gl;
@@ -18,14 +19,15 @@ pub fn main() {
.vsync(false)
.build().expect("Building Window");
let view = view::ViewRoot::<gfx_device_gl::Device,
view::ColorFormat,
view::DepthFormat>::create_view(&mut window, &mut vr);
let mut scene = scenes::world::WorldScene::new(&mut window.factory);
let view = view::ViewRoot::<gfx_device_gl::Device, view::ColorFormat, view::DepthFormat>
::create_view(&mut window, &mut vr);
'main:
//while let Some(_) = window.next() {
loop {
view.draw(&mut window, &mut vr);
scene.update();
view.draw(&mut window, &mut vr, &scene);
// handle window events
while let Some(ev) = window.poll_event() {

View File

@@ -10,6 +10,7 @@ pub trait Scene<D: gfx::Device,
fn render(&self,
factory: &mut F,
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>,
trans: &gfx::handle::Buffer<D::Resources, view::Trans>,
target: &gfx::handle::RenderTargetView<D::Resources, view::ColorFormat>,
depth: &gfx::handle::DepthStencilView<D::Resources, view::DepthFormat>);
}

View File

@@ -40,7 +40,6 @@ fn get_model(/*x: u16, y: u16,*/ tile: &tile::Tile) -> ([Vertex; 4], [u16; 6]) {
pub struct WorldScene<D: gfx::Device,
F: gfx::Factory<D::Resources>> {
pso: gfx::PipelineState<D::Resources, pipe::Meta>,
trans: gfx::handle::Buffer<D::Resources, view::Trans>,
atlas: gfx::handle::ShaderResourceView<D::Resources,
<view::ColorFormat as gfx::format::Formatted>::View>,
sampler: gfx::handle::Sampler<D::Resources>,
@@ -48,13 +47,12 @@ pub struct WorldScene<D: gfx::Device,
}
impl<D: gfx::Device, F: gfx::Factory<D::Resources>> WorldScene<D, F> {
pub fn new(factory: &mut F, trans: &gfx::handle::Buffer<D::Resources, view::Trans>) -> WorldScene<D, F> {
pub fn new(factory: &mut F) -> WorldScene<D, F> {
WorldScene {
pso: factory.create_pipeline_simple(VERTEX_SHADER_SRC,
FRAGMENT_SHADER_SRC,
pipe::new())
.expect("create pipeline"),
trans: trans.clone(),
atlas: tile::get_tiles::<_, _, view::ColorFormat>(factory),
sampler: factory.create_sampler(tex::SamplerInfo::new(tex::FilterMethod::Scale,
tex::WrapMode::Clamp)),
@@ -72,6 +70,7 @@ impl<D: gfx::Device,
fn render(&self,
factory: &mut F,
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>,
trans: &gfx::handle::Buffer<D::Resources, view::Trans>,
target: &gfx::handle::RenderTargetView<D::Resources, view::ColorFormat>,
depth: &gfx::handle::DepthStencilView<D::Resources, view::DepthFormat>) {
let (model, model_idx) = get_model(&tile::Tile { val: 31 });
@@ -80,7 +79,7 @@ impl<D: gfx::Device,
let pipe = pipe::Data {
vbuf: vertex_buffer.clone(),
trans: self.trans.clone(),
trans: trans.clone(),
atlas: (self.atlas.clone(), self.sampler.clone()),
pixcolor: target.clone(),
depth: depth.clone(),

View File

@@ -1,4 +1,3 @@
use scenes;
use vr;
use vr::{AsMatrix4, VR};
@@ -34,7 +33,6 @@ pub struct ViewRoot<Dev, T, D>
left: vr::EyeBuffer<T, D>,
right: vr::EyeBuffer<T, D>,
trans: gfx::handle::Buffer<Dev::Resources, Trans>,
scene: Box<::scene::Scene<Dev, gfx_device_gl::Factory>>,
}
impl ViewRoot<gfx_device_gl::Device, ColorFormat, DepthFormat> {
@@ -54,13 +52,13 @@ impl ViewRoot<gfx_device_gl::Device, ColorFormat, DepthFormat> {
left: left,
right: right,
trans: trans.clone(),
scene: Box::new(scenes::world::WorldScene::new(&mut window.factory, &trans)),
}
}
pub fn draw(&self,
window: &mut PistonWindow,
vr: &mut vr::VR) {
vr: &mut vr::VR,
scene: &::scene::Scene<gfx_device_gl::Device, gfx_device_gl::Factory>) {
// Get the current sensor state
let poses = vr.poses();
@@ -85,8 +83,9 @@ impl ViewRoot<gfx_device_gl::Device, ColorFormat, DepthFormat> {
let trans = Trans { matrix: *(proj_mat * eye_mat * hmd_mat * model_mat).as_ref() };
window.encoder.update_constant_buffer(&self.trans, &trans);
self.scene.render(&mut window.factory,
scene.render(&mut window.factory,
&mut window.encoder,
&self.trans,
&buffers.target,
&buffers.depth);
},
@@ -100,7 +99,11 @@ impl ViewRoot<gfx_device_gl::Device, ColorFormat, DepthFormat> {
&Trans { matrix: *na::Matrix4::one().as_ref() });
window.encoder.clear(&window.output_color, [0.1, 0.2, 0.3, 1.0]);
window.encoder.clear_depth(&window.output_stencil, 1.0);
self.scene.render(&mut window.factory, &mut window.encoder, &window.output_color, &window.output_stencil);
scene.render(&mut window.factory,
&mut window.encoder,
&self.trans,
&window.output_color,
&window.output_stencil);
window.encoder.flush(&mut window.device);
vr.submit(vr::Eye::Left, &self.left.tex);