Skip to content

Commit 032bc09

Browse files
+ 水体的光
1 parent b1fd815 commit 032bc09

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

Chapter 8 Lighting/GameApp.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,23 @@ void GameApp::Update(float deltaT)
169169

170170
if (!m_bRenderShapes)
171171
UpdateWaves(deltaT);
172+
173+
// land and wave light
174+
const float dt = deltaT;
175+
176+
if (GameInput::IsPressed(GameInput::kKey_left))
177+
mSunTheta -= 1.0f * dt;
178+
179+
if (GameInput::IsPressed(GameInput::kKey_right))
180+
mSunTheta += 1.0f * dt;
181+
182+
if(GameInput::IsPressed(GameInput::kKey_up))
183+
mSunPhi -= 1.0f * dt;
184+
185+
if (GameInput::IsPressed(GameInput::kKey_down))
186+
mSunPhi += 1.0f * dt;
187+
188+
mSunPhi = Clamp(mSunPhi, 0.1f, XM_PIDIV2);
172189
}
173190

174191
void GameApp::RenderScene(void)
@@ -509,12 +526,38 @@ XMFLOAT3 GameApp::GetHillsNormal(float x, float z)const
509526

510527
void GameApp::renderLandAndWaves(GraphicsContext& gfxContext)
511528
{
529+
gfxContext.SetDynamicConstantBufferView(0, sizeof(m_waveWorld), &m_waveWorld);
530+
531+
PassConstants psc;
532+
// https://www.cnblogs.com/X-Jun/p/9808727.html
533+
// C++代码端进行转置,HLSL中使用matrix(列矩阵)
534+
// mul函数让向量放在左边(行向量),实际运算是(行向量 X 行矩阵) 然后行矩阵为了使用dp4运算发生了转置成了列矩阵
535+
psc.viewProj = Transpose(m_ViewProjMatrix);
536+
psc.eyePosW = m_Camera.GetPosition();
537+
psc.ambientLight = { 0.25f, 0.25f, 0.35f, 1.0f };
538+
539+
XMVECTOR lightDir = -DirectX::XMVectorSet(
540+
1.0f * sinf(mSunTheta) * cosf(mSunTheta),
541+
1.0f * cosf(mSunTheta),
542+
1.0f * sinf(mSunTheta) * sinf(mSunTheta),
543+
1.0f);
544+
XMStoreFloat3(&psc.Lights[0].Direction, lightDir);
545+
psc.Lights[0].Strength = { 1.0f, 1.0f, 0.9f };
546+
547+
gfxContext.SetDynamicConstantBufferView(1, sizeof(psc), &psc);
548+
512549
// land
513550
// 设置顶点视图
514551
gfxContext.SetVertexBuffer(0, m_VertexBufferLand.VertexBufferView());
515552
// 设置索引视图
516553
gfxContext.SetIndexBuffer(m_IndexBufferLand.IndexBufferView());
517554

555+
MaterialConstants mc;
556+
mc.DiffuseAlbedo = { 0.2f, 0.6f, 0.2f, 1.0f };
557+
mc.FresnelR0 = { 0.01f, 0.01f, 0.01f };
558+
mc.Roughness = 0.125f;
559+
gfxContext.SetDynamicConstantBufferView(2, sizeof(mc), &mc);
560+
518561
// 绘制
519562
gfxContext.DrawIndexedInstanced(m_IndexBufferLand.GetElementCount(), 1, 0, 0, 0);
520563

@@ -523,6 +566,10 @@ void GameApp::renderLandAndWaves(GraphicsContext& gfxContext)
523566

524567
gfxContext.SetDynamicVB(0, m_verticesWaves.size(), sizeof(Vertex), m_verticesWaves.data());
525568

569+
mc.DiffuseAlbedo = { 0.0f, 0.2f, 0.6f, 1.0f };
570+
mc.FresnelR0 = { 0.1f, 0.1f, 0.1f };
571+
mc.Roughness = 0.0f;
572+
gfxContext.SetDynamicConstantBufferView(2, sizeof(mc), &mc);
526573
// 绘制
527574
gfxContext.DrawIndexedInstanced(m_IndexBufferWaves.GetElementCount(), 1, 0, 0, 0);
528575
}

Chapter 8 Lighting/GameApp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class GameApp : public GameCore::IGameApp
7878
ByteAddressBuffer m_IndexBufferWaves;
7979
std::vector<Vertex> m_verticesWaves;
8080
Matrix4 m_waveWorld = Transpose(Matrix4(kIdentity));
81+
float mSunTheta = 1.25f * XM_PI;
82+
float mSunPhi = XM_PIDIV4;
8183

8284
// 摄像机
8385
Camera m_Camera;

0 commit comments

Comments
 (0)