Skip to content

Instantly share code, notes, and snippets.

@seivan
Last active November 20, 2018 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seivan/a0cac284d3318aaa9dfa6077e7c0d26d to your computer and use it in GitHub Desktop.
Save seivan/a0cac284d3318aaa9dfa6077e7c0d26d to your computer and use it in GitHub Desktop.
gfx-rs. 3.2
#[macro_use]
extern crate gfx;
extern crate gfx_window_sdl;
extern crate sdl2;
use gfx::Device;
pub type ColorFormat = gfx::format::Srgba8;
pub type DepthFormat = gfx::format::DepthStencil;
const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
//#[macro_use]
// use gfx;
// use gfx_window_sdl;
// use sdl2;
// use gfx::Device;
gfx_defines!{
vertex Vertex {
pos: [f32; 4] = "a_Pos",
color: [f32; 3] = "a_Color",
}
constant Transform {
transform: [[f32; 4];4] = "u_Transform",
}
pipeline pipe {
vbuf: gfx::VertexBuffer<Vertex> = (),
transform: gfx::ConstantBuffer<Transform> = "Transform",
out: gfx::RenderTarget<ColorFormat> = "Target0",
}
}
#[derive(Debug, Clone, Copy)]
struct Square {
pub pos: (f32, f32),
pub size: f32,
pub color: [f32; 3]
}
pub fn main() {
use gfx::traits::FactoryExt;
use sdl2::video::GLProfile;
let sdl_context = sdl2::init().unwrap();
let video = sdl_context.video().unwrap();
let gl_attr = video.gl_attr();
gl_attr.set_context_profile(GLProfile::Core);
// Set the context into debug mode
// gl_attr.set_context_flags().debug().set();
let mut builder = video.window("Example", 800, 600);
let (mut window, mut gl_context, mut device, mut factory, color_view, depth_view) =
gfx_window_sdl::init::<ColorFormat, DepthFormat>(&video, builder).unwrap();
println!("{:?}", device.get_info());
println!("{:?}", device.get_info().shading_language);
println!("{:?}", device.get_info().version);
println!("{:?}", device.get_info().platform_name);
let vertex_shader = r#"
#version 150 core
in vec4 a_Pos;
in vec3 a_Color;
uniform Transform {
mat4 u_Transform;
};
out vec4 v_Color;
void main() {
v_Color = vec4(a_Color, 1.0);
gl_Position = a_Pos * u_Transform;
}
"#;
let fragment_shader = r#"
#version 150 core
in vec4 v_Color;
out vec4 Target0;
void main() {
Target0 = v_Color;
}
"#;
let pso = factory.create_pipeline_simple(
vertex_shader.as_bytes(),
fragment_shader.as_bytes(),
pipe::new()
).unwrap();
// Vertex { pos: [ -0.5, -0.5, 0.0, 1.0 ], color: [1.0, 0.0, 0.0] },
// Vertex { pos: [ 0.5, -0.5, 0.0, 1.0 ], color: [0.0, 1.0, 0.0] },
// Vertex { pos: [ 0.0, 0.5, 0.0, 1.0 ], color: [0.0, 0.0, 1.0] },
let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
const TRIANGLE: [Vertex; 6] = [
Vertex { pos: [ 0.0, 1.0, 0.0, 1.0 ], color: [1.0, 0.0, 0.0] },
Vertex { pos: [ 1.0, 1.0, 0.0, 1.0 ], color: [0.0, 1.0, 0.0] },
Vertex { pos: [ 1.0, 0.0, 0.0, 1.0 ], color: [0.0, 0.0, 1.0] },
Vertex { pos: [ 1.0, 0.0, 0.0, 1.0 ], color: [0.0, 0.0, 1.0] },
Vertex { pos: [ 0.0, 0.0, 0.0, 1.0 ], color: [0.0, 0.0, 1.0] },
Vertex { pos: [ 0.0, 1.0, 0.0, 1.0 ], color: [0.0, 0.0, 1.0] },
];
//Identity Matrix
const TRANSFORM: Transform = Transform {
transform: [[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]]
};
let (vertex_buffer, slice) = factory.create_vertex_buffer_with_slice(&TRIANGLE, ());
let transform_buffer = factory.create_constant_buffer(1);
let data = pipe::Data {
vbuf: vertex_buffer,
transform: transform_buffer,
out: color_view.clone(),
};
'main: loop {
let mut event_pump = sdl_context.event_pump().unwrap();
for event in event_pump.poll_iter() {
match event {
sdl2::event::Event::Quit { .. } => {
break 'main;
}
_ => {}
}
}
encoder.clear(&color_view, BLACK); //clear the framebuffer with a color(color needs to be an array of 4 f32s, RGBa)
encoder.update_buffer(&data.transform, &[TRANSFORM], 0); //update buffers
encoder.draw(&slice, &pso, &data); // draw commands with buffer data and attached pso
encoder.flush(&mut device); // execute draw commands
window.gl_swap_window();
device.cleanup();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment