공부중

[DirectX12]Hello Wolrd Sample - D3D12HelloTriangle - 1 본문

Programing/DirectX

[DirectX12]Hello Wolrd Sample - D3D12HelloTriangle - 1

곤란 2018. 5. 16. 19:26
반응형

 

이번 프로젝트는 실행하면 위와같이 삼각형이 하나 찍혀 나오는것이다.

이전 프로젝트와 비교를 통해서 이전 프로젝트의 코드와 겹치는 부분은 거의 빠르게 넘어갈 예정이다.

 

먼저 해당 프로그램의 시작점부터 순서대로 가보자....

 

#include "stdafx.h"
#include "D3D12HelloTriangle.h"

_Use_decl_annotations_
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
	D3D12HelloTriangle sample(1280, 720, L"D3D12 Hello Triangle");
	return Win32Application::Run(&sample, hInstance, nCmdShow);
}

이전 프로젝트의 WinMain문과 다른점이 없다.

굳이 다른점을 뽑으라면은 프로젝트 명을 따라가는 D3D12HelloWindow가 D3D12HelloTriangle로 바뀌었다는점?

 

D3D12HelloTriangle도 똑같이 DXSample을 상속 받고 있다.

 

D3D12HelloTriangle의 생성자를 살펴보자.

 

D3D12HelloTriangle::D3D12HelloTriangle(UINT width, UINT height, std::wstring name) :
	DXSample(width, height, name),
	m_frameIndex(0),
	m_viewport(0.0f, 0.0f, static_cast<float>(width), static_cast<float>(height)),
	m_scissorRect(0, 0, static_cast<LONG>(width), static_cast<LONG>(height)),
	m_rtvDescriptorSize(0)
{
}

 

생성자에서 변경된점은 멤버 변수의 초기화가 추가 되었다는점이다.

m_viewport와 m_scissorRect 이 두 변수의 초기화가 추가되었다.

이 두개의 변수에 대해서는 추후에 설명을 하기로 하고 넘어가자.

DXSample 생성자는 변경된점이 없다.

 

D3D12HelloTriangle 헤더를 살펴보자.

 

class D3D12HelloTriangle : public DXSample
{
public:
	D3D12HelloTriangle(UINT width, UINT height, std::wstring name);

	virtual void OnInit();
	virtual void OnUpdate();
	virtual void OnRender();
	virtual void OnDestroy();

private:
	static const UINT FrameCount = 2;

        // >>>>>>  Add
	struct Vertex
	{
		XMFLOAT3 position;
		XMFLOAT4 color;
	};
        // <<<<<<  Add

	// Pipeline objects.
        // >>>>>>  Add
	CD3DX12_VIEWPORT m_viewport;
	CD3DX12_RECT m_scissorRect;
        // <<<<<<  Add

	ComPtr<IDXGISwapChain3> m_swapChain;
	ComPtr<ID3D12Device> m_device;
	ComPtr<ID3D12Resource> m_renderTargets[FrameCount];
	ComPtr<ID3D12CommandAllocator> m_commandAllocator;
	ComPtr<ID3D12CommandQueue> m_commandQueue;

        // >>>>>>  Add
	ComPtr<ID3D12RootSignature> m_rootSignature;
        // <<<<<<  Add

	ComPtr<ID3D12DescriptorHeap> m_rtvHeap;
	ComPtr<ID3D12PipelineState> m_pipelineState;
	ComPtr<ID3D12GraphicsCommandList> m_commandList;
	UINT m_rtvDescriptorSize;

	// App resources.
        // >>>>>>  Add
	ComPtr<ID3D12Resource> m_vertexBuffer;
	D3D12_VERTEX_BUFFER_VIEW m_vertexBufferView;
        // <<<<<<  Add

	// Synchronization objects.
	UINT m_frameIndex;
	HANDLE m_fenceEvent;
	ComPtr<ID3D12Fence> m_fence;
	UINT64 m_fenceValue;

	void LoadPipeline();
	void LoadAssets();
	void PopulateCommandList();
	void WaitForPreviousFrame();
};

 

새로 추가된 부분은 주석으로 표시를 해두었다.

제일 눈에 띄는것은 Vertex라는 구조체가 선언되었고 뷰표트와 scissor라는 구역(RECT).

그리고 Signature라는것과 vertexBuffer와 vertexBufferView라는것.

 

이것들이 추가 되었는데 추후에 사용되는 부분에서 설명을 하도록 하자.

 

D3D12HelloTriangle의 생성자에서 특별히 큰 변화는 없었다.

몇개의 멤버변수 추가로 인한 초기화를 해준것 외에는 특별한것이 없다.

 

이제 Run함수 안의 내용은 윈도우를 만들어서 띄우고 그 외에 초기화 작업(pSample->OnInit( );)과

 

메시지 루프 등등

 

그리고 메시지 루프를 나오면 파괴해주는 작업까지 작성 되어있다.

 

int Win32Application::Run(DXSample* pSample, HINSTANCE hInstance, int nCmdShow)
{
	// Parse the command line parameters
	int argc;
	LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
	pSample->ParseCommandLineArgs(argv, argc);
	LocalFree(argv);

	// Initialize the window class.
	WNDCLASSEX windowClass = { 0 };
	windowClass.cbSize = sizeof(WNDCLASSEX);
	windowClass.style = CS_HREDRAW | CS_VREDRAW;
	windowClass.lpfnWndProc = WindowProc;
	windowClass.hInstance = hInstance;
	windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	windowClass.lpszClassName = L"DXSampleClass";
	RegisterClassEx(&windowClass);

	RECT windowRect = { 0, 0, static_cast<LONG>(pSample->GetWidth()), static_cast<LONG>(pSample->GetHeight()) };
	AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);

	// Create the window and store a handle to it.
	m_hwnd = CreateWindow(
		windowClass.lpszClassName,
		pSample->GetTitle(),
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		windowRect.right - windowRect.left,
		windowRect.bottom - windowRect.top,
		nullptr,		// We have no parent window.
		nullptr,		// We aren't using menus.
		hInstance,
		pSample);

	// Initialize the sample. OnInit is defined in each child-implementation of DXSample.
	pSample->OnInit();

	ShowWindow(m_hwnd, nCmdShow);

	// Main sample loop.
	MSG msg = {};
	while (msg.message != WM_QUIT)
	{
		// Process any messages in the queue.
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	pSample->OnDestroy();

	// Return this part of the WM_QUIT message to Windows.
	return static_cast<char>(msg.wParam);
}

 

 

이전 프로젝트와 달라진점은 없으며 세세한 부분으로 파고 들어가는것은 다음글에서 살펴보자.

 

다음글에서....

 

반응형