Skip to content

Commit

Permalink
Deduplicate projection matrix check.
Browse files Browse the repository at this point in the history
  • Loading branch information
tormeh committed Sep 18, 2023
1 parent 0ce2fe2 commit c11cf62
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ impl Camera {
self.computed.projection_matrix
}

#[inline]
fn finite_projection_matrix(&self) -> Result<Mat4, ProjectionMatrixNotFiniteError> {
let projection_matrix = self.computed.projection_matrix;
if !projection_matrix.is_finite() {
return Err(ProjectionMatrixNotFiniteError(projection_matrix));
}
Ok(projection_matrix)
}

/// Given a position in world space, use the camera to compute the viewport-space coordinates.
///
/// To get the coordinates in Normalized Device Coordinates, you should use
Expand Down Expand Up @@ -295,13 +304,10 @@ impl Camera {
camera_transform_matrix,
));
}
// TODO: Do some code deduplication of these two snippets..
let projection_matrix = self.computed.projection_matrix;
if !projection_matrix.is_finite() {
return Err(ViewportToWorldError::ProjectionMatrixNotFiniteError(
projection_matrix,
));
}
// TODO: Do some code deduplication of the above snippet..
let projection_matrix = self
.finite_projection_matrix()
.map_err(ViewportToWorldError::ProjectionMatrixNotFiniteError)?;

let ndc_to_world = camera_transform_matrix * projection_matrix.inverse();
let world_near_plane = ndc_to_world.project_point3(ndc.extend(1.));
Expand Down Expand Up @@ -371,12 +377,9 @@ impl Camera {
));
}

let projection_matrix = self.computed.projection_matrix;
if !projection_matrix.is_finite() {
return Err(WorldToNdcError::ProjectionMatrixNotFiniteError(
projection_matrix,
));
}
let projection_matrix = self
.finite_projection_matrix()
.map_err(WorldToNdcError::ProjectionMatrixNotFiniteError)?;

// Build a transformation matrix to convert from world space to NDC using camera data
let world_to_ndc: Mat4 = projection_matrix * camera_transform_matrix.inverse();
Expand Down Expand Up @@ -412,12 +415,9 @@ impl Camera {
));
}

let projection_matrix = self.computed.projection_matrix;
if !projection_matrix.is_finite() {
return Err(NdcToWorldError::ProjectionMatrixNotFiniteError(
projection_matrix,
));
}
let projection_matrix = self
.finite_projection_matrix()
.map_err(NdcToWorldError::ProjectionMatrixNotFiniteError)?;

// Build a transformation matrix to convert from NDC to world space using camera data
let ndc_to_world = camera_transform_matrix * projection_matrix.inverse();
Expand All @@ -437,14 +437,14 @@ impl Camera {
pub enum NdcToWorldError {
WorldSpaceCoordsNotFiniteError(Vec3),
CameraTransformNotFiniteError(Mat4),
ProjectionMatrixNotFiniteError(Mat4),
ProjectionMatrixNotFiniteError(ProjectionMatrixNotFiniteError),
}

#[derive(Debug)]
pub enum WorldToNdcError {
NdcSpaceCoordsNotFiniteError(Vec3),
CameraTransformNotFiniteError(Mat4),
ProjectionMatrixNotFiniteError(Mat4),
ProjectionMatrixNotFiniteError(ProjectionMatrixNotFiniteError),
}

#[derive(Debug)]
Expand All @@ -458,7 +458,7 @@ pub struct LogicalViewportSizeError {
pub enum ViewportToWorldError {
LogicalViewportSizeError(LogicalViewportSizeError),
CameraTransformNotFiniteError(Mat4),
ProjectionMatrixNotFiniteError(Mat4),
ProjectionMatrixNotFiniteError(ProjectionMatrixNotFiniteError),
WorldNearPlaneNotFiniteError(Vec3),
WorldFarPlaneNanError(Vec3), //Presumably this being infinite is sometimes ok?
}
Expand All @@ -476,6 +476,9 @@ pub enum WorldToViewportError {
NdcCoordsOutsideFrustumError(Vec3),
}

#[derive(Debug)]
pub struct ProjectionMatrixNotFiniteError(Mat4);

/// Control how this camera outputs once rendering is completed.
#[derive(Debug, Clone, Copy)]
pub enum CameraOutputMode {
Expand Down

0 comments on commit c11cf62

Please sign in to comment.