Skip to content

Commit a8533dd

Browse files
Revert "* 修改摄像机为左手坐标系"
This reverts commit c8bb31c.
1 parent 9afa1de commit a8533dd

17 files changed

+301
-597
lines changed

Chapter 11 Stenciling/Core/Graphics/Camera.cpp

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,62 @@ using namespace Math;
1919

2020
void BaseCamera::SetLookDirection( Vector3 forward, Vector3 up )
2121
{
22-
// 计算前方
22+
// Given, but ensure normalization
2323
Scalar forwardLenSq = LengthSquare(forward);
24-
forward = Select(forward * RecipSqrt(forwardLenSq), Vector3(kZUnitVector), forwardLenSq < Scalar(0.000001f));
24+
forward = Select(forward * RecipSqrt(forwardLenSq), -Vector3(kZUnitVector), forwardLenSq < Scalar(0.000001f));
2525

26-
// 根据提供的上和前方,计算右方
27-
Vector3 right = Cross(up, forward);
26+
// Deduce a valid, orthogonal right vector
27+
Vector3 right = Cross(forward, up);
2828
Scalar rightLenSq = LengthSquare(right);
29-
right = Select(right * RecipSqrt(rightLenSq), Cross(Vector3(kYUnitVector), forward), rightLenSq < Scalar(0.000001f));
29+
right = Select(right * RecipSqrt(rightLenSq), Quaternion(Vector3(kYUnitVector), -XM_PIDIV2) * forward, rightLenSq < Scalar(0.000001f));
3030

31-
// 正交化,计算实际的上方
32-
up = Cross(forward, right);
31+
// Compute actual up vector
32+
up = Cross(right, forward);
3333

34-
// 计算摄像机的转换矩阵
35-
m_Basis = Matrix3(right, up, forward);
34+
// Finish constructing basis
35+
m_Basis = Matrix3(right, up, -forward);
3636
m_CameraToWorld.SetRotation(Quaternion(m_Basis));
3737
}
3838

3939
void BaseCamera::Update()
4040
{
41-
// 计算视角变换矩阵,还没有看懂 m_CameraToWorld
41+
m_PreviousViewProjMatrix = m_ViewProjMatrix;
42+
4243
m_ViewMatrix = Matrix4(~m_CameraToWorld);
43-
44-
// Matrix4中的*重载,故意反着写的。所以这里反着乘
45-
// 计算视角投影转换矩阵。这样拿到世界矩阵再乘以这个值就可以算出最终的投影坐标了
4644
m_ViewProjMatrix = m_ProjMatrix * m_ViewMatrix;
45+
m_ReprojectMatrix = m_PreviousViewProjMatrix * Invert(GetViewProjMatrix());
46+
47+
m_FrustumVS = Frustum( m_ProjMatrix );
48+
m_FrustumWS = m_CameraToWorld * m_FrustumVS;
4749
}
4850

51+
4952
void Camera::UpdateProjMatrix( void )
5053
{
51-
DirectX::XMMATRIX mat = XMMatrixPerspectiveFovLH(m_VerticalFOV, m_AspectRatio, m_NearClip, m_FarClip);
54+
float Y = 1.0f / std::tanf( m_VerticalFOV * 0.5f );
55+
float X = Y * m_AspectRatio;
5256

53-
SetProjMatrix(Matrix4(mat));
54-
}
57+
float Q1, Q2;
58+
59+
// ReverseZ puts far plane at Z=0 and near plane at Z=1. This is never a bad idea, and it's
60+
// actually a great idea with F32 depth buffers to redistribute precision more evenly across
61+
// the entire range. It requires clearing Z to 0.0f and using a GREATER variant depth test.
62+
// Some care must also be done to properly reconstruct linear W in a pixel shader from hyperbolic Z.
63+
if (m_ReverseZ)
64+
{
65+
Q1 = m_NearClip / (m_FarClip - m_NearClip);
66+
Q2 = Q1 * m_FarClip;
67+
}
68+
else
69+
{
70+
Q1 = m_FarClip / (m_NearClip - m_FarClip);
71+
Q2 = Q1 * m_NearClip;
72+
}
73+
74+
SetProjMatrix( Matrix4(
75+
Vector4( X, 0.0f, 0.0f, 0.0f ),
76+
Vector4( 0.0f, Y, 0.0f, 0.0f ),
77+
Vector4( 0.0f, 0.0f, Q1, -1.0f ),
78+
Vector4( 0.0f, 0.0f, Q2, 0.0f )
79+
) );
80+
}

Chapter 11 Stenciling/Core/Graphics/Camera.h

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ namespace Math
4545
const Matrix4& GetViewMatrix() const { return m_ViewMatrix; }
4646
const Matrix4& GetProjMatrix() const { return m_ProjMatrix; }
4747
const Matrix4& GetViewProjMatrix() const { return m_ViewProjMatrix; }
48+
const Matrix4& GetReprojectionMatrix() const { return m_ReprojectMatrix; }
49+
const Frustum& GetViewSpaceFrustum() const { return m_FrustumVS; }
50+
const Frustum& GetWorldSpaceFrustum() const { return m_FrustumWS; }
4851

4952
protected:
5053

@@ -57,19 +60,29 @@ namespace Math
5760
// Redundant data cached for faster lookups.
5861
Matrix3 m_Basis;
5962

60-
// 0 矩阵变换
61-
// 1. 渲染目标从模型坐标系转到世界坐标系--->世界变换矩阵
62-
// 2. 再从世界坐标系转到视角坐标系--->视角变换矩阵 m_ViewMatrix
63-
// 3. 从视角坐标系转换到投影坐标系--->投影变换矩阵 m_ProjMatrix
64-
65-
// 世界坐标系转换到视角坐标系
63+
// Transforms homogeneous coordinates from world space to view space. In this case, view space is defined as +X is
64+
// to the right, +Y is up, and -Z is forward. This has to match what the projection matrix expects, but you might
65+
// also need to know what the convention is if you work in view space in a shader.
6666
Matrix4 m_ViewMatrix; // i.e. "World-to-View" matrix
6767

68-
// 视角坐标系转到投影坐标系
68+
// The projection matrix transforms view space to clip space. Once division by W has occurred, the final coordinates
69+
// can be transformed by the viewport matrix to screen space. The projection matrix is determined by the screen aspect
70+
// and camera field of view. A projection matrix can also be orthographic. In that case, field of view would be defined
71+
// in linear units, not angles.
6972
Matrix4 m_ProjMatrix; // i.e. "View-to-Projection" matrix
7073

71-
// 从世界坐标系直接转换到投影坐标系
74+
// A concatenation of the view and projection matrices.
7275
Matrix4 m_ViewProjMatrix; // i.e. "World-To-Projection" matrix.
76+
77+
// The view-projection matrix from the previous frame
78+
Matrix4 m_PreviousViewProjMatrix;
79+
80+
// Projects a clip-space coordinate to the previous frame (useful for temporal effects).
81+
Matrix4 m_ReprojectMatrix;
82+
83+
Frustum m_FrustumVS; // View-space view frustum
84+
Frustum m_FrustumWS; // World-space view frustum
85+
7386
};
7487

7588
class Camera : public BaseCamera
@@ -78,14 +91,16 @@ namespace Math
7891
Camera();
7992

8093
// Controls the view-to-projection matrix
81-
void SetPerspectiveMatrix( float verticalFovRadians, float aspectWidthOverHeight, float nearZClip, float farZClip );
94+
void SetPerspectiveMatrix( float verticalFovRadians, float aspectHeightOverWidth, float nearZClip, float farZClip );
8295
void SetFOV( float verticalFovInRadians ) { m_VerticalFOV = verticalFovInRadians; UpdateProjMatrix(); }
83-
void SetAspectRatio( float widthOverHeight) { m_AspectRatio = widthOverHeight; UpdateProjMatrix(); }
96+
void SetAspectRatio( float heightOverWidth ) { m_AspectRatio = heightOverWidth; UpdateProjMatrix(); }
8497
void SetZRange( float nearZ, float farZ) { m_NearClip = nearZ; m_FarClip = farZ; UpdateProjMatrix(); }
98+
void ReverseZ( bool enable ) { m_ReverseZ = enable; UpdateProjMatrix(); }
8599

86100
float GetFOV() const { return m_VerticalFOV; }
87101
float GetNearClip() const { return m_NearClip; }
88102
float GetFarClip() const { return m_FarClip; }
103+
float GetClearDepth() const { return m_ReverseZ ? 0.0f : 1.0f; }
89104

90105
private:
91106

@@ -95,6 +110,7 @@ namespace Math
95110
float m_AspectRatio;
96111
float m_NearClip;
97112
float m_FarClip;
113+
bool m_ReverseZ; // Invert near and far clip distances so that Z=0 is the far plane
98114
};
99115

100116
inline void BaseCamera::SetEyeAtUp( Vector3 eye, Vector3 at, Vector3 up )
@@ -121,19 +137,21 @@ namespace Math
121137
m_Basis = Matrix3(m_CameraToWorld.GetRotation());
122138
}
123139

124-
inline Camera::Camera()
140+
inline Camera::Camera() : m_ReverseZ(true)
125141
{
126-
SetPerspectiveMatrix( XM_PIDIV4, 16.0f / 9.0f, 1.0f, 1000.0f );
142+
SetPerspectiveMatrix( XM_PIDIV4, 9.0f / 16.0f, 1.0f, 1000.0f );
127143
}
128144

129-
inline void Camera::SetPerspectiveMatrix( float verticalFovRadians, float aspectWidthOverHeight, float nearZClip, float farZClip )
145+
inline void Camera::SetPerspectiveMatrix( float verticalFovRadians, float aspectHeightOverWidth, float nearZClip, float farZClip )
130146
{
131147
m_VerticalFOV = verticalFovRadians;
132-
m_AspectRatio = aspectWidthOverHeight;
148+
m_AspectRatio = aspectHeightOverWidth;
133149
m_NearClip = nearZClip;
134150
m_FarClip = farZClip;
135151

136152
UpdateProjMatrix();
153+
154+
m_PreviousViewProjMatrix = m_ViewProjMatrix;
137155
}
138156

139157
} // namespace Math

Chapter 11 Stenciling/Core/Shaders/defaultPS.hlsl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ cbuffer cbMaterial : register(b2)
5151
{
5252
float4 gDiffuseAlbedo;
5353
float3 gFresnelR0;
54-
float gPad;
5554
float gRoughness;
5655
};
5756

@@ -94,7 +93,7 @@ float4 main(VertexOut pin) : SV_Target0
9493
float4 litColor = ambient + directLight;
9594

9695
// 雾,如果雾的颜色alpha为0,则不处理
97-
if (gFogColor.a > 0.01)
96+
if (gFogColor.a > 0)
9897
{
9998
float fogAmount = saturate((distToEye - gFogStart) / gFogRange);
10099
litColor = lerp(litColor, gFogColor, fogAmount);

0 commit comments

Comments
 (0)