WIP playing around with gfx-rs

This commit is contained in:
Dustin Carlino 2018-09-09 19:26:06 -07:00
parent 571aa2f786
commit b30332dff8
3 changed files with 168 additions and 0 deletions

View File

@ -14,4 +14,5 @@ members = [
"map_model", "map_model",
"playground_gui", "playground_gui",
"sim", "sim",
"tmp_gfx",
] ]

10
tmp_gfx/Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "tmp_gfx"
version = "0.1.0"
authors = ["Dustin Carlino <dabreegster@gmail.com>"]
[dependencies]
gfx = "0.17.1"
gfx_device_gl = "0.15.3"
gfx_window_glutin = "0.26.0"
glutin = "0.18.0"

157
tmp_gfx/src/main.rs Normal file
View File

@ -0,0 +1,157 @@
#[macro_use]
extern crate gfx;
extern crate gfx_device_gl;
extern crate gfx_window_glutin;
extern crate glutin;
use gfx::traits::{Device, FactoryExt};
use glutin::dpi::LogicalSize;
use glutin::GlContext;
type ColorFormat = gfx::format::Rgba8;
type DepthFormat = gfx::format::DepthStencil;
const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
gfx_defines!{
vertex GpuFillVertex {
position: [f32; 2] = "a_position",
}
pipeline fill_pipeline {
vbo: gfx::VertexBuffer<GpuFillVertex> = (),
out_color: gfx::RenderTarget<ColorFormat> = "out_color",
}
}
fn main() {
let mut events_loop = glutin::EventsLoop::new();
let glutin_builder = glutin::WindowBuilder::new()
.with_dimensions(LogicalSize::new(700.0, 700.0))
.with_decorations(true)
.with_title("Simple tessellation".to_string());
let context = glutin::ContextBuilder::new().with_vsync(true);
let (window, mut device, mut factory, mut main_fbo, mut main_depth) =
gfx_window_glutin::init::<ColorFormat, DepthFormat>(glutin_builder, context, &events_loop);
let shader = factory
.link_program(VERTEX_SHADER.as_bytes(), FRAGMENT_SHADER.as_bytes())
.unwrap();
let pso = factory
.create_pipeline_from_program(
&shader,
gfx::Primitive::TriangleList,
gfx::state::Rasterizer::new_fill(),
fill_pipeline::new(),
)
.unwrap();
// The geometry!
let vertices = vec![
GpuFillVertex {
position: [-1.0, -1.0],
},
GpuFillVertex {
position: [1.0, -1.0],
},
GpuFillVertex {
position: [-1.0, 1.0],
},
GpuFillVertex {
position: [1.0, 1.0],
},
];
let indices: Vec<u16> = vec![0, 1, 2, 2, 3, 0];
let (vbo, ibo) = factory.create_vertex_buffer_with_slice(&vertices, &indices[..]);
let mut cmd_queue: gfx::Encoder<_, _> = factory.create_command_buffer().into();
loop {
if !update_inputs(&mut events_loop) {
break;
}
gfx_window_glutin::update_views(&window, &mut main_fbo, &mut main_depth);
cmd_queue.clear(&main_fbo.clone(), BLACK);
cmd_queue.draw(
&ibo,
&pso,
&fill_pipeline::Data {
vbo: vbo.clone(),
out_color: main_fbo.clone(),
},
);
cmd_queue.flush(&mut device);
window.swap_buffers().unwrap();
device.cleanup();
}
}
fn update_inputs(event_loop: &mut glutin::EventsLoop) -> bool {
use glutin::ElementState::Pressed;
use glutin::Event;
use glutin::VirtualKeyCode;
let mut status = true;
event_loop.poll_events(|event| match event {
Event::WindowEvent {
event: glutin::WindowEvent::CloseRequested,
..
} => {
println!("Window Closed!");
status = false;
}
Event::WindowEvent {
event:
glutin::WindowEvent::KeyboardInput {
input:
glutin::KeyboardInput {
state: Pressed,
virtual_keycode: Some(key),
..
},
..
},
..
} => match key {
VirtualKeyCode::Escape => {
status = false;
}
_key => {}
},
_ => {}
});
status
}
static VERTEX_SHADER: &'static str = "
#version 140
in vec2 a_position;
out vec4 v_color;
void main() {
gl_Position = vec4(a_position, 0.0, 1.0);
gl_Position.y *= -1.0;
v_color = vec4(1.0, 0.0, 0.0, 0.5);
}
";
static FRAGMENT_SHADER: &'static str = "
#version 140
in vec4 v_color;
out vec4 out_color;
void main() {
out_color = v_color;
}
";