Commit 67bf391c authored by Administrator's avatar Administrator
Browse files

re-structured source into multiple layers

parent 7402f027
...@@ -175,14 +175,17 @@ void Box::Update(float dt) noexcept ...@@ -175,14 +175,17 @@ void Box::Update(float dt) noexcept
// eyePosition = XMVectorSet(20.0f * sin, 10.0f, -20.0f * cos, 1.0f); // eyePosition = XMVectorSet(20.0f * sin, 10.0f, -20.0f * cos, 1.0f);
//}; //};
DirectX::XMMATRIX Box::GetTransformXM() const noexcept DirectX::XMMATRIX Box::GetTransformXM(Graphics& gfx) const noexcept
{ {
/*return DirectX::XMMatrixRotationRollPitchYaw(pitch, yaw, roll) * /*return DirectX::XMMatrixRotationRollPitchYaw(pitch, yaw, roll) *
DirectX::XMMatrixTranslation(r, 0.0f, 0.0f) * DirectX::XMMatrixTranslation(r, 0.0f, 0.0f) *
DirectX::XMMatrixRotationRollPitchYaw(theta, phi, chi) * DirectX::XMMatrixRotationRollPitchYaw(theta, phi, chi) *
DirectX::XMMatrixTranslation(0.0f, 0.0f, 20.0f);*/ DirectX::XMMatrixTranslation(0.0f, 0.0f, 20.0f);*/
DirectX::XMMATRIX lookAtMatrix = gfx.getLookAtMatrix();
return return
DirectX::XMMatrixTranslation(dx, dy, dz) DirectX::XMMatrixTranslation(dx, dy, dz)
* DirectX::XMMatrixLookAtLH(eyePosition, focusPosition, upDirection); //* DirectX::XMMatrixLookAtLH(eyePosition, focusPosition, upDirection);
* lookAtMatrix;
} }
\ No newline at end of file
...@@ -11,7 +11,7 @@ public: ...@@ -11,7 +11,7 @@ public:
void Update(float dt) noexcept override; void Update(float dt) noexcept override;
//void UpdateCamera(float sin, float cos) noexcept; //void UpdateCamera(float sin, float cos) noexcept;
//void SetCamera(DirectX::XMVECTOR upDirection, DirectX::XMVECTOR eyePosition, DirectX::XMVECTOR focusPosition) noexcept; //void SetCamera(DirectX::XMVECTOR upDirection, DirectX::XMVECTOR eyePosition, DirectX::XMVECTOR focusPosition) noexcept;
DirectX::XMMATRIX GetTransformXM() const noexcept override; DirectX::XMMATRIX GetTransformXM(Graphics& gfx) const noexcept override;
private: private:
//StructVertex vertices[24]; //StructVertex vertices[24];
// trasformation // trasformation
......
#include "CoreHandler.h"
CoreHandler::CoreHandler(Logger& logger)
{
// init local variable
seed = 0.0f;
seed = int(seed) % 10000;
};
void CoreHandler::CoreProcess(Window& window, Graphics& gfx, Logger& logger)
{
// Display background and set depthstencil
window.GetGfx().RendorFrame();
// Set camera
SetCamera(window, logger);
// Draw all scenes
window.DrawScenes(window.GetGfx(), logger);
// Set view port and present
window.GetGfx().EndFrame();
};
void CoreHandler::SetCamera(Window& window, Logger& logger)
{
// Put camera calculation logic here
seed++;
auto dt = sin(seed / 1000);
logger.PutLog("seed", std::to_string(seed));
logger.PutLog("dt", std::to_string(dt));
auto sinValue = sin(seed / 5000);
auto cosValue = cos(seed / 5000);
// Set camera
DirectX::XMVECTOR eyePosition = DirectX::XMVectorSet(5.0f * sinValue, 3.0f, -5.0f * cosValue, 1.0f);
DirectX::XMVECTOR focusPosition = DirectX::XMVectorZero();
DirectX::XMVECTOR upDirection = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f);
window.GetGfx().SetCamera(logger, eyePosition, focusPosition, upDirection);
}
\ No newline at end of file
#pragma once
#include "Window.h"
#include "Graphics.h"
#include "Logger.h"
class CoreHandler
{
public:
CoreHandler(Logger& logger);
void CoreProcess(Window& window, Graphics& gfx, Logger& logger);
void SetCamera(Window& window, Logger& logger);
private:
double seed;
};
\ No newline at end of file
...@@ -34,31 +34,4 @@ void Drawable::AddIndexBuffer(std::unique_ptr<IndexBuffer> ibuf) noexcept ...@@ -34,31 +34,4 @@ void Drawable::AddIndexBuffer(std::unique_ptr<IndexBuffer> ibuf) noexcept
assert("Attempting to add index buffer a second time" && pIndexBuffer == nullptr); assert("Attempting to add index buffer a second time" && pIndexBuffer == nullptr);
pIndexBuffer = ibuf.get(); pIndexBuffer = ibuf.get();
binds.push_back(std::move(ibuf)); binds.push_back(std::move(ibuf));
} }
\ No newline at end of file
void Drawable::SetCamera(
Logger& logger,
DirectX::XMVECTOR eyePosition,
DirectX::XMVECTOR focusPosition,
DirectX::XMVECTOR upDirection) noexcept
{
using namespace DirectX;
this->eyePosition = eyePosition;
this->focusPosition = focusPosition;
this->upDirection = upDirection;
/*this->upDirection = XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f);
this->focusPosition = XMVectorZero();
this->eyePosition = XMVectorSet(20.0f * sinValue, 10.0f, -20.0f * cosValue, 1.0f);
XMFLOAT4 upDirection4;
XMFLOAT4 eyePosition4;
XMFLOAT4 focusPosition4;
XMStoreFloat4(&upDirection4, this->upDirection);
XMStoreFloat4(&eyePosition4, this->eyePosition);
XMStoreFloat4(&focusPosition4, this->focusPosition);
logger.PutLog("Drawable::SetCamera", "upDirection: {" + std::to_string(upDirection4.x) + ", " + std::to_string(upDirection4.y) + ", " + std::to_string(upDirection4.z) + ", " + std::to_string(upDirection4.w) + "}");
logger.PutLog("Drawable::SetCamera", "eyePosition: {" + std::to_string(eyePosition4.x) + ", " + std::to_string(eyePosition4.y) + ", " + std::to_string(eyePosition4.z) + ", " + std::to_string(eyePosition4.w) + "}");
logger.PutLog("Drawable::SetCamera", "focusPosition: {" + std::to_string(focusPosition4.x) + ", " + std::to_string(focusPosition4.y) + ", " + std::to_string(focusPosition4.z) + ", " + std::to_string(focusPosition4.w) + "}");*/
};
\ No newline at end of file
...@@ -16,15 +16,10 @@ class Drawable ...@@ -16,15 +16,10 @@ class Drawable
public: public:
Drawable() = default; Drawable() = default;
Drawable(const Drawable&) = delete; Drawable(const Drawable&) = delete;
virtual DirectX::XMMATRIX GetTransformXM() const noexcept = 0; virtual DirectX::XMMATRIX GetTransformXM(Graphics& gfx) const noexcept = 0;
void Draw(Graphics& gfx, Logger& logger) const noexcept; void Draw(Graphics& gfx, Logger& logger) const noexcept;
virtual void Update(float dt) noexcept = 0; virtual void Update(float dt) noexcept = 0;
void SetCamera(
Logger& logger,
DirectX::XMVECTOR eyePosition,
DirectX::XMVECTOR focusPosition,
DirectX::XMVECTOR upDirection) noexcept;
void AddBind(std::unique_ptr<Bindable> bind) noexcept; void AddBind(std::unique_ptr<Bindable> bind) noexcept;
void AddIndexBuffer(std::unique_ptr<class IndexBuffer> ibuf) noexcept; void AddIndexBuffer(std::unique_ptr<class IndexBuffer> ibuf) noexcept;
void ExecuteBind(Graphics& gfx, Logger& logger) noexcept; void ExecuteBind(Graphics& gfx, Logger& logger) noexcept;
...@@ -33,9 +28,4 @@ private: ...@@ -33,9 +28,4 @@ private:
const IndexBuffer* pIndexBuffer = nullptr; const IndexBuffer* pIndexBuffer = nullptr;
std::vector<std::unique_ptr<Bindable>> binds; std::vector<std::unique_ptr<Bindable>> binds;
virtual const std::vector<std::unique_ptr<Bindable>>& GetStaticBinds() const noexcept = 0; virtual const std::vector<std::unique_ptr<Bindable>>& GetStaticBinds() const noexcept = 0;
protected:
// for camera
DirectX::XMVECTOR upDirection;
DirectX::XMVECTOR eyePosition;
DirectX::XMVECTOR focusPosition;
}; };
\ No newline at end of file
...@@ -184,4 +184,21 @@ wrl::ComPtr<IDXGISwapChain> Graphics::getSwapChain() ...@@ -184,4 +184,21 @@ wrl::ComPtr<IDXGISwapChain> Graphics::getSwapChain()
wrl::ComPtr<ID3D11DeviceContext> Graphics::getContext() wrl::ComPtr<ID3D11DeviceContext> Graphics::getContext()
{ {
return pContext; return pContext;
};
void Graphics::SetCamera(
Logger& logger,
DirectX::XMVECTOR eyePosition,
DirectX::XMVECTOR focusPosition,
DirectX::XMVECTOR upDirection) noexcept
{
using namespace DirectX;
this->eyePosition = eyePosition;
this->focusPosition = focusPosition;
this->upDirection = upDirection;
};
DirectX::XMMATRIX Graphics::getLookAtMatrix() noexcept
{
return DirectX::XMMatrixLookAtLH(this->eyePosition, this->focusPosition, this->upDirection);
}; };
\ No newline at end of file
#pragma once #pragma once
#include <d3d11.h> #include <d3d11.h>
#pragma comment (lib, "d3d11.lib")
#include <d3dcompiler.h>
#pragma comment (lib, "d3dcompiler.lib")
#include <wrl.h> #include <wrl.h>
#include <d3dcompiler.h> #include <d3dcompiler.h>
#include <DirectXMath.h> #include <DirectXMath.h>
#include "Logger.h"
namespace wrl = Microsoft::WRL; namespace wrl = Microsoft::WRL;
...@@ -28,6 +33,15 @@ public: ...@@ -28,6 +33,15 @@ public:
void SetProjection(DirectX::FXMMATRIX proj) noexcept; void SetProjection(DirectX::FXMMATRIX proj) noexcept;
DirectX::XMMATRIX GetProjection() const noexcept; DirectX::XMMATRIX GetProjection() const noexcept;
// for camera
void SetCamera(
Logger& logger,
DirectX::XMVECTOR eyePosition,
DirectX::XMVECTOR focusPosition,
DirectX::XMVECTOR upDirection) noexcept;
DirectX::XMMATRIX getLookAtMatrix() noexcept;
// for debug // for debug
wrl::ComPtr<ID3D11Device> getDevice(); wrl::ComPtr<ID3D11Device> getDevice();
wrl::ComPtr<IDXGISwapChain> getSwapChain(); wrl::ComPtr<IDXGISwapChain> getSwapChain();
...@@ -45,4 +59,8 @@ private: ...@@ -45,4 +59,8 @@ private:
wrl::ComPtr<ID3D11RenderTargetView> pTarget; wrl::ComPtr<ID3D11RenderTargetView> pTarget;
// 指向Depth Buffer View的指针 // 指向Depth Buffer View的指针
wrl::ComPtr<ID3D11DepthStencilView> pDSView; wrl::ComPtr<ID3D11DepthStencilView> pDSView;
// for camera
DirectX::XMVECTOR upDirection;
DirectX::XMVECTOR eyePosition;
DirectX::XMVECTOR focusPosition;
}; };
\ No newline at end of file
...@@ -88,107 +88,4 @@ Ground::Ground(Graphics & gfx, Logger & logger) noexcept ...@@ -88,107 +88,4 @@ Ground::Ground(Graphics & gfx, Logger & logger) noexcept
shapes.push_back(std::make_unique<Box>(gfx, vertices, logger)); shapes.push_back(std::make_unique<Box>(gfx, vertices, logger));
} }
// Init a 11 X 11 area as ground
//for (int i = -5; i < 6; i++)
//{
// for (int j = -5; j < 6; j++)
// {
// float boxPos[8][3] =
// {
// // left bottom back
// { -1.0f + i, -1.0f, -1.0f + j },
// // right bottom back
// { 1.0f + i, -1.0f, -1.0f + j },
// // left top back
// { -1.0f + i, 1.0f, -1.0f + j },
// // right top back
// { 1.0f + i, 1.0f, -1.0f + j },
// // left bottom front
// { -1.0f + i, -1.0f, 1.0f + j },
// // right bottom front
// { 1.0f + i, -1.0f, 1.0f + j },
// // left top front
// { -1.0f + i, 1.0f, 1.0f + j },
// // right top front
// { 1.0f + i, 1.0f, 1.0f + j },
// };
// float boxCol[6][3] =
// {
// { 0.0f, 1.0f, 0.0f }, // Green
// { 1.0f, 1.0f, 0.0f }, // Yellow
// { 1.0f, 0.0f, 0.0f }, // Red
// { 0.0f, 0.0f, 1.0f }, // Blue
// { 0.0f, 1.0f, 1.0f }, // Cyan
// { 1.0f, 0.0f, 1.0f }, // Magenta
// };
// shapes.push_back(std::make_unique<Box>(gfx, boxPos, boxCol, logger));
// }
//}
//float boxPos2[8][3] =
//{
// // left bottom back
// { -1.0f, -1.0f + 5.0f, -1.0f },
// // right bottom back
// { 1.0f, -1.0f + 5.0f, -1.0f },
// // left top back
// { -1.0f, 1.0f + 5.0f, -1.0f },
// // right top back
// { 1.0f, 1.0f + 5.0f, -1.0f },
// // left bottom front
// { -1.0f, -1.0f + 5.0f, 1.0f },
// // right bottom front
// { 1.0f, -1.0f + 5.0f, 1.0f },
// // left top front
// { -1.0f, 1.0f + 5.0f, 1.0f },
// // right top front
// { 1.0f, 1.0f + 5.0f, 1.0f },
//};
//float boxCol2[6][3] =
//{
// { 0.0f, 1.0f, 0.0f }, // Green
// { 1.0f, 1.0f, 0.0f }, // Yellow
// { 1.0f, 0.0f, 0.0f }, // Red
// { 0.0f, 0.0f, 1.0f }, // Blue
// { 0.0f, 1.0f, 1.0f }, // Cyan
// { 1.0f, 0.0f, 1.0f }, // Magenta
//};
//shapes.push_back(std::make_unique<Box>(gfx, boxPos2, boxCol2, logger));
//float boxPos3[8][3] =
//{
// // left bottom back
// { -1.0f, -1.0f - 5.0f, -1.0f },
// // right bottom back
// { 1.0f, -1.0f - 5.0f, -1.0f },
// // left top back
// { -1.0f, 1.0f - 5.0f, -1.0f },
// // right top back
// { 1.0f, 1.0f - 5.0f, -1.0f },
// // left bottom front
// { -1.0f, -1.0f - 5.0f, 1.0f },
// // right bottom front
// { 1.0f, -1.0f - 5.0f, 1.0f },
// // left top front
// { -1.0f, 1.0f - 5.0f, 1.0f },
// // right top front
// { 1.0f, 1.0f - 5.0f, 1.0f },
//};
//float boxCol3[6][3] =
//{
// { 0.0f, 1.0f, 0.0f }, // Green
// { 1.0f, 1.0f, 0.0f }, // Yellow
// { 1.0f, 0.0f, 0.0f }, // Red
// { 0.0f, 0.0f, 1.0f }, // Blue
// { 0.0f, 1.0f, 1.0f }, // Cyan
// { 1.0f, 0.0f, 1.0f }, // Magenta
//};
//shapes.push_back(std::make_unique<Box>(gfx, boxPos3, boxCol3, logger));
}; };
...@@ -130,6 +130,7 @@ ...@@ -130,6 +130,7 @@
<ClCompile Include="Bindable.cpp" /> <ClCompile Include="Bindable.cpp" />
<ClCompile Include="Box.cpp" /> <ClCompile Include="Box.cpp" />
<ClCompile Include="ConstantBuffer.h" /> <ClCompile Include="ConstantBuffer.h" />
<ClCompile Include="CoreHandler.cpp" />
<ClCompile Include="Drawable.cpp" /> <ClCompile Include="Drawable.cpp" />
<ClCompile Include="DrawableBase.cpp" /> <ClCompile Include="DrawableBase.cpp" />
<ClCompile Include="Graphics.cpp" /> <ClCompile Include="Graphics.cpp" />
...@@ -148,6 +149,7 @@ ...@@ -148,6 +149,7 @@
<ClCompile Include="Triangle.cpp" /> <ClCompile Include="Triangle.cpp" />
<ClCompile Include="VertexBuffer.cpp" /> <ClCompile Include="VertexBuffer.cpp" />
<ClCompile Include="VertexShader.cpp" /> <ClCompile Include="VertexShader.cpp" />
<ClCompile Include="Window.cpp" />
<ClCompile Include="WinMain.cpp" /> <ClCompile Include="WinMain.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
...@@ -183,6 +185,7 @@ ...@@ -183,6 +185,7 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="Bindable.h" /> <ClInclude Include="Bindable.h" />
<ClInclude Include="Box.h" /> <ClInclude Include="Box.h" />
<ClInclude Include="CoreHandler.h" />
<ClInclude Include="Drawable.h" /> <ClInclude Include="Drawable.h" />
<ClInclude Include="DrawableBase.h" /> <ClInclude Include="DrawableBase.h" />
<ClInclude Include="Graphics.h" /> <ClInclude Include="Graphics.h" />
...@@ -202,6 +205,7 @@ ...@@ -202,6 +205,7 @@
<ClInclude Include="Triangle.h" /> <ClInclude Include="Triangle.h" />
<ClInclude Include="VertexBuffer.h" /> <ClInclude Include="VertexBuffer.h" />
<ClInclude Include="VertexShader.h" /> <ClInclude Include="VertexShader.h" />
<ClInclude Include="Window.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
......
...@@ -40,17 +40,17 @@ ...@@ -40,17 +40,17 @@
<Filter Include="Source Files\Scenes"> <Filter Include="Source Files\Scenes">
<UniqueIdentifier>{8f6ea4b7-1762-4def-8d85-fc7cf5d7d9ed}</UniqueIdentifier> <UniqueIdentifier>{8f6ea4b7-1762-4def-8d85-fc7cf5d7d9ed}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="Header Files\Util">
<UniqueIdentifier>{6a06adba-c01d-4061-bc39-8e477199746c}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Util">
<UniqueIdentifier>{65115cd4-ea49-4525-a0c8-6863874ba3f5}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="WinMain.cpp"> <ClCompile Include="WinMain.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Timer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Graphics.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ConstantBuffer.h"> <ClCompile Include="ConstantBuffer.h">
<Filter>Header Files\Bindables</Filter> <Filter>Header Files\Bindables</Filter>
</ClCompile> </ClCompile>
...@@ -84,9 +84,6 @@ ...@@ -84,9 +84,6 @@
<ClCompile Include="VertexShader.cpp"> <ClCompile Include="VertexShader.cpp">
<Filter>Source Files\Bindables</Filter> <Filter>Source Files\Bindables</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Logger.cpp">
<Filter>Source Files\Common</Filter>
</ClCompile>
<ClCompile Include="Box.cpp"> <ClCompile Include="Box.cpp">
<Filter>Source Files\Drawables</Filter> <Filter>Source Files\Drawables</Filter>
</ClCompile> </ClCompile>
...@@ -99,14 +96,29 @@ ...@@ -99,14 +96,29 @@
<ClCompile Include="DrawableBase.cpp"> <ClCompile Include="DrawableBase.cpp">
<Filter>Source Files\Drawables</Filter> <Filter>Source Files\Drawables</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Texture.cpp">
<Filter>Source Files\Bindables</Filter>
</ClCompile>
<ClCompile Include="Sampler.cpp"> <ClCompile Include="Sampler.cpp">
<Filter>Source Files</Filter> <Filter>Source Files\Bindables</Filter>
</ClCompile>
<ClCompile Include="Window.cpp">
<Filter>Source Files\Common</Filter>
</ClCompile>
<ClCompile Include="Timer.cpp">
<Filter>Source Files\Util</Filter>
</ClCompile>
<ClCompile Include="Graphics.cpp">
<Filter>Source Files\Common</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Surface.cpp"> <ClCompile Include="Surface.cpp">
<Filter>Source Files</Filter> <Filter>Source Files\Common</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Texture.cpp"> <ClCompile Include="Logger.cpp">
<Filter>Source Files</Filter> <Filter>Source Files\Util</Filter>
</ClCompile>
<ClCompile Include="CoreHandler.cpp">
<Filter>Source Files\Common</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
...@@ -124,12 +136,6 @@ ...@@ -124,12 +136,6 @@
</FxCompile> </FxCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Timer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Graphics.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Bindable.h"> <ClInclude Include="Bindable.h">
<Filter>Header Files\Bindables</Filter> <Filter>Header Files\Bindables</Filter>
</ClInclude> </ClInclude>
...@@ -163,9 +169,6 @@ ...@@ -163,9 +169,6 @@
<ClInclude Include="TransformCBuf.h"> <ClInclude Include="TransformCBuf.h">
<Filter>Header Files\Bindables</Filter> <Filter>Header Files\Bindables</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Logger.h">
<Filter>Header Files\Common</Filter>
</ClInclude>
<ClInclude Include="Box.h"> <ClInclude Include="Box.h">
<Filter>Header Files\Drawables</Filter> <Filter>Header Files\Drawables</Filter>
</ClInclude> </ClInclude>
...@@ -179,13 +182,28 @@ ...@@ -179,13 +182,28 @@
<Filter>Header Files\Drawables</Filter> <Filter>Header Files\Drawables</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Sampler.h"> <ClInclude Include="Sampler.h">
<Filter>Header Files</Filter> <Filter>Header Files\Bindables</Filter>
</ClInclude>
<ClInclude Include="Texture.h">
<Filter>Header Files\Bindables</Filter>
</ClInclude>
<ClInclude Include="Window.h">
<Filter>Header Files\Common</Filter>
</ClInclude>
<ClInclude Include="Timer.h">
<Filter>Header Files\Util</Filter>
</ClInclude>
<ClInclude Include="Graphics.h">
<Filter>Header Files\Common</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Surface.h"> <ClInclude Include="Surface.h">
<Filter>Header Files</Filter> <Filter>Header Files\Common</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Texture.h"> <ClInclude Include="Logger.h">
<Filter>Header Files</Filter> <Filter>Header Files\Util</Filter>
</ClInclude>
<ClInclude Include="CoreHandler.h">
<Filter>Header Files\Common</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>
\ No newline at end of file
...@@ -2,18 +2,6 @@ ...@@ -2,18 +2,6 @@
#include "Section.h" #include "Section.h"
#include "Drawable.h" #include "Drawable.h"
void Section::SetCamera(
Logger& logger,
DirectX::XMVECTOR upDirection,
DirectX::XMVECTOR eyePosition,
DirectX::XMVECTOR focusPosition) noexcept
{
for (auto& shape : shapes)
{
shape->SetCamera(logger, upDirection, eyePosition, focusPosition);
}
};
void Section::DrawShapes( void Section::DrawShapes(
Graphics& gfx, Graphics& gfx,
Logger& logger) noexcept Logger& logger) noexcept
......
...@@ -10,11 +10,7 @@ class Section ...@@ -10,11 +10,7 @@ class Section
public: public:
Section() = default; Section() = default;
Section(const Section&) = delete; Section(const Section&) = delete;
void SetCamera(
Logger& logger,
DirectX::XMVECTOR upDirection,
DirectX::XMVECTOR eyePosition,
DirectX::XMVECTOR focusPosition) noexcept;
void DrawShapes( void DrawShapes(
Graphics& gfx, Graphics& gfx,
Logger& logger) noexcept; Logger& logger) noexcept;
......
...@@ -10,7 +10,7 @@ void TransformCbuf::Bind(Graphics& gfx, Logger& logger) noexcept ...@@ -10,7 +10,7 @@ void TransformCbuf::Bind(Graphics& gfx, Logger& logger) noexcept
{ {
vcbuf.Update(gfx, vcbuf.Update(gfx,
DirectX::XMMatrixTranspose( DirectX::XMMatrixTranspose(
parent.GetTransformXM() * gfx.GetProjection() parent.GetTransformXM(gfx) * gfx.GetProjection()
) )
); );
vcbuf.Bind(gfx, logger); vcbuf.Bind(gfx, logger);
......
...@@ -137,7 +137,7 @@ void Triangle::Update(float dt) noexcept ...@@ -137,7 +137,7 @@ void Triangle::Update(float dt) noexcept
dz = dt; dz = dt;
} }
DirectX::XMMATRIX Triangle::GetTransformXM() const noexcept DirectX::XMMATRIX Triangle::GetTransformXM(Graphics& gfx) const noexcept
{ {
/*return DirectX::XMMatrixRotationRollPitchYaw(pitch, yaw, roll) * /*return DirectX::XMMatrixRotationRollPitchYaw(pitch, yaw, roll) *
DirectX::XMMatrixTranslation(r, 0.0f, 0.0f) * DirectX::XMMatrixTranslation(r, 0.0f, 0.0f) *
...@@ -145,5 +145,6 @@ DirectX::XMMATRIX Triangle::GetTransformXM() const noexcept ...@@ -145,5 +145,6 @@ DirectX::XMMATRIX Triangle::GetTransformXM() const noexcept
DirectX::XMMatrixTranslation(0.0f, 0.0f, 20.0f);*/ DirectX::XMMatrixTranslation(0.0f, 0.0f, 20.0f);*/
return return
DirectX::XMMatrixTranslation(0.0f, 0.0f, 4.0f) DirectX::XMMatrixTranslation(0.0f, 0.0f, 4.0f)
* DirectX::XMMatrixTranslation(dx, dy, dz); * DirectX::XMMatrixTranslation(dx, dy, dz)
* gfx.getLookAtMatrix();
} }
\ No newline at end of file
...@@ -9,7 +9,7 @@ class Triangle : public Drawable ...@@ -9,7 +9,7 @@ class Triangle : public Drawable
public: public:
Triangle(Graphics& gfx, StructVertex vertices[], Logger& logger); Triangle(Graphics& gfx, StructVertex vertices[], Logger& logger);
void Update(float dt) noexcept override; void Update(float dt) noexcept override;
DirectX::XMMATRIX GetTransformXM() const noexcept override; DirectX::XMMATRIX GetTransformXM(Graphics& gfx) const noexcept override;
private: private:
// positional // positional
//float position[3][3]; //float position[3][3];
......
// include the basic windows header file #pragma once
#include <windows.h> #include "Window.h"
#include <windowsx.h>
#include <d3d11.h>
#pragma comment (lib, "d3d11.lib")
#include <d3dcompiler.h>
#pragma comment (lib, "d3dcompiler.lib")
#include <wrl.h>
#include <DirectXMath.h>
namespace dx = DirectX;
#include <array> // to usd std::size
#include <cmath> // to use std::sin and std::cos
#include "Timer.h" // to use timer
Timer timer;
#include "Graphics.h" #include "Graphics.h"
#include <memory>
#include "Triangle.h"
#include "Box.h"
#include "Logger.h" #include "Logger.h"
#include "CoreHandler.h"
Logger logger("log.txt"); Logger logger("log.txt");
#include <cmath>
#include "Ground.h"
/************************************
* 1.1 CreateDeviceAndSwapChain
************************************/
// global declarations
Microsoft::WRL::ComPtr<IDXGISwapChain> swapchain; // the pointer to the swap chain interface
Microsoft::WRL::ComPtr<ID3D11Device> dev; // the pointer to our Direct3D device interface
Microsoft::WRL::ComPtr<ID3D11DeviceContext> devcon; // the pointer to our Direct3D device context
/************************************
* 1.2 DefineRenderTargetView
************************************/
Microsoft::WRL::ComPtr<ID3D11Texture2D> pBackBuffer;
// 定义Targe View
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> targetView;
/************************************
* 1.3 DefineDepthStencilView
************************************/
// Define depth stencil buffer
Microsoft::WRL::ComPtr<ID3D11DepthStencilState> pDepthStencilState;
// Define depth stencil texture
Microsoft::WRL::ComPtr<ID3D11Texture2D> depthStencilTexture;
// Define depth stencil view
Microsoft::WRL::ComPtr<ID3D11DepthStencilView> pDSView;
/************************************
* 2.3 DefineVertexBuffer
************************************/
// Define vertex buffer
Microsoft::WRL::ComPtr<ID3D11Buffer> pVertexBuffer;
// Create buffer description
struct Vertex
{
float x;
float y;
float z;
float r;
float g;
float b;
};
// create vertex buffer (8 vertices for one cube)
const Vertex vertices[] =
{
// left bottom back
{ -1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f },
// right bottom back
{ 1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f },
// left top back
{ -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f },
// right top back
{ 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f },
// left bottom front
{ -1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f },
// right bottom front
{ 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f },
// left top front
{ -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f },
// right top front
{ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
};
/************************************
* 2.4 DefineIndexBuffer
************************************/
// Define index buffer
Microsoft::WRL::ComPtr<ID3D11Buffer> pIndexBuffer;
// Create index buffer
const unsigned short indices[] =
{
// back surface
0, 2, 1, 2, 3, 1,
// right surface
1, 3, 5, 3, 7, 5,
// top surface
2, 6, 3, 3, 6, 7,
// front surface
4, 5, 7, 4, 7, 6,
// left surface
0, 4, 2, 2, 4, 6,
// bottom surface
0, 1, 4, 1, 5, 4,
};
/************************************
* 2.5 DefineTransConstBuffer
************************************/
// Define transformation constant buffer
Microsoft::WRL::ComPtr<ID3D11Buffer> pTransConstBuffer;
// Create transformation constant buffer struct
struct TransConstBuffer
{
dx::XMMATRIX transform;
};
// create constant buffer
const float heightWidthRatio = 0.3f / 0.4f;
float angle = 0.0f;
TransConstBuffer tcb =
{
// transpose the matrix to change from row_major to column_major
dx::XMMatrixTranspose(
// rotate Z axis
dx::XMMatrixRotationZ(-angle)
// rotate X axis
* dx::XMMatrixRotationX(-angle)
// scaling
//* dx::XMMatrixScaling(heightWidthRatio, 1.0f, 1.0f)
// translation
* dx::XMMatrixTranslation(0.0f, 0.0f, 4.0f)
// projection
* dx::XMMatrixPerspectiveLH(1.0f, heightWidthRatio, 0.1f, 10.0f)
)
};
/************************************
* 2.6 DefineColorConstBuffer
************************************/
// Define color constant buffer
Microsoft::WRL::ComPtr<ID3D11Buffer> pColorConstBuffer;
// Create constant buffer for color
struct ColorConstantBuffer
{
struct
{
float r;
float g;
float b;
float a;
} face_colors[6];
};
const ColorConstantBuffer ccb =
{
{
{ 1.0f, 0.0f, 1.0f },
{ 1.0f, 0.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f },
{ 0.0f, 0.0f, 1.0f },
{ 1.0f, 1.0f, 0.0f },
{ 0.0f, 1.0f, 1.0f },
}
};
/************************************
* 2.7 DefineVertexShader
************************************/
// Define Vertex Shader
Microsoft::WRL::ComPtr<ID3DBlob> pCompiledVertexBlob;
Microsoft::WRL::ComPtr<ID3D11VertexShader> pVertexShader;
/************************************
* 2.8 DefinePixelShader
************************************/
// Define Pixel Shader
Microsoft::WRL::ComPtr<ID3DBlob> pCompiledPixelBlob;
Microsoft::WRL::ComPtr<ID3D11PixelShader> pPixelShader;
/************************************
* 2.9 DefineInputLayout
************************************/
// Define input layout
ID3D11InputLayout* pInputLayout;
/************************************
* 2.3 DefineVertexBuffer
************************************/
void DefineVertexBuffer()
{
D3D11_BUFFER_DESC bd = {};
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.Usage = D3D11_USAGE_DEFAULT;
bd.CPUAccessFlags = 0u;
bd.MiscFlags = 0u;
bd.ByteWidth = sizeof(vertices);
bd.StructureByteStride = sizeof(Vertex);
logger.PutLog("DefineVertexBuffer", "ByteWidth: " + std::to_string(sizeof(vertices)));
logger.PutLog("DefineVertexBuffer", "StructureByteStride: " + std::to_string(sizeof(Vertex)));
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = vertices;
// Create vertex buffer
dev->CreateBuffer(&bd, &sd, &pVertexBuffer);
// Bind vertex buffer to pipeline
const UINT stride = sizeof(Vertex);
const UINT offset = 0u;
devcon->IASetVertexBuffers(0u, 1u, pVertexBuffer.GetAddressOf(), &stride, &offset);
}
/************************************
* 2.4 DefineIndexBuffer
************************************/
void DefineIndexBuffer()
{
D3D11_BUFFER_DESC ibd = {};
ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
ibd.Usage = D3D11_USAGE_DEFAULT;
ibd.CPUAccessFlags = 0u;
ibd.MiscFlags = 0u;
ibd.ByteWidth = sizeof(indices);
ibd.StructureByteStride = sizeof(unsigned short);
logger.PutLog("DefineIndexBuffer", "size of ByteWidth: " + std::to_string(ibd.ByteWidth));
logger.PutLog("DefineIndexBuffer", "size of StructureByteStride: " + std::to_string(ibd.StructureByteStride));
D3D11_SUBRESOURCE_DATA isd = {};
isd.pSysMem = indices;
// Create index buffer
dev->CreateBuffer(&ibd, &isd, &pIndexBuffer);
// Bind index buffer to pipeline
devcon->IASetIndexBuffer(pIndexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0u);
}
/************************************
* 2.5 DefineTransConstBuffer
************************************/
void DefineTransConstBuffer(float angle, float z)
{
tcb =
{
// transpose the matrix to change from row_major to column_major
dx::XMMatrixTranspose(
// rotate Z axis
//dx::XMMatrixRotationZ(-angle)
// rotate X axis
//* dx::XMMatrixRotationX(-angle)
// scaling
//* dx::XMMatrixScaling(heightWidthRatio, 1.0f, 1.0f)
// translation
dx::XMMatrixTranslation(0.0f, 0.0f, z)
// projection
* dx::XMMatrixPerspectiveLH(1.0f, heightWidthRatio, 0.5f, 10.0f)
)
};
D3D11_BUFFER_DESC cbd = {};
cbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbd.Usage = D3D11_USAGE_DYNAMIC;
cbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbd.MiscFlags = 0u;
cbd.ByteWidth = sizeof(tcb);
cbd.StructureByteStride = 0u;
D3D11_SUBRESOURCE_DATA csd = {};
csd.pSysMem = &tcb;
// Create constant buffer
dev->CreateBuffer(&cbd, &csd, &pTransConstBuffer);
// Bind constant buffer to vertex buffer
devcon->VSSetConstantBuffers(0u, 1u, pTransConstBuffer.GetAddressOf());
}
/************************************
* 2.6 DefineColorConstBuffer
************************************/
void DefineColorConstBuffer()
{
D3D11_BUFFER_DESC ccbd = {};
ccbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
ccbd.Usage = D3D11_USAGE_DEFAULT;
ccbd.CPUAccessFlags = 0u;
ccbd.MiscFlags = 0u;
ccbd.ByteWidth = sizeof(ccb);
ccbd.StructureByteStride = 0u;
D3D11_SUBRESOURCE_DATA ccsd = {};
ccsd.pSysMem = &ccb;
dev->CreateBuffer(&ccbd, &ccsd, &pColorConstBuffer);
// Bind constant buffer to vertex
devcon->PSSetConstantBuffers(0u, 1u, pColorConstBuffer.GetAddressOf());
}
/************************************
* 2.7 DefineVertexShader
************************************/
void DefineVertexShader()
{
// Load pixel shader file into BLOB, and create COM object of vertex shader
D3DReadFileToBlob(L"VertexShader.cso", &pCompiledVertexBlob);
dev->CreateVertexShader(pCompiledVertexBlob->GetBufferPointer(), pCompiledVertexBlob->GetBufferSize(), nullptr, &pVertexShader);
// bind vertex shader
devcon->VSSetShader(pVertexShader.Get(), nullptr, 0u);
}
/************************************
* 2.8 DefinePixelShader
************************************/
void DefinePixelShader()
{
// Load vertex shader file into BLOB, and create COM object of pixel shader
D3DReadFileToBlob(L"PixelShader.cso", &pCompiledPixelBlob);
dev->CreatePixelShader(pCompiledPixelBlob->GetBufferPointer(), pCompiledPixelBlob->GetBufferSize(), nullptr, &pPixelShader);
// bind pixel shader
devcon->PSSetShader(pPixelShader.Get(), nullptr, 0u);
}
/************************************
* 2.9 DefineInputLayout
************************************/
void DefineInputLayout()
{
// Create input elements description, choosing whatever need to send to GPU
const D3D11_INPUT_ELEMENT_DESC ied[] =
{
//{ "Position",0,DXGI_FORMAT_R32G32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
{ "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
//{ "Color",0,DXGI_FORMAT_R32G32B32_FLOAT,0,8,D3D11_INPUT_PER_VERTEX_DATA,0 },
//{ "Color",0,DXGI_FORMAT_R32G32B32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0 },
};
// Create input layout, using the description Defined above
dev->CreateInputLayout
(
ied, (UINT)std::size(ied),
pCompiledVertexBlob->GetBufferPointer(),
pCompiledVertexBlob->GetBufferSize(),
&pInputLayout
);
// Bind layout to pipeline
// bind vertex layout
devcon->IASetInputLayout(pInputLayout);
}
/************************************
* 2.10 DefinePrimitiveTopology
************************************/
void DefinePrimitiveTopology()
{
// Set the primitive type to use
// Set primitive topology to triangle list (groups of 3 vertices)
devcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
}
/************************************
* 2.11 DefineViewPorts
************************************/
void DefineViewPorts()
{
// 指定View Port
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
// 控制View port的位置
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = 800;
viewport.Height = 600;
viewport.MinDepth = 0;
viewport.MaxDepth = 1;
devcon->RSSetViewports(1, &viewport);
}
/************************************
* 2.11 DrawPrimitive
************************************/
void DrawPrimitive()
{
// Draw the triangle.
// devcon->Draw((UINT)sizeof(vertices), 0u);
devcon->DrawIndexed((UINT)std::size(indices), 0u, 0u);
}
void drawSingleTriangle(float angle, float z)
{
/************************************
* 2.3 DefineVertexBuffer
************************************/
DefineVertexBuffer();
/************************************
* 2.4 DefineIndexBuffer
************************************/
DefineIndexBuffer();
/************************************
* 2.5 DefineTransConstBuffer
************************************/
DefineTransConstBuffer(angle, z);
/************************************
* 2.6 DefineColorConstBuffer
************************************/
DefineColorConstBuffer();
/************************************
* 2.7 DefineVertexShader
************************************/
DefineVertexShader();
/************************************
* 2.8 DefinePixelShader
************************************/
DefinePixelShader();
/************************************
* 2.9 DefineInputLayout
************************************/
DefineInputLayout();
/************************************
* 2.10 DefinePrimitiveTopology
************************************/
DefinePrimitiveTopology();
/************************************
* 2.11 DefineViewPorts
************************************/
DefineViewPorts();
/************************************
* 2.12 DrawPrimitive
************************************/
DrawPrimitive();
}
void drawTriangle()
{
drawSingleTriangle(timer.Peek(), 4.0f);
//drawSingleTriangle(-timer.Peek(), 4.5f);
}
/************************************
* 1.1 CreateDeviceAndSwapChain
************************************/
void CreateDeviceAndSwapChain(HWND hWnd)
{
// create a struct to hold information about the swap chain
DXGI_SWAP_CHAIN_DESC scd;
// clear out the struct for use
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
// fill the swap chain description struct
scd.BufferCount = 1; // one back buffer
scd.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // use 32-bit color
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
scd.OutputWindow = hWnd; // the window to be used
scd.SampleDesc.Count = 1; // how many multisamples
scd.Windowed = TRUE; // windowed/full-screen mode
// create a device, device context and swap chain using the information in the scd struct
D3D11CreateDeviceAndSwapChain(NULL, // IDXGIAdapter *pAdapter - Let DXGI take care of what adapter to use (default adapter)
D3D_DRIVER_TYPE_HARDWARE, // D3D_DRIVER_TYPE DriverType - hardware or software, or others
NULL, // HMODULE Software
NULL, // UINT Flags
NULL, // D3D_FEATURE_LEVEL *pFeatureLevels
NULL, // UINT FeatureLevels
D3D11_SDK_VERSION, // UINT SDKVersion
&scd, // DXGI_SWAP_CHAIN_DESC *pSwapChainDesc
&swapchain, // IDXGISwapChain **ppSwapChain
&dev, // ID3D11Device **ppDevice
NULL, // D3D_FEATURE_LEVEL *FeatureLevel
&devcon); // ID3D11DeviceContext **ppImmediateContext
}
/************************************
* 1.2 DefineRenderTargetView
************************************/
void DefineRenderTargetView()
{
// 从SwapChain中取得back buffer
swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
// 根据back buffer,创建新的View Target
dev->CreateRenderTargetView(pBackBuffer.Get(), NULL, &targetView);
}
/************************************
* 1.3 DefineDepthStencilView
************************************/
void DefineDepthStencilView()
{
// Create depth stencil state
D3D11_DEPTH_STENCIL_DESC depthStencilDesc = {};
depthStencilDesc.DepthEnable = TRUE;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
// Create depth stencil state
dev->CreateDepthStencilState(&depthStencilDesc, &pDepthStencilState);
// bind depth state
devcon->OMSetDepthStencilState(pDepthStencilState.Get(), 1u);
// Create the depth stencil texture
D3D11_TEXTURE2D_DESC depthStencilTextureDesc = {};
depthStencilTextureDesc.Width = 800;
depthStencilTextureDesc.Height = 600;
depthStencilTextureDesc.MipLevels = 1u;
depthStencilTextureDesc.ArraySize = 1u;
depthStencilTextureDesc.Format = DXGI_FORMAT_D32_FLOAT;
depthStencilTextureDesc.SampleDesc.Count = 1u;
depthStencilTextureDesc.SampleDesc.Quality = 0u;
depthStencilTextureDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilTextureDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
// Create texture
dev->CreateTexture2D(&depthStencilTextureDesc, nullptr, &depthStencilTexture);
// Create the depth stencil view
D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc = {};
depthStencilViewDesc.Format = DXGI_FORMAT_D32_FLOAT;
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0u;
dev->CreateDepthStencilView(depthStencilTexture.Get(), &depthStencilViewDesc, &pDSView);
}
/************************************
* 1.4 DefineRenderTarget
************************************/
void DefineRenderTarget()
{
// 将渲染目标设置成我们新创建的View Target
//devcon->OMSetRenderTargets(1u, targetView.GetAddressOf(), NULL);
devcon->OMSetRenderTargets(1u, targetView.GetAddressOf(), pDSView.Get());
}
// this function initializes and prepares Direct3D for use
void InitD3D(HWND hWnd)
{
/************************************
* 1.1 CreateDeviceAndSwapChain
************************************/
CreateDeviceAndSwapChain(hWnd);
/************************************
* 1.2 DefineRenderTargetView
************************************/
DefineRenderTargetView();
/************************************
* 1.3 DefineDepthStencilView
************************************/
DefineDepthStencilView();
/************************************
* 1.4 DefineRenderTarget
************************************/
DefineRenderTarget();
}
// this is the function used to render a single frame
void RenderFrame(void)
{
/************************************
* 2.1 ClearRenderTargetView
************************************/
// clear the back buffer to a deep blue
const float color[] = { 0.0f, 0.2f, 0.4f, 1.0f };
devcon->ClearRenderTargetView(targetView.Get(), color);
/************************************
* 2.2 ClearDepthStencilView
************************************/
// clear the depth stencil view
devcon->ClearDepthStencilView(pDSView.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0u);
/************************************
* 2.3 - 2.12 Repeat graphic objects
************************************/
// do 3D rendering on the back buffer here
drawTriangle();
/************************************
* 3 DrawPrimitive
************************************/
// switch the back buffer and the front buffer
// 切换显示,将back buffer的内容交换到front buffer
swapchain->Present(0, 0);
}
// this is the function that cleans up Direct3D and COM
void CleanD3D()
{
// close and release all existing COM objects
// changed to use smart pointer, do NOT need to manually release
// pPixelShader->Release();
// pCompiledPixelBlob->Release();
// pVertexShader->Release();
// pCompiledVertexBlob->Release();
// pVertexBuffer->Release();
// pInputLayout->Release();
// swapchain->Release();
// targetView->Release();
// dev->Release();
// devcon->Release();
}
// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
// the entry point for any Windows program // the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance, int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance, HINSTANCE hPrevInstance,
...@@ -644,98 +13,15 @@ int WINAPI WinMain(HINSTANCE hInstance, ...@@ -644,98 +13,15 @@ int WINAPI WinMain(HINSTANCE hInstance,
int nCmdShow) int nCmdShow)
{ {
logger.start(); logger.start();
// the handle for the window, filled by a function
HWND hWnd;
// this struct holds information for the window class
WNDCLASSEX wc;
// clear out the window class for use
ZeroMemory(&wc, sizeof(WNDCLASSEX));
// fill in the struct with the needed information
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "WindowClass";
// register the window class
RegisterClassEx(&wc);
// create rectangle for client area // the handle for the window, filled by a function
RECT wr = { 0, 0, 800, 600 }; // set the size only, but not the position HWND hWnd = nullptr;
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); // adjust the size
// create the window and use the result as the handle
hWnd = CreateWindowEx(
NULL,
"WindowClass", // name of the window class
"Oasis Demo", // title of the window
WS_OVERLAPPEDWINDOW, // window style
300, // x-position of the window
200, // y-position of the window
800, // width of the window
600, // height of the window
NULL, // we have no parent window, NULL
NULL, // we aren't using menus, NULL
hInstance, // application handle
NULL); // used with multiple windows, NULL
// display the window on the screen
ShowWindow(hWnd, nCmdShow);
// set up and initialize Direct3D
//InitD3D(hWnd);
//std::unique_ptr<Graphics> pGfx = std::make_unique<Graphics>(hWnd);
Graphics gfx(hWnd); // register window
gfx.SetProjection(DirectX::XMMatrixPerspectiveLH(1.0f, 3.0f / 4.0f, 0.5f, 40.0f)); Window window(hWnd, hInstance, nCmdShow, 800, 600, logger);
//std::vector<std::unique_ptr<Box>> boxes; // init core handler for interactive process, i.e. retrieve IO and process graphics
//for (int i = -5; i < 6; i++) CoreHandler coreHandler(logger);
//{
// for (int j = -5; j < 6; j++)
// {
// float boxPos[8][3] =
// {
// // left bottom back
// { -1.0f + i, -1.0f, -1.0f + j },
// // right bottom back
// { 1.0f + i, -1.0f, -1.0f + j },
// // left top back
// { -1.0f + i, 1.0f, -1.0f + j },
// // right top back
// { 1.0f + i, 1.0f, -1.0f + j },
// // left bottom front
// { -1.0f + i, -1.0f, 1.0f + j },
// // right bottom front
// { 1.0f + i, -1.0f, 1.0f + j },
// // left top front
// { -1.0f + i, 1.0f, 1.0f + j },
// // right top front
// { 1.0f + i, 1.0f, 1.0f + j },
// };
// float boxCol[6][3] =
// {
// { 0.0f, 1.0f, 0.0f }, // Green
// { 1.0f, 1.0f, 0.0f }, // Yellow
// { 1.0f, 0.0f, 0.0f }, // Red
// { 0.0f, 0.0f, 1.0f }, // Blue
// { 0.0f, 1.0f, 1.0f }, // Cyan
// { 1.0f, 0.0f, 1.0f }, // Magenta
// };
// boxes.push_back(std::make_unique<Box>(gfx, boxPos, boxCol, logger));
// }
//}
Ground ground(gfx, logger);
double seed = 0;
seed = int(seed) % 10000;
// enter the main loop:
// this struct holds Windows event messages // this struct holds Windows event messages
MSG msg; MSG msg;
...@@ -759,55 +45,13 @@ int WINAPI WinMain(HINSTANCE hInstance, ...@@ -759,55 +45,13 @@ int WINAPI WinMain(HINSTANCE hInstance,
} }
else else
{ {
//RenderFrame(); // Retrieve IO and process graphics
gfx.RendorFrame(); coreHandler.CoreProcess(window, window.GetGfx(), logger);
seed++;
auto dt = sin(seed / 1000);
logger.PutLog("seed", std::to_string(seed));
logger.PutLog("dt", std::to_string(dt));
auto sinValue = sin(seed / 5000);
auto cosValue = cos(seed / 5000);
// Set camera
DirectX::XMVECTOR eyePosition = DirectX::XMVectorSet(5.0f * sinValue, 3.0f, -5.0f * cosValue, 1.0f);
DirectX::XMVECTOR focusPosition = DirectX::XMVectorZero();
DirectX::XMVECTOR upDirection = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f);
ground.SetCamera(logger, eyePosition, focusPosition, upDirection);
ground.DrawShapes(gfx, logger);
gfx.EndFrame();
} }
} }
// clean up DirectX and COM
CleanD3D();
logger.end(); logger.end();
// return this part of the WM_QUIT message to Windows // return this part of the WM_QUIT message to Windows
return msg.wParam; return msg.wParam;
}
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// sort through and find what code to run for the message given
switch (message)
{
// this message is read when the window is closed
case WM_DESTROY:
{
// close the application entirely
PostQuitMessage(0);
return 0;
}
break;
}
// Handle any messages the switch statement didn't
return DefWindowProc(hWnd, message, wParam, lParam);
} }
\ No newline at end of file
#include "Window.h"
#include "Ground.h"
Window::Window(HWND& hWnd, HINSTANCE& hInstance, int nCmdShow, int x, int y, Logger& logger)
{
// this struct holds information for the window class
WNDCLASSEX wc;
// clear out the window class for use
ZeroMemory(&wc, sizeof(WNDCLASSEX));
// fill in the struct with the needed information
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = HandleMsgSetup;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "WindowClass";
// register the window class
RegisterClassEx(&wc);
// create rectangle for client area
RECT wr = { 0, 0, x, y }; // set the size only, but not the position
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); // adjust the size
// create the window and use the result as the handle
hWnd = CreateWindowEx(
NULL,
"WindowClass", // name of the window class
"Oasis Demo", // title of the window
WS_OVERLAPPEDWINDOW, // window style
300, // x-position of the window
200, // y-position of the window
x, // width of the window
y, // height of the window
NULL, // we have no parent window, NULL
NULL, // we aren't using menus, NULL
hInstance, // application handle
NULL); // used with multiple windows, NULL
// display the window on the screen
ShowWindow(hWnd, nCmdShow);
// Init graphics
pGfx = std::make_unique<Graphics>(hWnd);
// Set projection
pGfx->SetProjection(DirectX::XMMatrixPerspectiveLH(1.0f, 3.0f / 4.0f, 0.5f, 40.0f));
// Init scenes
this->InitScene(logger);
}
LRESULT CALLBACK Window::HandleMsgSetup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept
{
// use create parameter passed in from CreateWindow() to store window class pointer at WinAPI side
if (msg == WM_NCCREATE)
{
// extract ptr to window class from creation data
// 使用CREATESTRUCT函数的第一个参数lpCreateParams,通过reinterpret_cast,获取到CREATESTRUCT函数的指针
// lpCreateParams的指针和CREATESTRUCT的指针指向同一个物理地址,只是寻址对象不同
const CREATESTRUCTW* const pCreate = reinterpret_cast<CREATESTRUCTW*>(lParam);
// 取得指向Window的指针
Window* const pWnd = static_cast<Window*>(pCreate->lpCreateParams);
// 将Window类,存储到WinAPI
// set WinAPI-managed user data to store ptr to window class
SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pWnd));
// set message proc to normal (non-setup) handler now that setup is finished
SetWindowLongPtr(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(&Window::HandleMsgThunk));
// forward message to window class handler
return pWnd->HandleMsg(hWnd, msg, wParam, lParam);
}
}
LRESULT CALLBACK Window::HandleMsgThunk(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept
{
// retrieve ptr to window class
Window* const pWnd = reinterpret_cast<Window*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
// forward message to window class handler
return pWnd->HandleMsg(hWnd, msg, wParam, lParam);
}
LRESULT Window::HandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept
{
switch (msg)
{
// we don't want the DefProc to handle this message because
// we want our destructor to destroy the window, so return 0 instead of break
case WM_CLOSE:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
Graphics& Window::GetGfx()
{
return *pGfx;
}
void Window::InitScene(Logger &logger)
{
//Ground ground(*pGfx, logger);
pSections.push_back(make_unique<Ground>(*pGfx, logger));
}
void Window::DrawScenes(
Graphics& gfx,
Logger& logger) noexcept
{
for (auto& section : pSections)
{
section->DrawShapes(gfx, logger);
}
};
\ No newline at end of file
#pragma once
#include <Windows.h>
#include "Graphics.h"
#include <memory>
#include "Logger.h"
#include <vector>
#include "Section.h"
using namespace std;
class Window
{
public:
// 캯
Window(HWND& hWnd, HINSTANCE& hInstance, int nCmdShow, int x, int y, Logger& logger);
//~Window();
Window(const Window&) = delete;
Window& operator=(const Window&) = delete;
// Get Graphics member
Graphics& GetGfx();
// Put all scene initialization here
void InitScene(Logger& logger);
// Draw all scenes
void DrawScenes(Graphics& gfx, Logger& logger) noexcept;
private:
static LRESULT CALLBACK HandleMsgSetup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept;
static LRESULT CALLBACK HandleMsgThunk(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept;
LRESULT HandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept;
private:
unique_ptr<Graphics> pGfx;
vector<unique_ptr<Section>> pSections;
};
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment