@@ -38,8 +38,9 @@ void GameApp::Startup(void)
38
38
buildLandAndWaves ();
39
39
40
40
// 根签名
41
- m_RootSignature.Reset (1 , 0 );
41
+ m_RootSignature.Reset (2 , 0 );
42
42
m_RootSignature[0 ].InitAsConstantBuffer (0 , D3D12_SHADER_VISIBILITY_VERTEX);
43
+ m_RootSignature[1 ].InitAsConstantBuffer (1 , D3D12_SHADER_VISIBILITY_ALL);
43
44
m_RootSignature.Finalize (L" box signature" , D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
44
45
45
46
D3D12_INPUT_ELEMENT_DESC mInputLayout [] =
@@ -192,6 +193,19 @@ void GameApp::RenderScene(void)
192
193
// 设置顶点拓扑结构
193
194
gfxContext.SetPrimitiveTopology (D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
194
195
196
+ // 设置VS、PS通用的常量缓冲区
197
+ __declspec (align (16 )) struct
198
+ {
199
+ Matrix4 viewProj; // 从世界坐标转为投影坐标的矩阵
200
+ }passConstants;
201
+
202
+ // https://www.cnblogs.com/X-Jun/p/9808727.html
203
+ // C++代码端进行转置,HLSL中使用matrix(列矩阵)
204
+ // mul函数让向量放在左边(行向量),实际运算是(行向量 X 行矩阵) 然后行矩阵为了使用dp4运算发生了转置成了列矩阵
205
+ passConstants.viewProj = Transpose (m_ViewProjMatrix);
206
+ gfxContext.SetDynamicConstantBufferView (1 , sizeof (passConstants), &passConstants);
207
+
208
+ // 开始绘制
195
209
if (m_bRenderShapes)
196
210
renderShapes (gfxContext);
197
211
else
@@ -269,26 +283,26 @@ void GameApp::buildShapesData()
269
283
270
284
renderItem item;
271
285
// box
272
- item.modelToWorld = Matrix4 (AffineTransform (Matrix3::MakeScale (2 .0f , 2 .0f , 2 .0f ), Vector3 (0 .0f , 0 .5f , 0 .0f )));
286
+ item.modelToWorld = Transpose ( Matrix4 (AffineTransform (Matrix3::MakeScale (2 .0f , 2 .0f , 2 .0f ), Vector3 (0 .0f , 0 .5f , 0 .0f ) )));
273
287
item.indexCount = (UINT)box.Indices32 .size ();
274
288
item.startIndex = boxIndexOffset;
275
289
item.baseVertex = boxVertexOffset;
276
290
m_vecShapes.push_back (item);
277
291
278
292
// grid
279
- item.modelToWorld = Matrix4 (kIdentity );
293
+ item.modelToWorld = Transpose ( Matrix4 (kIdentity ) );
280
294
item.indexCount = (UINT)grid.Indices32 .size ();
281
295
item.startIndex = gridIndexOffset;
282
296
item.baseVertex = gridVertexOffset;
283
297
m_vecShapes.push_back (item);
284
298
285
299
for (int i = 0 ; i < 5 ; ++i)
286
300
{
287
- Matrix4 leftCylWorld = Matrix4 (AffineTransform (Vector3 (-5 .0f , 1 .5f , -10 .0f + i * 5 .0f )));
288
- Matrix4 rightCylWorld = Matrix4 (AffineTransform (Vector3 (+5 .0f , 1 .5f , -10 .0f + i * 5 .0f )));
301
+ Matrix4 leftCylWorld = Transpose ( Matrix4 (AffineTransform (Vector3 (-5 .0f , 1 .5f , -10 .0f + i * 5 .0f ) )));
302
+ Matrix4 rightCylWorld = Transpose ( Matrix4 (AffineTransform (Vector3 (+5 .0f , 1 .5f , -10 .0f + i * 5 .0f ) )));
289
303
290
- Matrix4 leftSphereWorld = Matrix4 (AffineTransform (Vector3 (-5 .0f , 3 .5f , -10 .0f + i * 5 .0f )));
291
- Matrix4 rightSphereWorld = Matrix4 (AffineTransform (Vector3 (+5 .0f , 3 .5f , -10 .0f + i * 5 .0f )));
304
+ Matrix4 leftSphereWorld = Transpose ( Matrix4 (AffineTransform (Vector3 (-5 .0f , 3 .5f , -10 .0f + i * 5 .0f ) )));
305
+ Matrix4 rightSphereWorld = Transpose ( Matrix4 (AffineTransform (Vector3 (+5 .0f , 3 .5f , -10 .0f + i * 5 .0f ) )));
292
306
293
307
// cylinder
294
308
item.indexCount = (UINT)cylinder.Indices32 .size ();
@@ -319,9 +333,8 @@ void GameApp::renderShapes(GraphicsContext& gfxContext)
319
333
320
334
for (auto & item : m_vecShapes)
321
335
{
322
- Matrix4 a = m_ViewProjMatrix * item.modelToWorld ;
323
336
// 设置常量缓冲区数据
324
- gfxContext.SetDynamicConstantBufferView (0 , sizeof (a ), &a );
337
+ gfxContext.SetDynamicConstantBufferView (0 , sizeof (item. modelToWorld ), &item. modelToWorld );
325
338
// 绘制
326
339
gfxContext.DrawIndexedInstanced (item.indexCount , 1 , item.startIndex , item.baseVertex , 0 );
327
340
}
@@ -413,7 +426,7 @@ float GameApp::GetHillsHeight(float x, float z) const
413
426
void GameApp::renderLandAndWaves (GraphicsContext& gfxContext)
414
427
{
415
428
// 设置常量缓冲区数据
416
- gfxContext.SetDynamicConstantBufferView (0 , sizeof (m_ViewProjMatrix ), &m_ViewProjMatrix );
429
+ gfxContext.SetDynamicConstantBufferView (0 , sizeof (m_waveWorld ), &m_waveWorld );
417
430
418
431
// land
419
432
// 设置顶点视图
0 commit comments