create ChaseCamera struct

This commit is contained in:
2025-10-02 16:18:34 +02:00
parent 0f81c1bdc8
commit a8c4d897bb
2 changed files with 47 additions and 18 deletions

View File

@@ -57,17 +57,42 @@ pub enum Nodes {
SceneRoot,
}
pub struct ChaseCamera {
pub position: glm::Vec3,
pub target: glm::Vec3,
pub radius: f32, // set to big number to get normal camera
pub perspective: glm::Mat4,
pitch: f32,
yaw: f32
}
impl ChaseCamera {
pub fn new(radius: f32, perspective: glm::Mat4) -> Self {
ChaseCamera {
position: glm::vec3(100.0, 35.0, 0.0),
target: glm::vec3(0.0, 0.0, 0.0),
radius,
perspective,
pitch: 0.0,
yaw: 0.0
}
}
}
pub struct World {
pub nodes: HashMap<Nodes, Vec<ManuallyDrop<Pin<Box<SceneNode>>>>>,
pub anim_ctxs: Vec<AnimCTX>
pub anim_ctxs: Vec<AnimCTX>,
pub camera: ChaseCamera,
pub model_transformation: glm::Mat4
}
impl World {
pub fn new() -> Self {
pub fn new(radius: f32, perspective: glm::Mat4) -> Self {
let meshes = Self::load_models();
let nodes = Self::setup_scene_graph(&meshes);
let anim_ctxs:Vec<AnimCTX> = Vec::new();
World { nodes, anim_ctxs }
let camera = ChaseCamera::new(radius, perspective);
World { nodes, anim_ctxs, camera, model_transformation: glm::identity() }
}
fn load_models() -> HashMap<Nodes, Mesh> {
@@ -147,8 +172,15 @@ impl World {
return nodes;
}
pub fn update(&mut self, elapsed: f32, perspective: glm::Mat4, transformation: glm::Mat4, shader: &Shader) {
let transform_thus_far = perspective * transformation;
fn view_matrix(&self) -> glm::Mat4 {
glm::look_at(
&self.camera.position,
&self.camera.target,
&glm::vec3(0.0, 1.0, 0.0)
)
}
pub fn update(&mut self, elapsed: f32, shader: &Shader) {
let rotor_speed = 60.0;
for i in 0..self.nodes[&Nodes::HeliRoot].len() {

View File

@@ -91,16 +91,13 @@ fn main() {
}
let mut transformation: glm::Mat4;
let perspective: glm::Mat4 = glm::perspective(
let mut world = draw::World::new(100.0, glm::perspective(
window_aspect_ratio,
120.0f32,
1.0f32,
1000.0f32
);
));
let mut world = draw::World::new();
let mut camera_position: glm::Vec3 = glm::vec3(0.0, 0.0, 0.0);
let translation_speed = 30.0f32;
let rotation_speed = 1.5f32;
@@ -159,22 +156,22 @@ fn main() {
for key in keys.iter() {
match key {
VirtualKeyCode::W => {
camera_position += forward * trans_speed;
world.camera.position += forward * trans_speed;
}
VirtualKeyCode::A => {
camera_position -= right * trans_speed;
world.camera.position -= right * trans_speed;
}
VirtualKeyCode::S => {
camera_position -= forward * trans_speed;
world.camera.position -= forward * trans_speed;
}
VirtualKeyCode::D => {
camera_position += right * trans_speed;
world.camera.position += right * trans_speed;
}
VirtualKeyCode::Space => {
camera_position.y -= trans_speed;
world.camera.position.y -= trans_speed;
}
VirtualKeyCode::LShift => {
camera_position.y += trans_speed;
world.camera.position.y += trans_speed;
}
VirtualKeyCode::E => {
world.anim_ctxs.push(AnimCTX{
@@ -219,7 +216,7 @@ fn main() {
let yaw_matrix = glm::rotation(yaw, &glm::vec3(0.0, 1.0, 0.0));
let pitch_matrix = glm::rotation(pitch, &glm::vec3(1.0, 0.0, 0.0));
let translation_matrix = glm::translation(&(camera_position));
let translation_matrix = glm::translation(&(world.camera.position));
// apply yaw dependent pitch first.
let rotation_matrix = pitch_matrix * yaw_matrix;
@@ -232,7 +229,7 @@ fn main() {
gl::ClearColor(40.0f32 / 256.0f32, 42.0f32 / 256.0f32, 54.0f32 / 256.0f32, 1.0); // night sky
gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
world.update(elapsed, perspective, transformation, &simple_shader);
world.update(elapsed, &simple_shader);
// Display the new color buffer on the display
context.swap_buffers().unwrap(); // we use "double buffering" to avoid artifacts