Skip to content

Commit c8bb31c

Browse files
author
BlueRoom
committed
* 修改摄像机为左手坐标系
* ps shader一点点容错 + 添加第11章比较基础的部分。绘制墙、镜子、地板
1 parent 95dd395 commit c8bb31c

17 files changed

+597
-301
lines changed

Chapter 11 Stenciling/Core/Graphics/Camera.cpp

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

2020
void BaseCamera::SetLookDirection( Vector3 forward, Vector3 up )
2121
{
22-
// Given, but ensure normalization
22+
// 计算前方
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-
// Deduce a valid, orthogonal right vector
27-
Vector3 right = Cross(forward, up);
26+
// 根据提供的上和前方,计算右方
27+
Vector3 right = Cross(up, forward);
2828
Scalar rightLenSq = LengthSquare(right);
29-
right = Select(right * RecipSqrt(rightLenSq), Quaternion(Vector3(kYUnitVector), -XM_PIDIV2) * forward, rightLenSq < Scalar(0.000001f));
29+
right = Select(right * RecipSqrt(rightLenSq), Cross(Vector3(kYUnitVector), forward), rightLenSq < Scalar(0.000001f));
3030

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

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

3939
void BaseCamera::Update()
4040
{
41-
m_PreviousViewProjMatrix = m_ViewProjMatrix;
42-
41+
// 计算视角变换矩阵,还没有看懂 m_CameraToWorld
4342
m_ViewMatrix = Matrix4(~m_CameraToWorld);
43+
44+
// Matrix4中的*重载,故意反着写的。所以这里反着乘
45+
// 计算视角投影转换矩阵。这样拿到世界矩阵再乘以这个值就可以算出最终的投影坐标了
4446
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;
4947
}
5048

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

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-
}
53+
SetProjMatrix(Matrix4(mat));
54+
}

Chapter 11 Stenciling/Core/Graphics/Camera.h

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ 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; }
5148

5249
protected:
5350

@@ -60,29 +57,19 @@ namespace Math
6057
// Redundant data cached for faster lookups.
6158
Matrix3 m_Basis;
6259

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.
60+
// 0 矩阵变换
61+
// 1. 渲染目标从模型坐标系转到世界坐标系--->世界变换矩阵
62+
// 2. 再从世界坐标系转到视角坐标系--->视角变换矩阵 m_ViewMatrix
63+
// 3. 从视角坐标系转换到投影坐标系--->投影变换矩阵 m_ProjMatrix
64+
65+
// 世界坐标系转换到视角坐标系
6666
Matrix4 m_ViewMatrix; // i.e. "World-to-View" matrix
6767

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.
68+
// 视角坐标系转到投影坐标系
7269
Matrix4 m_ProjMatrix; // i.e. "View-to-Projection" matrix
7370

74-
// A concatenation of the view and projection matrices.
71+
// 从世界坐标系直接转换到投影坐标系
7572
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-
8673
};
8774

8875
class Camera : public BaseCamera
@@ -91,16 +78,14 @@ namespace Math
9178
Camera();
9279

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

10086
float GetFOV() const { return m_VerticalFOV; }
10187
float GetNearClip() const { return m_NearClip; }
10288
float GetFarClip() const { return m_FarClip; }
103-
float GetClearDepth() const { return m_ReverseZ ? 0.0f : 1.0f; }
10489

10590
private:
10691

@@ -110,7 +95,6 @@ namespace Math
11095
float m_AspectRatio;
11196
float m_NearClip;
11297
float m_FarClip;
113-
bool m_ReverseZ; // Invert near and far clip distances so that Z=0 is the far plane
11498
};
11599

116100
inline void BaseCamera::SetEyeAtUp( Vector3 eye, Vector3 at, Vector3 up )
@@ -137,21 +121,19 @@ namespace Math
137121
m_Basis = Matrix3(m_CameraToWorld.GetRotation());
138122
}
139123

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

145-
inline void Camera::SetPerspectiveMatrix( float verticalFovRadians, float aspectHeightOverWidth, float nearZClip, float farZClip )
129+
inline void Camera::SetPerspectiveMatrix( float verticalFovRadians, float aspectWidthOverHeight, float nearZClip, float farZClip )
146130
{
147131
m_VerticalFOV = verticalFovRadians;
148-
m_AspectRatio = aspectHeightOverWidth;
132+
m_AspectRatio = aspectWidthOverHeight;
149133
m_NearClip = nearZClip;
150134
m_FarClip = farZClip;
151135

152136
UpdateProjMatrix();
153-
154-
m_PreviousViewProjMatrix = m_ViewProjMatrix;
155137
}
156138

157139
} // namespace Math

Chapter 11 Stenciling/Core/Shaders/defaultPS.hlsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ cbuffer cbMaterial : register(b2)
5151
{
5252
float4 gDiffuseAlbedo;
5353
float3 gFresnelR0;
54+
float gPad;
5455
float gRoughness;
5556
};
5657

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

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

0 commit comments

Comments
 (0)