• Home
  • About
    • 게임 개발자 유정룡 photo

      게임 개발자 유정룡

      포트폴리오

    • Learn More
    • Email
    • Github
    • Bitbucket
  • Projects
    • All Projects
    • All Tags

DirectX11 공부 7일차(Rect)

07 Jun 2021

Reading time ~2 minutes

Rect

Wrapping을 끝낸 뒤 사각형 오브젝트를 만들기 위한 클래스인 Rect를 생성했다.

Rect.h

#pragma once

struct TRANSFORM_DATA
{
	D3DXMATRIX world;	// 4x4
};

class Rect final : public Object
{
public:
	Rect(class Graphics* graphics);
	~Rect();

	void Update();
	void Render(D3D11_Pipeline* pipeline);

	void SetPosition(const D3DXVECTOR3 position) { this->position = position; };
	void SetScale(const D3DXVECTOR3 scale) { this->scale = scale; };

private:
	ID3D11Device* device = nullptr;

	D3D11_VertexBuffer* vertex_buffer = nullptr;
	D3D11_IndexBuffer* index_buffer = nullptr;
	D3D11_InputLayout* input_layout = nullptr;
	D3D11_Shader* vertex_shader = nullptr;
	D3D11_Shader* pixel_shader = nullptr;
	D3D11_ConstantBuffer* gpu_buffer = nullptr;		// Constant Buffer
	D3D11_Texture* texture = nullptr;
	D3D11_SamplerState* sampler_state = nullptr;
	D3D11_RasterizerState* rasterizer_state = nullptr;
	D3D11_BlendState* blend_state = nullptr;

	D3DXVECTOR3 position = D3DXVECTOR3(0, 0, 0);
	D3DXVECTOR3 scale = D3DXVECTOR3(100, 100, 1);
	D3DXMATRIX world;
};

Object를 상속받은건 그냥 내가 하고싶어서 한거다 신경 안써도 괜찮다.

어디서 많이 본 변수 이름이다.

Execute 에 있던 변수와 함수를 거의 다 옮겨와서 작성했다.

왜??

각 객체마다 렌더링해야 하기 때문이다.

GUID_Generator.cpp

#include "stdafx.h"
#include "Rect.h"

Rect::Rect(Graphics* graphics)
{
	// Vertex Data
	D3D11_Geometry<D3D11_VertexTexture> geometry;
	Geometry_Generator::CreateQuad(geometry);

	// Vertex Buffer
	vertex_buffer = new D3D11_VertexBuffer(graphics);
	vertex_buffer->Create(geometry.GetVertices());

	// IndexBuffer
	index_buffer = new D3D11_IndexBuffer(graphics);
	index_buffer->Create(geometry.GetIndices());

	// VertexShader
	vertex_shader = new D3D11_Shader(graphics);
	vertex_shader->Create(ShaderScope_VS, "_Assets/Shader/Texture.hlsl");

	// Input Layout
	input_layout = new D3D11_InputLayout(graphics);
	input_layout->Create(D3D11_VertexTexture::descs, D3D11_VertexTexture::count, vertex_shader->GetShaderBlob());

	// Pixel Shader
	pixel_shader = new D3D11_Shader(graphics);
	pixel_shader->Create(ShaderScope_PS, "_Assets/Shader/Texture.hlsl");

	// Create Constant Buffer
	gpu_buffer = new D3D11_ConstantBuffer(graphics);
	gpu_buffer->Create<TRANSFORM_DATA>();

	// Create Rasterizer State
	rasterizer_state = new D3D11_RasterizerState(graphics);
	rasterizer_state->Create(D3D11_CULL_BACK, D3D11_FILL_SOLID);

	// Create Shader Resource View
	texture = new D3D11_Texture(graphics);
	texture->Create("_Assets/Texture/texture_img.png");

	// Create Sampler State
	sampler_state = new D3D11_SamplerState(graphics);
	sampler_state->Create(D3D11_FILTER_MIN_MAG_MIP_LINEAR, D3D11_TEXTURE_ADDRESS_CLAMP);

	// Create Blend State
	blend_state = new D3D11_BlendState(graphics);
	blend_state->Create(true);

	D3DXMatrixIdentity(&world);

	// World
	D3DXMATRIX S;
	D3DXMatrixScaling(&S, scale.x, scale.y, scale.z);

	D3DXMATRIX T;
	D3DXMatrixTranslation(&T, position.x, position.y, position.z);
	world = S * T;
}

Rect::~Rect()
{
	SAFE_DELETE(blend_state);
	SAFE_DELETE(sampler_state);
	SAFE_DELETE(texture);
	SAFE_DELETE(rasterizer_state);
	SAFE_DELETE(gpu_buffer);
	SAFE_DELETE(pixel_shader);
	SAFE_DELETE(input_layout);
	SAFE_DELETE(vertex_shader);
	SAFE_DELETE(index_buffer);
	SAFE_DELETE(vertex_buffer);
}

void Rect::Update()
{
	D3DXMATRIX S;
	D3DXMatrixScaling(&S, scale.x, scale.y, scale.z);

	D3DXMATRIX T;
	D3DXMatrixTranslation(&T, position.x, position.y, position.z);
	world = S * T;

	TRANSFORM_DATA* buffer = gpu_buffer->Map<TRANSFORM_DATA>();
	{
		D3DXMatrixTranspose(&buffer->world, &world);
	}
	gpu_buffer->UnMap();
}

void Rect::Render(D3D11_Pipeline* pipeline)
{
	D3D11_PipelineState pipeline_state;
	pipeline_state.input_layout = input_layout;
	pipeline_state.primitive_topology = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
	pipeline_state.vertex_shader = vertex_shader;
	pipeline_state.pixel_shader = pixel_shader;
	pipeline_state.rasterizer_state = rasterizer_state;
	pipeline_state.blend_state = blend_state;

	if (pipeline->Begin(pipeline_state))
	{
		pipeline->SetVertexBuffer(vertex_buffer);
		pipeline->SetIndexBuffer(index_buffer);
		pipeline->SetConstantBuffer(1, ShaderScope_VS, gpu_buffer);
		pipeline->SetShaderResource(0, ShaderScope_PS, texture);
		pipeline->SetSamplerState(0, ShaderScope_PS, sampler_state);
		pipeline->DrawIndexed(index_buffer->GetCount(), index_buffer->GetOffset(), vertex_buffer->GetOffset());

		pipeline->End();
	}
}


보면 좌표가 살짝 달라졌다.

World좌표만 신경써야 하기 때문에 구조체를 새로 생성해서 월드 좌표만 담았다.

그리고 월드 좌표를 받아와야 하기 때문에

cbuffer TransformBuffer : register(b1) // b : 버퍼형 자원, 숫자 -> 0 ~ 13 할당 가능 , 16byte 배수로 맞춰줘야 한다
{
    matrix world;
};

셰이더 부분에서 TransformBuffer를 새로 만들었다.

그리고 버퍼를 할당받는 곳은 1로 새로 받았다.

이제 Execute에서 rect를 렌더링해주고 업데이트까지 해주자

	rect = new Rect(graphics);
	rect->SetPosition(D3DXVECTOR3(100,0,0));
	rect2 = new Rect(graphics);
	rect2->SetPosition(D3DXVECTOR3(-100, 0, 0));
	rect2->SetScale(D3DXVECTOR3(100, 300, 0));

일부러 크기와 위치를 변경시켰다.

이렇게 생성하면

짜안! 2개가 생성됐다.



DirectX Share Tweet +1