Dairy-Drift/src/main.rs

171 lines
5.5 KiB
Rust
Raw Normal View History

use std::convert::TryInto;
use glfw;
use glfw::{Action, Context, Key};
use cgmath::{Matrix4, vec3, Point3, Deg, perspective};
use cgmath::prelude::*;
2023-03-08 15:09:25 +01:00
use std::rc::Rc;
const SCR_WIDTH: u32 = 1600;
const SCR_HEIGHT: u32 = 900;
const TITLE: &str = "GLFWtest";
2023-03-06 10:13:01 +01:00
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 {
2023-03-02 10:59:09 +01:00
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;
2023-03-05 00:49:10 +01:00
//gl::load_with(|ptr| window.get_proc_address(ptr) as *const _);
2023-03-08 15:09:25 +01:00
let gl = std::rc::Rc::new(unsafe{glow::Context::from_loader_function(|s| window.get_proc_address(s) as *const _)});
let (ourshader, ourModel) = unsafe {
2023-03-08 15:09:25 +01:00
gl.enable(glow::DEPTH_TEST);
2023-03-08 15:09:25 +01:00
let ourShader = shader::shader::new("model", Rc::clone(&gl));
2023-03-08 15:09:25 +01:00
let ourModel = model::Model::new("resources/models/TestCarModel/CarW4.obj", Rc::clone(&gl));
(ourShader, ourModel)
};
let projection: Matrix4<f32> = perspective(Deg(45.0), SCR_WIDTH as f32/ SCR_HEIGHT as f32, 0.1, 100.0);
println!("entering main loop");
2023-03-02 10:59:09 +01:00
while !window.should_close() {
let currentFrame = glfw.get_time() as f32;
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(&mut window, deltaTime, &mut camera);
unsafe {
2023-03-08 15:09:25 +01:00
gl.enable(glow::DEPTH_TEST);
gl.clear(glow::COLOR_BUFFER_BIT | glow::DEPTH_BUFFER_BIT);
2023-03-02 10:59:09 +01:00
// outline
ourshader.Use();
let Projection: Matrix4<f32> = perspective(Deg(camera.Zoom), SCR_WIDTH as f32 / SCR_HEIGHT as f32, 0.1, 100.0);
let view = camera.GetViewMatrix();
2023-03-08 15:09:25 +01:00
ourshader.setMat4("projection", &projection);
ourshader.setMat4("view", &view);
2023-03-02 10:59:09 +01:00
//let mut model = Matrix4::<f32>::from_translation(vec3(0.0, -0.3, 0.0));
let mut model: Matrix4<f32> = 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);
2023-03-08 15:09:25 +01:00
ourshader.setMat4("model", &model);
2023-03-02 10:59:09 +01:00
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)
}
2023-03-02 10:59:09 +01:00
/*
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);
}
2023-03-02 10:59:09 +01:00
*/
}
2023-03-08 15:09:25 +01:00
/*
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.
2023-03-08 15:09:25 +01:00
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);
}
_ => {}
}
}
}
2023-03-08 15:09:25 +01:00
*/