Guest User

Untitled

a guest
Sep 4th, 2023
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 7.23 KB | Source Code | 0 0
  1. use std::fs;
  2. use std::time::Instant;
  3.  
  4. use glium::Surface;
  5. use glium::implement_vertex;
  6. use glium::index::PrimitiveType;
  7. use glium::uniform;
  8. use glium::glutin::event::KeyboardInput;
  9. use glium::glutin::event::VirtualKeyCode;
  10.  
  11. use nalgebra as na;
  12.  
  13.  
  14.  
  15. fn main() {
  16.     //Create window and OpenGl context
  17.     let event_loop = glium::glutin::event_loop::EventLoop::new();
  18.     let wb = glium::glutin::window::WindowBuilder::new()
  19.         .with_inner_size(glium::glutin::dpi::LogicalSize::new(1024.0, 768.0))
  20.         .with_title("Hello world");
  21.     let cb = glium::glutin::ContextBuilder::new();
  22.     let display = glium::Display::new(wb, cb, &event_loop).unwrap();
  23.    
  24.     //Create data to represent the cube with its little arrow
  25.     #[derive(Copy, Clone)]
  26.     struct Vertex {
  27.         position: [f32; 3]
  28.     }
  29.     implement_vertex!(Vertex, position);
  30.  
  31.     let shape = vec![
  32.         Vertex{position: [0.5, 0.5, 0.5]},
  33.         Vertex{position: [-0.5, 0.5, 0.5]},
  34.         Vertex{position: [-0.5, 0.5, -0.5]},
  35.         Vertex{position: [0.5, 0.5, -0.5]},
  36.         Vertex{position: [0.5, -0.5, 0.5]},
  37.         Vertex{position: [-0.5, -0.5, 0.5]},
  38.         Vertex{position: [-0.5, -0.5, -0.5]},
  39.         Vertex{position: [0.5, -0.5, -0.5]},
  40.         //Arrow
  41.         Vertex{position: [-0.05, 1.0, 0.0]},
  42.         Vertex{position: [0.05, 1.0, 0.0]},
  43.         Vertex{position: [-0.05, -1.0, 0.0]},
  44.         Vertex{position: [0.05, -1.0, 0.0]},
  45.         //Arrow head
  46.         Vertex{position: [-0.1, -1.0, 0.0]},
  47.         Vertex{position: [0.1, -1.0, 0.0]},
  48.         Vertex{position: [-0.0, -1.2, 0.0]},
  49.         ];
  50.        
  51.     let indices: Vec<u32> = vec![
  52.         0, 1, 2,
  53.         0, 2, 3,
  54.         0, 3, 7,
  55.         0, 7, 4,
  56.         0, 4, 5,
  57.         0, 5, 1,
  58.         6, 3, 2,
  59.         6, 7, 3,
  60.         6, 2, 1,
  61.         6, 1, 5,
  62.         6, 5, 4,
  63.         6, 4, 7,
  64.         //Arrow
  65.         8, 9, 10,
  66.         9, 10, 11,
  67.         //arrow head
  68.         12, 13, 14
  69.     ];
  70.        
  71.     let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();
  72.     let indices = glium::index::IndexBuffer::new(&display, PrimitiveType::TrianglesList, &indices).unwrap();
  73.     //let indices = glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip);
  74.    
  75.     let vertex_shader_file = fs::read_to_string("shaders/vertex_shader.vert")
  76.         .expect("Could not open vertex shader source file!");
  77.     let fragment_shader_file = fs::read_to_string("shaders/fragment_shader.frag")
  78.         .expect("Could not open fragment shader source file!");
  79.     let program = glium::Program::from_source(&display, &vertex_shader_file, &fragment_shader_file, None).unwrap();
  80.    
  81.     let params = glium::DrawParameters {
  82.         depth: glium::Depth {
  83.             test: glium::draw_parameters::DepthTest::IfLess,
  84.             write: true,
  85.             .. Default::default()
  86.         },
  87.         .. Default::default()
  88.     };
  89.    
  90.     //The quaternion the cube is rotated by, and that gets rotated by the quaternions in "match ev"
  91.     let mut na_quart = na::Quaternion::new(0.0, 1.0, 0.0, 0.0);
  92.  
  93.     //The time the last frame started being handled
  94.     let mut last_t = Option::<Instant>::None;
  95.    
  96.     //Handle events caught by the event loop
  97.     event_loop.run(move |ev, _, control_flow| {
  98.         //Creating the delta-time (since last frame started) and making a new reference time
  99.         let current_t = Instant::now();
  100.         let delta_t = current_t - last_t.unwrap_or_else(|| current_t);
  101.         last_t = Some(current_t);
  102.  
  103.         // Some debugging for the performance issue, it shows the frame spikes on his machine,
  104.         // and mine when screen sharing. His bars were a lot longer than mine, and raw time in micros
  105.         //Was in multi-thousands intead of low hundreds
  106.         for _ in 0..(delta_t.as_micros()/24) {
  107.             print!("#");
  108.         }
  109.         println!();
  110.        
  111.         //Start handling events, this just catches the WS AD QE keys to rotate the cube
  112.         match ev {
  113.             glium::glutin::event::Event::WindowEvent { event, .. } => match event {
  114.                 glium::glutin::event::WindowEvent::CloseRequested => {
  115.                     *control_flow =  glium::glutin::event_loop::ControlFlow::Exit;
  116.                 }
  117.                 glium::glutin::event::WindowEvent::KeyboardInput {
  118.                     input:KeyboardInput {virtual_keycode:Some(keycode),..},
  119.                     ..
  120.                 } => match keycode{
  121.                     VirtualKeyCode::W => {
  122.                         let r_quart = na::Quaternion::new((100.0*delta_t.as_secs_f32()).cos(), 0.0_f32.sin(), 1.0_f32*(100.0*delta_t.as_secs_f32()).sin(), 0.0_f32.sin());
  123.                         na_quart = r_quart*na_quart;
  124.                         na_quart = na_quart*r_quart.conjugate();
  125.                     },
  126.                     VirtualKeyCode::S => {
  127.                         let r_quart = na::Quaternion::new((-100.0*delta_t.as_secs_f32()).cos(), 0.0_f32.sin(), -1.0_f32*(100.0*delta_t.as_secs_f32()).sin(), 0.0_f32.sin());
  128.                         na_quart = r_quart*na_quart;
  129.                         na_quart = na_quart*r_quart.conjugate();
  130.                     },
  131.                     VirtualKeyCode::Q => {
  132.                         let r_quart = na::Quaternion::new((100.0*delta_t.as_secs_f32()).cos(), 1.0_f32*(100.0*delta_t.as_secs_f32()).sin(), 0.0_f32.sin(), 0.0_f32.sin());
  133.                         na_quart = r_quart*na_quart;
  134.                         na_quart = na_quart*r_quart.conjugate();
  135.                     },
  136.                     VirtualKeyCode::E => {
  137.                         let r_quart = na::Quaternion::new((-100.0*delta_t.as_secs_f32()).cos(), -1.0_f32*(100.0*delta_t.as_secs_f32()).sin(), 0.0_f32.sin(), 0.0_f32.sin());
  138.                         na_quart = r_quart*na_quart;
  139.                         na_quart = na_quart*r_quart.conjugate();
  140.                     },
  141.                     VirtualKeyCode::A => {
  142.                         let r_quart = na::Quaternion::new((100.0*delta_t.as_secs_f32()).cos(), 0.0_f32.sin(), 0.0_f32.sin(), 1.0_f32*(100.0*delta_t.as_secs_f32()).sin());
  143.                         na_quart = r_quart*na_quart;
  144.                         na_quart = na_quart*r_quart.conjugate();
  145.                     },
  146.                     VirtualKeyCode::D => {
  147.                         let r_quart = na::Quaternion::new((-100.0*delta_t.as_secs_f32()).cos(), 0.0_f32.sin(), 0.0_f32.sin(), -1.0_f32*(100.0*delta_t.as_secs_f32()).sin());
  148.                         na_quart = r_quart*na_quart;
  149.                         na_quart = na_quart*r_quart.conjugate();
  150.                     },                  
  151.                     _=>()
  152.                 },
  153.                 _ => (),
  154.             },
  155.             _ => (),
  156.         }
  157.  
  158.         //Start rendering
  159.         let mut render_target = display.draw();
  160.         render_target.clear_color_and_depth((0.2, 0.2, 0.2, 1.0), 1.0);
  161.        
  162.         // According to the flame chart we made, this call takes up most of the program's time, and most
  163.         //of it is from the AMD drivers.
  164.         render_target.draw(&vertex_buffer, &indices, &program, &uniform! {
  165.             rot_quaternion: [na_quart.w, na_quart.i, na_quart.j, na_quart.k]
  166.         },
  167.             &params).unwrap();
  168.            
  169.         render_target.finish().unwrap();
  170.     });
  171.    
  172. }
Advertisement
Add Comment
Please, Sign In to add comment