Advertisement
Guest User

Untitled

a guest
Jan 15th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.42 KB | None | 0 0
  1. #[macro_use]
  2. extern crate glium;
  3. use glium::glutin;
  4. use glium::Surface;
  5.  
  6. #[derive(Clone, Copy)]
  7. struct Vertex {
  8.     position: [f32; 2],
  9. }
  10.  
  11. fn main() {
  12.     // 1. The **winit::EventsLoop** for handling events.
  13.     let mut events_loop = glium::glutin::EventsLoop::new();
  14.     // 2. Parameters for building the Window.
  15.     let window = glium::glutin::WindowBuilder::new()
  16.         .with_dimensions(1024, 768)
  17.         .with_title("Hello world");
  18.     // 3. Parameters for building the OpenGL context.
  19.     let context = glium::glutin::ContextBuilder::new();
  20.     // 4. Build the Display with the given window and OpenGL context parameters and register the
  21.     //    window with the events_loop.
  22.     let display = glium::Display::new(window, context, &events_loop).unwrap();
  23.  
  24.     implement_vertex!(Vertex, position);
  25.  
  26.     // we create the shape an add `t` to each x coordinate
  27.     let shape = vec![
  28.         Vertex { position: [-0.5, -0.5] },
  29.         Vertex { position: [ 0.0,  0.5] },
  30.         Vertex { position: [ 0.5, -0.25] },
  31.     ];
  32.     let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();
  33.     let index_buffer = glium::IndexBuffer::new(&display, glium::index::PrimitiveType::LineLoop, &vec![0, 1, 1, 2u16]).unwrap();
  34.  
  35.     let vertex_shader_src = r#"
  36.        #version 140
  37.  
  38.        in vec2 position;
  39.  
  40.        void main() {
  41.            gl_Position = vec4(position, 0.0, 1.0);
  42.        }
  43.    "#;
  44.  
  45.     let fragment_shader_src = r#"
  46.        #version 140
  47.  
  48.        out vec4 color;
  49.  
  50.        void main() {
  51.            color = vec4(1.0, 0.0, 0.0, 1.0);
  52.        }
  53.    "#;
  54.  
  55.     let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap();
  56.  
  57.     let mut closed = false;
  58.     while !closed {
  59.         // drawing
  60.         let mut target = display.draw();
  61.         target.clear_color(0.0, 0.0, 1.0, 1.0);
  62.         target.draw(&vertex_buffer, &index_buffer, &program, &uniform! {
  63.  
  64.         },
  65.         &glium::DrawParameters {
  66.             line_width: Some(10.0),
  67.             .. Default::default()
  68.         }).unwrap();
  69.         target.finish().unwrap();
  70.  
  71.         events_loop.poll_events(|event| {
  72.             match event {
  73.                 glutin::Event::WindowEvent { event, .. } => match event {
  74.                     glutin::WindowEvent::Closed => closed = true,
  75.                     _ => ()
  76.                 },
  77.                 _ => (),
  78.             }
  79.         });
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement