본문 바로가기

DirectX11 3D/기본 문법

<DirectX11 3D> 32. Renderer - Transform

 

필요한 개념


- Transform

World를 편하게 다루기 위한 class
Camera 클래스와 유사

 

- LU Decompose 

https://cpplab.tistory.com/25

 

 

 

 

 

.h


더보기
#pragma once

class Transform
{
public:
	// SRT를 사용하기 위해
	Transform();
	// World를 넘겨주기위해
	Transform(Shader* shader);
	~Transform();

	void Set(Transform* transform);

	void SetShader(Shader* shader);

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

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

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

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

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

	void World(Matrix& matrix);
	Matrix& World() { return bufferDesc.World; }

private:
	void UpdateWorld();

public:
	void Update();
	void Render();

private:
	struct BufferDesc
	{
		Matrix World;
	} bufferDesc;

private:
	Shader* shader;

	ConstantBuffer* buffer;
	ID3DX11EffectConstantBuffer* sBuffer;


	Vector3 position;
	Vector3 scale;
	Vector3 rotation;
};

 

 

 

 

 

 

 

.Cpp


더보기
#include "Framework.h"
#include "Transform.h"

Transform::Transform()
	: shader(NULL), buffer(NULL)
	, position(0, 0, 0), scale(1, 1, 1), rotation(0, 0, 0)
{
	D3DXMatrixIdentity(&bufferDesc.World);
}

Transform::Transform(Shader* shader)
	: position(0, 0, 0), scale(1, 1, 1), rotation(0, 0, 0)
{
	SetShader(shader);

	D3DXMatrixIdentity(&bufferDesc.World);
}

Transform::~Transform()
{
	SafeDelete(buffer);
}

void Transform::Set(Transform* transform)
{
	position = transform->position;
	scale = transform->scale;
	rotation = transform->rotation;

	UpdateWorld();
}

void Transform::SetShader(Shader* shader)
{
	this->shader = shader;

	buffer = new ConstantBuffer(&bufferDesc, sizeof(BufferDesc));
	sBuffer = shader->AsConstantBuffer("CB_World");
}

void Transform::Position(float x, float y, float z)
{
	Position(Vector3(x, y, z));
}

void Transform::Position(Vector3& vec)
{
	position = vec;

	UpdateWorld();
}

void Transform::Position(Vector3* vec)
{
	*vec = position;
}

void Transform::Scale(float x, float y, float z)
{
	Scale(Vector3(x, y, z));
}

void Transform::Scale(Vector3& vec)
{
	scale = vec;

	UpdateWorld();
}

void Transform::Scale(Vector3* vec)
{
	*vec = scale;
}

void Transform::Rotation(float x, float y, float z)
{
	Rotation(Vector3(x, y, z));
}

void Transform::Rotation(Vector3& vec)
{
	rotation = vec;

	UpdateWorld();
}

void Transform::Rotation(Vector3* vec)
{
	*vec = rotation;
}

void Transform::RotationDegree(float x, float y, float z)
{
	RotationDegree(Vector3(x, y, z));
}

void Transform::RotationDegree(Vector3& vec)
{
	Vector3 temp;

	temp.x = Math::ToRadian(vec.x);
	temp.y = Math::ToRadian(vec.y);
	temp.z = Math::ToRadian(vec.z);

	Rotation(temp);
}

void Transform::RotationDegree(Vector3* vec)
{
	Vector3 temp;

	temp.x = Math::ToDegree(rotation.x);
	temp.y = Math::ToDegree(rotation.y);
	temp.z = Math::ToDegree(rotation.z);

	*vec = temp;
}

Vector3 Transform::Forward()
{
	return Vector3(bufferDesc.World._31, bufferDesc.World._32, bufferDesc.World._33);
}

Vector3 Transform::Up()
{
	return Vector3(bufferDesc.World._21, bufferDesc.World._22, bufferDesc.World._23);
}

Vector3 Transform::Right()
{
	return Vector3(bufferDesc.World._11, bufferDesc.World._12, bufferDesc.World._13);
}

void Transform::World(Matrix& matrix)
{
	// Matrix의 분해를 위해 사용, 이 함수에서 분해 방식은 LU Decompose 사용
	// LU Decompose 공식 정리할것..
	Math::MatrixDecompose(matrix, scale, rotation, position);

	bufferDesc.World = matrix;
}

void Transform::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);

	Matrix world = S * R * T;
	bufferDesc.World = world;
}

void Transform::Update()
{

}

void Transform::Render()
{
	if (shader == NULL)
		return;

	buffer->Render();
	sBuffer->SetConstantBuffer(buffer->Buffer());
}

 

 

 

 

 

 

 

 

Math.cpp


더보기
void Math::MatrixDecompose(const D3DXMATRIX & m, OUT Vector3 & S, OUT Vector3 & R, OUT Vector3 & T)
{
	D3DXQUATERNION rotation;
	D3DXMatrixDecompose(&S, &rotation, &T, &m);

	D3DXMATRIX temp;
	D3DXMatrixRotationQuaternion(&temp, &rotation);

	R.x = asin(-temp._32);
	R.y = atan2(temp._31, temp._33);
	R.z = atan2(temp._12, temp._22);
}

 

 

 

 

 

 

 

 

 

 

 

결과