2018년 1월 13일 토요일

Hello World - Direct3D 기초 만들기

Hello World - Direct3D 기초 만들기


이번에는 앞서 설명한 Hello World - OpenGL 기초 만들기의 Direct3D 버전을 살펴보자.
결과는 OpenGL로 만든 창고 같은 모습이 될 것이다.

만들어 지는 함수와 구조는 OpenGL 에서 한 내용과 똑같다.

우리는 일차적으로 다음과 같은 내용에 대하여 프로그램 코딩을 할 것이다.

bool Init3D(HWND hwnd);
void SetBkColor(float r,float g,float b,float a);
void ClearBk();
void SwapBuffer();
void RenderScene();














include
#define D3D11_VIDEO_NO_HELPERS
#include <d3d11.h>
#include <d3dx10.h>
#include <d3dcompiler.h>


Variables
HWND m_hwnd;
bool m_initialized;

c_color4f m_bkcolor;

IDXGISwapChain *m_swapchain;             // the pointer to the swap chain interface
ID3D11Device *m_dev;                     // the pointer to our Direct3D device interface
ID3D11DeviceContext *m_devcon;           // the pointer to our Direct3D device context
ID3D11RenderTargetView *m_backbuffer;    // the pointer to our back buffer

Init3D Function
bool Init3D(HWND hwnd)
{
if(m_initialized) return false;
m_hwnd = hwnd;

  // create a struct to hold information about the swap chain
   DXGI_SWAP_CHAIN_DESC scd;

   // clear out the struct for use
   ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));

   // fill the swap chain description struct
   scd.BufferCount = 1;                                   // one back buffer
   scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;    // use 32-bit color
scd.BufferDesc.Width = 800;                   // set the back buffer width
scd.BufferDesc.Height = 600;                 // set the back buffer height
   scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;     // how swap chain is to be used
   scd.OutputWindow = m_hwnd;                               // the window to be used
   scd.SampleDesc.Count = 4;                              // how many multisamples
   scd.Windowed = TRUE;                                   // windowed/full-screen mode
   scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;    // allow full-screen switching

   // create a device, device context and swap chain using the information in the scd struct
   D3D11CreateDeviceAndSwapChain(NULL,
                                 D3D_DRIVER_TYPE_HARDWARE,
                                 NULL,
                                 NULL,
                                 NULL,
                                 NULL,
                                 D3D11_SDK_VERSION,
                                 &scd,
                                 &m_swapchain,
 &m_dev,
                                 NULL,
                                 &m_devcon);

   // get the address of the back buffer
ID3D11Texture2D *pBackBuffer;
m_swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);

   // use the back buffer address to create the render target
   m_dev->CreateRenderTargetView(pBackBuffer, NULL, &m_backbuffer);
   pBackBuffer->Release();

   // set the render target as the back buffer
   m_devcon->OMSetRenderTargets(1, &m_backbuffer, NULL);


/*
// Set the viewport
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = 800;
viewport.Height = 600;

m_devcon->RSSetViewports(1, &viewport);
   */

m_initialized = true;

return true;
}



SetBkColor Function
void SetBkColor(float r,float g,float b,float a)
{
   m_bkcolor = c_color4f(r,g,b,a);
}


RenderScene Function
void RenderScene()
{
if(m_initialized==false) return;

ClearBk();

SwapBuffer();
}

ClearBk Function
void ClearBk()
{
// clear the back buffer to a deep blue
m_devcon->ClearRenderTargetView(m_backbuffer, m_bkcolor.f);
}


SwapBuffer Function
void SwapBuffer()
{
   // switch the back buffer and the front buffer
   m_swapchain->Present(0, 0);
}


위의 코드로 프로그램을 구성하여 실행하게 되면 다음과 같은 창이 된다.


OpenGL 부분과 다른점은 OpenGL 은 HWND 에서 HDC 를 취득한후 OpenGL Context를 생셩하였는데 Direct3D는 HWND에서 device 를 만들거 낸다는 것이다.



앞으로 진행할 부분은 이러한 기본 코드에 창의 크기에 따른 Viewport의 설정 및 삼각형을 그리기 위한 버퍼의 구성 및 Shader Programming 에 관련한 글을 준비하도록 할 것이다.

댓글 없음:

댓글 쓰기

Vertex , 정점 정보의 확장

Vertex , 정점 정보의 확장 그동안의 설명에 있어서 우리는 VERTEX 정보에 대하여 3 차원의 위치 정보만 사용하였다. struct VERTEX {float x,y,z;}; 이제 이를 확장하여 색상을...