Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "directx.h"
- #include <D3Dcompiler.h>
- #include <Windows.h>
- using namespace std;
- struct Vertex {
- DirectX::XMFLOAT3 position;
- DirectX::XMFLOAT4 color;
- };
- struct PerObject
- {
- DirectX::XMMATRIX WVP;
- };
- PerObject PerObj;
- Vertex triangle[] = {
- DirectX::XMFLOAT3( -0.5f, -0.5f, 0.5f), DirectX::XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f ),
- DirectX::XMFLOAT3( -0.5f, 0.5f, 0.5f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f ),
- DirectX::XMFLOAT3( 0.5f, 0.5f, 0.5f), DirectX::XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f ),
- DirectX::XMFLOAT3( 0.5f, -0.5f, 0.5f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f ),
- };
- Directx::Directx() : g_Swapchain(NULL), g_Device(NULL), g_Devicecontext(NULL), g_Rendertarget(NULL){}
- Directx::~Directx() {}
- bool Directx::InitializeD3D(HWND hwnd, int width, int height, bool fullscreen) {
- DXGI_SWAP_CHAIN_DESC sd;
- ZeroMemory(&sd, sizeof(sd));
- sd.BufferCount = 1;
- sd.BufferDesc.Width = width;
- sd.BufferDesc.Height = height;
- sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- sd.BufferDesc.RefreshRate.Numerator = 60;
- sd.BufferDesc.RefreshRate.Denominator = 1;
- sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
- sd.OutputWindow = hwnd;
- sd.SampleDesc.Count = 4;
- sd.SampleDesc.Quality = 0;
- sd.Windowed = (fullscreen) ? false : true;
- D3D_FEATURE_LEVEL featurelevels[] =
- {
- D3D_FEATURE_LEVEL_11_0,
- D3D_FEATURE_LEVEL_10_1,
- D3D_FEATURE_LEVEL_10_0,
- D3D_FEATURE_LEVEL_9_3,
- D3D_FEATURE_LEVEL_9_2,
- D3D_FEATURE_LEVEL_9_1,
- };
- int numfeaturelevels = 5;
- if(FAILED(D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, D3D11_CREATE_DEVICE_DEBUG, featurelevels, numfeaturelevels, D3D11_SDK_VERSION, &sd, &g_Swapchain, &g_Device, NULL, &g_Devicecontext)))
- return false;
- ID3D11Texture2D *pBackBuffer;
- HRESULT hr;
- if(FAILED(g_Swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer)))
- return false;
- hr = g_Device->CreateRenderTargetView(pBackBuffer, NULL, &g_Rendertarget);
- pBackBuffer->Release();
- if(FAILED(hr))
- return false;
- g_Devicecontext->OMSetRenderTargets(1, &g_Rendertarget, g_DepthStencilview);
- D3D11_VIEWPORT vp;
- vp.Height = (FLOAT)height;
- vp.Width = (FLOAT)width;
- vp.MaxDepth = 1.0f;
- vp.MinDepth = 0.0f;
- vp.TopLeftX = 0;
- vp.TopLeftY = 0;
- D3D11_TEXTURE2D_DESC depthstencil;
- ZeroMemory(&depthstencil, sizeof(depthstencil));
- depthstencil.Width = width;
- depthstencil.Height = height;
- depthstencil.MipLevels = 1;
- depthstencil.ArraySize = 1;
- depthstencil.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
- depthstencil.SampleDesc.Count = 1;
- depthstencil.SampleDesc.Quality = 0;
- depthstencil.Usage = D3D11_USAGE_DEFAULT;
- depthstencil.BindFlags = D3D11_BIND_DEPTH_STENCIL;
- depthstencil.CPUAccessFlags = 0;
- depthstencil.MiscFlags = 0;
- g_Device->CreateTexture2D(&depthstencil, NULL, &g_Depthbuffer);
- g_Device->CreateDepthStencilView(g_Depthbuffer, NULL, &g_DepthStencilview);
- g_Devicecontext->RSSetViewports(1, &vp);
- D3D11_BUFFER_DESC cb;
- cb.Usage = D3D11_USAGE_DEFAULT;
- cb.ByteWidth = sizeof(g_PerObjectbuffer);
- cb.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
- cb.CPUAccessFlags = 0;
- cb.MiscFlags = 0;
- g_Device->CreateBuffer(&cb, NULL, &g_PerObjectbuffer);
- camPosition = DirectX::XMVectorSet(0.0f, 0.0f, -0.5f, 0.0f);
- camTarget = DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
- camUp = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
- camView = DirectX::XMMatrixLookAtLH(camPosition, camTarget, camUp);
- camProjection = DirectX::XMMatrixPerspectiveFovLH(0.4f * 3.14f, (float)width/height, 1.0f, 1000.0f);
- return true;
- }
- void Directx::Render() {
- float ClearColor[4] = { 0.0f, 0.125f, 0.6f, 1.0f };
- g_Devicecontext->ClearRenderTargetView(g_Rendertarget, ClearColor);
- g_Devicecontext->ClearDepthStencilView(g_DepthStencilview, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);
- World = DirectX::XMMatrixIdentity();
- WVP = World * camView * camProjection;
- PerObj.WVP = DirectX::XMMatrixTranspose(WVP);
- g_Devicecontext->UpdateSubresource(g_PerObjectbuffer, 0, NULL, &PerObj, 0, 0);
- g_Devicecontext->VSGetConstantBuffers(0, 1, &g_PerObjectbuffer);
- g_Devicecontext->DrawIndexed(6, 0, 0);
- g_Swapchain->Present(0, 0);
- }
- int Directx::DrawSquare() {
- DWORD indices[] = {
- 0, 1, 2,
- 0, 2, 3,
- };
- D3D11_BUFFER_DESC bd;
- ZeroMemory(&bd, sizeof(bd));
- bd.ByteWidth = sizeof(DWORD) * 2 * 3;
- bd.Usage = D3D11_USAGE_DEFAULT;
- bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
- bd.CPUAccessFlags = 0;
- bd.MiscFlags = 0;
- D3D11_SUBRESOURCE_DATA data;
- ZeroMemory(&data, sizeof(data));
- data.pSysMem = indices;
- g_Device->CreateBuffer(&bd, &data, &g_Indexbuffer);
- g_Devicecontext->IASetIndexBuffer(g_Indexbuffer, DXGI_FORMAT_R32_UINT, 0);
- D3D11_BUFFER_DESC vertexBufferDesc;
- ZeroMemory( &vertexBufferDesc, sizeof(vertexBufferDesc) );
- vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
- vertexBufferDesc.ByteWidth = sizeof( Vertex ) * 4;
- vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
- vertexBufferDesc.CPUAccessFlags = 0;
- vertexBufferDesc.MiscFlags = 0;
- D3D11_SUBRESOURCE_DATA vertexBufferData;
- ZeroMemory( &vertexBufferData, sizeof(vertexBufferData) );
- vertexBufferData.pSysMem = triangle;
- g_Device->CreateBuffer( &vertexBufferDesc, &vertexBufferData, &g_Vertexbuffer);
- CHAR* pVertexShaderCode = "struct VS_OUTPUT"
- "{"
- "float4 Pos : SV_POSITION;"
- "float4 Color : COLOR;"
- "};cbuffer PerObject { float4x4 WVP;};"
- "void vertex_shader( in float4 posIn : POSITION, in float4 colorIn : COLOR,"
- "out float4 posOut : SV_POSITION, out float4 colorOut : COLOR)"
- "{ VS_OUTPUT output; posOut = posIn; output.Pos = mul(inPos, WVP);"
- " colorOut = colorIn; return output; }";
- CHAR* pPixelShaderCode = "float4 pixel_shader( VS_OUTPUT input) : SV_TARGET"
- "{ return input.color; }";
- ID3D10Blob* pVertexShaderBytecode = NULL;
- ID3D10Blob* pCompileErrors = NULL;
- if( pCompileErrors != NULL ) {
- OutputDebugStringA( (CHAR*)pCompileErrors->GetBufferPointer() );
- pCompileErrors->Release();
- return 0;
- }
- D3DCompile(pVertexShaderCode, lstrlenA( pVertexShaderCode ) + 1, "vertex shader", NULL, NULL, "vertex_shader", "vs_4_0", NULL, 0, &pVertexShaderBytecode, &pCompileErrors );
- g_Device->CreateVertexShader( pVertexShaderBytecode->GetBufferPointer(), pVertexShaderBytecode->GetBufferSize(), NULL, &g_pVertexShader );
- ID3D10Blob* pPixelShaderBytecode = NULL;
- D3DCompile(pPixelShaderCode, 0, 0, NULL, NULL, "pixel_shader", "ps_4_0", NULL, 0, &pPixelShaderBytecode, &pCompileErrors );
- if( pCompileErrors != NULL ) {
- OutputDebugStringA( (CHAR*)pCompileErrors->GetBufferPointer() );
- pCompileErrors->Release();
- return 0;
- }
- g_Device->CreatePixelShader( pPixelShaderBytecode->GetBufferPointer(), pPixelShaderBytecode->GetBufferSize(), NULL, &g_pPixelShader );
- pPixelShaderBytecode->Release();
- D3D11_INPUT_ELEMENT_DESC inputLayoutDesc[] =
- {
- { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
- { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
- };
- UINT numElements = ARRAYSIZE( inputLayoutDesc );
- g_Device->CreateInputLayout( inputLayoutDesc, numElements, pVertexShaderBytecode->GetBufferPointer(), pVertexShaderBytecode->GetBufferSize(), &g_pInputLayout );
- pVertexShaderBytecode->Release();
- g_Devicecontext->IASetInputLayout( g_pInputLayout );
- UINT stride = sizeof( Vertex );
- UINT offset = 0;
- g_Devicecontext->IASetVertexBuffers(0, 1, &g_Vertexbuffer, &stride, &offset );
- g_Devicecontext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
- g_Devicecontext->VSSetShader( g_pVertexShader, NULL, 0 );
- g_Devicecontext->PSSetShader( g_pPixelShader, NULL, 0 );
- return true;
- }
- void Directx::ShutdownDirectx() {
- g_Swapchain->Release();
- g_Device->Release();
- g_Devicecontext->Release();
- g_Rendertarget->Release();
- g_Vertexbuffer->Release();
- g_Indexbuffer->Release();
- g_pPixelShader->Release();
- g_pInputLayout->Release();
- g_pVertexShader->Release();
- g_Depthbuffer->Release();
- g_DepthStencilview->Release();
- g_PerObjectbuffer->Release();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement