Compare commits
1 Commits
Author | SHA1 | Date |
---|---|---|
Milk.H | 0a712b4c72 |
29
src/scene.rs
29
src/scene.rs
|
@ -1,6 +1,7 @@
|
||||||
|
|
||||||
use crate::model::Model;
|
use crate::model::Model;
|
||||||
use crate::camera::Camera;
|
use crate::camera::Camera;
|
||||||
|
use crate::camera::Camera_Movement;
|
||||||
use crate::shader::shader;
|
use crate::shader::shader;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
@ -9,6 +10,7 @@ use glow::*;
|
||||||
use imgui_glow_renderer::AutoRenderer;
|
use imgui_glow_renderer::AutoRenderer;
|
||||||
use imgui_sdl2_support::SdlPlatform;
|
use imgui_sdl2_support::SdlPlatform;
|
||||||
use crate::debug;
|
use crate::debug;
|
||||||
|
mod objects;
|
||||||
use cgmath::{Matrix4, vec3, Point3, Deg, perspective};
|
use cgmath::{Matrix4, vec3, Point3, Deg, perspective};
|
||||||
use cgmath::prelude::*;
|
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",
|
const MODELS: [&str; 3]= ["resources/models/TestCarModel/CarW4.obj", "resources/models/TestCarModel/CarW1.obj",
|
||||||
"resources/models/TestCarModel/CarW0.obj"];
|
"resources/models/TestCarModel/CarW0.obj"];
|
||||||
|
|
||||||
|
|
||||||
/// XXX this is temporary, thus the name
|
/// XXX this is temporary, thus the name
|
||||||
pub struct tempData {
|
pub struct tempData {
|
||||||
pub projection: Matrix4<f32>,
|
pub projection: Matrix4<f32>,
|
||||||
|
@ -44,7 +45,7 @@ pub enum SceneEnum {
|
||||||
|
|
||||||
pub struct Scene {
|
pub struct Scene {
|
||||||
gl: Arc<glow::Context>,
|
gl: Arc<glow::Context>,
|
||||||
pub Car: Model,
|
pub Car: objects::Player,
|
||||||
camera: Camera,
|
camera: Camera,
|
||||||
debug: Debug,
|
debug: Debug,
|
||||||
shaders: Vec<Rc<shader>>,
|
shaders: Vec<Rc<shader>>,
|
||||||
|
@ -55,7 +56,7 @@ pub struct Scene {
|
||||||
impl Scene{
|
impl Scene{
|
||||||
pub fn new(gl: Arc<glow::Context>) -> 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)));
|
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.currentFrame = self.time.elapsed().as_secs_f32();
|
||||||
self.tempData.deltaTime = self.tempData.currentFrame - self.tempData.lastFrame;
|
self.tempData.deltaTime = self.tempData.currentFrame - self.tempData.lastFrame;
|
||||||
self.tempData.lastFrame = self.tempData.currentFrame;
|
self.tempData.lastFrame = self.tempData.currentFrame;
|
||||||
self.tempData.current_rad += self.tempData.spinSpeed/15.0;
|
|
||||||
let mut ret = SceneEnum::Resume;
|
let mut ret = SceneEnum::Resume;
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
self.gl.enable(glow::DEPTH_TEST);
|
self.gl.enable(glow::DEPTH_TEST);
|
||||||
self.gl.clear(glow::COLOR_BUFFER_BIT | glow::DEPTH_BUFFER_BIT);
|
self.gl.clear(glow::COLOR_BUFFER_BIT | glow::DEPTH_BUFFER_BIT);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
unsafe {
|
||||||
|
|
||||||
self.shaders[0].Use();
|
self.shaders[0].Use();
|
||||||
self.tempData.projection = perspective(Deg(self.camera.Zoom), SCR_WIDTH as f32 /SCR_HEIGHT as f32, 0.1, 100.0);
|
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);
|
self.shaders[0].setMat4("view", &view);
|
||||||
|
|
||||||
let mut model: Matrix4<f32> = Matrix4::from_axis_angle(vec3(0.0, -1.0, 0.0).normalize(),
|
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);
|
model = model * Matrix4::from_scale(0.2);
|
||||||
self.shaders[0].setMat4("model", &model);
|
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{
|
if self.tempData.DebugMode{
|
||||||
self.handleDebug(events_loop, window, &mut ret)
|
self.handleDebug(events_loop, window, &mut ret)
|
||||||
|
@ -137,7 +143,10 @@ impl Scene{
|
||||||
}
|
}
|
||||||
Event::KeyDown {keycode: Some(Keycode::M), ..} => {
|
Event::KeyDown {keycode: Some(Keycode::M), ..} => {
|
||||||
self.debug.addWindow(debug::OpenWindows::ModelInfo)
|
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
|
for (id, window) in windows
|
||||||
{
|
{
|
||||||
match window {
|
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::TestWind => Callbacks.push(debug::TestWind(self.tempData.spinSpeed, self.tempData.selectedModel)),
|
||||||
debug::OpenWindows::MainMenu => Callbacks.push(debug::Callback::MainMenu),
|
debug::OpenWindows::MainMenu => Callbacks.push(debug::Callback::MainMenu),
|
||||||
|
|
||||||
|
@ -167,7 +176,7 @@ impl Scene{
|
||||||
match command
|
match command
|
||||||
{
|
{
|
||||||
dEvent::exit => *ret = SceneEnum::Exit,
|
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) => {
|
dEvent::setTest(a, b) => {
|
||||||
self.tempData.spinSpeed = a;
|
self.tempData.spinSpeed = a;
|
||||||
self.tempData.selectedModel = b;
|
self.tempData.selectedModel = b;
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue