69 lines
1.8 KiB
C++
69 lines
1.8 KiB
C++
#include "render/dl_camera.h"
|
|
|
|
#ifndef DL_DISABLE_RENDER
|
|
#include "system/dl_time.h"
|
|
#include "system/dl_system.h"
|
|
#include "io/dl_input.h"
|
|
|
|
namespace dl
|
|
{
|
|
void Camera::RunControllerFPS()
|
|
{
|
|
float speed_position = 10.0f * float(g_time->GetDelta());
|
|
//自身矩阵
|
|
Matrix4 mat_self = Mat::CreateLookTo(_position, _lookDirection, _upDirection);
|
|
//自身逆矩阵
|
|
Matrix4 mat_self_inv = Mat::GetInverse(mat_self);
|
|
|
|
//自身偏移(自己的前方为-z轴)
|
|
Position3 pos_dt = {};
|
|
if (g_input->KeyState(KeyCode::W))
|
|
{//w 前进
|
|
pos_dt[2] -= speed_position;
|
|
}
|
|
|
|
if (g_input->KeyState(KeyCode::S))
|
|
{//s 后退
|
|
pos_dt[2] += speed_position;
|
|
}
|
|
|
|
if (g_input->KeyState(KeyCode::A))
|
|
{//a 左
|
|
pos_dt[0] -= speed_position;
|
|
}
|
|
|
|
if (g_input->KeyState(KeyCode::D))
|
|
{//d 右
|
|
pos_dt[0] += speed_position;
|
|
}
|
|
//偏移从自身到世界
|
|
Position4 pos_world = mat_self_inv * toLonger(pos_dt, 1.0f);
|
|
|
|
_position = toShorter(pos_world);
|
|
|
|
//---------------------------------------------旋转视角--------------------------------------
|
|
Point mouse_dt = g_input->GetCursorPositionDelta();
|
|
|
|
|
|
float speed_rotation = 0.001f;// *float(g_time->GetRealDelta());
|
|
if (mouse_dt[0] || mouse_dt[1])
|
|
{
|
|
//x移动 绕up轴旋转 看得方向
|
|
Matrix4 mat_up = Mat::CreateRotation(_upDirection, -mouse_dt[0] * speed_rotation);
|
|
Position3 v_right = Math::CrossProduct(_upDirection, _lookDirection);
|
|
//y移动 绕right轴旋转 看得方向
|
|
Matrix4 mat_right = Mat::CreateRotation(v_right, mouse_dt[1] * speed_rotation);
|
|
|
|
_lookDirection = toShorter(mat_up * mat_right * toLonger(_lookDirection, 1.0f));
|
|
}
|
|
//设置禁用鼠标
|
|
if (!g_input->KeyState(KeyCode::LEFT_CONTROL))
|
|
{
|
|
g_system->SetCursorMode(System::CursorMode::DISABLE);
|
|
}
|
|
else
|
|
g_system->SetCursorMode(System::CursorMode::NORMAL);
|
|
}
|
|
}
|
|
|
|
#endif |