Skip to content

Commit

Permalink
Use optionals in frustum
Browse files Browse the repository at this point in the history
  • Loading branch information
levinli303 committed Aug 8, 2023
1 parent 434938b commit 0236e5a
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/celengine/fisheyeprojectionmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ float FisheyeProjectionMode::getFieldCorrection(float /*zoom*/) const
}

celmath::Frustum
FisheyeProjectionMode::getFrustum(float nearZ, float farZ, float zoom) const
FisheyeProjectionMode::getFrustum(float nearZ, std::optional<float> farZ, float zoom) const
{
return celmath::Frustum(getFOV(zoom), width / height, nearZ, farZ);
}
Expand Down
2 changes: 1 addition & 1 deletion src/celengine/fisheyeprojectionmode.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FisheyeProjectionMode : public ProjectionMode
float getZoom(float fov) const override;
float getPixelSize(float zoom) const override;
float getFieldCorrection(float zoom) const override;
celmath::Frustum getFrustum(float nearZ, float farZ, float zoom) const override;
celmath::Frustum getFrustum(float nearZ, std::optional<float> farZ, float zoom) const override;
double getViewConeAngleMax(float zoom) const override;

float getNormalizedDeviceZ(float nearZ, float farZ, float z) const override;
Expand Down
2 changes: 1 addition & 1 deletion src/celengine/perspectiveprojectionmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ float PerspectiveProjectionMode::getFieldCorrection(float zoom) const
}

celmath::Frustum
PerspectiveProjectionMode::getFrustum(float nearZ, float farZ, float zoom) const
PerspectiveProjectionMode::getFrustum(float nearZ, std::optional<float> farZ, float zoom) const
{
return celmath::Frustum(getFOV(zoom), width / height, nearZ, farZ);
}
Expand Down
2 changes: 1 addition & 1 deletion src/celengine/perspectiveprojectionmode.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PerspectiveProjectionMode : public ProjectionMode
float getZoom(float fov) const override;
float getPixelSize(float zoom) const override;
float getFieldCorrection(float zoom) const override;
celmath::Frustum getFrustum(float nearZ, float farZ, float zoom) const override;
celmath::Frustum getFrustum(float nearZ, std::optional<float> farZ, float zoom) const override;
double getViewConeAngleMax(float zoom) const override;

float getNormalizedDeviceZ(float nearZ, float farZ, float z) const override;
Expand Down
3 changes: 2 additions & 1 deletion src/celengine/projectionmode.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once

#include <Eigen/Core>
#include <optional>

class ShaderManager;

Expand All @@ -36,7 +37,7 @@ class ProjectionMode
virtual float getZoom(float fov) const = 0;
virtual float getPixelSize(float zoom) const = 0;
virtual float getFieldCorrection(float zoom) const = 0;
virtual celmath::Frustum getFrustum(float nearZ, float farZ, float zoom) const = 0;
virtual celmath::Frustum getFrustum(float nearZ, std::optional<float> farZ, float zoom) const = 0;

// Calculate the cosine of half the maximum field of view. We'll use this for
// fast testing of object visibility.
Expand Down
4 changes: 2 additions & 2 deletions src/celengine/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,7 @@ void Renderer::draw(const Observer& observer,
m_cameraOrientation = Quaterniond(m_cameraTransform) * observer.getOrientation();

// Get the view frustum used for culling in camera space.
auto frustum = projectionMode->getFrustum(MinNearPlaneDistance, std::numeric_limits<float>::infinity(), zoom);
auto frustum = projectionMode->getFrustum(MinNearPlaneDistance, std::nullopt, zoom);

// Get the transformed frustum, used for culling in the astrocentric coordinate
// system.
Expand Down Expand Up @@ -3939,7 +3939,7 @@ void Renderer::renderDeepSkyObjects(const Universe& universe,
dsoRenderer.renderFlags = renderFlags;
dsoRenderer.labelMode = labelMode;

dsoRenderer.frustum = projectionMode->getFrustum(MinNearPlaneDistance, std::numeric_limits<float>::infinity(), observer.getZoom());
dsoRenderer.frustum = projectionMode->getFrustum(MinNearPlaneDistance, std::nullopt, observer.getZoom());
// Use pixelSize * screenDpi instead of FoV, to eliminate windowHeight dependence.
// = 1.0 at startup
float effDistanceToScreen = mmToInches((float) REF_DISTANCE_TO_SCREEN) * pixelSize * getScreenDpi();
Expand Down
12 changes: 6 additions & 6 deletions src/celmath/frustum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ Frustum::Frustum(float fov, float aspectRatio, float n) :
init(fov, aspectRatio, n, n);
}

Frustum::Frustum(float fov, float aspectRatio, float n, float f) :
infinite(std::isinf(f))
Frustum::Frustum(float fov, float aspectRatio, float n, std::optional<float> f) :
infinite(!f.has_value())
{
init(fov, aspectRatio, n, infinite ? n : f);
init(fov, aspectRatio, n, infinite ? n : f.value());
}

Frustum::Frustum(float l, float r, float t, float b, float n, float f) :
infinite(std::isinf(f))
Frustum::Frustum(float l, float r, float t, float b, float n, std::optional<float> f) :
infinite(!f.has_value())
{
init(l, r, t, b, n, infinite ? n : f);
init(l, r, t, b, n, infinite ? n : f.value());
}

void Frustum::init(float fov, float aspectRatio, float n, float f)
Expand Down
5 changes: 3 additions & 2 deletions src/celmath/frustum.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <Eigen/Core>
#include <Eigen/Geometry>

#include <optional>

namespace celmath
{
Expand All @@ -22,8 +23,8 @@ class Frustum
using PlaneType = Eigen::Hyperplane<float, 3>;

Frustum(float fov, float aspectRatio, float nearDist);
Frustum(float fov, float aspectRatio, float nearDist, float farDist);
Frustum(float left, float right, float top, float bottom, float nearDist, float farDist);
Frustum(float fov, float aspectRatio, float nearDist, std::optional<float> farDist);
Frustum(float left, float right, float top, float bottom, float nearDist, std::optional<float> farDist);

inline Eigen::Hyperplane<float, 3> plane(unsigned int which) const
{
Expand Down

0 comments on commit 0236e5a

Please sign in to comment.