@@ -169,6 +169,23 @@ void GameApp::Update(float deltaT)
169
169
170
170
if (!m_bRenderShapes)
171
171
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);
172
189
}
173
190
174
191
void GameApp::RenderScene (void )
@@ -509,12 +526,38 @@ XMFLOAT3 GameApp::GetHillsNormal(float x, float z)const
509
526
510
527
void GameApp::renderLandAndWaves (GraphicsContext& gfxContext)
511
528
{
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
+
512
549
// land
513
550
// 设置顶点视图
514
551
gfxContext.SetVertexBuffer (0 , m_VertexBufferLand.VertexBufferView ());
515
552
// 设置索引视图
516
553
gfxContext.SetIndexBuffer (m_IndexBufferLand.IndexBufferView ());
517
554
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
+
518
561
// 绘制
519
562
gfxContext.DrawIndexedInstanced (m_IndexBufferLand.GetElementCount (), 1 , 0 , 0 , 0 );
520
563
@@ -523,6 +566,10 @@ void GameApp::renderLandAndWaves(GraphicsContext& gfxContext)
523
566
524
567
gfxContext.SetDynamicVB (0 , m_verticesWaves.size (), sizeof (Vertex), m_verticesWaves.data ());
525
568
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);
526
573
// 绘制
527
574
gfxContext.DrawIndexedInstanced (m_IndexBufferWaves.GetElementCount (), 1 , 0 , 0 , 0 );
528
575
}
0 commit comments