Compare commits

..

2 commits

Author SHA1 Message Date
7724808d14 Update Rust crate rusqlite to 0.34.0 2025-03-08 15:02:29 +00:00
8e7ea3e8b9 Rotate view in example 2025-03-08 16:01:20 +01:00
2 changed files with 64 additions and 14 deletions

View file

@ -1,11 +1,12 @@
use crate::prelude::*; use crate::prelude::*;
use utilities::prelude::cgmath::{ use utilities::prelude::cgmath::{
vec3, EuclideanSpace, InnerSpace, Matrix4, One, Point3, Rad, Vector3, Zero, EuclideanSpace, InnerSpace, Matrix4, One, Point3, Rad, Vector3, Zero, vec3,
}; };
#[derive(Clone)] #[derive(Clone)]
pub struct Camera { pub struct Camera {
eye_offset: Vector3<f32>, eye_offset: Vector3<f32>,
eye_dir: Vector3<f32>,
center: Vector3<f32>, center: Vector3<f32>,
center_offset: Vector3<f32>, center_offset: Vector3<f32>,
up_vector: Vector3<f32>, up_vector: Vector3<f32>,
@ -15,6 +16,7 @@ pub struct Camera {
eye_rotation: f32, eye_rotation: f32,
view: Matrix4<f32>, view: Matrix4<f32>,
look_at: bool,
update: bool, update: bool,
} }
@ -23,6 +25,7 @@ impl Camera {
pub fn new(fov: f32) -> Camera { pub fn new(fov: f32) -> Camera {
let mut me = Camera { let mut me = Camera {
eye_offset: Vector3::zero(), eye_offset: Vector3::zero(),
eye_dir: Vector3::unit_y(),
center: Vector3::zero(), center: Vector3::zero(),
center_offset: Vector3::zero(), center_offset: Vector3::zero(),
up_vector: vec3(0.0, 0.0, 1.0), up_vector: vec3(0.0, 0.0, 1.0),
@ -32,6 +35,7 @@ impl Camera {
eye_rotation: 0.0, eye_rotation: 0.0,
view: Matrix4::one(), view: Matrix4::one(),
look_at: true,
update: true, update: true,
}; };
@ -41,6 +45,11 @@ impl Camera {
me 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) { pub fn set_fov(&mut self, fov: f32) {
let fovy = fov.to_radians() / 2.0; let fovy = fov.to_radians() / 2.0;
@ -94,10 +103,15 @@ impl Camera {
} }
// view matrix handling // view matrix handling
pub fn eye_position(&self) -> Vector3<f32> { fn eye_position(&self) -> Vector3<f32> {
rotate_z(self.eye_offset, self.eye_rotation) + self.center + self.center_offset 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> { pub fn view_matrix(&mut self) -> Matrix4<f32> {
if self.update { if self.update {
self.calculate_view_matrix(); self.calculate_view_matrix();
@ -112,11 +126,19 @@ impl Camera {
pub fn calculate_view_matrix(&mut self) { pub fn calculate_view_matrix(&mut self) {
self.update = false; self.update = false;
self.view = Matrix4::look_at_rh( self.view = if self.look_at {
Point3::from_vec(self.eye_position()), Matrix4::look_at_rh(
Point3::from_vec(self.center + self.center_offset), Point3::from_vec(self.eye_position()),
self.up_vector, 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,
)
};
} }
pub fn tan(&self) -> f32 { pub fn tan(&self) -> f32 {
@ -140,6 +162,11 @@ impl Camera {
self.up_vector self.up_vector
} }
pub fn set_up(&mut self, up: Vector3<f32>) {
self.up_vector = up;
self.update = true;
}
// calculation helper // calculation helper
pub fn add_rotation(&mut self, rotation: f32) { pub fn add_rotation(&mut self, rotation: f32) {
self.eye_rotation += rotation; self.eye_rotation += rotation;

View file

@ -1,9 +1,12 @@
use std::path::Path; use std::{path::Path, time::Duration};
use anyhow::Result; use anyhow::Result;
use ecs::*; use ecs::*;
use engine::prelude::{cgmath::vec3, *}; use engine::prelude::{
cgmath::{Deg, Matrix3, Vector3, vec3},
*,
};
use skybox::SkyBox; use skybox::SkyBox;
fn main() -> Result<()> { fn main() -> Result<()> {
@ -12,7 +15,7 @@ fn main() -> Result<()> {
Engine::new::<GameState>(EngineCreateInfo::default(), &mut world_builder)?; Engine::new::<GameState>(EngineCreateInfo::default(), &mut world_builder)?;
world_builder.add_system(GameState::update); world_builder.add_system(GameState::update);
world_builder.resources.insert(GameState::Startup); world_builder.resources.insert(GameState::default());
// let dir = Path::new("C:/Users/M.Huebner/Downloads/Space Skybox Generator/Export"); // let dir = Path::new("C:/Users/M.Huebner/Downloads/Space Skybox Generator/Export");
let dir = Path::new("/home/michaelh/Sync/skybox"); let dir = Path::new("/home/michaelh/Sync/skybox");
@ -32,13 +35,17 @@ fn main() -> Result<()> {
let view = world_builder.resources.get_mut::<Scene>().view_mut(); let view = world_builder.resources.get_mut::<Scene>().view_mut();
let camera = view.camera_mut(); let camera = view.camera_mut();
camera.set_eye_offset(vec3(0.0, 1.0, 0.0)); camera.look_at(false);
camera.set_center(vec3(0.0, 0.0, 0.0));
camera.set_eye_dir(Vector3::unit_y());
view.update_buffer()?; view.update_buffer()?;
world_builder.build().run() world_builder.build().run()
} }
#[derive(Default)]
enum GameState { enum GameState {
#[default]
Startup, Startup,
Loading, Loading,
Menu, Menu,
@ -47,8 +54,10 @@ enum GameState {
impl GameState { impl GameState {
fn update(world: &mut World) -> Result<bool> { fn update(world: &mut World) -> Result<bool> {
match world.resources.get_mut_unchecked::<Self>() { let me = world.resources.get_mut_unchecked::<Self>();
GameState::Startup => (),
match me {
GameState::Startup => *me = GameState::Game(Game { start: world.now() }),
GameState::Loading => (), GameState::Loading => (),
GameState::Menu => (), GameState::Menu => (),
GameState::Game(game) => game.update(world)?, GameState::Game(game) => game.update(world)?,
@ -71,10 +80,24 @@ impl EventConsumer for GameState {
} }
} }
struct Game {} struct Game {
start: Duration,
}
impl Game { impl Game {
fn update(&mut self, world: &mut World) -> Result<()> { 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(()) Ok(())
} }