Commit cb004c2c authored by Administrator's avatar Administrator
Browse files

fixed Graphics link error

parent 13cc941f
......@@ -7,7 +7,7 @@
************************************/
Graphics::Graphics(HWND hWnd)
{
// 初始化D3D
// 初始化D3D
InitD3D(hWnd);
}
......
#pragma once
#include "MyGraphics.h"
#include <iostream>
/************************************
* 构造函数Graphics
************************************/
MyGraphics::MyGraphics(HWND hWnd)
{
// 初始化D3D
InitD3D(hWnd);
}
/************************************
* 1 初始化D3D
************************************/
void MyGraphics::InitD3D(HWND hWnd)
{
/************************************
* 1.1 CreateDeviceAndSwapChain
************************************/
// 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
&pSwapChain, // IDXGISwapChain **ppSwapChain
&pDevice, // ID3D11Device **ppDevice
NULL, // D3D_FEATURE_LEVEL *FeatureLevel
&pContext); // ID3D11DeviceContext **ppImmediateContext
/************************************
* 1.2 DefineRenderTargetView
************************************/
Microsoft::WRL::ComPtr<ID3D11Texture2D> pBackBuffer;
// 从SwapChain中取得back buffer
pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
// 根据back buffer,创建新的View Target
pDevice->CreateRenderTargetView(pBackBuffer.Get(), NULL, &pTarget);
/************************************
* 1.3 DefineDepthStencilView
************************************/
// Define depth stencil buffer
Microsoft::WRL::ComPtr<ID3D11DepthStencilState> pDepthStencilState;
// Define depth stencil texture
Microsoft::WRL::ComPtr<ID3D11Texture2D> depthStencilTexture;
// 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
pDevice->CreateDepthStencilState(&depthStencilDesc, &pDepthStencilState);
// bind depth state
pContext->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
pDevice->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;
pDevice->CreateDepthStencilView(depthStencilTexture.Get(), &depthStencilViewDesc, &pDSView);
/************************************
* 1.4 DefineRenderTarget
************************************/
// 将渲染目标设置成我们新创建的View Target
pContext->OMSetRenderTargets(1u, pTarget.GetAddressOf(), pDSView.Get());
}
/************************************
* 2 渲染单帧
************************************/
void MyGraphics::RendorFrame(void)
{
/************************************
* 2.1 ClearRenderTargetView
************************************/
// clear the back buffer to a deep blue
const float color[] = { 0.0f, 0.2f, 0.4f, 1.0f };
pContext->ClearRenderTargetView(pTarget.Get(), color);
/************************************
* 2.2 ClearDepthStencilViews
************************************/
// clear the depth stencil view
pContext->ClearDepthStencilView(pDSView.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0u);
/************************************
* 2.3 - 2.12 Repeat graphic objects
************************************/
// do 3D rendering on the back buffer here
//drawObjects();
/************************************
* 3 DrawPrimitive
************************************/
// switch the back buffer and the front buffer
// 切换显示,将back buffer的内容交换到front buffer
pSwapChain->Present(0, 0);
}
\ No newline at end of file
#pragma once
#include <d3d11.h>
#include <wrl.h>
namespace wrl = Microsoft::WRL;
class MyGraphics
{
friend class Bindable;
public:
// 构造函数
MyGraphics(HWND hWnd);
// 单例设计模式,禁止复制
MyGraphics(const MyGraphics&) = delete;
MyGraphics& operator=(const MyGraphics&) = delete;
~MyGraphics() = default;
// 初始化3大件,Device/SwapChain/deviceContext
void InitD3D(HWND hWnd);
void RendorFrame(void);
//void drawObjects();
private:
// 指向Device的指针
wrl::ComPtr<ID3D11Device> pDevice;
// 指向交换链的指针
wrl::ComPtr<IDXGISwapChain> pSwapChain;
// 指向Context的指针
wrl::ComPtr<ID3D11DeviceContext> pContext;
// 指向View的指针
wrl::ComPtr<ID3D11RenderTargetView> pTarget;
// 指向Depth Buffer View的指针
wrl::ComPtr<ID3D11DepthStencilView> pDSView;
};
\ No newline at end of file
......@@ -130,8 +130,6 @@
<ClCompile Include="Bindable.cpp" />
<ClCompile Include="Drawable.cpp" />
<ClCompile Include="Graphics.cpp" />
<ClCompile Include="Graphics.h" />
<ClCompile Include="MyGraphics.cpp" />
<ClCompile Include="Timer.cpp" />
<ClCompile Include="Triangle.cpp" />
<ClCompile Include="VertexBuffer.cpp" />
......@@ -160,7 +158,7 @@
<ItemGroup>
<ClInclude Include="Bindable.h" />
<ClInclude Include="Drawable.h" />
<ClInclude Include="MyGraphics.h" />
<ClInclude Include="Graphics.h" />
<ClInclude Include="Timer.h" />
<ClInclude Include="Triangle.h" />
<ClInclude Include="VertexBuffer.h" />
......
......@@ -18,7 +18,6 @@ namespace dx = DirectX;
Timer timer;
#include "Graphics.h"
#include "MyGraphics.h"
#include <memory>
/************************************
......@@ -672,9 +671,7 @@ int WINAPI WinMain(HINSTANCE hInstance,
// set up and initialize Direct3D
//InitD3D(hWnd);
//std::unique_ptr<Graphics> pGfx = std::make_unique<Graphics>(hWnd);
MyGraphics gfx(hWnd);
//Graphics gfx2(hWnd);
Graphics gfx(hWnd);
// enter the main loop:
......
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