use std::convert::TryInto; use glfw; use glfw::{Action, Context, Key}; use cgmath::{Matrix4, vec3, Point3, Deg, perspective}; use cgmath::prelude::*; use std::rc::Rc; const SCR_WIDTH: u32 = 1600; const SCR_HEIGHT: u32 = 900; const TITLE: &str = "GLFWtest"; use glow::*; use std::sync::mpsc::Receiver; use std::ptr; use std::mem; use std::os::raw::c_void; use std::path::Path; use std::ffi::{CString, CStr}; mod shader; mod model; mod camera; fn main() { let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); glfw.window_hint(glfw::WindowHint::ContextVersion(3, 3)); glfw.window_hint(glfw::WindowHint::OpenGlProfile(glfw::OpenGlProfileHint::Core)); glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true)); glfw.window_hint(glfw::WindowHint::Resizable(false)); glfw.window_hint(glfw::WindowHint::TransparentFramebuffer(true)); glfw.window_hint(glfw::WindowHint::Decorated(false)); let (mut window, events) = glfw.create_window(SCR_WIDTH, SCR_HEIGHT, TITLE, glfw::WindowMode::Windowed).unwrap(); let (screen_width, screen_height) = window.get_framebuffer_size(); window.make_current(); window.set_key_polling(true); window.set_framebuffer_size_polling(true); window.set_cursor_pos_polling(true); window.set_cursor_mode(glfw::CursorMode::Disabled); let mut camera = camera::Camera { Position: Point3::new(0.0, 0.40, 1.0), Pitch: -20.0, ..camera::Camera::default() }; let mut firstMouse = true; let mut lastX: f32 = SCR_WIDTH as f32 / 2.0; let mut lastY: f32 = SCR_HEIGHT as f32 / 2.0; // timing let mut deltaTime: f32; // time between current frame and last frame let mut lastFrame: f32 = 0.0; //gl::load_with(|ptr| window.get_proc_address(ptr) as *const _); let gl = std::rc::Rc::new(unsafe{glow::Context::from_loader_function(|s| window.get_proc_address(s) as *const _)}); let (ourshader, ourModel) = unsafe { gl.enable(glow::DEPTH_TEST); let ourShader = shader::shader::new("model", Rc::clone(&gl)); let ourModel = model::Model::new("resources/models/TestCarModel/CarW4.obj", Rc::clone(&gl)); (ourShader, ourModel) }; let projection: Matrix4 = perspective(Deg(45.0), SCR_WIDTH as f32/ SCR_HEIGHT as f32, 0.1, 100.0); println!("entering main loop"); while !window.should_close() { let currentFrame = glfw.get_time() as f32; deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; processInput(&mut window, deltaTime, &mut camera); unsafe { gl.enable(glow::DEPTH_TEST); gl.clear(glow::COLOR_BUFFER_BIT | glow::DEPTH_BUFFER_BIT); // outline ourshader.Use(); let Projection: Matrix4 = perspective(Deg(camera.Zoom), SCR_WIDTH as f32 / SCR_HEIGHT as f32, 0.1, 100.0); let view = camera.GetViewMatrix(); ourshader.setMat4("projection", &projection); ourshader.setMat4("view", &view); //let mut model = Matrix4::::from_translation(vec3(0.0, -0.3, 0.0)); let mut model: Matrix4 = Matrix4::from_axis_angle(vec3(0.0, -1.0, 0.0).normalize(), cgmath::Rad(glfw.get_time() as f32)); model = model * Matrix4::from_scale(0.2); ourshader.setMat4("model", &model); ourModel.Draw(&ourshader); } window.swap_buffers(); glfw.poll_events(); } } fn processInput(window: &mut glfw::Window, deltaTime: f32, camera: &mut camera::Camera) { if window.get_key(Key::Escape) == Action::Press { window.set_should_close(true) } /* if window.get_key(Key::W) == Action::Press { camera.ProcessKeyboard(camera::Camera_Movement::FORWARD, deltaTime); } if window.get_key(Key::S) == Action::Press { camera.ProcessKeyboard(camera::Camera_Movement::BACKWARD, deltaTime); } if window.get_key(Key::A) == Action::Press { camera.ProcessKeyboard(camera::Camera_Movement::LEFT, deltaTime); } if window.get_key(Key::D) == Action::Press { camera.ProcessKeyboard(camera::Camera_Movement::RIGHT, deltaTime); } */ } /* fn process_events(events: &Receiver<(f64, glfw::WindowEvent)>, firstMouse: &mut bool, lastX: &mut f32, lastY: &mut f32, camera: &mut camera::Camera) { for (_, event) in glfw::flush_messages(events) { match event { glfw::WindowEvent::FramebufferSize(width, height) => { // make sure the viewport matches the new window dimensions; note that width and // height will be significantly larger than specified on retina displays. unsafe { gl.viewport(0, 0, width, height) } } glfw::WindowEvent::CursorPos(xpos, ypos) => { let (xpos, ypos) = (xpos as f32, ypos as f32); if *firstMouse { *lastX = xpos; *lastY = ypos; *firstMouse = false; } let xoffset = xpos - *lastX; let yoffset = *lastY - ypos; // reversed since y-coordinates go from bottom to top *lastX = xpos; *lastY = ypos; camera.ProcessMouseMovement(xoffset, yoffset, true); } glfw::WindowEvent::Scroll(_xoffset, yoffset) => { camera.ProcessMouseScroll(yoffset as f32); } _ => {} } } } */