Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::fs;
- use std::time::Instant;
- use glium::Surface;
- use glium::implement_vertex;
- use glium::index::PrimitiveType;
- use glium::uniform;
- use glium::glutin::event::KeyboardInput;
- use glium::glutin::event::VirtualKeyCode;
- use nalgebra as na;
- fn main() {
- //Create window and OpenGl context
- let event_loop = glium::glutin::event_loop::EventLoop::new();
- let wb = glium::glutin::window::WindowBuilder::new()
- .with_inner_size(glium::glutin::dpi::LogicalSize::new(1024.0, 768.0))
- .with_title("Hello world");
- let cb = glium::glutin::ContextBuilder::new();
- let display = glium::Display::new(wb, cb, &event_loop).unwrap();
- //Create data to represent the cube with its little arrow
- #[derive(Copy, Clone)]
- struct Vertex {
- position: [f32; 3]
- }
- implement_vertex!(Vertex, position);
- let shape = vec![
- Vertex{position: [0.5, 0.5, 0.5]},
- Vertex{position: [-0.5, 0.5, 0.5]},
- Vertex{position: [-0.5, 0.5, -0.5]},
- Vertex{position: [0.5, 0.5, -0.5]},
- Vertex{position: [0.5, -0.5, 0.5]},
- Vertex{position: [-0.5, -0.5, 0.5]},
- Vertex{position: [-0.5, -0.5, -0.5]},
- Vertex{position: [0.5, -0.5, -0.5]},
- //Arrow
- Vertex{position: [-0.05, 1.0, 0.0]},
- Vertex{position: [0.05, 1.0, 0.0]},
- Vertex{position: [-0.05, -1.0, 0.0]},
- Vertex{position: [0.05, -1.0, 0.0]},
- //Arrow head
- Vertex{position: [-0.1, -1.0, 0.0]},
- Vertex{position: [0.1, -1.0, 0.0]},
- Vertex{position: [-0.0, -1.2, 0.0]},
- ];
- let indices: Vec<u32> = vec![
- 0, 1, 2,
- 0, 2, 3,
- 0, 3, 7,
- 0, 7, 4,
- 0, 4, 5,
- 0, 5, 1,
- 6, 3, 2,
- 6, 7, 3,
- 6, 2, 1,
- 6, 1, 5,
- 6, 5, 4,
- 6, 4, 7,
- //Arrow
- 8, 9, 10,
- 9, 10, 11,
- //arrow head
- 12, 13, 14
- ];
- let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();
- let indices = glium::index::IndexBuffer::new(&display, PrimitiveType::TrianglesList, &indices).unwrap();
- //let indices = glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip);
- let vertex_shader_file = fs::read_to_string("shaders/vertex_shader.vert")
- .expect("Could not open vertex shader source file!");
- let fragment_shader_file = fs::read_to_string("shaders/fragment_shader.frag")
- .expect("Could not open fragment shader source file!");
- let program = glium::Program::from_source(&display, &vertex_shader_file, &fragment_shader_file, None).unwrap();
- let params = glium::DrawParameters {
- depth: glium::Depth {
- test: glium::draw_parameters::DepthTest::IfLess,
- write: true,
- .. Default::default()
- },
- .. Default::default()
- };
- //The quaternion the cube is rotated by, and that gets rotated by the quaternions in "match ev"
- let mut na_quart = na::Quaternion::new(0.0, 1.0, 0.0, 0.0);
- //The time the last frame started being handled
- let mut last_t = Option::<Instant>::None;
- //Handle events caught by the event loop
- event_loop.run(move |ev, _, control_flow| {
- //Creating the delta-time (since last frame started) and making a new reference time
- let current_t = Instant::now();
- let delta_t = current_t - last_t.unwrap_or_else(|| current_t);
- last_t = Some(current_t);
- // Some debugging for the performance issue, it shows the frame spikes on his machine,
- // and mine when screen sharing. His bars were a lot longer than mine, and raw time in micros
- //Was in multi-thousands intead of low hundreds
- for _ in 0..(delta_t.as_micros()/24) {
- print!("#");
- }
- println!();
- //Start handling events, this just catches the WS AD QE keys to rotate the cube
- match ev {
- glium::glutin::event::Event::WindowEvent { event, .. } => match event {
- glium::glutin::event::WindowEvent::CloseRequested => {
- *control_flow = glium::glutin::event_loop::ControlFlow::Exit;
- }
- glium::glutin::event::WindowEvent::KeyboardInput {
- input:KeyboardInput {virtual_keycode:Some(keycode),..},
- ..
- } => match keycode{
- VirtualKeyCode::W => {
- 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());
- na_quart = r_quart*na_quart;
- na_quart = na_quart*r_quart.conjugate();
- },
- VirtualKeyCode::S => {
- 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());
- na_quart = r_quart*na_quart;
- na_quart = na_quart*r_quart.conjugate();
- },
- VirtualKeyCode::Q => {
- 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());
- na_quart = r_quart*na_quart;
- na_quart = na_quart*r_quart.conjugate();
- },
- VirtualKeyCode::E => {
- 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());
- na_quart = r_quart*na_quart;
- na_quart = na_quart*r_quart.conjugate();
- },
- VirtualKeyCode::A => {
- 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());
- na_quart = r_quart*na_quart;
- na_quart = na_quart*r_quart.conjugate();
- },
- VirtualKeyCode::D => {
- 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());
- na_quart = r_quart*na_quart;
- na_quart = na_quart*r_quart.conjugate();
- },
- _=>()
- },
- _ => (),
- },
- _ => (),
- }
- //Start rendering
- let mut render_target = display.draw();
- render_target.clear_color_and_depth((0.2, 0.2, 0.2, 1.0), 1.0);
- // According to the flame chart we made, this call takes up most of the program's time, and most
- //of it is from the AMD drivers.
- render_target.draw(&vertex_buffer, &indices, &program, &uniform! {
- rot_quaternion: [na_quart.w, na_quart.i, na_quart.j, na_quart.k]
- },
- ¶ms).unwrap();
- render_target.finish().unwrap();
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment