DirectX - Drawing

This chapter, in turn, focuses on the Direct3D API interfaces and methods which are needed to configure the rendering pipeline, define vertex and pixel shaders, and submit geometry to the rendering pipeline for drawing. After understanding the chapter, the user should be able to draw various geometric shapes with coloring or in wireframe mode.
Here, we will focus on drawing a triangle with Direct3D API.
In the first step, we include the basic header files, especially Direct3D header files which are required to draw a triangle with the code given below −
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
In the second step, we define the necessary parameters such as screen resolution, function parameters, and Direct3D library file. Include the global declarations and function prototypes as given in the below code −
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#pragma comment (lib, "d3d9.lib")
LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL; // the pointer to the vertex buffer
void initD3D(HWND hWnd); // sets up and initializes Direct3D
void render_frame(void); // renders a single frame
void cleanD3D(void); // closes Direct3D and releases memory
void init_graphics(void); // 3D declarations
struct CUSTOMVERTEX {FLOAT X, Y, Z, RHW; DWORD COLOR;};
In the next step, we declare the WindowProc function prototype with the required parameters as shown in the below code −
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
   LPSTR lpCmdLine, int nCmdShow){
   
   HWND hWnd;
   WNDCLASSEX wc;
   ZeroMemory(&wc, sizeof(WNDCLASSEX));
   wc.cbSize = sizeof(WNDCLASSEX);
   wc.style = CS_HREDRAW | CS_VREDRAW;
   wc.lpfnWndProc = WindowProc;
   wc.hInstance = hInstance;
   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
   wc.lpszClassName = L"WindowClass";
   RegisterClassEx(&wc);
   hWnd = CreateWindowEx(
      NULL, 
      L"WindowClass",
      L"Our Direct3D Program",
      WS_OVERLAPPEDWINDOW,
      0, 0,
      SCREEN_WIDTH, SCREEN_HEIGHT,
      NULL,
      NULL,
      hInstance,
      NULL
   );
   ShowWindow(hWnd, nCmdShow);
}
In step 4, we call for the function which initializes the graphics and the necessary parameters as given in the below code −
void init_graphics(void){
   // create the vertices using the CUSTOMVERTEX struct
   CUSTOMVERTEX vertices[] = {
      { 400.0f, 62.5f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
      { 650.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
      { 150.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
   };
   // create a vertex buffer interface called v_buffer
   d3ddev->CreateVertexBuffer(
      3*sizeof(CUSTOMVERTEX),
      0,
      CUSTOMFVF,
      D3DPOOL_MANAGED,
      &v_buffer,
      NULL
   );
   VOID* pVoid; // a void pointer
   // lock v_buffer and load the vertices into it
   v_buffer->Lock(0, 0, (void**)&pVoid, 0);
   memcpy(pVoid, vertices, sizeof(vertices));
   v_buffer->Unlock();
}
The output generated is mentioned below −
 Print
 Add Notes
 Bookmark this page