Advertisement
Guest User

GFX_problems

a guest
Apr 12th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.16 KB | None | 0 0
  1. #[macro_use]
  2. extern crate gfx;
  3.  
  4. extern crate gfx_window_glutin;
  5. extern crate gfx_device_gl;
  6. extern crate glutin;
  7.  
  8. use gfx::traits::FactoryExt;
  9. use gfx::Device;
  10. use gfx_window_glutin as gfx_glutin;
  11. use glutin::{GlContext, GlRequest};
  12. use glutin::Api::OpenGl;
  13. use gfx_device_gl as gfx_gl;
  14.  
  15. pub type ColorFormat = gfx::format::Rgba8;
  16. pub type DepthFormat = gfx::format::DepthStencil;
  17.  
  18. const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
  19.  
  20. gfx_defines!{
  21.     vertex Vertex {
  22.         pos: [f32; 4] = "a_Pos",
  23.         color: [f32; 3] = "a_Color",
  24.     }
  25.  
  26.     constant Transform {
  27.         transform: [[f32; 4];4] = "u_Transform",
  28.     }
  29.  
  30.     pipeline pipe {
  31.         vbuf: gfx::VertexBuffer<Vertex> = (),
  32.         transform: gfx::ConstantBuffer<Transform> = "Transform",
  33.         out: gfx::RenderTarget<ColorFormat> = "Target0",
  34.     }
  35. }
  36.  
  37. pub fn main() {
  38.  
  39.     let mut events_loop = glutin::EventsLoop::new();
  40.     let windowbuilder = glutin::WindowBuilder::new()
  41.         .with_title("Triangle Example".to_string())
  42.         .with_dimensions(512, 512);
  43.     let contextbuilder = glutin::ContextBuilder::new()
  44.         .with_gl(GlRequest::Specific(OpenGl,(3,2)))
  45.         .with_vsync(true);
  46.  
  47.  
  48.  
  49.     let (window, mut device, mut factory, color_view, mut depth_view) =
  50.         gfx_glutin::init::<ColorFormat, DepthFormat>(windowbuilder, contextbuilder, &events_loop);
  51.  
  52.     let pso = factory.create_pipeline_simple(
  53.         include_bytes!("shaders/simple_vert.glsl"),
  54.         include_bytes!("shaders/simple_frag.glsl"),
  55.         pipe::new()
  56.     ).unwrap();
  57.  
  58.     let mut command_buffer = factory.create_command_buffer();
  59.     let mut encoder: gfx::Encoder<_, _> = command_buffer.into();
  60.     //let mut c_buffer: () =
  61.     const TRIANGLE: [Vertex; 3] = [
  62.         Vertex { pos: [ -0.5, -0.5, 0.0, 1.0 ], color: [1.0, 0.0, 0.0] },
  63.         Vertex { pos: [  0.5, -0.5, 0.0, 1.0 ], color: [0.0, 1.0, 0.0] },
  64.         Vertex { pos: [  0.0,  0.5, 0.0, 1.0 ], color: [0.0, 0.0, 1.0] },
  65.     ];
  66.     //Identity Matrix
  67.     const TRANSFORM: Transform = Transform {
  68.         transform: [[1.0, 0.0, 0.0, 0.0],
  69.                     [0.0, 1.0, 0.0, 0.0],
  70.                     [0.0, 0.0, 1.0, 0.0],
  71.                     [0.0, 0.0, 0.0, 1.0]]
  72.     };
  73.  
  74.     let (vertex_buffer, slice) = factory.create_vertex_buffer_with_slice(&TRIANGLE, ());
  75.     let transform_buffer = factory.create_constant_buffer(1);
  76.     let data = pipe::Data {
  77.         vbuf: vertex_buffer,
  78.         transform: transform_buffer,
  79.         out: color_view.clone(),
  80.     };
  81.  
  82.     let mut running = true;
  83.     while running {
  84.         events_loop.poll_events(|event| {
  85.             if let glutin::Event::WindowEvent { event, .. } = event {
  86.                 match event {
  87.                     glutin::WindowEvent::Closed |
  88.                     glutin::WindowEvent::KeyboardInput {
  89.                         input: glutin::KeyboardInput {
  90.                             virtual_keycode: Some(glutin::VirtualKeyCode::Escape), ..
  91.                         }, ..
  92.                     } => running = false,
  93.                     _ => {}
  94.                 }
  95.             }
  96.         });
  97.  
  98.         window.swap_buffers().unwrap();
  99.         device.cleanup();
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement