Advertisement
Guest User

updated

a guest
Dec 20th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.41 KB | None | 0 0
  1. #include "directx.h"
  2. #include <D3Dcompiler.h>
  3. #include <Windows.h>
  4.  
  5. using namespace std;
  6.  
  7. struct Vertex {
  8.     DirectX::XMFLOAT3 position;
  9.     DirectX::XMFLOAT4 color;
  10. };
  11.  
  12. struct PerObject
  13. {
  14.     DirectX::XMMATRIX  WVP;
  15. };
  16.  
  17. PerObject PerObj;
  18.  
  19. Vertex triangle[] = {
  20.  
  21.     DirectX::XMFLOAT3( -0.5f, -0.5f, 0.5f), DirectX::XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f ),
  22.     DirectX::XMFLOAT3( -0.5f,  0.5f, 0.5f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f ),
  23.     DirectX::XMFLOAT3(  0.5f,  0.5f, 0.5f), DirectX::XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f ),
  24.     DirectX::XMFLOAT3(  0.5f, -0.5f, 0.5f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f ),
  25. };
  26.  
  27. Directx::Directx() : g_Swapchain(NULL), g_Device(NULL), g_Devicecontext(NULL), g_Rendertarget(NULL){}
  28.  
  29. Directx::~Directx() {}
  30.  
  31. bool Directx::InitializeD3D(HWND hwnd, int width, int height, bool fullscreen) {
  32.    
  33.     DXGI_SWAP_CHAIN_DESC sd;
  34.  
  35.     ZeroMemory(&sd, sizeof(sd));
  36.  
  37.     sd.BufferCount = 1;
  38.     sd.BufferDesc.Width = width;
  39.     sd.BufferDesc.Height = height;
  40.     sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  41.     sd.BufferDesc.RefreshRate.Numerator = 60;
  42.     sd.BufferDesc.RefreshRate.Denominator = 1;
  43.     sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  44.     sd.OutputWindow = hwnd;
  45.     sd.SampleDesc.Count = 4;
  46.     sd.SampleDesc.Quality = 0;
  47.     sd.Windowed = (fullscreen) ? false : true;
  48.  
  49.  
  50.     D3D_FEATURE_LEVEL featurelevels[] =
  51.     {
  52.         D3D_FEATURE_LEVEL_11_0,
  53.         D3D_FEATURE_LEVEL_10_1,
  54.         D3D_FEATURE_LEVEL_10_0,
  55.         D3D_FEATURE_LEVEL_9_3,
  56.         D3D_FEATURE_LEVEL_9_2,
  57.         D3D_FEATURE_LEVEL_9_1,
  58.     };
  59.  
  60.     int numfeaturelevels = 5;
  61.  
  62.     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)))
  63.         return false;
  64.  
  65.  
  66.     ID3D11Texture2D *pBackBuffer;
  67.     HRESULT hr;
  68.  
  69.     if(FAILED(g_Swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer)))
  70.             return false;
  71.  
  72.     hr = g_Device->CreateRenderTargetView(pBackBuffer, NULL, &g_Rendertarget);
  73.     pBackBuffer->Release();
  74.  
  75.     if(FAILED(hr))
  76.             return false;
  77.  
  78.     g_Devicecontext->OMSetRenderTargets(1, &g_Rendertarget, g_DepthStencilview);
  79.  
  80.     D3D11_VIEWPORT vp;
  81.  
  82.     vp.Height = (FLOAT)height;
  83.     vp.Width = (FLOAT)width;
  84.     vp.MaxDepth = 1.0f;
  85.     vp.MinDepth = 0.0f;
  86.     vp.TopLeftX = 0;
  87.     vp.TopLeftY = 0;
  88.  
  89.     D3D11_TEXTURE2D_DESC depthstencil;
  90.  
  91.     ZeroMemory(&depthstencil, sizeof(depthstencil));
  92.  
  93.     depthstencil.Width = width;
  94.     depthstencil.Height = height;
  95.     depthstencil.MipLevels = 1;
  96.     depthstencil.ArraySize = 1;
  97.     depthstencil.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  98.     depthstencil.SampleDesc.Count = 1;
  99.     depthstencil.SampleDesc.Quality = 0;
  100.     depthstencil.Usage = D3D11_USAGE_DEFAULT;
  101.     depthstencil.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  102.     depthstencil.CPUAccessFlags = 0;
  103.     depthstencil.MiscFlags = 0;
  104.  
  105.     g_Device->CreateTexture2D(&depthstencil, NULL, &g_Depthbuffer);
  106.     g_Device->CreateDepthStencilView(g_Depthbuffer, NULL, &g_DepthStencilview);
  107.  
  108.     g_Devicecontext->RSSetViewports(1, &vp);
  109.  
  110.     D3D11_BUFFER_DESC cb;
  111.  
  112.     cb.Usage = D3D11_USAGE_DEFAULT;
  113.     cb.ByteWidth = sizeof(g_PerObjectbuffer);
  114.     cb.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  115.     cb.CPUAccessFlags = 0;
  116.     cb.MiscFlags = 0;
  117.  
  118.     g_Device->CreateBuffer(&cb, NULL, &g_PerObjectbuffer);
  119.  
  120.     camPosition = DirectX::XMVectorSet(0.0f, 0.0f, -0.5f, 0.0f);
  121.     camTarget = DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
  122.     camUp = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
  123.  
  124.     camView = DirectX::XMMatrixLookAtLH(camPosition, camTarget, camUp);
  125.  
  126.     camProjection = DirectX::XMMatrixPerspectiveFovLH(0.4f * 3.14f, (float)width/height, 1.0f, 1000.0f);
  127.  
  128.     return true;
  129. }
  130.  
  131. void Directx::Render() {
  132.  
  133.     float ClearColor[4] = { 0.0f, 0.125f, 0.6f, 1.0f };
  134.     g_Devicecontext->ClearRenderTargetView(g_Rendertarget, ClearColor);
  135.     g_Devicecontext->ClearDepthStencilView(g_DepthStencilview, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);
  136.  
  137.     World = DirectX::XMMatrixIdentity();
  138.  
  139.     WVP = World * camView * camProjection;
  140.  
  141.     PerObj.WVP = DirectX::XMMatrixTranspose(WVP);
  142.  
  143.     g_Devicecontext->UpdateSubresource(g_PerObjectbuffer, 0, NULL, &PerObj, 0, 0);
  144.     g_Devicecontext->VSGetConstantBuffers(0, 1, &g_PerObjectbuffer);
  145.  
  146.  
  147.     g_Devicecontext->DrawIndexed(6, 0, 0);
  148.     g_Swapchain->Present(0, 0);
  149. }
  150.  
  151. int Directx::DrawSquare() {
  152.  
  153.     DWORD indices[] = {
  154.         0, 1, 2,
  155.         0, 2, 3,
  156.     };
  157.    
  158.     D3D11_BUFFER_DESC bd;
  159.     ZeroMemory(&bd, sizeof(bd));
  160.  
  161.     bd.ByteWidth = sizeof(DWORD) * 2 * 3;
  162.     bd.Usage = D3D11_USAGE_DEFAULT;
  163.     bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
  164.     bd.CPUAccessFlags = 0;
  165.     bd.MiscFlags = 0;
  166.  
  167.     D3D11_SUBRESOURCE_DATA data;
  168.  
  169.     ZeroMemory(&data, sizeof(data));
  170.  
  171.     data.pSysMem = indices;
  172.     g_Device->CreateBuffer(&bd, &data, &g_Indexbuffer);
  173.  
  174.     g_Devicecontext->IASetIndexBuffer(g_Indexbuffer, DXGI_FORMAT_R32_UINT, 0);
  175.  
  176.     D3D11_BUFFER_DESC vertexBufferDesc;
  177.     ZeroMemory( &vertexBufferDesc, sizeof(vertexBufferDesc) );
  178.  
  179.     vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  180.     vertexBufferDesc.ByteWidth = sizeof( Vertex ) * 4;
  181.     vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  182.     vertexBufferDesc.CPUAccessFlags = 0;
  183.     vertexBufferDesc.MiscFlags = 0;
  184.  
  185.     D3D11_SUBRESOURCE_DATA vertexBufferData;
  186.  
  187.     ZeroMemory( &vertexBufferData, sizeof(vertexBufferData) );
  188.     vertexBufferData.pSysMem = triangle;
  189.     g_Device->CreateBuffer( &vertexBufferDesc, &vertexBufferData, &g_Vertexbuffer);
  190.  
  191.  
  192.     CHAR* pVertexShaderCode =   "struct VS_OUTPUT"
  193.                                 "{"
  194.                                     "float4 Pos : SV_POSITION;"
  195.                                     "float4 Color : COLOR;"
  196.                                 "};cbuffer PerObject { float4x4 WVP;};"
  197.                             "void vertex_shader( in float4 posIn : POSITION, in float4 colorIn : COLOR,"
  198.                             "out float4 posOut : SV_POSITION, out float4 colorOut : COLOR)"
  199.                             "{ VS_OUTPUT output; posOut = posIn; output.Pos = mul(inPos, WVP);"
  200.                             "  colorOut = colorIn; return output; }";
  201.  
  202.     CHAR* pPixelShaderCode =    "float4 pixel_shader( VS_OUTPUT input) : SV_TARGET"    
  203.                             "{ return input.color; }";
  204.  
  205.     ID3D10Blob* pVertexShaderBytecode = NULL;  
  206.     ID3D10Blob* pCompileErrors = NULL;
  207.  
  208.     if( pCompileErrors != NULL ) {
  209.         OutputDebugStringA( (CHAR*)pCompileErrors->GetBufferPointer() );
  210.         pCompileErrors->Release();  
  211.         return 0;
  212.     }
  213.  
  214.     D3DCompile(pVertexShaderCode,  lstrlenA( pVertexShaderCode ) + 1, "vertex shader", NULL, NULL, "vertex_shader", "vs_4_0", NULL, 0, &pVertexShaderBytecode, &pCompileErrors );  
  215.  
  216.      
  217.  
  218.     g_Device->CreateVertexShader( pVertexShaderBytecode->GetBufferPointer(), pVertexShaderBytecode->GetBufferSize(), NULL, &g_pVertexShader );      
  219.  
  220.  
  221.     ID3D10Blob* pPixelShaderBytecode = NULL;  
  222.     D3DCompile(pPixelShaderCode, 0, 0, NULL, NULL, "pixel_shader", "ps_4_0", NULL, 0, &pPixelShaderBytecode, &pCompileErrors );    
  223.        
  224.  
  225.     if( pCompileErrors != NULL ) {    
  226.         OutputDebugStringA( (CHAR*)pCompileErrors->GetBufferPointer() );
  227.         pCompileErrors->Release();  
  228.         return 0;
  229.     }    
  230.  
  231.  
  232.     g_Device->CreatePixelShader( pPixelShaderBytecode->GetBufferPointer(), pPixelShaderBytecode->GetBufferSize(), NULL, &g_pPixelShader );    
  233.     pPixelShaderBytecode->Release();  
  234.  
  235.  
  236.  
  237.     D3D11_INPUT_ELEMENT_DESC inputLayoutDesc[] =
  238.     {
  239.         { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  240.         { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
  241.     };
  242.  
  243.     UINT numElements = ARRAYSIZE( inputLayoutDesc );
  244.  
  245.     g_Device->CreateInputLayout( inputLayoutDesc, numElements, pVertexShaderBytecode->GetBufferPointer(), pVertexShaderBytecode->GetBufferSize(), &g_pInputLayout );    
  246.     pVertexShaderBytecode->Release();    
  247.  
  248.     g_Devicecontext->IASetInputLayout( g_pInputLayout );
  249.     UINT stride = sizeof( Vertex );  
  250.     UINT offset = 0;
  251.     g_Devicecontext->IASetVertexBuffers(0, 1, &g_Vertexbuffer, &stride, &offset );      
  252.  
  253.     g_Devicecontext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );  
  254.  
  255.     g_Devicecontext->VSSetShader( g_pVertexShader, NULL, 0 );  
  256.     g_Devicecontext->PSSetShader( g_pPixelShader, NULL, 0 );
  257.  
  258.     return true;
  259. }
  260.  
  261. void Directx::ShutdownDirectx() {
  262.  
  263.     g_Swapchain->Release();
  264.     g_Device->Release();
  265.     g_Devicecontext->Release();
  266.     g_Rendertarget->Release();
  267.     g_Vertexbuffer->Release();
  268.     g_Indexbuffer->Release();
  269.     g_pPixelShader->Release();
  270.     g_pInputLayout->Release();
  271.     g_pVertexShader->Release();
  272.     g_Depthbuffer->Release();
  273.     g_DepthStencilview->Release();
  274.     g_PerObjectbuffer->Release();
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement