본문 바로가기

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()));
        
    /*
	  			   
					 5
	 
	
	  			      
9			 4  	 |	    3              6
    				 |	     
		-------------*------------		         *(좌표 x : 0, y :0)
					 |
	      1			 |		   2
	 				 0

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

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

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

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

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

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

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

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

		vertics[8].position = D3DXVECTOR3(-0.25f, 0.3f, 0.0f); // 4
		vertics[8].color = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);

		vertics[9].position = D3DXVECTOR3(-0.25f, 0.3f, 0.0f); // 4
		vertics[9].color = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);

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

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

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

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

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

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

		vertics[16].position = D3DXVECTOR3(0.4f, -0.6f, 0.0f); // 7
		vertics[16].color = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);

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

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

		vertics[19].position = D3DXVECTOR3(-0.4f, -0.6f, 0.0f); // 8
		vertics[19].color = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);

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

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

		vertics[22].position = D3DXVECTOR3(-0.55f, 0.3f, 0.0f); // 9
		vertics[22].color = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);

		vertics[23].position = D3DXVECTOR3(-0.25f, 0.3f, 0.0f); // 4
		vertics[23].color = D3DXCOLOR(0.0f, 1.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) * 24;

    
		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
		(
			24, 
			0
		);
	}
	graphics->End();
}

오각형(삼각형 3개로 이루어진) + 삼각형 5개를 덧 붙였습니다.

 

 

출력결과