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

      게임 개발자 유정룡

      포트폴리오

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

Direct3D11 공부 19일차(Mesh)

22 Jun 2021

Reading time ~2 minutes

Mesh

그물망이라는 뜻이 있는데 수많은 삼각형으로 이루어진 도형이라 그렇게 불린다.

지금까지는 면, 점, 선만 만들었지만 이젠 물체를 만들어보자

Mesh.h

#pragma once

class Mesh
{
public:
	typedef VertexTextureNormal MeshVertex;

public:
	Mesh(Shader* shader);
	virtual ~Mesh();

	void Update();
	void Render();

public:
	void Pass(const UINT val) { pass = val; }

	void Position(float x, float y, float z);
	void Position(const Vector3& vec);
	void Position(Vector3* vec);

	void Rotation(float x, float y, float z);
	void Rotation(const Vector3& vec);
	void Rotation(Vector3* vec);

	void RotationDegree(float x, float y, float z);
	void RotationDegree(const Vector3& vec);
	void RotationDegree(Vector3* vec);

	void Scale(float x, float y, float z);
	void Scale(const Vector3& vec);
	void Scale(Vector3* vec);

	Matrix World() { return world; }

	Vector3 Forward();
	Vector3 Up();
	Vector3 Right();

	void DiffuseMap(wstring file);

protected:
	virtual void Create() = 0;
	void CreateBuffer();

private:
	void UpdateWorld();

protected:
	MeshVertex* vertices = nullptr;
	UINT* indices = nullptr;

	UINT vertexCount;
	UINT indexCount;

private:
	Shader* shader;

	UINT pass = 0;

	Vector3 position = Vector3(0, 0, 0);
	Vector3 scale = Vector3(1, 1, 1);
	Vector3 rotation = Vector3(0, 0, 0);

	Matrix world;

	ID3D11Buffer* vertexBuffer = nullptr;
	ID3D11Buffer* indexBuffer = nullptr;

	ID3DX11EffectMatrixVariable *sWorld, *sView, *sProjection;

	Texture* diffuseMap = nullptr;
	ID3DX11EffectShaderResourceVariable* sDiffuseMap;
};

Mesh에서 담당할것 들은 Render Update 그리고 크기 회전 이동을 맡고있다.

ID3DX11EffectMatrixVariable 이건 매번 AsMatrix나 AsSRV로 갖고오는데 시간이 걸리기 때문에 미리 할당받고 사용하기 위해 선언했다.

Mesh.cpp

생성자

Mesh::Mesh(Shader * shader)
	: shader(shader)
{
	D3DXMatrixIdentity(&world);

	sWorld = shader->AsMatrix("World");
	sView = shader->AsMatrix("View");
	sProjection = shader->AsMatrix("Projection");

	sDiffuseMap = shader->AsSRV("DiffuseMap");
}

각각 쉐이더에 설정할 값들을 받아주고 world를 단위행렬로 초기화해준다.

Update는 각 메쉬마다 설정해줘야 한다.

Render

void Mesh::Render()
{
	if (vertexBuffer == nullptr && indexBuffer == nullptr)
	{
		Create();
		CreateBuffer();
	}

	UINT stride = sizeof(MeshVertex);
	UINT offset = 0;

	D3D::GetDC()->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
	D3D::GetDC()->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R32_UINT, 0);
	D3D::GetDC()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	sWorld->SetMatrix(world);
	sView->SetMatrix(Context::Get()->View());
	sProjection->SetMatrix(Context::Get()->Projection());

	sDiffuseMap->SetResource(diffuseMap->SRV());

	shader->DrawIndexed(0, pass, indexCount);
}

늘 하던것 처럼 IA부분에 버퍼를 설정해준다.

UpdateWorld

void Mesh::UpdateWorld()
{
	Matrix S, R, T;
	D3DXMatrixScaling(&S, scale.x, scale.y, scale.z);
	D3DXMatrixRotationYawPitchRoll(&R, rotation.y, rotation.x, rotation.z);
	D3DXMatrixTranslation(&T, position.x, position.y, position.z);

	world = S * R * T;
}

스자이공부 이 순서로 곱해줘야 하며, 회전일 경우에는 yaw pich roll 이 함수를 써서 한번에 초기화를 해줘야 한다.

그리고 나머지 함수 경우에는 늘 하던 초기화 및 크기, 회전, 이동이다.



DirectX Share Tweet +1