Skip to content

Commit 5a7a0c2

Browse files
* 修改深度检测为less,默认深度值为1(因为我们已经把坐标系改成了左手坐标系)
+ 添加skull的绘制,以及世界转换矩阵的更新
1 parent c8bb31c commit 5a7a0c2

File tree

7 files changed

+95252
-4
lines changed

7 files changed

+95252
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
\x64
3535
*.user
3636

37+
# nuget
38+
\packages
39+
3740
# =========================
3841
# Operating System Files
3942
# =========================

Chapter 11 Stenciling/Core/Graphics/GraphicsCommon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void Graphics::InitializeCommonState(void)
159159
DepthStateReadWrite = DepthStateDisabled;
160160
DepthStateReadWrite.DepthEnable = TRUE;
161161
DepthStateReadWrite.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
162-
DepthStateReadWrite.DepthFunc = D3D12_COMPARISON_FUNC_GREATER_EQUAL;
162+
DepthStateReadWrite.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
163163

164164
DepthStateReadOnly = DepthStateReadWrite;
165165
DepthStateReadOnly.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO;

Chapter 11 Stenciling/Core/Graphics/Resource/DepthBuffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class EsramAllocator;
2929
class DepthBuffer : public PixelBuffer
3030
{
3131
public:
32-
DepthBuffer( float ClearDepth = 0.0f, uint8_t ClearStencil = 0 )
32+
DepthBuffer( float ClearDepth = 1.0f, uint8_t ClearStencil = 0 )
3333
: m_ClearDepth(ClearDepth), m_ClearStencil(ClearStencil)
3434
{
3535
m_hDSV[0].ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;

Chapter 11 Stenciling/GameApp.cpp

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,34 @@ void GameApp::Update(float deltaT)
144144
m_MainScissor.top = 0;
145145
m_MainScissor.right = (LONG)Graphics::g_SceneColorBuffer.GetWidth();
146146
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));
147175
}
148176

149177
void GameApp::RenderScene(void)
@@ -318,7 +346,59 @@ void GameApp::buildRoomGeo()
318346

319347
void GameApp::buildSkullGeo()
320348
{
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);
322402
}
323403

324404
void GameApp::buildMaterials()
@@ -346,9 +426,17 @@ void GameApp::buildMaterials()
346426
icemirror->roughness = 0.5f;
347427
icemirror->srv = TextureManager::LoadFromFile(L"ice", true)->GetSRV();
348428

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+
349436
m_mapMaterial["bricks"] = std::move(bricks);
350437
m_mapMaterial["checkertile"] = std::move(checkertile);
351438
m_mapMaterial["icemirror"] = std::move(icemirror);
439+
m_mapMaterial["skullMat"] = std::move(skullMat);
352440
}
353441

354442
void GameApp::buildRenderItem()
@@ -379,4 +467,14 @@ void GameApp::buildRenderItem()
379467
mirrorRItem->geo = m_mapGeometries["roomGeo"].get();
380468
mirrorRItem->mat = m_mapMaterial["icemirror"].get();
381469
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));
382480
}

Chapter 11 Stenciling/GameApp.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class GameApp : public GameCore::IGameApp
2828
void buildMaterials();
2929
void buildRenderItem();
3030

31+
void updateSkull(float deltaT);
32+
3133
void drawRenderItems(GraphicsContext& gfxContext, std::vector<std::unique_ptr<RenderItem>>& ritems);
3234

3335
private:
@@ -59,6 +61,10 @@ class GameApp : public GameCore::IGameApp
5961
};
6062
std::unordered_map<int, GraphicsPSO> m_mapPSO;
6163

64+
// render item skull point
65+
RenderItem* mSkullRitem = nullptr;
66+
Math::Vector3 mSkullTranslation = { 0.0f, 1.0f, -5.0f };
67+
6268
// 摄像机
6369
// 以(0, 0, -m_radius) 为初始位置
6470
Math::Camera m_Camera;
@@ -70,7 +76,7 @@ class GameApp : public GameCore::IGameApp
7076
float m_radius = 27.0f;
7177

7278
// x方向弧度,摄像机的x坐标增加,则m_xRotate增加
73-
float m_xRotate = 0.0f;
79+
float m_xRotate = -Math::XM_PIDIV4 / 2.0f;
7480
float m_xLast = 0.0f;
7581
float m_xDiff = 0.0f;
7682

0 commit comments

Comments
 (0)