본문 바로가기

그래픽스

[DX12] Orthographic Projection

 

Orthographic Projection(직교 투영)은 고정된 UI를 만들 때 사용하는 방식이다. 그래서 크기는 사용자의 화면 만큼이 된다.

 

 

왼쪽은 스크린을 (-1,-1) ~ (1,1) 사이 그림으로 만들었을 때 x,y의 실제 좌표를 구하는 식이다. 실제 화면의 크기의 절반씩을 곱해주면 화면상 좌표를 알 수 있다. z값은 깊이를 나타내는데 z값에 n이 들어갈 경우 0, f가 들어갈 경우 1이 나온다. 오른쪽은 식을 행렬로 표현한 것이다. 

 

array<wstring, MAX_LAYER> _layerNames;
map<wstring, uint8> _layerIndex;

 

실제로 Orthographic을 사용할 때는 레이어를 만들어 Perspective 레이어와 Orthographic(UI) 레이어를 만들어 관리한다. gameobject마다 자신이 어떤 레이어에 있는지 설정할 수 있다.

void SetCullingMaskLayerOnOff(uint8 layer, bool on)
	{
		if (on)
			_cullingMask |= (1 << layer);
		else
			_cullingMask &= ~(1 << layer);
	}

 

카메라는 여러 레이어를 찍을 수 있으므로 cullingMask라는 비트플래그를 가져 지정한 레이어만 찍지 않는다.  비트 연산을 통해 지정한 레이어의 비트만 켠다. 

 

	PROJECTION_TYPE _type = PROJECTION_TYPE::PERSPECTIVE;
	if (_type == PROJECTION_TYPE::PERSPECTIVE)
		_matProjection = ::XMMatrixPerspectiveFovLH(_fov, width / height, _near, _far);
	else
		_matProjection = ::XMMatrixOrthographicLH(width * _scale, height * _scale, _near, _far);

 

카메라는 Projection Type을 가지고 있고 Perspective 이냐 Orthographic이냐에 따라 _matProjection을 구하는 식이 달라진다. Orthographic은 fov가 없기 때문에 인자에 없는 모습이다.

 

#pragma region Camera
	{
		shared_ptr<GameObject> camera = make_shared<GameObject>();
		camera->SetName(L"Main_Camera");
		camera->AddComponent(make_shared<Transform>());
		camera->AddComponent(make_shared<Camera>()); // Near=1, Far=1000, FOV=45도
		camera->AddComponent(make_shared<TestCameraScript>());
		camera->GetTransform()->SetLocalPosition(Vec3(0.f, 0.f, 0.f));
		uint8 layerIndex = GET_SINGLE(SceneManager)->LayerNameToIndex(L"UI");
		camera->GetCamera()->SetCullingMaskLayerOnOff(layerIndex, true); // UI는 안 찍음
		scene->AddGameObject(camera);
	}	
#pragma endregion

#pragma region UI_Camera
	{
		shared_ptr<GameObject> camera = make_shared<GameObject>();
		camera->SetName(L"Orthographic_Camera");
		camera->AddComponent(make_shared<Transform>());
		camera->AddComponent(make_shared<Camera>()); // Near=1, Far=1000, 800*600
		camera->GetTransform()->SetLocalPosition(Vec3(0.f, 0.f, 0.f));
		camera->GetCamera()->SetProjectionType(PROJECTION_TYPE::ORTHOGRAPHIC);
		uint8 layerIndex = GET_SINGLE(SceneManager)->LayerNameToIndex(L"UI");
		camera->GetCamera()->SetCullingMaskAll(); // 다 끄고
		camera->GetCamera()->SetCullingMaskLayerOnOff(layerIndex, false); // UI만 찍음
		scene->AddGameObject(camera);
	}
#pragma endregion

 

Scene에 Perspective 카메라와 Orthographic 카메라를 만들면 각각이 컬링 마스크가 꺼져있는 레이어에 대하여 찍는다.

그리고 실행하면 두 카메라가 찍고 있는 것이 합쳐져서 화면에 보이는데 이는 Scene에서 모든 카메라에 대해 돌면서 Camera->Render을 하고 있기 때문이다. 

'그래픽스' 카테고리의 다른 글

[DX12] Deferred Rendering  (0) 2022.07.28
[DX12] Render Target  (0) 2022.07.28
[DX12] Quaternion #1  (0) 2022.07.27
[DX12] Skybox  (0) 2022.07.26
[DX12] Lighting 코드 #2  (0) 2022.07.25