@@ -144,6 +144,34 @@ void GameApp::Update(float deltaT)
144
144
m_MainScissor.top = 0 ;
145
145
m_MainScissor.right = (LONG)Graphics::g_SceneColorBuffer.GetWidth ();
146
146
m_MainScissor.bottom = (LONG)Graphics::g_SceneColorBuffer.GetHeight ();
147
+
148
+ updateSkull (deltaT);
149
+ }
150
+
151
+ void GameApp::updateSkull (float deltaT)
152
+ {
153
+ if (GameInput::IsPressed (GameInput::kKey_a ))
154
+ mSkullTranslation -= { 1 .0f * deltaT, 0 .0f , 0 .0f };
155
+
156
+ if (GameInput::IsPressed (GameInput::kKey_d ))
157
+ mSkullTranslation += { 1 .0f * deltaT, 0 .0f , 0 .0f };
158
+
159
+ if (GameInput::IsPressed (GameInput::kKey_w ))
160
+ mSkullTranslation += { 0 .0f , 1 .0f * deltaT, 0 .0f };
161
+
162
+ if (GameInput::IsPressed (GameInput::kKey_s ))
163
+ mSkullTranslation -= { 0 .0f , 1 .0f * deltaT, 0 .0f };
164
+
165
+ // y坐标不允许低于地板
166
+ float y = (float )mSkullTranslation .GetY ();
167
+ if (y < 0 .0f )
168
+ mSkullTranslation .SetY (0 .0f );
169
+
170
+ // 更新最新的skull世界矩阵
171
+ auto rotationMatrix = Math::AffineTransform::MakeYRotation (Math::XM_PIDIV2);
172
+ auto scallMatrix = Math::AffineTransform::MakeScale ({ 0 .45f , 0 .45f , 0 .45f });
173
+ auto translateMatrix = Math::AffineTransform::MakeTranslation (mSkullTranslation );
174
+ mSkullRitem ->modeToWorld = Math::Transpose (Math::Matrix4 (translateMatrix * scallMatrix * rotationMatrix));
147
175
}
148
176
149
177
void GameApp::RenderScene (void )
@@ -318,7 +346,59 @@ void GameApp::buildRoomGeo()
318
346
319
347
void GameApp::buildSkullGeo ()
320
348
{
321
-
349
+ std::ifstream fin (" Models/skull.txt" );
350
+
351
+ if (!fin)
352
+ {
353
+ MessageBox (0 , L" Models/skull.txt not found." , 0 , 0 );
354
+ return ;
355
+ }
356
+
357
+ UINT vcount = 0 ;
358
+ UINT tcount = 0 ;
359
+ std::string ignore;
360
+
361
+ fin >> ignore >> vcount;
362
+ fin >> ignore >> tcount;
363
+ fin >> ignore >> ignore >> ignore >> ignore;
364
+
365
+ std::vector<Vertex> vertices (vcount);
366
+ for (UINT i = 0 ; i < vcount; ++i)
367
+ {
368
+ fin >> vertices[i].Pos .x >> vertices[i].Pos .y >> vertices[i].Pos .z ;
369
+ fin >> vertices[i].Normal .x >> vertices[i].Normal .y >> vertices[i].Normal .z ;
370
+
371
+ // Model does not have texture coordinates, so just zero them out.
372
+ vertices[i].TexC = { 0 .0f , 0 .0f };
373
+ }
374
+
375
+ fin >> ignore;
376
+ fin >> ignore;
377
+ fin >> ignore;
378
+
379
+ std::vector<std::int32_t > indices (3 * tcount);
380
+ for (UINT i = 0 ; i < tcount; ++i)
381
+ {
382
+ fin >> indices[i * 3 + 0 ] >> indices[i * 3 + 1 ] >> indices[i * 3 + 2 ];
383
+ }
384
+
385
+ fin.close ();
386
+
387
+
388
+ auto geo = std::make_unique<MeshGeometry>();
389
+ geo->name = " skullGeo" ;
390
+
391
+ geo->createVertex (L" skull vertex" , (UINT)vertices.size (), sizeof (Vertex), vertices.data ());
392
+ geo->createIndex (L" skull index" , (UINT)indices.size (), sizeof (std::int32_t ), indices.data ());
393
+
394
+ SubmeshGeometry submesh;
395
+ submesh.IndexCount = (UINT)indices.size ();
396
+ submesh.StartIndexLocation = 0 ;
397
+ submesh.BaseVertexLocation = 0 ;
398
+
399
+ geo->geoMap [" skull" ] = submesh;
400
+
401
+ m_mapGeometries[geo->name ] = std::move (geo);
322
402
}
323
403
324
404
void GameApp::buildMaterials ()
@@ -346,9 +426,17 @@ void GameApp::buildMaterials()
346
426
icemirror->roughness = 0 .5f ;
347
427
icemirror->srv = TextureManager::LoadFromFile (L" ice" , true )->GetSRV ();
348
428
429
+ auto skullMat = std::make_unique<Material>();
430
+ skullMat->name = " skullMat" ;
431
+ skullMat->diffuseAlbedo = { 1 .0f , 1 .0f , 1 .0f , 1 .0f };
432
+ skullMat->fresnelR0 = { 0 .05f , 0 .05f , 0 .05f };
433
+ skullMat->roughness = 0 .3f ;
434
+ skullMat->srv = TextureManager::LoadFromFile (L" white1x1" , true )->GetSRV ();
435
+
349
436
m_mapMaterial[" bricks" ] = std::move (bricks);
350
437
m_mapMaterial[" checkertile" ] = std::move (checkertile);
351
438
m_mapMaterial[" icemirror" ] = std::move (icemirror);
439
+ m_mapMaterial[" skullMat" ] = std::move (skullMat);
352
440
}
353
441
354
442
void GameApp::buildRenderItem ()
@@ -379,4 +467,14 @@ void GameApp::buildRenderItem()
379
467
mirrorRItem->geo = m_mapGeometries[" roomGeo" ].get ();
380
468
mirrorRItem->mat = m_mapMaterial[" icemirror" ].get ();
381
469
m_vecRenderItems[(int )RenderLayer::Opaque].push_back (std::move (mirrorRItem));
470
+
471
+ // skull
472
+ auto skullRItem = std::make_unique<RenderItem>();
473
+ skullRItem->IndexCount = m_mapGeometries[" skullGeo" ]->geoMap [" skull" ].IndexCount ;
474
+ skullRItem->StartIndexLocation = m_mapGeometries[" skullGeo" ]->geoMap [" skull" ].StartIndexLocation ;
475
+ skullRItem->BaseVertexLocation = m_mapGeometries[" skullGeo" ]->geoMap [" skull" ].BaseVertexLocation ;
476
+ skullRItem->geo = m_mapGeometries[" skullGeo" ].get ();
477
+ skullRItem->mat = m_mapMaterial[" skullMat" ].get ();
478
+ mSkullRitem = skullRItem.get ();
479
+ m_vecRenderItems[(int )RenderLayer::Opaque].push_back (std::move (skullRItem));
382
480
}
0 commit comments