refactor view/scene to separate modules
This commit is contained in:
123
src/scenes/world.rs
Normal file
123
src/scenes/world.rs
Normal file
@@ -0,0 +1,123 @@
|
||||
use scene;
|
||||
use tile;
|
||||
use view;
|
||||
|
||||
extern crate gfx;
|
||||
extern crate piston_window;
|
||||
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use gfx::tex;
|
||||
use gfx::traits::FactoryExt;
|
||||
|
||||
gfx_defines! {
|
||||
vertex Vertex {
|
||||
pos: [f32; 3] = "a_pos",
|
||||
uv: [f32; 2] = "a_uv",
|
||||
tileidx: u32 = "a_tileidx",
|
||||
}
|
||||
|
||||
pipeline pipe {
|
||||
vbuf: gfx::VertexBuffer<Vertex> = (),
|
||||
trans: gfx::ConstantBuffer<::view::Trans> = "b_trans",
|
||||
atlas: gfx::TextureSampler<[f32; 4]> = "t_atlas",
|
||||
pixcolor: gfx::RenderTarget<::view::ColorFormat> = "pixcolor",
|
||||
depth: gfx::DepthTarget<::view::DepthFormat> = gfx::preset::depth::LESS_EQUAL_WRITE,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn get_model(/*x: u16, y: u16,*/ tile: &tile::Tile) -> ([Vertex; 4], [u16; 6]) {
|
||||
let tileidx = tile.val as u32;
|
||||
([Vertex { pos: [ 0., 0., 0. ], uv: [0., 0.], tileidx: tileidx },
|
||||
Vertex { pos: [ 1., 0., 0. ], uv: [1., 0.], tileidx: tileidx },
|
||||
Vertex { pos: [ 1., 0., 1. ], uv: [1., 1.], tileidx: tileidx },
|
||||
Vertex { pos: [ 0., 0., 1. ], uv: [0., 1.], tileidx: tileidx },],
|
||||
[ 0, 1, 2,
|
||||
2, 3, 0 ])
|
||||
}
|
||||
|
||||
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>,
|
||||
f: PhantomData<F>
|
||||
}
|
||||
|
||||
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> {
|
||||
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)),
|
||||
f: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: gfx::Device,
|
||||
F: gfx::Factory<D::Resources>> scene::Scene<D, F> for WorldScene<D, F> {
|
||||
|
||||
fn update(&mut self) {
|
||||
}
|
||||
|
||||
fn render(&self,
|
||||
factory: &mut F,
|
||||
encoder: &mut gfx::Encoder<D::Resources, D::CommandBuffer>,
|
||||
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 });
|
||||
let (vertex_buffer, slice) =
|
||||
factory.create_vertex_buffer_with_slice(&model, &model_idx[..]);
|
||||
|
||||
let pipe = pipe::Data {
|
||||
vbuf: vertex_buffer.clone(),
|
||||
trans: self.trans.clone(),
|
||||
atlas: (self.atlas.clone(), self.sampler.clone()),
|
||||
pixcolor: target.clone(),
|
||||
depth: depth.clone(),
|
||||
};
|
||||
encoder.draw(&slice, &self.pso, &pipe);
|
||||
}
|
||||
}
|
||||
|
||||
const VERTEX_SHADER_SRC: &'static [u8] = br#"
|
||||
#version 150
|
||||
|
||||
in vec3 a_pos;
|
||||
in vec2 a_uv;
|
||||
in uint a_tileidx;
|
||||
out vec2 v_uv;
|
||||
flat out uint v_tileidx;
|
||||
uniform b_trans {
|
||||
mat4 u_matrix;
|
||||
};
|
||||
|
||||
void main() {
|
||||
v_uv = a_uv;
|
||||
v_tileidx = a_tileidx;
|
||||
gl_Position = u_matrix * vec4(a_pos, 1.0);
|
||||
}
|
||||
"#;
|
||||
|
||||
const FRAGMENT_SHADER_SRC: &'static [u8] = br#"
|
||||
#version 150
|
||||
|
||||
in vec2 v_uv;
|
||||
flat in uint v_tileidx;
|
||||
out vec4 pixcolor;
|
||||
uniform sampler2D t_atlas;
|
||||
|
||||
void main() {
|
||||
vec2 uv = vec2(v_uv.x, float(v_tileidx) / 256.0 + (1.0 - v_uv.y) / 256.0);
|
||||
pixcolor = texture(t_atlas, uv);
|
||||
}
|
||||
"#;
|
||||
Reference in New Issue
Block a user