Made the first Version of the Player Object

master
Milk.H 2023-03-27 15:34:00 +02:00
parent 9b9fd65574
commit 0a712b4c72
No known key found for this signature in database
GPG Key ID: 3D9DAE46AAC37BD8
2 changed files with 95 additions and 12 deletions

View File

@ -1,6 +1,7 @@
use crate::model::Model;
use crate::camera::Camera;
use crate::camera::Camera_Movement;
use crate::shader::shader;
use std::sync::Arc;
use std::rc::Rc;
@ -9,6 +10,7 @@ use glow::*;
use imgui_glow_renderer::AutoRenderer;
use imgui_sdl2_support::SdlPlatform;
use crate::debug;
mod objects;
use cgmath::{Matrix4, vec3, Point3, Deg, perspective};
use cgmath::prelude::*;
@ -21,7 +23,6 @@ const SCR_HEIGHT: u32 = 900;
const MODELS: [&str; 3]= ["resources/models/TestCarModel/CarW4.obj", "resources/models/TestCarModel/CarW1.obj",
"resources/models/TestCarModel/CarW0.obj"];
/// XXX this is temporary, thus the name
pub struct tempData {
pub projection: Matrix4<f32>,
@ -44,7 +45,7 @@ pub enum SceneEnum {
pub struct Scene {
gl: Arc<glow::Context>,
pub Car: Model,
pub Car: objects::Player,
camera: Camera,
debug: Debug,
shaders: Vec<Rc<shader>>,
@ -55,7 +56,7 @@ pub struct Scene {
impl Scene{
pub fn new(gl: Arc<glow::Context>) -> Scene
{
let Car = Model::new(MODELS[0], Arc::clone(&gl));
let Car = objects::Player::new(Arc::clone(&gl));
let shader = Rc::new(shader::new("model", Arc::clone(&gl)));
@ -96,12 +97,13 @@ impl Scene{
self.tempData.currentFrame = self.time.elapsed().as_secs_f32();
self.tempData.deltaTime = self.tempData.currentFrame - self.tempData.lastFrame;
self.tempData.lastFrame = self.tempData.currentFrame;
self.tempData.current_rad += self.tempData.spinSpeed/15.0;
let mut ret = SceneEnum::Resume;
unsafe {
self.gl.enable(glow::DEPTH_TEST);
self.gl.clear(glow::COLOR_BUFFER_BIT | glow::DEPTH_BUFFER_BIT);
self.gl.enable(glow::DEPTH_TEST);
self.gl.clear(glow::COLOR_BUFFER_BIT | glow::DEPTH_BUFFER_BIT);
}
/*
unsafe {
self.shaders[0].Use();
self.tempData.projection = perspective(Deg(self.camera.Zoom), SCR_WIDTH as f32 /SCR_HEIGHT as f32, 0.1, 100.0);
@ -110,12 +112,16 @@ impl Scene{
self.shaders[0].setMat4("view", &view);
let mut model: Matrix4<f32> = Matrix4::from_axis_angle(vec3(0.0, -1.0, 0.0).normalize(),
cgmath::Rad(self.tempData.current_rad));
cgmath::Rad(1.0));
model = model * Matrix4::from_scale(0.2);
self.shaders[0].setMat4("model", &model);
}
self.Car.Draw(&self.shaders[0]);
*/
self.Car.update();
self.Car.turn(self.tempData.spinSpeed);
self.Car.Draw(&self.shaders[0], &self.camera);
if self.tempData.DebugMode{
self.handleDebug(events_loop, window, &mut ret)
@ -137,7 +143,10 @@ impl Scene{
}
Event::KeyDown {keycode: Some(Keycode::M), ..} => {
self.debug.addWindow(debug::OpenWindows::ModelInfo)
}
},
Event::KeyDown {keycode: Some(Keycode::W), ..} => {
self.camera.ProcessKeyboard(Camera_Movement::FORWARD, self.tempData.deltaTime);
},
_ => {}
}
@ -153,7 +162,7 @@ impl Scene{
for (id, window) in windows
{
match window {
debug::OpenWindows::ModelInfo => Callbacks.push(debug::ModelInfo(&self.Car)),
debug::OpenWindows::ModelInfo => Callbacks.push(debug::ModelInfo(&self.Car.Model)),
debug::OpenWindows::TestWind => Callbacks.push(debug::TestWind(self.tempData.spinSpeed, self.tempData.selectedModel)),
debug::OpenWindows::MainMenu => Callbacks.push(debug::Callback::MainMenu),
@ -167,7 +176,7 @@ impl Scene{
match command
{
dEvent::exit => *ret = SceneEnum::Exit,
dEvent::loadModel(i) => self.Car = Model::new(MODELS[i as usize], Arc::clone(&self.gl)),
dEvent::loadModel(i) => self.Car.Model = Model::new(MODELS[i as usize], Arc::clone(&self.gl)),
dEvent::setTest(a, b) => {
self.tempData.spinSpeed = a;
self.tempData.selectedModel = b;

74
src/scene/objects.rs Normal file
View File

@ -0,0 +1,74 @@
//! an Object Module for defining Objects
const SCR_WIDTH: u32 = 1600;
const SCR_HEIGHT: u32 = 900;
use cgmath::{Vector3, Quaternion, Rotation3};
use cgmath::prelude::*;
use crate::model::Model;
use crate::camera::Camera;
pub struct Transform {
Position: Vector3<f32>,
Rotation: Quaternion<f32>,
Velocity: Vector3<f32>,
Scale: f32,
}
impl Default for Transform {
fn default() -> Transform
{
Transform {
Position: Vector3::<f32>::new(0.0, 0.0, 0.0),
Rotation: Quaternion::from_angle_y(cgmath::Deg(0.0)),
Velocity: Vector3::<f32>::new(0.0, 0.0, 0.0),
Scale: 0.2,
}
}
}
pub struct Player {
pub Transform: Transform,
pub Model: Model
}
impl Player {
pub fn new(gl: std::sync::Arc<glow::Context>) -> Self {
Player {
Transform: Transform::default(),
Model: Model::new("resources/models/TestCarModel/CarW4.obj", gl),
}
}
pub fn update(&mut self)
{
self.Transform.Position += self.Transform.Velocity;
self.Transform.Rotation = self.Transform.Rotation.normalize();
}
pub fn turn(&mut self, amount: f32)
{
let up = Vector3::<f32>::unit_y();
let delta_rotation = Quaternion::from_axis_angle(up, cgmath::Deg(amount));
self.Transform.Rotation = self.Transform.Rotation * delta_rotation;
}
pub fn Draw(&self, shader: &crate::shader::shader, camera: &Camera)
{
shader.Use();
let translation = cgmath::Matrix4::from_translation(self.Transform.Position);
let rotation = cgmath::Matrix4::from(self.Transform.Rotation);
let scale = cgmath::Matrix4::from_scale(self.Transform.Scale);
let model = translation * rotation * scale;
let projection = cgmath::perspective(cgmath::Deg(camera.Zoom), SCR_WIDTH as f32 /SCR_HEIGHT as f32, 0.1, 100.0 );
let view = camera.GetViewMatrix();
shader.setMat4("projection", &projection);
shader.setMat4("view", &view);
shader.setMat4("model", &model);
self.Model.Draw(shader);
}
}