6. 아핀 공간

CJB_ny·2023년 2월 27일
0

이득우 겜수

목록 보기
4/9
post-thumbnail

정리 글

6-1 아핀 변환을 사용해 크기, 회전, 이동 구현

p203

// 렌더링 로직을 담당하는 함수
void SoftRenderer::Render2D()
{
	// 렌더링 로직에서 사용하는 모듈 내 주요 레퍼런스
	auto& r = GetRenderer();
	const auto& g = Get2DGameEngine();

	// 배경에 격자 그리기
	DrawGizmo2D();

	// 렌더링 로직의 로컬 변수
	float rad = 0.f;
	static float increment = 0.001f;
	static std::vector<Vector2> hearts;
	HSVColor hsv(0.f, 1.f, 0.85f);

	// 하트를 구성하는 점 생성
	if (hearts.empty())
	{
		for (rad = 0.f; rad < Math::TwoPI; rad += increment)
		{
			float sin = sinf(rad);
			float cos = cosf(rad);
			float cos2 = cosf(2 * rad);
			float cos3 = cosf(3 * rad);
			float cos4 = cosf(4 * rad);
			float x = 16.f * sin * sin * sin;
			float y = 13 * cos - 5 * cos2 - 2 * cos3 - cos4;
			hearts.push_back(Vector2(x, y));
		}
	}

	// 아핀 변환행렬 (크기)
	Vector3 sBasis1(currentScale, 0.f, 0.f);
	Vector3 sBasis2(0.f, currentScale, 0.f);
	Vector3 sBasis3 = Vector3::UnitZ;
	Matrix3x3 sMatrix(sBasis1, sBasis2, sBasis3);
	
	// 아핀 변환행렬 (회전)
	float sin = 0.f, cos = 0.f;
	Math::GetSinCos(sin ,cos, currentDegree);
	Vector3 rBasis1(cos, sin, 0.f);
	Vector3 rBasis2(-sin, cos, 0.f);
	Vector3 rBasis3 = Vector3::UnitZ;
	Matrix3x3 rMatrix(rBasis1, rBasis2, rBasis3);

	// 아핀 변환행렬 (이동)
	Vector3 tBasis1 = Vector3::UnitX;
	Vector3 tBasis2 = Vector3::UnitY;
	Vector3 tBasis3(currentPosition.X, currentPosition.Y, 1.f);
	Matrix3x3 tMatrix(tBasis1, tBasis2, tBasis3);
	
	// 모든 아핀 변환 행렬을 곱한 합성행렬. 크기 -> 회전 -> 이동 순으로 적용
	Matrix3x3 finalMatrix = tMatrix * rMatrix * sMatrix;

	// 각 값을 초기화 한 후 동일하게 증가시키면서 색상값을 지정
	rad = 0.f;
	for (auto const& v : hearts)
	{
		// 곱셈을 위해 하트를 구성하는 벡터를 3차원으로 변경
		Vector3 newV(v.X, v.Y, 1.f);

		// 아핀 변환을 적용하고 마지막 차원의 값을 제거하고 사용
		Vector3 finalV = finalMatrix * newV;

		hsv.H = rad / Math::TwoPI;
		r.DrawPoint(finalV.ToVector2(), hsv.ToLinearColor());
		rad += increment;
	}

	// 현재 위치, 크기, 각도를 화면에 출력
	r.PushStatisticText(std::string("Position : ") + currentPosition.ToString());
	r.PushStatisticText(std::string("Scale : ") + std::to_string(currentScale));
	r.PushStatisticText(std::string("Degree : ") + std::to_string(currentDegree));
}

아핀 변환행렬 (크기)를 만들어 준다. 3차원으로 만드는 이유는 2차원으로 물체를 그리고 나머지 남는 한차원으로 이동을 시켜주는 것이다. 이게 아핀공간임.

아핀 회전변환행렬, 아핀 이동변환 행렬 다 만들어 준다.

그리고 크기 -> 회전 -> 이동 순으로 합성행렬을 만들어 하트 방정식에 찍힌 점 v에다가 아핀 변환을 적용시켜준다.

결과는 5-2, 5-1이랑 똑같다.

profile
https://cjbworld.tistory.com/ <- 이사중

0개의 댓글