Dairy-Drift/src/main.rs

242 lines
6.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";
enum gameState {
Paused,
Playing
}
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;
mod scene;
fn main() {
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()
};
// 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 mut State = gameState::Playing;
let time = std::time::Instant::now();
let mut spinSpeed = 1.0;
let mut current_rad = 1.0;
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
'main: loop {
match &State {
gameState::Playing => {
let currentFrame = time.elapsed().as_secs_f32();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
current_rad += spinSpeed/15.0;
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(current_rad));
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");
ui.slider("The Spin Speed", 0.1, 10.0, &mut spinSpeed);
});
drawDebugUI(ui, &ourModel);
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 'main
},
Event::KeyDown {keycode: Some(Keycode::P), ..} => {
State = gameState::Paused;
}
_ => {}
}
}
}
gameState::Paused => {
for event in events_loop.poll_iter() {
platform.handle_event(&mut imgui, &event);
match event {
Event::KeyDown {keycode: Some(Keycode::P), ..} => {
State = gameState::Playing;
}
_ => {}
}
}
}
}
}
}
fn drawDebugUI(ui: &imgui::Ui, model: &model::Model) {
ui.window("Stats")
.size([300.0,500.0], imgui::Condition::Always)
.build(|| {
ui.text("Model Information");
ui.separator();
ui.text(format!("count meshes: {}", model.meshes.len()));
for i in 0..model.meshes.len()
{
ui.text(format!("Mesh Number: {}",i ));
ui.text(format!("count vertices: {}", model.meshes[i].vertices.len()));
ui.separator();
}
});
}