DirectX/응용

STRIP 이용한 원 그려보기

컴맹학자 2021. 6. 29. 22:25
728x90

중요 핵심

1. Cos, Sin 이용해서 그려줘야함

2. 현재 화면비율은 World 셋팅이 아니므로 창크기 값 가져와서 값을 넣어줘야함

3. 정점이 많으면 많을 수록 원에 가까워짐


cpp

더보기
#include "stdafx.h"
#include "VertexLine2Demo.h"

void VertexLine2Demo::Initialize()
{
	shader = new Shader(L"02_Pass.fx");

	//화면 비율에 맞는 원그리기
	float RadioX = 300.0f / D3D::GetDesc().Width;
	float RadioY = 300.0f / D3D::GetDesc().Height;

	//0 ~ 19 까지 돌림
	for (int i = 0; i < 20; i++)
	{
		//cos, sin은 라디안값으로 받음 
		float CosX = cos(Math::ToRadian(i * 18.0f)) * RadioX;
		float SinY = sin(Math::ToRadian(i * 18.0f)) * RadioY;
		vertices[i].Position = Vector3(CosX, SinY, 0);
	}
	//마지막에 19 - 20 연결하기위한 선
	vertices[20].Position = vertices[0].Position;


	D3D11_BUFFER_DESC desc;
	ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
	desc.ByteWidth = sizeof(Vertex) * 21;
	desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;

	D3D11_SUBRESOURCE_DATA data = { 0 };
	data.pSysMem = vertices;

	Check(D3D::GetDevice()->CreateBuffer(&desc, &data, &vertexBuffer));
}

void VertexLine2Demo::Destroy()
{
	SafeDelete(shader);
	SafeRelease(vertexBuffer);
}

void VertexLine2Demo::Update()
{
}

void VertexLine2Demo::Render()
{
	UINT stride = sizeof(Vertex);
	UINT offset = 0;
	
	D3D::GetDC()->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);

	static bool bStrip = false;
	{
		ImGui::Checkbox("Strip", &bStrip);
		D3D11_PRIMITIVE_TOPOLOGY topology[2] =
		{
			D3D11_PRIMITIVE_TOPOLOGY_LINELIST,
			D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP
		};
		D3D::GetDC()->IASetPrimitiveTopology(bStrip ? topology[1] : topology[0]);
	}

	static UINT startLocation = 0;
	{
		ImGui::SliderInt("Start", (int*)&startLocation, 0 , 6);
	}

	static UINT pass = 0;
	{
		ImGui::InputInt("Pass", (int*)&pass);
		pass = (UINT)Math::Clamp((int)pass, 0, 3);

		static Color color = Color(1, 1, 1, 1);
		ImGui::ColorEdit3("Color", color);
		shader->AsVector("Color")->SetFloatVector(color);
	}

	shader->Draw(0, pass, 21, startLocation);
}

실행


 


90도 기준으로 0~ 1로 나와야 하지만 화면 비율을 곱해야 하기 떄문에 저렇게 숫자로 나옴