DirectX/DirectX 3D_(구)

07_Camera

컴맹학자 2021. 6. 30. 20:03
728x90

Camera.cpp , h

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

Camera::Camera()
{
	D3DXMatrixIdentity(&matRotation);
	D3DXMatrixIdentity(&matView);

	Rotation();
	Move();
}

Camera::~Camera()
{
}


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

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

	Move();
}

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

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

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

	Rotation();
}

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

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

void Camera::RotationDegree(Vector3 & vec)
{
	rotation = vec * 0.017453f;

	Rotation(rotation);
}

void Camera::RotationDegree(Vector3 * vec)
{
	*vec = rotation * 57.295791f;
}

void Camera::GetMatrix(Matrix * matrix)
{
	memcpy(matrix, &matView, sizeof(Matrix));
}

void Camera::Rotation()
{
	///Matrix X, Y, Z;
	///D3DXMatrixRotationX(&matRotation, rotation.x);
	///D3DXMatrixRotationY(&matRotation, rotation.y);
	///D3DXMatrixRotationZ(&matRotation, rotation.z);
	///matRotation = X * Y * Z;
	D3DXMatrixRotationYawPitchRoll(&matRotation, rotation.y, rotation.x, rotation.z);
	/*
	D3DXVec3TransformCoord() // mul(vec4 , mat) 위치를 공간변환
	D3DXVec3TransformNormal() //mul(vec3 , mat) 방향만 공간변환
	*/
	D3DXVec3TransformNormal(&forward, &Vector3(0, 0, 1), &matRotation);
	D3DXVec3TransformNormal(&up, &Vector3(0, 1, 0), &matRotation);
	D3DXVec3TransformNormal(&right, &Vector3(1, 0, 0), &matRotation);
}

//다른데서 다시 재정의
void Camera::Move()
{
	View();
}

//카메라를 만들어주는곳
void Camera::View()
{
	D3DXMatrixLookAtLH(&matView, &position, &(position + forward), &up);
}
#pragma once

class Camera
{
public:
	Camera();
	virtual ~Camera();

	virtual void Update() = 0;

public:
	void Position(float x, float y, float z);
	void Position(Vector3& vec);
	void Position(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);

	void GetMatrix(Matrix* matrix);

	Vector3 Forward() { return forward; }
	Vector3 Up() { return up; }
	Vector3 Right() { return right; }

protected:
	virtual void Rotation();
	virtual void Move();

protected:
	void View();

private:
	Vector3 position = Vector3(0, 0, 0);
	Vector3 rotation = Vector3(0, 0, 0);

	Vector3 forward = Vector3(0, 0, 1);
	Vector3 right = Vector3(1, 0, 0);
	Vector3 up = Vector3(0, 1, 0);

	//회전용
	Matrix matRotation;
	Matrix matView;
};

Freedom.cpp , h

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

Freedom::Freedom()
{
}

Freedom::~Freedom()
{
}

void Freedom::Update()
{
	//오른쪽 마우스 눌렀을때 밑에 내용 실행
	if (Mouse::Get()->Press(1) == false) return;

	Vector3 f = Forward();
	Vector3 r = Right();
	Vector3 u = Up();

	//Move
	{
		Vector3 P;
		Position(&P);

		//전방
		if (Keyboard::Get()->Press('W'))
			P = P + f * move * Time::Delta();
		else if (Keyboard::Get()->Press('S'))
			P = P - f * move * Time::Delta();

		//좌우
		if (Keyboard::Get()->Press('D'))
			P = P + r * move * Time::Delta();
		else if (Keyboard::Get()->Press('A'))
			P = P - r * move * Time::Delta();
		
		//상하
		if (Keyboard::Get()->Press('E'))
			P = P + u * move * Time::Delta();
		else if (Keyboard::Get()->Press('Q'))
			P = P - u * move * Time::Delta();

		Position(P);
	}

	//Rotation
	{
		Vector3 R;
		Rotation(&R);
	
		//마우스 회전값
		Vector3 val = Mouse::Get()->GetMoveValue();
		R.x = R.x + val.y * rotation;
		R.y = R.y + val.x * rotation;
		R.z = 0.0f;
	
		Rotation(R);
	}
}

void Freedom::Speed(float move, float rotation)
{
	this->move = move;
	this->rotation = rotation;
}
#pragma once

class Freedom : public Camera
{
public:
	Freedom();
	~Freedom();

	void Update() override;

	void Speed(float move, float rotation = 2.0f);

private:
	float move = 20.0f; //이동속도
	float rotation = 0.01f; //회전속도
};

Context.cpp, h

더보기
#include "Framework.h"
#include "Context.h"
#include "Viewer/Viewport.h"
#include "Viewer/Perspective.h"

Context* Context::instance = NULL;

Context * Context::Get()
{
	//assert(instance != NULL);

	return instance;
}

void Context::Create()
{
	assert(instance == NULL);

	instance = new Context();
}

void Context::Delete()
{
	SafeDelete(instance);
}

Context::Context()
{
	D3DDesc desc = D3D::GetDesc();

	perspective = new Perspective(desc.Width, desc.Height);
	viewport = new Viewport(desc.Width, desc.Height);

	camera = new Freedom();

	//position = D3DXVECTOR3(0, 0, -10);
	//D3DXVECTOR3 forward(0, 0, 1);
	//D3DXVECTOR3 right(1, 0, 0);
	//D3DXVECTOR3 up(0, 1, 0);
	//D3DXMatrixLookAtLH(&view, &position, &(position + forward), &up);
}

Context::~Context()
{
	SafeDelete(perspective);
	SafeDelete(viewport);
}

void Context::ResizeScreen()
{
	perspective->Set(D3D::Width(), D3D::Height());
	viewport->Set(D3D::Width(), D3D::Height());
}

void Context::Update()
{
	//ImGui::SliderFloat("EyeX", &position.x, 0, 128);
	//ImGui::SliderFloat("EyeY", &position.y, -20, 20);
	//ImGui::SliderFloat("EyeZ", &position.z, -128, 128);
	//D3DXVECTOR3 forward(0, 0, 1);
	//D3DXVECTOR3 right(1, 0, 0);
	//D3DXVECTOR3 up(0, 1, 0);
	//D3DXMatrixLookAtLH(&view, &position, &(position + forward), &up);
	camera->Update();
}

void Context::Render()
{
	viewport->RSSetViewport();
}

D3DXMATRIX Context::View()
{
	Matrix view;
	camera->GetMatrix(&view);
	return view;
}

D3DXMATRIX Context::Projection()
{
	D3DXMATRIX projection;
	perspective->GetMatrix(&projection);

	return projection;
}
#pragma once

class Context
{
public:
	static Context* Get();
	static void Create();
	static void Delete();

private:
	Context();
	~Context();

public:
	void ResizeScreen();

	void Update();
	void Render();

	D3DXMATRIX View();
	D3DXMATRIX Projection();

	class Perspective* GetPerspective() { return perspective; }
	class Viewport* GetViewport() { return viewport; }
	class Camera* GetCamera() { return camera; }

private:
	static Context* instance;

private:
	class Perspective* perspective;
	class Viewport* viewport;

	Camera* camera;
};

실행


Framework 안에 있는 두개의 폴더 수정

 

Renders 폴더 안에는 출력 관련으로 Context 역할이 Viewer에 있는 카메라 관련이 출력 하게 만드는 클래스

 

 

 

Camera, Freedom을 작성한 폴더로 카메라 관련 클래스들이 정의된 곳

 

Camera : 모든 카메라들의 부모역할 함수만 정의되어 있고 관련 카메라 옵션들은 재정의 

Freedom : 영어 뜻 대로 자유롭게 움직일수 있게 만든 카메라

'DirectX > DirectX 3D_(구)' 카테고리의 다른 글

09_Texture  (0) 2021.06.30
08_Cube  (0) 2021.06.30
06_Grid  (0) 2021.06.30
05_Index  (0) 2021.06.30
04_World  (0) 2021.06.28