Files
dl_old/dl/render/dl_camera.cpp
T
2026-05-14 21:17:14 +08:00

78 lines
2.0 KiB
C++

#include "render/dl_camera.h"
#ifndef DL_DISABLE_RENDER
#include "dl_type.h"
#include "system/dl_time.h"
#include "system/dl_system.h"
#include "io/dl_input.h"
#include "dl_graphics.h"
namespace dl
{
void Camera::RunControllerFPS()
{
float speed_position = 10.0f * float(g_time->GetDelta());
if (g_input->KeyState(KeyCode::LEFT_SHIFT))
speed_position *= 5.0f;
else if(g_input->KeyState(KeyCode::LEFT_CONTROL))
speed_position /= 5.0f;
Position3 pos_dt = _position;
if (g_input->KeyState(KeyCode::W))
{// w 前进
pos_dt += _lookDirection * speed_position;
}
if (g_input->KeyState(KeyCode::S))
{//s 后退
pos_dt -= _lookDirection * speed_position;
}
if (g_input->KeyState(KeyCode::A))
{//a 左
pos_dt += _leftDirection * speed_position;
}
if (g_input->KeyState(KeyCode::D))
{//d 右
pos_dt -= _leftDirection * speed_position;
}
_position = pos_dt;
//---------------------------------------------旋转视角--------------------------------------
Point mouse_dt = g_input->GetCursorPositionDelta();
float speed_rotation = 0.001f;// *float(g_time->GetRealDelta());
if (mouse_dt[0])
{
// x移动 left绕z轴旋转
Matrix4 mat_up = Mat::CreateRotation(AXIS_Z, -mouse_dt[0] * speed_rotation);
_lookDirection = Mat::ApplyDir(mat_up, _lookDirection);
_upDirection = Mat::ApplyDir(mat_up, _upDirection);;
_leftDirection = Math::CrossProduct(AXIS_Z, _lookDirection);
}
if (mouse_dt[1])
{
// y移动 绕left轴旋转
Matrix4 mat_left = Mat::CreateRotation(_leftDirection, mouse_dt[1] * speed_rotation);
_lookDirection = Mat::ApplyDir(mat_left, _lookDirection);
_upDirection = Math::CrossProduct(_lookDirection, _leftDirection);
}
// 设置禁用鼠标
if (!g_input->KeyState(KeyCode::LEFT_CONTROL))
{
g_system->SetCursorMode(System::CursorMode::DISABLE);
}
else
g_system->SetCursorMode(System::CursorMode::NORMAL);
g_gfx->SetColor(ColorDef::AXIS_X);
g_gfx->Line({}, _lookDirection);
g_gfx->SetColor(ColorDef::AXIS_Y);
g_gfx->Line({}, _upDirection);
g_gfx->SetColor(ColorDef::AXIS_Z);
g_gfx->Line({}, _leftDirection);
}
}
#endif