diff --git a/Source/Engine/Core/Math/Matrix.cpp b/Source/Engine/Core/Math/Matrix.cpp
index 028cc70b0..872468134 100644
--- a/Source/Engine/Core/Math/Matrix.cpp
+++ b/Source/Engine/Core/Math/Matrix.cpp
@@ -506,10 +506,10 @@ void Matrix::OrthoOffCenter(float left, float right, float bottom, float top, fl
result = Identity;
result.M11 = 2.0f / (right - left);
result.M22 = 2.0f / (top - bottom);
- result.M33 = zRange;
+ result.M33 = -zRange;
result.M41 = (left + right) / (left - right);
result.M42 = (top + bottom) / (bottom - top);
- result.M43 = -zNear * zRange;
+ result.M43 = zFar * zRange;
}
void Matrix::PerspectiveFov(float fov, float aspect, float zNear, float zFar, Matrix& result)
@@ -525,16 +525,16 @@ void Matrix::PerspectiveFov(float fov, float aspect, float zNear, float zFar, Ma
void Matrix::PerspectiveOffCenter(float left, float right, float bottom, float top, float zNear, float zFar, Matrix& result)
{
- const float zRange = zFar / (zFar - zNear);
+ const float zRange = zNear / (zFar - zNear);
result = Zero;
result.M11 = 2.0f * zNear / (right - left);
result.M22 = 2.0f * zNear / (top - bottom);
result.M31 = (left + right) / (left - right);
result.M32 = (top + bottom) / (bottom - top);
- result.M33 = zRange;
+ result.M33 = -zRange;
result.M34 = 1.0f;
- result.M43 = -zNear * zRange;
+ result.M43 = zFar * zRange;
}
void Matrix::RotationX(float angle, Matrix& result)
diff --git a/Source/Engine/Core/Math/Matrix.cs b/Source/Engine/Core/Math/Matrix.cs
index 34e6b4bec..34dcc9c52 100644
--- a/Source/Engine/Core/Math/Matrix.cs
+++ b/Source/Engine/Core/Math/Matrix.cs
@@ -2176,10 +2176,10 @@ namespace FlaxEngine
result = Identity;
result.M11 = 2.0f / (right - left);
result.M22 = 2.0f / (top - bottom);
- result.M33 = zRange;
+ result.M33 = -zRange;
result.M41 = (left + right) / (left - right);
result.M42 = (top + bottom) / (bottom - top);
- result.M43 = -znear * zRange;
+ result.M43 = zfar * zRange;
}
///
@@ -2238,14 +2238,14 @@ namespace FlaxEngine
public static void PerspectiveFov(float fov, float aspect, float znear, float zfar, out Matrix result)
{
var yScale = (float)(1.0f / Math.Tan(fov * 0.5f));
- var q = zfar / (zfar - znear);
+ var zRange = znear / (zfar - znear);
result = new Matrix
{
M11 = yScale / aspect,
M22 = yScale,
- M33 = q,
+ M33 = -zRange,
M34 = 1.0f,
- M43 = -q * znear,
+ M43 = zRange * zfar,
};
}
@@ -2275,16 +2275,16 @@ namespace FlaxEngine
/// When the method completes, contains the created projection matrix.
public static void PerspectiveOffCenter(float left, float right, float bottom, float top, float znear, float zfar, out Matrix result)
{
- float zRange = zfar / (zfar - znear);
+ float zRange = znear / (zfar - znear);
result = new Matrix
{
M11 = 2.0f * znear / (right - left),
M22 = 2.0f * znear / (top - bottom),
M31 = (left + right) / (left - right),
M32 = (top + bottom) / (bottom - top),
- M33 = zRange,
+ M33 = -zRange,
M34 = 1.0f,
- M43 = -znear * zRange,
+ M43 = zfar * zRange,
};
}
diff --git a/Source/Engine/Graphics/GPUContext.h b/Source/Engine/Graphics/GPUContext.h
index a222686db..6f450de1e 100644
--- a/Source/Engine/Graphics/GPUContext.h
+++ b/Source/Engine/Graphics/GPUContext.h
@@ -186,7 +186,7 @@ public:
///
/// The depth buffer to clear.
/// The clear depth value.
- API_FUNCTION() virtual void ClearDepth(GPUTextureView* depthBuffer, float depthValue = 1.0f) = 0;
+ API_FUNCTION() virtual void ClearDepth(GPUTextureView* depthBuffer, float depthValue = 0.0f) = 0;
///
/// Clears an unordered access buffer with a float value.
diff --git a/Source/Engine/Graphics/GPUDevice.cpp b/Source/Engine/Graphics/GPUDevice.cpp
index 0a4277a40..979907bf4 100644
--- a/Source/Engine/Graphics/GPUDevice.cpp
+++ b/Source/Engine/Graphics/GPUDevice.cpp
@@ -130,7 +130,7 @@ GPUPipelineState::Description GPUPipelineState::Description::Default =
true, // DepthEnable
true, // DepthWriteEnable
true, // DepthClipEnable
- ComparisonFunc::Less, // DepthFunc
+ ComparisonFunc::Greater, // DepthFunc
false, // StencilEnable
0xff, // StencilReadMask
0xff, // StencilWriteMask
@@ -154,7 +154,7 @@ GPUPipelineState::Description GPUPipelineState::Description::DefaultNoDepth =
false, // DepthEnable
false, // DepthWriteEnable
false, // DepthClipEnable
- ComparisonFunc::Less, // DepthFunc
+ ComparisonFunc::Greater, // DepthFunc
false, // StencilEnable
0xff, // StencilReadMask
0xff, // StencilWriteMask
@@ -178,7 +178,7 @@ GPUPipelineState::Description GPUPipelineState::Description::DefaultFullscreenTr
false, // DepthEnable
false, // DepthWriteEnable
false, // DepthClipEnable
- ComparisonFunc::Less, // DepthFunc
+ ComparisonFunc::Greater, // DepthFunc
false, // StencilEnable
0xff, // StencilReadMask
0xff, // StencilWriteMask
diff --git a/Source/Shaders/GBuffer.hlsl b/Source/Shaders/GBuffer.hlsl
index 9378b3be5..a35519992 100644
--- a/Source/Shaders/GBuffer.hlsl
+++ b/Source/Shaders/GBuffer.hlsl
@@ -27,13 +27,13 @@ Texture2D GBuffer3 : register(t4);
// Linearize raw device depth
float LinearizeZ(GBufferData gBuffer, float depth)
{
- return gBuffer.ViewInfo.w / (depth - gBuffer.ViewInfo.z);
+ return gBuffer.ViewInfo.w / ((1-depth) - gBuffer.ViewInfo.z);
}
// Convert linear depth to device depth
float LinearZ2DeviceDepth(GBufferData gBuffer, float linearDepth)
{
- return (gBuffer.ViewInfo.w / linearDepth) + gBuffer.ViewInfo.z;
+ return 1 - ((gBuffer.ViewInfo.w / linearDepth) + gBuffer.ViewInfo.z);
}
// Get view space position at given pixel coordinate with given device depth