Skip to content

Instantly share code, notes, and snippets.

@dimitriye98
Last active July 22, 2016 13:07
Show Gist options
  • Save dimitriye98/2f01b089464a92a57da7e3bf9865c136 to your computer and use it in GitHub Desktop.
Save dimitriye98/2f01b089464a92a57da7e3bf9865c136 to your computer and use it in GitHub Desktop.
fn draw(&self, display: &GlutinFacade) -> () {
let mut target = display.draw();
target.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
let uniforms = uniform! {
model: [
[0.01, 0.0, 0.0, 0.0],
[0.0, 0.01, 0.0, 0.0],
[0.0, 0.0, 0.01, 0.0],
[0.0, 0.0, 1.0, 1.0f32],
],
view: self.camera.to_isometry().to_homogeneous().as_ref().clone(),
perspective: {
let (width, height) = target.get_dimensions();
let fov: f32 = ::std::f32::consts::PI / 3.0;
let zfar = 1024.0;
let znear = 0.1;
// perspective_matrix(width, height, fov, zfar, znear)
PerspectiveMatrix3::new(width as f32 / height as f32, fov, znear, zfar).to_matrix().as_ref().clone()
},
u_light: [-1.0, 0.4, 0.9f32],
};
use glium::{DrawParameters, Depth};
use glium::draw_parameters::{DepthTest, BackfaceCullingMode};
use glium::index::{PrimitiveType, NoIndices};
let params = DrawParameters {
depth: Depth {
test: DepthTest::IfLess,
write: true,
.. Default::default()
},
backface_culling: BackfaceCullingMode::CullClockwise,
.. Default::default()
};
let vertices = self.chunk.build_mesh(display).unwrap();
let indices = NoIndices(PrimitiveType::TrianglesList);
target.draw(&vertices, &indices, &self.program, &uniforms, &params).unwrap();
target.finish().unwrap();
}
fn main() {
use std::f32;
use glium::{DisplayBuild, Surface};
use glium::glutin::{CursorState, MouseCursor};
use time::{Duration, PreciseTime};
use na::{Point3, Vector3, Isometry3, PerspectiveMatrix3, Rotation3, ToHomogeneous, Norm, Rotate, Cross};
use gl_util::{Camera, SimpleCamera};
use state::StateManager;
use engine::{Game, StatePlaying};
let vertex_shader_src = include_str!("vertex.glsl");
let fragment_shader_src = include_str!("fragment.glsl");
let display = glium::glutin::WindowBuilder::new().with_depth_buffer(24).build_glium().unwrap();
let window = display.get_window().unwrap();
// // window.set_cursor_state(CursorState::Hide);
// window.set_cursor_state(CursorState::Grab);
// {
// let (size_x, size_y) = window.get_inner_size_points().unwrap();
// window.set_cursor_position((size_x / 2) as i32, (size_y / 2) as i32);
// }
let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap();
let mut game = Game::new(Box::new(StatePlaying::new(&display, program)));
let mut last_tick: PreciseTime = PreciseTime::now();
let (mut left, mut right, mut back, mut forward, mut up, mut down) = (false, false, false, false, false, false);
loop {
let old = last_tick;
last_tick = PreciseTime::now();
let time_elapsed = old.to(last_tick);
game.update(&time_elapsed);
game.draw(&display);
// match (left, right) {
// (true, true) => (),
// (false, false) => (),
//
// (true, false) => {
// camera.position -= camera.direction.cross(&camera.up) * elapsed_time.num_milliseconds() as f32 * motion_sensitivity;
// },
// (false, true) => {
// camera.position -= -1.0 * camera.direction.cross(&camera.up) * elapsed_time.num_milliseconds() as f32 * motion_sensitivity;
// },
// }
//
// match (forward, back) {
// (true, true) => (),
// (false, false) => (),
//
// (true, false) => {
// camera.position -= -1.0 * camera.direction * elapsed_time.num_milliseconds() as f32 * motion_sensitivity;
// },
// (false, true) => {
// camera.position -= camera.direction * elapsed_time.num_milliseconds() as f32 * motion_sensitivity;
// },
// }
//
// match (up, down) {
// (true, true) => (),
// (false, false) => (),
//
// (true, false) => {
// camera.position -= -1.0 * camera.up * elapsed_time.num_milliseconds() as f32 * motion_sensitivity;
// },
// (false, true) => {
// camera.position -= camera.up * elapsed_time.num_milliseconds() as f32 * motion_sensitivity;
// },
// }
//
// // listing the events produced by the window and waiting to be received
// for ev in display.poll_events() {
// use glium::glutin::{Event, VirtualKeyCode, ElementState};
// match ev {
// Event::Closed => return, // the window has been closed by the user
// Event::KeyboardInput(pressed, _, key) => match key {
// None => (),
// Some(key) => match key {
// VirtualKeyCode::Escape => return,
// VirtualKeyCode::A => left = match pressed {
// ElementState::Pressed => true,
// ElementState::Released => false,
// },
// VirtualKeyCode::D => right = match pressed {
// ElementState::Pressed => true,
// ElementState::Released => false,
// },
// VirtualKeyCode::S => back = match pressed {
// ElementState::Pressed => true,
// ElementState::Released => false,
// },
// VirtualKeyCode::W => forward = match pressed {
// ElementState::Pressed => true,
// ElementState::Released => false,
// },
// VirtualKeyCode::LShift => down = match pressed {
// ElementState::Pressed => true,
// ElementState::Released => false,
// },
// VirtualKeyCode::Space => up = match pressed {
// ElementState::Pressed => true,
// ElementState::Released => false,
// },
//
// _ => ()
// }
// },
// Event::MouseMoved((raw_x, raw_y)) => {
// let (size_x, size_y) = window.get_inner_size_points().unwrap();
// window.set_cursor_position((size_x / 2) as i32, (size_y / 2) as i32);
// let (delta_x, delta_y) = (raw_x - size_x as i32, raw_y - size_y as i32);
// camera.direction *= Rotation3::new(camera.up * (delta_x as f32) * mouse_sensitivity);
// camera.direction *= Rotation3::new(camera.up.cross(&camera.direction) * (delta_y as f32) * mouse_sensitivity);
// },
//
// _ => ()
// }
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment