Dairy-Drift/src/main.rs

183 lines
5.2 KiB
Rust
Raw Normal View History

use std::convert::TryInto;
2023-03-15 12:38:22 +01:00
//use glfw;
//use glfw::{Action, Context, Key};
use cgmath::{Matrix4, vec3, Point3, Deg, perspective};
use cgmath::prelude::*;
2023-03-15 12:38:22 +01:00
use std::sync::Arc;
const SCR_WIDTH: u32 = 1600;
const SCR_HEIGHT: u32 = 900;
const TITLE: &str = "GLFWtest";
2023-03-15 12:38:22 +01:00
2023-03-15 15:51:30 +01:00
use imgui_glow_renderer::AutoRenderer;
use imgui_sdl2_support::SdlPlatform;
2023-03-06 10:13:01 +01:00
use glow::*;
2023-03-15 12:38:22 +01:00
use sdl2;
use sdl2::pixels::Color;
2023-03-15 12:38:22 +01:00
use sdl2::keyboard::Keycode;
use sdl2::event::Event;
2023-03-15 15:51:30 +01:00
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};
2023-03-15 15:51:30 +01:00
mod shader;
mod model;
mod camera;
fn main() {
2023-03-15 12:38:22 +01:00
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)
2023-03-15 12:38:22 +01:00
};
2023-03-15 15:51:30 +01:00
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 {
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 _);
let (ourshader, ourModel) = unsafe {
2023-03-08 15:09:25 +01:00
gl.enable(glow::DEPTH_TEST);
2023-03-15 12:38:22 +01:00
let ourShader = shader::shader::new("model", Arc::clone(&gl));
2023-03-15 12:38:22 +01:00
let ourModel = model::Model::new("resources/models/TestCarModel/CarW4.obj", Arc::clone(&gl));
(ourShader, ourModel)
};
2023-03-15 12:38:22 +01:00
let time = std::time::Instant::now();
2023-03-15 16:18:38 +01:00
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");
2023-03-15 12:38:22 +01:00
// NOTE main loop here
'running: loop {
let currentFrame = time.elapsed().as_secs_f32();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
2023-03-15 16:18:38 +01:00
current_rad += spinSpeed/15.0;
2023-03-15 12:38:22 +01:00
unsafe{
gl.enable(glow::DEPTH_TEST);
gl.clear(glow::COLOR_BUFFER_BIT | glow::DEPTH_BUFFER_BIT);
2023-03-15 12:38:22 +01:00
ourshader.Use();
let Projection: Matrix4<f32> = perspective(Deg(camera.Zoom), SCR_WIDTH as f32 / SCR_HEIGHT as f32, 0.1, 100.0);
2023-03-15 12:38:22 +01:00
let view = camera.GetViewMatrix();
ourshader.setMat4("projection", &projection);
ourshader.setMat4("view", &view);
2023-03-15 16:18:38 +01:00
2023-03-15 12:38:22 +01:00
let mut model: Matrix4<f32> = Matrix4::from_axis_angle(vec3(0.0, -1.0, 0.0).normalize(),
2023-03-15 16:18:38 +01:00
cgmath::Rad(current_rad));
2023-03-15 12:38:22 +01:00
model = model * Matrix4::from_scale(0.2);
ourshader.setMat4("model", &model);
2023-03-15 15:51:30 +01:00
}
2023-03-15 12:38:22 +01:00
ourModel.Draw(&ourshader);
2023-03-15 15:51:30 +01:00
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");
2023-03-15 16:18:38 +01:00
ui.slider("The Spin Speed", 0.1, 10.0, &mut spinSpeed);
2023-03-15 15:51:30 +01:00
});
let draw_data = imgui.render();
renderer.render(draw_data).unwrap();
2023-03-15 12:38:22 +01:00
window.gl_swap_window();
for event in events_loop.poll_iter() {
2023-03-15 15:51:30 +01:00
platform.handle_event(&mut imgui, &event);
2023-03-15 12:38:22 +01:00
match event {
Event::Quit {..} |
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running
},
_ => {}
}
}
}
}