directx 11 draw 3d line
[alphabetize]
Hi Triangle
Direct3D eleven Upwardly and Running
Anton Gerdelan. Concluding Updated 29 November 2019
This tutorial aims to become you set up up with a minimal Direct3D 11 program that displays 1 triangle. If you lot can describe 1 triangle then you tin can draw anything. For clarity no construction or framework is used or introduced.
Tools Set-Up
- Direct3D 11 comes in the Windows SDK. You no longer need to download any separate D3D libraries.
- Direct3D 11 has a C++ and C interface. For the beginning use C++ equally most documentation/examples support C++.
- Most guides wait you lot are using Visual Studio and the Microsoft C++ compiler. You can apply other IDEs, and you can besides compile on the command line with the Microsoft compiler, simply stick to Visual Studio at first and so that you are only solving one trouble at a fourth dimension.
- For your first Direct3D program, try to write all your C/C++ in i source code file. Don't write wrappers, classes, and structures.
Brand a Win32 Programme
We will create a window to draw to. First create a minimal Win32 program. Follow the guide from Microsoft. It is important to proceeds a basic understanding of Microsoft coding conventions and data types, the COM (Component Object Module) API framework, and the window message loop and WindowProc callback because Direct3D besides uses these. When your Win32 program is working we tin modify the basic Win32 program to add together Direct3D support.
Change Your Win32 Programme
Add some code to your WindowProc function capture when a mouse button or particular keyboard primal is clicked, so prints a bulletin or puts up a MessageBox(). This is how y'all will handle user input for moving things in your scene like a 3D photographic camera.
The while() message loop in your program will form the master loop of your Direct3D program. After checking for window letters similar key-presses and the window closing, we will describe a frame of rendering. The default GetMessage() function is blocking. This means that information technology halts your program until an event occurs like a key printing or a mouse click. For rendering nosotros don't desire to stop drawing until a key is pressed, and then we tin can change to the non-blocking variant of the bulletin function - PeekMessage(). We can change the bulletin loop to look something like:
MSG msg = {}; bool should_close = false; while ( !should_close ) { /**** handle user input and other window events ****/ if ( PeekMessage( &msg, Cypher, 0, 0, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } if ( msg.message == WM_QUIT ) { break; } /*** TODO: Render A FRAME HERE WITH DIRECT3D ***/ } We demand to include the Direct3D library. We can also make the Windows header smaller by defining WIN32_LEAN_AND_MEAN before we include it, to exclude some actress utilities. All of these headers should already be on your system path from the Windows SDK. We likewise need to link these libraries. You can add these in the library list under project settings, but it'due south easier to just add a pragma statement to the source code, under the headers.
#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <d3d11.h> // D3D interface #include <dxgi.h> // DirectX driver interface #include <d3dcompiler.h> // shader compiler #pragma annotate( lib, "user32" ) // link confronting the win32 library #pragma comment( lib, "d3d11.lib" ) // direct3D library #pragma comment( lib, "dxgi.lib" ) // directx graphics interface #pragma comment( lib, "d3dcompiler.lib" ) // shader compiler
Remove the WM_PAINT example from your window bulletin callback - we will exercise this manually instead.
Coding Tips for Direct3D
Direct3D functions typically require yous to create a struct with a "purse of parameters", as input to the function. Some of these structs besides comprise structs. It'south quite easy to make a error in the parameters here. Whenever you need to add a new Direct3D function call, HLSL shader function, or Win32 part phone call, await it up on the Microsoft website and carefully read the parameter options and the Remarks section. Y'all tin can as well cross-reference your lawmaking against a known working instance from someone else. But don't only copy-paste code, or you'll get mystery bugs that are impossible to empathize!
You can reduce the amount of time required to fill out struct parameters by setting their memory to 0, and then only setting the parameters which required values that are not nix. You can employ memset(), ZeroMemory(), or the design SOME_STRUCT_TYPE my_struct = {}; to achieve this most of the time.
Direct3D device functions typically return an HRESULT value. It is a proficient idea to bank check every one of these for errors whilst building your first plan. You lot can utilize the SUCCEEDED() office to cheque if in that location were no errors. You can use the assert() role to deliberately halt and crash the program if an expression is 0/fake/Naught. To use assert() you lot will need to #include<assert.h> This gives us a pattern as follows.
HRESULT hr = someDirect3DFunction(); affirm( SUCCEEDED( hr ) );
If you are using Visual Studio and an exclamation fails, and then you go a dialog box pop up. It reports the file and line of the error, and also gives you lot three buttons. If you hit retry then Visual Studio volition use its Just In Time (JIT) debugger to jump to the line that failed in a debug session. This is really handy. Check your Output window for debug reports. Direct3D is usually quite verbose at reporting problems with role parameters.
Offset Direct3D
After your win32 creation lawmaking, but earlier the message loop starts, nosotros can initialise some D3D11 systems.
- D3D11CreateDeviceAndSwapChain()
- CreateRenderTargetView()
These functions require creating a few structs of parameters. D3D11CreateDeviceAndSwapChain() gives the states several pointers that nosotros will utilise as interfaces to Direct3D functions; a ID3D11Device pointer, a ID3D11DeviceContext pointer, and a IDXGISwapChain pointer. CreateRenderTargetView() also gives us a ID3D11RenderTargetView pointer. Nosotros need to apply these a lot, so it makes sense to declare these globally or at the tiptop of your main function.
ID3D11Device* device_ptr = NULL; ID3D11DeviceContext* device_context_ptr = Zip; IDXGISwapChain* swap_chain_ptr = Nada; ID3D11RenderTargetView* render_target_view_ptr = Zip;
Adjacent, nosotros can fill up out a DXGI_SWAP_CHAIN_DESC struct. Nosotros tin can use some bones default values.
DXGI_SWAP_CHAIN_DESC swap_chain_descr = { 0 }; swap_chain_descr.BufferDesc.RefreshRate.Numerator = 0; swap_chain_descr.BufferDesc.RefreshRate.Denominator = 1; swap_chain_descr.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; swap_chain_descr.SampleDesc.Count = 1; swap_chain_descr.SampleDesc.Quality = 0; swap_chain_descr.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; swap_chain_descr.BufferCount = one; swap_chain_descr.OutputWindow = hwnd; swap_chain_descr.Windowed = true; We are going to brandish to a window, not in full screen, so we don't need to set a width or height value, and we gear up Windowed to true. As you can see in the documentation, the Numerator, and Denominator can be set to synchronise output frame rate to your monitor. Here we say "just draw as fast equally possible". DXGI_FORMAT_B8G8R8A8_UNORM is a reasonable default colour output, but does not have gamma correction. We could use DXGI_FORMAT_B8G8R8A8_UNORM_SRGB for that. We are not enabling multisampling anti-aliasing yet, and so SampleDesc parameters are set to defaults. BufferCount is the count of dorsum buffers to add to the bandy chain. So, in windowed way, for a typical double-buffering set-upwardly with i front buffer and 1 back buffer, nosotros can prepare this to i. We want to necktie our output buffers to the window, so use your Win32 handle, returned by CreateWindow(), for OutputWindow. Next we can give our struct to the function.
D3D_FEATURE_LEVEL feature_level; UINT flags = D3D11_CREATE_DEVICE_SINGLETHREADED; #if defined( DEBUG ) || divers( _DEBUG ) flags |= D3D11_CREATE_DEVICE_DEBUG;
0 Response to "directx 11 draw 3d line"
Post a Comment