Skip to content

Commit

Permalink
Compute screen2ray on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
asny committed Nov 27, 2024
1 parent 9dd9be4 commit 832f486
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ pub struct Camera {
up: Vec3,
view: Mat4,
projection: Mat4,
screen2ray: Mat4,
}

impl Camera {
Expand Down Expand Up @@ -295,7 +294,6 @@ impl Camera {
self.projection_type = ProjectionType::Perspective { field_of_view_y };
self.projection =
cgmath::perspective(field_of_view_y, self.viewport.aspect(), z_near, z_far);
self.update_screen2ray();
}

///
Expand All @@ -320,7 +318,6 @@ impl Camera {
zoom * z_near,
zoom * z_far,
);
self.update_screen2ray();
}

///
Expand Down Expand Up @@ -360,7 +357,6 @@ impl Camera {
if let ProjectionType::Orthographic { height } = self.projection_type {
self.set_orthographic_projection(height, self.z_near, self.z_far);
}
self.update_screen2ray();
}

/// Returns the [Frustum] for this camera.
Expand Down Expand Up @@ -389,7 +385,7 @@ impl Camera {
ProjectionType::Orthographic { .. } => {
let coords = coords.into();
let screen_pos = vec4(2. * coords.u - 1., 2. * coords.v - 1.0, -1.0, 1.);
(self.screen2ray * screen_pos).truncate()
(self.screen2ray() * screen_pos).truncate()
}
ProjectionType::Perspective { .. } => self.position,
}
Expand Down Expand Up @@ -417,7 +413,7 @@ impl Camera {
ProjectionType::Perspective { .. } => {
let coords = coords.into();
let screen_pos = vec4(2. * coords.u - 1., 2. * coords.v - 1.0, 0., 1.);
(self.screen2ray * screen_pos).truncate().normalize()
(self.screen2ray() * screen_pos).truncate().normalize()
}
}
}
Expand Down Expand Up @@ -561,18 +557,17 @@ impl Camera {
up: vec3(0.0, 1.0, 0.0),
view: Mat4::identity(),
projection: Mat4::identity(),
screen2ray: Mat4::identity(),
}
}

fn update_screen2ray(&mut self) {
fn screen2ray(&self) -> Mat4 {
let mut v = self.view;
if let ProjectionType::Perspective { .. } = self.projection_type {
v[3] = vec4(0.0, 0.0, 0.0, 1.0);
}
self.screen2ray = (self.projection * v)
(self.projection * v)
.invert()
.unwrap_or_else(|| Mat4::identity());
.unwrap_or_else(|| Mat4::identity())
}

///
Expand Down

0 comments on commit 832f486

Please sign in to comment.