본문 바로가기

DirectX11 2D

<DirectX11 2D 과제> 원 출력

IndexBuffer를 활용해서 원 출력을 했습니다. TOPOLOGY는 TRIANGLESTRIP을 사용했습니다.

 

 

 

 

Vertex 초기화 

		vertics = new vertexColor[180];

		for (int i = 0; i < 180; ++i)
		{
			int angleDouble = i * 2;
			double angle = D3DXToRadian(angleDouble);
			vertics[i].position = D3DXVECTOR3(static_cast<FLOAT>(cos(angle)), static_cast<FLOAT>(sin(angle)), 0.0f);
			vertics[i].color = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f);
		}

 

 

 

IndexBuffer 초기화

	{
		indices = new uint[360];


		indices[0] = { static_cast<uint>(0) };

		int j = 1;
		for (int i = 1; i < 360; i++)
		{
			if (i % 2 == 0)
			{
				indices[i] = { static_cast<uint>(j) };
				++j;
			}

			else
			{
				indices[i] = { static_cast<uint>(0) };
			}
		}
	}

 

 

/*

     개선 부분이 있다.

https://cpplab.tistory.com/20

*/

 

 

 

코드 전체

#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()));


	{	
		vertics = new vertexColor[180];

		for (int i = 0; i < 180; ++i)
		{
			int angleDouble = i * 2;
			double angle = D3DXToRadian(angleDouble);
			vertics[i].position = D3DXVECTOR3(static_cast<FLOAT>(cos(angle)), static_cast<FLOAT>(sin(angle)), 0.0f);
			vertics[i].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) * 180;

		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));
	}

	{
		indices = new uint[360];


		indices[0] = { static_cast<uint>(0) };

		int j = 1;
		for (int i = 1; i < 360; i++)
		{
			if (i % 2 == 0)
			{
				indices[i] = { static_cast<uint>(j) };
				++j;
			}

			else
			{
				indices[i] = { static_cast<uint>(0) };
			}
		}
	}

	{
		D3D11_BUFFER_DESC desc;
		ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
		desc.Usage = D3D11_USAGE_IMMUTABLE;
		desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
		desc.ByteWidth = sizeof(uint) * 360;

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

		auto hr = graphics->GetDevice()->CreateBuffer(&desc, &sub_data, &index_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(index_buffer);
	SAFE_DELETE_ARRAY(indices);

	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()->IASetIndexBuffer(index_buffer, DXGI_FORMAT_R32_UINT, 0);

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

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

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

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


		graphics->GetDeviceContext()->DrawIndexed(360, 0, 0);

	}
	graphics->End();
	// Present

}

 

 

 

출력결과

'DirectX11 2D' 카테고리의 다른 글

<DirectX11 2D> 기초 정리  (0) 2021.09.16