Skip to content

Commit d4e34ba

Browse files
MathLib: updated projection functions
1 parent a28ae84 commit d4e34ba

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

Common/interface/BasicMath.hpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -1710,10 +1710,13 @@ template <class T> struct Matrix4x4
17101710
};
17111711
}
17121712

1713-
1714-
void SetNearFarClipPlanes(T zNear, T zFar, T bIsGL)
1713+
// All graphics APIs except for OpenGL use [0, 1] as NDC Z range.
1714+
// OpenGL uses [-1, 1] unless glClipControl is used to change it.
1715+
// Use IRenderDevice::GetDeviceInfo().NDC to get the NDC Z range.
1716+
// See https://github.com/DiligentGraphics/DiligentCore/blob/master/doc/CoordinateSystem.md
1717+
void SetNearFarClipPlanes(T zNear, T zFar, bool NegativeOneToOneZ)
17151718
{
1716-
if (bIsGL)
1719+
if (NegativeOneToOneZ)
17171720
{
17181721
// https://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml
17191722
// http://www.terathon.com/gdc07_lengyel.pdf
@@ -1742,9 +1745,9 @@ template <class T> struct Matrix4x4
17421745
}
17431746
}
17441747

1745-
void GetNearFarClipPlanes(T& zNear, T& zFar, bool bIsGL) const
1748+
void GetNearFarClipPlanes(T& zNear, T& zFar, bool NegativeOneToOneZ) const
17461749
{
1747-
if (bIsGL)
1750+
if (NegativeOneToOneZ)
17481751
{
17491752
zNear = _43 / (-1 - _33);
17501753
zFar = _43 / (+1 - _33);
@@ -1756,23 +1759,23 @@ template <class T> struct Matrix4x4
17561759
}
17571760
}
17581761

1759-
static Matrix4x4 Projection(T fov, T aspectRatio, T zNear, T zFar, bool bIsGL) // Left-handed projection
1762+
static Matrix4x4 Projection(T fov, T aspectRatio, T zNear, T zFar, bool NegativeOneToOneZ) // Left-handed projection
17601763
{
17611764
Matrix4x4 mOut;
17621765
auto yScale = static_cast<T>(1) / std::tan(fov / static_cast<T>(2));
17631766
auto xScale = yScale / aspectRatio;
17641767
mOut._11 = xScale;
17651768
mOut._22 = yScale;
17661769

1767-
mOut.SetNearFarClipPlanes(zNear, zFar, bIsGL);
1770+
mOut.SetNearFarClipPlanes(zNear, zFar, NegativeOneToOneZ);
17681771

17691772
return mOut;
17701773
}
17711774

1772-
static Matrix4x4 OrthoOffCenter(T left, T right, T bottom, T top, T zNear, T zFar, bool bIsGL) // Left-handed ortho projection
1775+
static Matrix4x4 OrthoOffCenter(T left, T right, T bottom, T top, T zNear, T zFar, bool NegativeOneToOneZ) // Left-handed ortho projection
17731776
{
1774-
auto _22 = (bIsGL ? 2 : 1) / (zFar - zNear);
1775-
auto _32 = (bIsGL ? zNear + zFar : zNear) / (zNear - zFar);
1777+
auto _22 = (NegativeOneToOneZ ? 2 : 1) / (zFar - zNear);
1778+
auto _32 = (NegativeOneToOneZ ? zNear + zFar : zNear) / (zNear - zFar);
17761779
// clang-format off
17771780
return Matrix4x4
17781781
{
@@ -1784,14 +1787,14 @@ template <class T> struct Matrix4x4
17841787
// clang-format on
17851788
}
17861789

1787-
static Matrix4x4 Ortho(T width, T height, T zNear, T zFar, bool bIsGL) // Left-handed ortho projection
1790+
static Matrix4x4 Ortho(T width, T height, T zNear, T zFar, bool NegativeOneToOneZ) // Left-handed ortho projection
17881791
{
17891792
return OrthoOffCenter(
17901793
-width * static_cast<T>(0.5),
17911794
+width * static_cast<T>(0.5),
17921795
-height * static_cast<T>(0.5),
17931796
+height * static_cast<T>(0.5),
1794-
zNear, zFar, bIsGL);
1797+
zNear, zFar, NegativeOneToOneZ);
17951798
}
17961799

17971800
static Matrix4x4 Mul(const Matrix4x4& m1, const Matrix4x4& m2)

0 commit comments

Comments
 (0)