Compare commits
1 commit
7724808d14
...
837828a774
Author | SHA1 | Date | |
---|---|---|---|
837828a774 |
2 changed files with 14 additions and 64 deletions
|
@ -1,12 +1,11 @@
|
|||
use crate::prelude::*;
|
||||
use utilities::prelude::cgmath::{
|
||||
EuclideanSpace, InnerSpace, Matrix4, One, Point3, Rad, Vector3, Zero, vec3,
|
||||
vec3, EuclideanSpace, InnerSpace, Matrix4, One, Point3, Rad, Vector3, Zero,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Camera {
|
||||
eye_offset: Vector3<f32>,
|
||||
eye_dir: Vector3<f32>,
|
||||
center: Vector3<f32>,
|
||||
center_offset: Vector3<f32>,
|
||||
up_vector: Vector3<f32>,
|
||||
|
@ -16,7 +15,6 @@ pub struct Camera {
|
|||
eye_rotation: f32,
|
||||
|
||||
view: Matrix4<f32>,
|
||||
look_at: bool,
|
||||
|
||||
update: bool,
|
||||
}
|
||||
|
@ -25,7 +23,6 @@ impl Camera {
|
|||
pub fn new(fov: f32) -> Camera {
|
||||
let mut me = Camera {
|
||||
eye_offset: Vector3::zero(),
|
||||
eye_dir: Vector3::unit_y(),
|
||||
center: Vector3::zero(),
|
||||
center_offset: Vector3::zero(),
|
||||
up_vector: vec3(0.0, 0.0, 1.0),
|
||||
|
@ -35,7 +32,6 @@ impl Camera {
|
|||
eye_rotation: 0.0,
|
||||
|
||||
view: Matrix4::one(),
|
||||
look_at: true,
|
||||
|
||||
update: true,
|
||||
};
|
||||
|
@ -45,11 +41,6 @@ impl Camera {
|
|||
me
|
||||
}
|
||||
|
||||
pub fn look_at(&mut self, look_at: bool) {
|
||||
self.look_at = look_at;
|
||||
self.update = true;
|
||||
}
|
||||
|
||||
pub fn set_fov(&mut self, fov: f32) {
|
||||
let fovy = fov.to_radians() / 2.0;
|
||||
|
||||
|
@ -103,15 +94,10 @@ impl Camera {
|
|||
}
|
||||
|
||||
// view matrix handling
|
||||
fn eye_position(&self) -> Vector3<f32> {
|
||||
pub fn eye_position(&self) -> Vector3<f32> {
|
||||
rotate_z(self.eye_offset, self.eye_rotation) + self.center + self.center_offset
|
||||
}
|
||||
|
||||
pub fn set_eye_dir(&mut self, eye_dir: Vector3<f32>) {
|
||||
self.eye_dir = eye_dir;
|
||||
self.update = true;
|
||||
}
|
||||
|
||||
pub fn view_matrix(&mut self) -> Matrix4<f32> {
|
||||
if self.update {
|
||||
self.calculate_view_matrix();
|
||||
|
@ -126,19 +112,11 @@ impl Camera {
|
|||
|
||||
pub fn calculate_view_matrix(&mut self) {
|
||||
self.update = false;
|
||||
self.view = if self.look_at {
|
||||
Matrix4::look_at_rh(
|
||||
Point3::from_vec(self.eye_position()),
|
||||
Point3::from_vec(self.center + self.center_offset),
|
||||
self.up_vector,
|
||||
)
|
||||
} else {
|
||||
Matrix4::look_to_rh(
|
||||
Point3::from_vec(self.center + self.center_offset),
|
||||
self.eye_dir,
|
||||
self.up_vector,
|
||||
)
|
||||
};
|
||||
self.view = Matrix4::look_at_rh(
|
||||
Point3::from_vec(self.eye_position()),
|
||||
Point3::from_vec(self.center + self.center_offset),
|
||||
self.up_vector,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn tan(&self) -> f32 {
|
||||
|
@ -162,11 +140,6 @@ impl Camera {
|
|||
self.up_vector
|
||||
}
|
||||
|
||||
pub fn set_up(&mut self, up: Vector3<f32>) {
|
||||
self.up_vector = up;
|
||||
self.update = true;
|
||||
}
|
||||
|
||||
// calculation helper
|
||||
pub fn add_rotation(&mut self, rotation: f32) {
|
||||
self.eye_rotation += rotation;
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
use std::{path::Path, time::Duration};
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
use ecs::*;
|
||||
use engine::prelude::{
|
||||
cgmath::{Deg, Matrix3, Vector3, vec3},
|
||||
*,
|
||||
};
|
||||
use engine::prelude::{cgmath::vec3, *};
|
||||
use skybox::SkyBox;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
|
@ -15,7 +12,7 @@ fn main() -> Result<()> {
|
|||
Engine::new::<GameState>(EngineCreateInfo::default(), &mut world_builder)?;
|
||||
|
||||
world_builder.add_system(GameState::update);
|
||||
world_builder.resources.insert(GameState::default());
|
||||
world_builder.resources.insert(GameState::Startup);
|
||||
|
||||
// let dir = Path::new("C:/Users/M.Huebner/Downloads/Space Skybox Generator/Export");
|
||||
let dir = Path::new("/home/michaelh/Sync/skybox");
|
||||
|
@ -35,17 +32,13 @@ fn main() -> Result<()> {
|
|||
let view = world_builder.resources.get_mut::<Scene>().view_mut();
|
||||
let camera = view.camera_mut();
|
||||
|
||||
camera.look_at(false);
|
||||
camera.set_center(vec3(0.0, 0.0, 0.0));
|
||||
camera.set_eye_dir(Vector3::unit_y());
|
||||
camera.set_eye_offset(vec3(0.0, 1.0, 0.0));
|
||||
view.update_buffer()?;
|
||||
|
||||
world_builder.build().run()
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
enum GameState {
|
||||
#[default]
|
||||
Startup,
|
||||
Loading,
|
||||
Menu,
|
||||
|
@ -54,10 +47,8 @@ enum GameState {
|
|||
|
||||
impl GameState {
|
||||
fn update(world: &mut World) -> Result<bool> {
|
||||
let me = world.resources.get_mut_unchecked::<Self>();
|
||||
|
||||
match me {
|
||||
GameState::Startup => *me = GameState::Game(Game { start: world.now() }),
|
||||
match world.resources.get_mut_unchecked::<Self>() {
|
||||
GameState::Startup => (),
|
||||
GameState::Loading => (),
|
||||
GameState::Menu => (),
|
||||
GameState::Game(game) => game.update(world)?,
|
||||
|
@ -80,24 +71,10 @@ impl EventConsumer for GameState {
|
|||
}
|
||||
}
|
||||
|
||||
struct Game {
|
||||
start: Duration,
|
||||
}
|
||||
struct Game {}
|
||||
|
||||
impl Game {
|
||||
fn update(&mut self, world: &mut World) -> Result<()> {
|
||||
let now = world.now();
|
||||
let view = world.resources.get_mut::<Scene>().view_mut();
|
||||
let camera = view.camera_mut();
|
||||
|
||||
camera.set_eye_dir(
|
||||
Matrix3::from_angle_z(Deg((now.as_secs_f32() - self.start.as_secs_f32())
|
||||
* 36.0
|
||||
* 2.0))
|
||||
* Vector3::unit_y(),
|
||||
);
|
||||
view.update_buffer()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue