Dairy-Drift/src/main.rs

195 lines
5.7 KiB
Rust

use std::convert::TryInto;
//use glfw;
//use glfw::{Action, Context, Key};
use cgmath::{Matrix4, vec3, Point3, Deg, perspective};
use cgmath::prelude::*;
use std::sync::Arc;
const SCR_WIDTH: u32 = 1600;
const SCR_HEIGHT: u32 = 900;
const TITLE: &str = "GLFWtest";
use imgui_glow_renderer::AutoRenderer;
use imgui_sdl2_support::SdlPlatform;
use glow::*;
use sdl2;
use sdl2::pixels::Color;
use sdl2::keyboard::Keycode;
use sdl2::event::Event;
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(false));
// glfw.window_hint(glfw::WindowHint::Decorated(true));
// 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();
let (gl, shader_version, window, mut events_loop, _context) = {
let sdl = match sdl2::init()
{
Ok(sdl) => sdl,
Err(s) => panic!("Error with Initializing SDL: {}", s),
};
let video = sdl.video().unwrap();
let gl_attr = video.gl_attr();
gl_attr.set_context_profile(sdl2::video::GLProfile::Core);
gl_attr.set_context_version(3, 3);
let window = match video
.window("DairyTest", SCR_WIDTH, SCR_HEIGHT)
.opengl()
.resizable()
.build() {
Ok(ret) => ret,
Err(s) => panic!("Error with Initializing SDL Window: {}", s),
};
let gl_context = match window.gl_create_context()
{
Ok(ret) => ret,
Err(s) => panic!("Error with Initializing SDL Context: {}", s)
};
let gl = std::sync::Arc::new(unsafe{glow::Context::from_loader_function(|s| video.gl_get_proc_address(s) as *const _)});
let event_loop = match sdl.event_pump()
{
Ok(ret) => ret,
Err(s) => panic!("Error with Initializing SDL Event Loop: {}", s)
};
(gl, "#version 330", window, event_loop, gl_context)
};
let mut imgui = imgui::Context::create();
imgui
.fonts()
.add_font(&[imgui::FontSource::DefaultFontData {config: None}]);
let mut platform = SdlPlatform::init(&mut imgui);
let mut renderer = match AutoRenderer::initialize(gl.clone(), &mut imgui)
{
Ok(ret) => ret,
Err(s) => panic!("Failed to initialize Imgui Platform Renderer: {}", s),
};
// NOTE intiialize Camera
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 (ourshader, ourModel) = unsafe {
gl.enable(glow::DEPTH_TEST);
let ourShader = shader::shader::new("model", Arc::clone(&gl));
let ourModel = model::Model::new("resources/models/TestCarModel/CarW4.obj", Arc::clone(&gl));
(ourShader, ourModel)
};
let time = std::time::Instant::now();
let projection: Matrix4<f32> = perspective(Deg(45.0), SCR_WIDTH as f32/ SCR_HEIGHT as f32, 0.1, 100.0);
println!("entering main loop");
// NOTE main loop here
'running: loop {
let currentFrame = time.elapsed().as_secs_f32();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
unsafe{
gl.enable(glow::DEPTH_TEST);
gl.clear(glow::COLOR_BUFFER_BIT | glow::DEPTH_BUFFER_BIT);
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();
ourshader.setMat4("projection", &projection);
ourshader.setMat4("view", &view);
let mut model: Matrix4<f32> = Matrix4::from_axis_angle(vec3(0.0, -1.0, 0.0).normalize(),
cgmath::Rad(time.elapsed().as_secs_f32()));
model = model * Matrix4::from_scale(0.2);
ourshader.setMat4("model", &model);
}
ourModel.Draw(&ourshader);
platform.prepare_frame(&mut imgui, &window, &events_loop);
let ui = imgui.new_frame();
ui.window("POLY I WILL FUCKING")
.size([500.0, 100.0], imgui::Condition::FirstUseEver)
.build(|| {
ui.text("you serve no purpose in life");
ui.text("Your Purpose in life is to suck my dick");
});
let draw_data = imgui.render();
renderer.render(draw_data).unwrap();
window.gl_swap_window();
for event in events_loop.poll_iter() {
platform.handle_event(&mut imgui, &event);
match event {
Event::Quit {..} |
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running
},
_ => {}
}
}
}
}