본문 바로가기

DirectX11 2D/과제

<DirectX11 2D 과제> 육각형 출력

#include "stdafx.h"
#include "Execute.h"
#include "graphics.h"

Execute::Execute()
{
	graphics = new Graphics;
	graphics->Initialize();
	graphics->CreateBackBuffer(static_cast<uint>(Settings::Get().GetWidth()),
		static_cast<uint>(Settings::Get().GetHeight()));
        
    /*
	  			   
	    6  	 |	  5    
    		 |	     
	 0-------1-------4         *(좌표 x : 0, y :0)
			 |
	    2	 |    3
			

    시계방향으로!!!
	시계방향으로 회전하는 직각삼각형을 만들어 본다. (0, 1, 2 는 정점)
	0번 부터 1번 
	1번 부터 2번 

	한변의 길이가 1이다.
	0 의 좌표는 (-0.5, -0.5)가 된다.
	
	*/
	// Vertex 데이터
	{	
		vertics = new vertexColor[18];
		vertics[0].position = D3DXVECTOR3(-0.5f, 0.0f, 0.0f); // 0
		vertics[0].color = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);

		vertics[1].position = D3DXVECTOR3(0.0f, 0.0f, 0.0f); // 1
		vertics[1].color = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);

		vertics[2].position = D3DXVECTOR3(-0.3f, -0.5f, 0.0f); // 2
		vertics[2].color = D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f);

		vertics[3].position = D3DXVECTOR3(-0.3f, -0.5f, 0.0f); // 2
		vertics[3].color = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);

		vertics[4].position = D3DXVECTOR3(0.0f, 0.0f, 0.0f); // 1
		vertics[4].color = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);

		vertics[5].position = D3DXVECTOR3(0.3f, -0.5f, 0.0f); // 3
		vertics[5].color = D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f);

		vertics[6].position = D3DXVECTOR3(0.3f, -0.5f, 0.0f); // 3
		vertics[6].color = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);

		vertics[7].position = D3DXVECTOR3(0.0f, 0.0f, 0.0f); // 1
		vertics[7].color = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);

		vertics[8].position = D3DXVECTOR3(+0.5f, +0.0f, 0.0f); // 4
		vertics[8].color = D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f);

		vertics[9].position = D3DXVECTOR3(+0.5f, +0.0f, 0.0f); // 4
		vertics[9].color = D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f);

		vertics[10].position = D3DXVECTOR3(0.0f, 0.0f, 0.0f); // 1
		vertics[10].color = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);

		vertics[11].position = D3DXVECTOR3(0.3f, 0.5f, 0.0f); // 5
		vertics[11].color = D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f);

		vertics[12].position = D3DXVECTOR3(0.3f, 0.5f, 0.0f); // 5
		vertics[12].color = D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f);

		vertics[13].position = D3DXVECTOR3(0.0f, 0.0f, 0.0f); // 1
		vertics[13].color = D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f);

		vertics[14].position = D3DXVECTOR3(-0.3f, 0.5f, 0.0f); // 6
		vertics[14].color = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);

		vertics[15].position = D3DXVECTOR3(-0.3f, 0.5f, 0.0f); // 6
		vertics[15].color = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);

		vertics[16].position = D3DXVECTOR3(0.0f, 0.0f, 0.0f); // 1
		vertics[16].color = D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f);

		vertics[17].position = D3DXVECTOR3(-0.5f, 0.0f, 0.0f); // 0
		vertics[17].color = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);
        
    }

	
	
	
	
	{
		D3D11_BUFFER_DESC desc;
		ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
		desc.Usage = D3D11_USAGE_IMMUTABLE;
		desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
		desc.ByteWidth = sizeof(vertexColor) * 18;

    
		D3D11_SUBRESOURCE_DATA sub_data;
		ZeroMemory(&sub_data, sizeof(D3D11_SUBRESOURCE_DATA));
		sub_data.pSysMem = vertics;

		auto hr = graphics->GetDevice()->CreateBuffer
		(
			&desc,
			&sub_data,
			&vertex_buffer
		);

		assert(SUCCEEDED(hr));
	}



	{
		auto hr = D3DX11CompileFromFileA
		(
			// 원본 파일
			"Color.hlsl",
			nullptr,
			nullptr,
			"VS",
			"vs_5_0",
			0,
			0,
			nullptr,
			&vs_blob,
			nullptr,
			nullptr
		);

		assert(SUCCEEDED(hr));

		hr = graphics->GetDevice()->CreateVertexShader
		(
			vs_blob->GetBufferPointer(),
			vs_blob->GetBufferSize(),
			nullptr,
			&vertex_shader
		);

		assert(SUCCEEDED(hr));
	}

	{
		D3D11_INPUT_ELEMENT_DESC layout_desc[]
		{
			{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
			{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}
		};

		auto hr = graphics->GetDevice()->CreateInputLayout
		(
			layout_desc,
			2,
			vs_blob->GetBufferPointer(),
			vs_blob->GetBufferSize(),
			&input_layout
		);

		assert(SUCCEEDED(hr));
	}


	{
		auto hr = D3DX11CompileFromFileA
		(	
			"Color.hlsl",
			nullptr,
			nullptr,
			"PS",
			"ps_5_0",
			0,
			0,
			nullptr,
			&ps_blob,
			nullptr,
			nullptr
		);

		assert(SUCCEEDED(hr));

		hr = graphics->GetDevice()->CreatePixelShader
		(
			ps_blob->GetBufferPointer(),
			ps_blob->GetBufferSize(),
			nullptr,
			&pixel_shader
		);

		assert(SUCCEEDED(hr));
	}
}

Execute::~Execute()
{
	SAFE_RELEASE(pixel_shader);
	SAFE_RELEASE(ps_blob);
	SAFE_RELEASE(input_layout);
	SAFE_RELEASE(vertex_shader);
	SAFE_RELEASE(vs_blob);
	SAFE_RELEASE(vertex_buffer);
	SAFE_DELETE_ARRAY(vertics);
	SAFE_DELETE(graphics);
}

void Execute::Update()
{
}

void Execute::Render()
{
	uint stride = sizeof(vertexColor);
	uint offset = 0;
	graphics->Begin();
	{
		graphics->GetDeviceContext()->IASetVertexBuffers
		(
			0,
			1,
			&vertex_buffer,
			&stride,
			&offset
		);

		graphics->GetDeviceContext()->IASetInputLayout(input_layout);


		graphics->GetDeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);


		graphics->GetDeviceContext()->VSSetShader(vertex_shader, nullptr, 0);


		graphics->GetDeviceContext()->PSSetShader(pixel_shader, nullptr, 0);

		graphics->GetDeviceContext()->Draw
		(
			18, 
			0
		);
	}
	graphics->End();
}

 

삼각형 6개로 출력했습니다.

 

출력결과