Dairy-Drift/src/scene/objects.rs

120 lines
3.2 KiB
Rust
Raw Normal View History

//! 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 Input {
pub Accel: bool,
pub Decel: bool,
pub Left: bool,
pub Right: bool
}
impl Default for Input {
fn default() -> Input {
Input {
Accel: false,
Decel: false,
Left: false,
Right: false
}
}
}
pub struct Transform {
2023-03-30 10:46:33 +02:00
pub Position: Vector3<f32>,
pub Rotation: Quaternion<f32>,
pub Velocity: Vector3<f32>,
pub 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,
}
}
}
2023-03-30 10:46:33 +02:00
impl Transform {
pub fn setPos(&mut self, input: Vector3<f32>)
{
self.Position = input;
}
}
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, input: &Input)
{
if input.Accel {
self.forward(0.01);
}
if input.Decel {
self.forward(-0.01)
}
if input.Left {
self.turn(1.5)
}
if input.Right {
self.turn(-1.5)
}
self.Transform.Position += self.Transform.Velocity;
self.Transform.Rotation = self.Transform.Rotation.normalize();
}
pub fn forward(&mut self, amount: f32)
2023-03-30 15:57:25 +02:00
{
let forward = self.Transform.Rotation.rotate_vector(cgmath::Vector3::unit_z());
let distance = forward * amount;
2023-03-30 15:57:25 +02:00
self.Transform.Position += distance;
}
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);
}
}