95 lines
3.3 KiB
C++
95 lines
3.3 KiB
C++
#include "Direct3D.h"
|
|
#include "Utilities.h"
|
|
|
|
INT Direct3D::init(HWND hWnd, UINT width, UINT height, BOOL isFullscreen)
|
|
{
|
|
HRESULT hr = {};
|
|
|
|
// 1. define swap chain description
|
|
DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
|
|
swapChainDesc.OutputWindow = hWnd; // target window instance handle
|
|
swapChainDesc.Windowed = !isFullscreen; // windowed or fullscreen
|
|
swapChainDesc.BufferCount = 1; // amount of backbuffers, minimum 1
|
|
swapChainDesc.BufferDesc.Width = width;
|
|
swapChainDesc.BufferDesc.Height = height;
|
|
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // pixel format, here 4 channels (red, green, blue, alpha) with 8bit depth each and unsigned normalized floating points
|
|
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // which technique is used to render to back buffer
|
|
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // what happens to front buffer after swap
|
|
swapChainDesc.SampleDesc.Count = 1; // have to be at least one
|
|
|
|
// 2. create Direct3D device, device context & swap chain
|
|
D3D_FEATURE_LEVEL featureLevels[] = {
|
|
D3D_FEATURE_LEVEL_11_1,
|
|
D3D_FEATURE_LEVEL_11_0,
|
|
D3D_FEATURE_LEVEL_10_1,
|
|
D3D_FEATURE_LEVEL_10_0
|
|
};
|
|
|
|
D3D_FEATURE_LEVEL chosenFeatureLevel = {};
|
|
|
|
hr = D3D11CreateDeviceAndSwapChain(
|
|
nullptr, // graphic card, nullptr -> default adapter
|
|
D3D_DRIVER_TYPE_HARDWARE, // rendering with hard- or software renderer
|
|
nullptr, // software renderer module reference
|
|
0, // optional flags
|
|
featureLevels, std::size(featureLevels), // which api features are supported
|
|
D3D11_SDK_VERSION, &swapChainDesc, &_pDXGISwapChain, &_pD3DDevice, &chosenFeatureLevel, &_pD3DDeviceContext);
|
|
CheckFailed(hr, 20);
|
|
|
|
// 3. create render target
|
|
ID3D11Texture2D* pBackBuffer = nullptr;
|
|
hr = _pDXGISwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
|
|
CheckFailed(hr, 22);
|
|
|
|
hr = _pD3DDevice->CreateRenderTargetView(pBackBuffer, nullptr, &_pD3DRenderTargetView);
|
|
CheckFailed(hr, 24);
|
|
|
|
safeRelease(pBackBuffer);
|
|
|
|
// 4. create depth stencil buffer
|
|
|
|
// 5. create rasterizer state
|
|
//d3d.getDevice()->SetRenderState(D3DRS_LIGHTING, TRUE);
|
|
//d3d.getDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
|
//d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
|
|
//d3d.getDevice()->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
|
|
//d3d.getDevice()->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
|
|
//d3d.getDevice()->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
|
|
|
|
// 6. describe viewport
|
|
D3D11_VIEWPORT viewport = {};
|
|
viewport.TopLeftX = 0.0f;
|
|
viewport.TopLeftY = 0.0f;
|
|
viewport.Width = width;
|
|
viewport.Height = height;
|
|
|
|
// 7. setup render pipeline (device context)
|
|
_pD3DDeviceContext->OMSetRenderTargets(1, &_pD3DRenderTargetView, nullptr);
|
|
_pD3DDeviceContext->RSSetViewports(1, &viewport);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void Direct3D::beginScene(FLOAT r, FLOAT g, FLOAT b)
|
|
{
|
|
// clear back buffer with a solid color
|
|
FLOAT backgroundColor[] = { r, g, b, 1.0f };
|
|
_pD3DDeviceContext->ClearRenderTargetView(_pD3DRenderTargetView, backgroundColor);
|
|
}
|
|
|
|
void Direct3D::endScene()
|
|
{
|
|
_pDXGISwapChain->Present(
|
|
0, // sync interval, 0,1,2,3,4 -> (de-)active vsync setting and how often should be synced
|
|
0 // optional flags
|
|
);
|
|
}
|
|
|
|
void Direct3D::deInit()
|
|
{
|
|
safeRelease(_pD3DRenderTargetView);
|
|
safeRelease(_pDXGISwapChain);
|
|
safeRelease(_pD3DDeviceContext);
|
|
safeRelease(_pD3DDevice);
|
|
}
|