Camera
이젠 카메라를 만들어 보자!!
Camera.cpp
void Camera::Position(const Vector3& vec)
{
position = vec;
Move();
}
void Camera::Move()
{
View();
}
void Camera::View()
{
D3DXMatrixLookAtLH(&matView, &position, &(position + forward), &up);
}
확실히 이동은 쉽다. 그냥 앞으로 가주면 된다.
void Camera::Rotation()
{
Matrix X, Y, Z;
D3DXMatrixRotationX(&X, rotation.x);
D3DXMatrixRotationY(&Y, rotation.y);
D3DXMatrixRotationZ(&Z, rotation.z);
matRotation = X * Y * Z;
D3DXVec3TransformNormal(&forward, &Vector3(0, 0, 1), &matRotation);
D3DXVec3TransformNormal(&up, &Vector3(0, 1, 0), &matRotation);
D3DXVec3TransformNormal(&right, &Vector3(1, 0, 0), &matRotation);
}
각 축을 회전하는 행렬을 생성하고,
현제의 벡터와 만든 행렬을 연산해준다.
앞 위 옆 순으로 연산해주면 끝난다.
void Camera::RotationDegree(const Vector3 & vec)
{
//rotation = vec * Math::PI / 180.0f;
// 매직 넘버
rotation = vec * 0.01745328f;
//Rotation();
}
void Camera::RotationDegree(Vector3 * vec)
{
//*vec = rotation * 180.0f / Math::PI;
*vec = rotation * 57.29577957f;
}
그리고 각도로 갖고오는 함수인대 위에 계산식을 일부러 변수로 둬 계산을 한번 줄여준다.
Freedom Camera
void Freedom::Update()
{
if (Mouse::Get()->Press(1) == false) { return; }
Vector3 f = Forward();
Vector3 u = Up();
Vector3 r = Right();
{
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(VK_SPACE))
{
P = P + u * move * Time::Delta();
}
else if (Keyboard::Get()->Press(VK_CONTROL))
{
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 * Time::Delta();
R.y = R.y + val.x * rotation * Time::Delta();
R.z = 0.0f;
Rotation(R);
}
}
이동과 회전을 마우스와 키보드로 할 수 있게 한다.
현재 방향 벡터를 갖고오고 움직여 주고
회전은 마우스의 프레임당 이동값을 갖고와 회전시켜 준다.