Dairy-Drift/src/scene/objects.rs

75 lines
2.2 KiB
Rust

//! 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);
}
}