Advertisement
SomeGenericUsername

Untitled

Aug 11th, 2019
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.76 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <d3d9.h>
  3. #include <d3dx9.h>
  4. #include <wrl.h>
  5. #include <vector>
  6.  
  7. using namespace Microsoft::WRL;
  8.  
  9. ComPtr<IDirect3D9> _d3d{ };
  10. ComPtr<IDirect3DDevice9> _device{ };
  11. ComPtr<IDirect3DVertexBuffer9> _vertexBuffer{ };
  12. ComPtr<IDirect3DIndexBuffer9> _indexBuffer{ };
  13. ComPtr<IDirect3DVertexDeclaration9> _vertexDecl{ };
  14. ComPtr<IDirect3DVertexShader9> _vertexShader{ };
  15. ComPtr<IDirect3DPixelShader9> _pixelShader{ };
  16. ComPtr<ID3DXConstantTable> _vertexTable{ };
  17. D3DXMATRIX _worldMatrix{ };
  18. D3DXMATRIX _rotationMatrix{ };
  19.  
  20. bool _running{ false };
  21.  
  22. template <typename T>
  23. unsigned int GetByteSize(const std::vector<T>& vec) {
  24.     return sizeof(vec[0]) * vec.size();
  25. }
  26.  
  27. struct VStruct {
  28.     float x, y, z;
  29.     D3DCOLOR color;
  30. };
  31.  
  32.  
  33. bool SetupTransform() {
  34.     D3DXMatrixIdentity(&_worldMatrix);
  35.     D3DXMatrixIdentity(&_rotationMatrix);
  36.    
  37.     D3DXMATRIX scaling{ };
  38.     D3DXMatrixScaling(&scaling, 10, 10, 1);
  39.  
  40.     D3DXMatrixMultiply(&_worldMatrix, &scaling, &_rotationMatrix);
  41.     HRESULT result = _vertexTable->SetMatrix(_device.Get(), "worldMatrix", &_worldMatrix);
  42.     if (FAILED(result))
  43.         return false;
  44.  
  45.     D3DXMATRIX view{ };
  46.     D3DXMatrixLookAtLH(&view, &D3DXVECTOR3{ 0.0f, 0.0f, -20.0f }, &D3DXVECTOR3{ 0.0f, 0.0f, 0.0f }, &D3DXVECTOR3{ 0.0f, 1.0f, 0.0f });
  47.     result = _vertexTable->SetMatrix(_device.Get(), "viewMatrix", &view);
  48.     if (FAILED(result))
  49.         return false;
  50.  
  51.     D3DXMATRIX projection{ };
  52.     D3DXMatrixPerspectiveFovLH(&projection, D3DXToRadian(60.0f), 1024.0f / 768.0f, 0.0f, 100.0f);
  53.     result = _vertexTable->SetMatrix(_device.Get(), "projectionMatrix", &projection);
  54.     if (FAILED(result))
  55.         return false;
  56.  
  57.     D3DXMatrixRotationY(&_rotationMatrix, D3DXToRadian(0.5f));
  58.  
  59.     return true;
  60. }
  61.  
  62. IDirect3DVertexBuffer9* CreateVertexBuffer(const std::vector<VStruct>& vertices) {
  63.     IDirect3DVertexBuffer9* buffer{ };
  64.     HRESULT result{ };
  65.    
  66.     result = _device->CreateVertexBuffer(GetByteSize(vertices), 0, 0, D3DPOOL_DEFAULT, &buffer, nullptr);
  67.     if (FAILED(result))
  68.         return nullptr;
  69.  
  70.     void* data{ };
  71.     result = buffer->Lock(0, GetByteSize(vertices), &data, 0);    
  72.     if (FAILED(result))
  73.         return nullptr;
  74.  
  75.     memcpy(data, vertices.data(), GetByteSize(vertices));
  76.     buffer->Unlock();
  77.  
  78.     return buffer;
  79. }
  80.  
  81. IDirect3DIndexBuffer9* CreateIndexBuffer(std::vector<unsigned int>& indices) {
  82.     IDirect3DIndexBuffer9* buffer{ };
  83.     HRESULT result{ };
  84.    
  85.     result = _device->CreateIndexBuffer(GetByteSize(indices), 0, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &buffer, nullptr);
  86.     if (FAILED(result))
  87.         return nullptr;
  88.  
  89.     void* data{ };
  90.     result = buffer->Lock(0, GetByteSize(indices), &data, 0);
  91.     if (FAILED(result))
  92.         return nullptr;
  93.  
  94.     memcpy(data, indices.data(), GetByteSize(indices));
  95.     buffer->Unlock();
  96.  
  97.     return buffer;
  98. }
  99.  
  100. bool InitD3D(HWND hWnd) {
  101.     RECT clientRect{ };
  102.     GetClientRect(hWnd, &clientRect);
  103.  
  104.     _d3d = Direct3DCreate9(D3D_SDK_VERSION);
  105.  
  106.     D3DDISPLAYMODE displayMode{ };
  107.     _d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode);
  108.  
  109.     D3DPRESENT_PARAMETERS pp{ };
  110.     pp.BackBufferWidth = clientRect.right;
  111.     pp.BackBufferHeight = clientRect.bottom;
  112.     pp.BackBufferFormat = displayMode.Format;
  113.     pp.BackBufferCount = 1;                
  114.     pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  115.     pp.hDeviceWindow = hWnd;              
  116.     pp.Windowed = true;                    
  117.     pp.Flags = 0;                    
  118.  
  119.     HRESULT result{ };
  120.     result = _d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &pp, &_device);
  121.     if (FAILED(result))
  122.         return false;
  123.  
  124.     D3DVIEWPORT9 viewport{ };
  125.     viewport.X = 0;      
  126.     viewport.Y = 0;  
  127.     viewport.Width = clientRect.right;
  128.     viewport.Height = clientRect.bottom;
  129.     viewport.MinZ = 0.0f;
  130.     viewport.MaxZ = 100.0f;
  131.  
  132.     result = _device->SetViewport(&viewport);
  133.     if (FAILED(result))
  134.         return false;
  135.  
  136.     return true;
  137. }
  138.  
  139. LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  140.     switch (msg) {
  141.         case WM_DESTROY:
  142.             _running = false;
  143.             PostQuitMessage(0);
  144.             break;
  145.     }
  146.     return DefWindowProc(hWnd, msg, wParam, lParam);
  147. }
  148.  
  149. int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {
  150.     WNDCLASSEX wndEx{ };
  151.     wndEx.cbSize = sizeof(WNDCLASSEX);
  152.     wndEx.style = CS_VREDRAW | CS_HREDRAW;
  153.     wndEx.lpfnWndProc = WndProc;
  154.     wndEx.cbClsExtra = 0;
  155.     wndEx.cbWndExtra = 0;
  156.     wndEx.hInstance = hInstance;
  157.     wndEx.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
  158.     wndEx.hCursor = LoadCursor(nullptr, IDC_ARROW);
  159.     wndEx.hbrBackground = (HBRUSH)COLOR_WINDOW;
  160.     wndEx.lpszMenuName = nullptr;
  161.     wndEx.lpszClassName = "DirectXClass";
  162.     wndEx.hIconSm = nullptr;
  163.  
  164.     RegisterClassEx(&wndEx);
  165.  
  166.     HWND hWnd{ nullptr };
  167.     hWnd = CreateWindow("DirectXClass", "directx window", WS_OVERLAPPEDWINDOW, 50, 50, 1024, 768, nullptr, nullptr, hInstance, nullptr);
  168.  
  169.     if (!hWnd)
  170.         return -1;
  171.  
  172.     ShowWindow(hWnd, nCmdShow);
  173.     UpdateWindow(hWnd);
  174.  
  175.     _running = true;
  176.  
  177.     if (!InitD3D(hWnd))
  178.         return -1;
  179.  
  180.     std::vector<VStruct> vertices{
  181.         VStruct{ -1.0f, -1.0f, 1.0f, D3DXCOLOR{ 1.0f, 0.0f, 0.0f, 1.0f } },
  182.         VStruct{ -1.0f,  1.0f, 1.0f, D3DXCOLOR{ 0.0f, 1.0f, 0.0f, 1.0f } },
  183.         VStruct{  1.0f,  1.0f, 1.0f, D3DXCOLOR{ 0.0f, 0.0f, 1.0f, 1.0f } },
  184.         VStruct{  1.0f, -1.0f, 1.0f, D3DXCOLOR{ 1.0f, 1.0f, 0.0f, 1.0f } }
  185.     };
  186.  
  187.     std::vector<unsigned int> indices{
  188.         0, 1, 2,
  189.         0, 2, 3
  190.     };
  191.  
  192.     if (!(_vertexBuffer = CreateVertexBuffer(vertices)))
  193.         return -1;
  194.  
  195.     if (!(_indexBuffer = CreateIndexBuffer(indices)))
  196.         return -1;
  197.  
  198.     HRESULT result = _device->SetStreamSource(0, _vertexBuffer.Get(), 0, sizeof(VStruct));
  199.     if (FAILED(result))
  200.         return -1;
  201.  
  202.     result = _device->SetIndices(_indexBuffer.Get());
  203.     if (FAILED(result))
  204.         return -1;
  205.  
  206.     std::vector<D3DVERTEXELEMENT9> vertexDecl{
  207.         { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
  208.         { 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
  209.         D3DDECL_END()
  210.     };
  211.  
  212.     result = _device->CreateVertexDeclaration(vertexDecl.data(), &_vertexDecl);
  213.     if (FAILED(result))
  214.         return -1;
  215.  
  216.     _device->SetVertexDeclaration(_vertexDecl.Get());
  217.  
  218.     D3DCAPS9 deviceCaps{ };
  219.     _device->GetDeviceCaps(&deviceCaps);
  220.  
  221.     if (deviceCaps.VertexShaderVersion < D3DVS_VERSION(3, 0))
  222.         return -1;
  223.  
  224.     if (deviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
  225.         return -1;
  226.    
  227.     ID3DXBuffer* vertexShaderBuffer{ };
  228.     ID3DXBuffer* pixelShaderBuffer{ };
  229.  
  230.     result = D3DXCompileShaderFromFile("vertex.hlsl", nullptr, nullptr, "main", "vs_3_0", 0, &vertexShaderBuffer, nullptr, &_vertexTable);
  231.     if (FAILED(result))
  232.         return -1;
  233.  
  234.     result = D3DXCompileShaderFromFile("pixel.hlsl", nullptr, nullptr, "main", "ps_3_0", 0, &pixelShaderBuffer, nullptr, nullptr);
  235.     if (FAILED(result))
  236.         return -1;
  237.  
  238.     result = _device->CreateVertexShader((DWORD*)vertexShaderBuffer->GetBufferPointer(), &_vertexShader);
  239.     if (FAILED(result))
  240.         return -1;
  241.    
  242.     result = _device->CreatePixelShader((DWORD*)pixelShaderBuffer->GetBufferPointer(), &_pixelShader);
  243.     if (FAILED(result))
  244.         return -1;
  245.  
  246.     vertexShaderBuffer->Release();
  247.     pixelShaderBuffer->Release();
  248.  
  249.     _device->SetVertexShader(_vertexShader.Get());
  250.     _device->SetPixelShader(_pixelShader.Get());
  251.  
  252.     if (!SetupTransform())
  253.         return -1;
  254.  
  255.     _device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
  256.  
  257.     MSG msg{ };
  258.     while (_running) {
  259.         while (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)) {
  260.             TranslateMessage(&msg);
  261.             DispatchMessage(&msg);
  262.         }
  263.         _device->Clear(0, nullptr, D3DCLEAR_TARGET, D3DXCOLOR{ 0.0f, 0.0f, 0.0f, 1.0f }, 0.0f, 0);  
  264.  
  265.         _device->BeginScene();
  266.         _device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, indices.size(), 0, 2);
  267.         _device->EndScene();
  268.  
  269.         _device->Present(nullptr, nullptr, nullptr, nullptr);
  270.  
  271.         D3DXMatrixMultiply(&_worldMatrix, &_worldMatrix, &_rotationMatrix);
  272.         _vertexTable->SetMatrix(_device.Get(), "worldMatrix", &_worldMatrix);
  273.     }
  274.  
  275.     return 0;
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement