Skip to content

Commit

Permalink
Add Gf.Frustum.
Browse files Browse the repository at this point in the history
Signed-off-by: furby™ <[email protected]>
  • Loading branch information
furby-tm committed Sep 17, 2024
1 parent bb5959f commit 9bd56e3
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Sources/Gf/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ GfFrustum GfCamera::GetFrustum() const
const GfRange1d clippingRange(_clippingRange.GetMin(), _clippingRange.GetMax());

const GfFrustum::ProjectionType projection = _projection == Orthographic ?
GfFrustum::Orthographic :
GfFrustum::Perspective;
GfFrustum::ProjectionType::ProjectionTypeOrthographic :
GfFrustum::ProjectionType::ProjectionTypePerspective;

return GfFrustum(_transform, window, clippingRange, projection);
}
Expand Down
36 changes: 18 additions & 18 deletions Sources/Gf/frustum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ TF_REGISTRY_FUNCTION(TfType)

TF_REGISTRY_FUNCTION(TfEnum)
{
TF_ADD_ENUM_NAME(GfFrustum::Orthographic);
TF_ADD_ENUM_NAME(GfFrustum::Perspective);
TF_ADD_ENUM_NAME(GfFrustum::ProjectionType::ProjectionTypeOrthographic);
TF_ADD_ENUM_NAME(GfFrustum::ProjectionType::ProjectionTypePerspective);
}

GfFrustum::GfFrustum()
: _position(0),
_window(GfVec2d(-1.0, -1.0), GfVec2d(1.0, 1.0)),
_nearFar(1.0, 10.0),
_viewDistance(5.0),
_projectionType(GfFrustum::Perspective),
_projectionType(GfFrustum::ProjectionType::ProjectionTypePerspective),
_planes(nullptr)
{
_rotation.SetIdentity();
Expand Down Expand Up @@ -97,7 +97,7 @@ void GfFrustum::SetPerspective(double fieldOfView,
double nearDistance,
double farDistance)
{
_projectionType = GfFrustum::Perspective;
_projectionType = GfFrustum::ProjectionType::ProjectionTypePerspective;

double yDist = 1.0;
double xDist = 1.0;
Expand Down Expand Up @@ -142,7 +142,7 @@ bool GfFrustum::GetPerspective(bool isFovVertical,
double *nearDistance,
double *farDistance) const
{
if (_projectionType != GfFrustum::Perspective)
if (_projectionType != GfFrustum::ProjectionType::ProjectionTypePerspective)
return false;

GfVec2d winSize = _window.GetSize();
Expand All @@ -165,7 +165,7 @@ double GfFrustum::GetFOV(bool isFovVertical /* = false */)
{
double result = 0.0;

if (GetProjectionType() == GfFrustum::Perspective) {
if (GetProjectionType() == GfFrustum::ProjectionType::ProjectionTypePerspective) {
double aspectRatio;
double nearDistance;
double farDistance;
Expand All @@ -179,7 +179,7 @@ double GfFrustum::GetFOV(bool isFovVertical /* = false */)
void GfFrustum::SetOrthographic(
double left, double right, double bottom, double top, double nearPlane, double farPlane)
{
_projectionType = GfFrustum::Orthographic;
_projectionType = GfFrustum::ProjectionType::ProjectionTypeOrthographic;

_window.SetMin(GfVec2d(left, bottom));
_window.SetMax(GfVec2d(right, top));
Expand All @@ -196,7 +196,7 @@ bool GfFrustum::GetOrthographic(double *left,
double *nearPlane,
double *farPlane) const
{
if (_projectionType != GfFrustum::Orthographic)
if (_projectionType != GfFrustum::ProjectionType::ProjectionTypeOrthographic)
return false;

*left = _window.GetMin()[0];
Expand All @@ -218,7 +218,7 @@ void GfFrustum::FitToSphere(const GfVec3d &center, double radius, double slack)
// and top) coordinates of the frustum as necessary.
//

if (_projectionType == GfFrustum::Orthographic) {
if (_projectionType == GfFrustum::ProjectionType::ProjectionTypeOrthographic) {
// Set the distance so the viewpoint is outside the sphere.
_viewDistance = radius + slack;
// Set the camera window to enclose the sphere.
Expand Down Expand Up @@ -454,7 +454,7 @@ GfFrustum &GfFrustum::Transform(const GfMatrix4d &matrix)
// y coordinates can be directly used to construct the new
// transformed reference plane. Skip the scaling step for an
// orthographic projection, though.
if (_projectionType == Perspective) {
if (_projectionType == GfFrustum::ProjectionType::ProjectionTypePerspective) {
leftBottom /= scale;
rightTop /= scale;
}
Expand Down Expand Up @@ -534,7 +534,7 @@ GfMatrix4d GfFrustum::ComputeProjectionMatrix() const
const double tb = t - b;
const double fn = f - n;

if (_projectionType == GfFrustum::Orthographic) {
if (_projectionType == GfFrustum::ProjectionType::ProjectionTypeOrthographic) {
matrix[0][0] = 2.0 / rl;
matrix[1][1] = 2.0 / tb;
matrix[2][2] = -2.0 / fn;
Expand Down Expand Up @@ -583,7 +583,7 @@ vector<GfVec3d> GfFrustum::ComputeCorners() const
vector<GfVec3d> corners;
corners.reserve(8);

if (_projectionType == Perspective) {
if (_projectionType == GfFrustum::ProjectionType::ProjectionTypePerspective) {
// Compute the eye-space corners of the near-plane and
// far-plane frustum rectangles using similar triangles. The
// reference plane in which the window rectangle is defined is
Expand Down Expand Up @@ -631,7 +631,7 @@ vector<GfVec3d> GfFrustum::ComputeCornersAtDistance(double d) const
vector<GfVec3d> corners;
corners.reserve(4);

if (_projectionType == Perspective) {
if (_projectionType == GfFrustum::ProjectionType::ProjectionTypePerspective) {
// Similar to ComputeCorners
corners.push_back(GfVec3d(d * winMin[0], d * winMin[1], -d));
corners.push_back(GfVec3d(d * winMax[0], d * winMin[1], -d));
Expand Down Expand Up @@ -682,7 +682,7 @@ GfFrustum GfFrustum::ComputeNarrowedFrustum(const GfVec3d &worldPoint, const GfV
}

GfVec2d windowPoint(camSpacePoint[0], camSpacePoint[1]);
if (_projectionType == Perspective) {
if (_projectionType == GfFrustum::ProjectionType::ProjectionTypePerspective) {
// project the camera space point to the reference plane (-1 to 1)
// XXX Note: If we ever allow reference plane depth to be other
// than 1.0, we'll need to revisit this.
Expand Down Expand Up @@ -732,7 +732,7 @@ static GfRay _ComputeUntransformedRay(GfFrustum::ProjectionType projectionType,
// direction (toward the point on the window).
GfVec3d pos;
GfVec3d dir;
if (projectionType == GfFrustum::Perspective) {
if (projectionType == GfFrustum::ProjectionType::ProjectionTypePerspective) {
// Note that the ray is starting at the origin and not
// the near plane.
pos = GfVec3d(0);
Expand Down Expand Up @@ -776,7 +776,7 @@ GfRay GfFrustum::ComputeRay(const GfVec3d &worldSpacePos) const
// direction (toward the point camSpaceToPos).
GfVec3d pos;
GfVec3d dir;
if (_projectionType == Perspective) {
if (_projectionType == GfFrustum::ProjectionType::ProjectionTypePerspective) {
pos = GfVec3d(0);
dir = camSpaceToPos.GetNormalized();
}
Expand All @@ -802,7 +802,7 @@ GfRay GfFrustum::ComputePickRay(const GfVec3d &worldSpacePos) const
// direction (toward the point camSpaceToPos).
GfVec3d pos;
GfVec3d dir;
if (_projectionType == Perspective) {
if (_projectionType == GfFrustum::ProjectionType::ProjectionTypePerspective) {
pos = GfVec3d(0);
dir = camSpaceToPos.GetNormalized();
}
Expand Down Expand Up @@ -1071,7 +1071,7 @@ void GfFrustum::_CalculateFrustumPlanes() const
// corners of the near-plane frustum rectangle to define the 4
// planes forming the left, right, top, and bottom sides of the
// frustum.
if (_projectionType == GfFrustum::Perspective) {
if (_projectionType == GfFrustum::ProjectionType::ProjectionTypePerspective) {

//
// Get the eye-space viewpoint (the origin) and the four corners
Expand Down
16 changes: 8 additions & 8 deletions Sources/Gf/include/Gf/frustum.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ class GfFrustum {
public:
/// This enum is used to determine the type of projection represented by a
/// frustum.
enum ProjectionType {
Orthographic, ///< Orthographic projection
Perspective, ///< Perspective projection
enum class ProjectionType {
ProjectionTypeOrthographic, ///< Orthographic projection
ProjectionTypePerspective, ///< Perspective projection
};

/// This constructor creates an instance with default viewing parameters:
Expand All @@ -84,7 +84,7 @@ class GfFrustum {
/// \li The window is -1 to +1 in both dimensions.
/// \li The near/far interval is (1, 10).
/// \li The view distance is 5.0.
/// \li The projection type is \c GfFrustum::Perspective.
/// \li The projection type is \c GfFrustum::ProjectionType::ProjectionTypePerspective.
GF_API GfFrustum();

/// Copy constructor.
Expand Down Expand Up @@ -320,7 +320,7 @@ class GfFrustum {

/// Sets up the frustum in a manner similar to \c gluPerspective().
///
/// It sets the projection type to \c GfFrustum::Perspective and sets the
/// It sets the projection type to \c GfFrustum::ProjectionType::ProjectionTypePerspective and sets the
/// window specification so that the resulting symmetric frustum encloses
/// an angle of \p fieldOfViewHeight degrees in the vertical direction,
/// with \p aspectRatio used to figure the angle in the horizontal
Expand All @@ -342,7 +342,7 @@ class GfFrustum {

/// Sets up the frustum in a manner similar to gluPerspective().
///
/// It sets the projection type to \c GfFrustum::Perspective and
/// It sets the projection type to \c GfFrustum::ProjectionType::ProjectionTypePerspective and
/// sets the window specification so that:
///
/// If \a isFovVertical is true, the resulting symmetric frustum encloses
Expand Down Expand Up @@ -395,7 +395,7 @@ class GfFrustum {
/// The displayed fov is a function of the focal length or FOV avar. The
/// frustum's fov may be different due to things like lens breathing.
///
/// If the frustum is not of type \c GfFrustum::Perspective, the returned
/// If the frustum is not of type \c GfFrustum::ProjectionType::ProjectionTypePerspective, the returned
/// FOV will be 0.0.
///
/// \note The default value for \c isFovVertical is false so calling \c
Expand All @@ -405,7 +405,7 @@ class GfFrustum {

/// Sets up the frustum in a manner similar to \c glOrtho().
///
/// Sets the projection to \c GfFrustum::Orthographic and sets the window
/// Sets the projection to \c GfFrustum::ProjectionType::ProjectionTypeOrthographic and sets the window
/// and near/far specifications based on the given values.
GF_API
void SetOrthographic(
Expand Down
Loading

0 comments on commit 9bd56e3

Please sign in to comment.