标签:include viewport resize 矩形 lists tar oid 开启 模板
初始化Driect3D类:
#include "Common\d3dApp.h"
#include <DirectXColors.h>
using namespace DirectX;
class InitDirect3DApp : public D3DApp
{
public:
InitDirect3DApp(HINSTANCE hInstance);
~InitDirect3DApp();
virtual bool Initialize()override;
private:
virtual void OnResize()override;
virtual void Update(const GameTimer& gt)override;
virtual void Draw(const GameTimer& gt)override;
};
InitDirect3DApp::InitDirect3DApp(HINSTANCE hInscance) :D3DApp(hInscance)
{
}
InitDirect3DApp::~InitDirect3DApp()
{
}
bool InitDirect3DApp::Initialize()
{
if (!D3DApp::Initialize())
{
return false;
}
return true;
}
void InitDirect3DApp::OnResize()
{
D3DApp::OnResize();
}
void InitDirect3DApp::Update(const GameTimer& gt)
{
}
void InitDirect3DApp::Draw(const GameTimer& gt)
{
//重置命令分配器
ThrowIfFailed(mDirectCmdListAlloc->Reset());
//重置命令列表
ThrowIfFailed(mCommandList->Reset(mDirectCmdListAlloc.Get(), nullptr));
//对资源的状态进行转变,将资源从呈现状态转变到渲染目标状态
mCommandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(
CurrentBackBuffer(),
D3D12_RESOURCE_STATE_PRESENT,
D3D12_RESOURCE_STATE_RENDER_TARGET
));
//设置视口和裁剪矩形,他们需要跟随命令列表的重置而重置
mCommandList->RSSetViewports(1, &mScreenViewport);
mCommandList->RSSetScissorRects(1, &mScissorRect);
//清除后台缓冲区和深度缓冲区
mCommandList->ClearRenderTargetView(CurrentBackBufferView(), Colors::LightSteelBlue, 0, nullptr);
mCommandList->ClearDepthStencilView(DepthStencilView(), D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL,
1.0f, 0, 0, nullptr);
//指定将要渲染的缓冲区
mCommandList->OMSetRenderTargets(1, &CurrentBackBufferView(), true, &DepthStencilView());
//再次对资源状态进行转变,将资源从渲染目标状态转变为呈现状态
mCommandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(
CurrentBackBuffer(), D3D12_RESOURCE_STATE_RENDER_TARGET,
D3D12_RESOURCE_STATE_PRESENT
));
//完成命令的记录
ThrowIfFailed(mCommandList->Close());
//将命令列表的命令加入到命令队列中
ID3D12CommandList* cmdsList[] = { mCommandList.Get() };
mCommandQueue->ExecuteCommandLists(_countof(cmdsList), cmdsList);
//交换前后台缓冲区
ThrowIfFailed(mSwapChain->Present(0, 0));;
mCurrBackBuffer = (mCurrBackBuffer + 1) % SwapChainBufferCount;
//等待此帧的命令执行完毕
FlushCommandQueue();
}
主函数:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
{
//为调试版本开启运行时内存检测,方便监督内存泄漏的情况
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
try
{
InitDirect3DApp theApp(hInstance);
if (!theApp.Initialize())
{
return 0;
}
return theApp.Run();
}
catch (DxException& e)
{
MessageBox(nullptr, e.ToString().c_str(), L"HR Failed", MB_OK);
return 0;
}
}
运行结果
接下来我们将介绍一些在前面没有提到的方法:
1、ClearRenderTargetView():将指定的渲染目标清理为给定的颜色
2、ClearDepthStencilView():清理指定的深度/模板缓冲区
3、OMSetRenderTargets():设置我们希望在渲染流水线上使用的渲染目标和深度/模板缓冲区
标签:include viewport resize 矩形 lists tar oid 开启 模板
原文地址:https://www.cnblogs.com/yaya12138/p/11562194.html