Skip to content

Commit

Permalink
🎮 Added cvar gfx_camera_speed + top menu UI slider
Browse files Browse the repository at this point in the history
Changes:
* cvar `gfx_camera_speed` replaces `CameraManager::m_cam_ratio` which was used/updated chaotically and didn't always account frame time. Default value is 4.0 like before.
* New `smoothXXX()` helpers to CameraManager, replacing copypasted smoothing code.
* Clarified staticcam fov exponent smoothing: Removed `Cameramanager::m_staticcam_fov_exponent` which just duplicated `gfx_static_cam_fov_exp`. Added `Cameramanager::m_staticcam_fov_exp_current` which replaces `static fovExp` (CameraManager.cpp, line 789). Comments clearly indicate what is what.
  • Loading branch information
ohlidalp committed Mar 11, 2024
1 parent 941dd6e commit 8fdf59d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 35 deletions.
1 change: 1 addition & 0 deletions source/main/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ CVar* gfx_shadow_quality;
CVar* gfx_skidmarks_mode;
CVar* gfx_sight_range;
CVar* gfx_camera_height;
CVar* gfx_camera_speed;
CVar* gfx_fov_external;
CVar* gfx_fov_external_default;
CVar* gfx_fov_internal;
Expand Down
1 change: 1 addition & 0 deletions source/main/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ extern CVar* gfx_shadow_quality;
extern CVar* gfx_skidmarks_mode;
extern CVar* gfx_sight_range;
extern CVar* gfx_camera_height;
extern CVar* gfx_camera_speed;
extern CVar* gfx_fov_external;
extern CVar* gfx_fov_external_default;
extern CVar* gfx_fov_internal;
Expand Down
64 changes: 32 additions & 32 deletions source/main/gfx/camera/CameraManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,13 @@ CameraManager::CameraManager() :
, m_splinecam_mo(0)
, m_splinecam_spline_pos(0.5f)
, m_staticcam_force_update(false)
, m_staticcam_fov_exponent(1.0f)
, m_cam_rot_x(0.0f)
, m_cam_rot_y(0.3f)
, m_cam_dist(5.f)
, m_cam_dist_min(0.f)
, m_cam_dist_max(0.f)
, m_cam_target_direction(0.f)
, m_cam_target_pitch(0.f)
, m_cam_ratio (11.f)
, m_cam_look_at(Ogre::Vector3::ZERO)
, m_cam_look_at_last(Ogre::Vector3::ZERO)
, m_cam_look_at_smooth(Ogre::Vector3::ZERO)
Expand Down Expand Up @@ -199,7 +197,6 @@ void CameraManager::UpdateCurrentBehavior()
}

case CAMERA_BEHAVIOR_STATIC:
m_staticcam_fov_exponent = App::gfx_static_cam_fov_exp->getFloat();
this->UpdateCameraBehaviorStatic();
return;

Expand Down Expand Up @@ -335,21 +332,19 @@ void CameraManager::ResetCurrentBehavior()
{
m_cam_rot_y = 0.1f;
m_cam_dist = 0.1f;
m_cam_ratio = 0.0f;
}
else
{
m_cam_rot_y = 0.3f;
m_cam_dist = 5.0f;
m_cam_ratio = 11.0f;
}
m_cam_dist_min = 0;
m_cam_target_pitch = 0.0f;
return;
}

case CAMERA_BEHAVIOR_STATIC:
m_staticcam_fov_exponent = 1.0f;
m_staticcam_fov_exp_current = 1.0f;
App::gfx_static_cam_fov_exp->setVal(1.0f);
return;

Expand Down Expand Up @@ -564,7 +559,24 @@ bool CameraManager::mouseMoved(const OIS::MouseEvent& _arg)

return CameraManager::CameraBehaviorOrbitMouseMoved(_arg);
}
case CAMERA_BEHAVIOR_STATIC: return CameraBehaviorStaticMouseMoved(_arg);
case CAMERA_BEHAVIOR_STATIC:
{
const OIS::MouseState ms = _arg.state;

if (ms.buttonDown(OIS::MB_Right))
{
// Note: `gfx_static_cam_fov_exp` is the desired (target) value; current (smooth) value is `m_staticcam_fov_exp_current`

float scale = RoR::App::GetInputEngine()->isKeyDown(OIS::KC_LMENU) ? 0.00002f : 0.0002f;
float exp = App::gfx_static_cam_fov_exp->getFloat();
exp += ms.Z.rel * scale;
exp = Math::Clamp(exp, 0.8f, 1.50f);
App::gfx_static_cam_fov_exp->setVal(exp);
return true;
}

return false;
}
case CAMERA_BEHAVIOR_VEHICLE: return CameraBehaviorOrbitMouseMoved(_arg);
case CAMERA_BEHAVIOR_VEHICLE_SPLINE: return this->CameraBehaviorVehicleSplineMouseMoved(_arg);
case CAMERA_BEHAVIOR_VEHICLE_CINECAM: return CameraBehaviorOrbitMouseMoved(_arg);
Expand Down Expand Up @@ -786,9 +798,7 @@ void CameraManager::UpdateCameraBehaviorStatic()
}
}

static float fovExp = m_staticcam_fov_exponent;
fovExp = (1.0f / (m_cam_ratio + 1.0f)) * m_staticcam_fov_exponent + (m_cam_ratio / (m_cam_ratio + 1.0f)) * fovExp;

float fovExp = this->smoothFloat(m_staticcam_fov_exp_current, App::gfx_static_cam_fov_exp->getFloat(), m_cct_dt, App::gfx_camera_speed->getFloat());
float camDist = m_staticcam_position.distance(m_staticcam_look_at);
float fov = atan2(20.0f, std::pow(camDist, fovExp));

Expand All @@ -797,22 +807,6 @@ void CameraManager::UpdateCameraBehaviorStatic()
App::GetCameraManager()->GetCamera()->setFOVy(Radian(fov));
}

bool CameraManager::CameraBehaviorStaticMouseMoved(const OIS::MouseEvent& _arg)
{
const OIS::MouseState ms = _arg.state;

if (ms.buttonDown(OIS::MB_Right))
{
float scale = RoR::App::GetInputEngine()->isKeyDown(OIS::KC_LMENU) ? 0.00002f : 0.0002f;
m_staticcam_fov_exponent += ms.Z.rel * scale;
m_staticcam_fov_exponent = Math::Clamp(m_staticcam_fov_exponent, 0.8f, 1.50f);
App::gfx_static_cam_fov_exp->setVal(m_staticcam_fov_exponent);
return true;
}

return false;
}

void CameraManager::CameraBehaviorOrbitUpdate()
{
if (RoR::App::GetInputEngine()->getEventBoolValueBounce(EV_CAMERA_LOOKBACK))
Expand Down Expand Up @@ -916,7 +910,7 @@ void CameraManager::CameraBehaviorOrbitUpdate()
Vector3 precedingLookAt = m_cam_look_at_smooth_last + camDisplacement;
Vector3 precedingPosition = this->GetCameraNode()->getPosition() + camDisplacement;

Vector3 camPosition = (1.0f / (m_cam_ratio + 1.0f)) * desiredPosition + (m_cam_ratio / (m_cam_ratio + 1.0f)) * precedingPosition;
Vector3 camPosition = this->smoothVector3(precedingPosition, desiredPosition, m_cct_dt, App::gfx_camera_speed->getFloat());

if (App::GetGameContext()->GetTerrain()->GetCollisions() && App::GetGameContext()->GetTerrain()->GetCollisions()->forcecam)
{
Expand All @@ -931,7 +925,7 @@ void CameraManager::CameraBehaviorOrbitUpdate()
this->GetCameraNode()->setPosition(camPosition);
}

m_cam_look_at_smooth = (1.0f / (m_cam_ratio + 1.0f)) * m_cam_look_at + (m_cam_ratio / (m_cam_ratio + 1.0f)) * precedingLookAt;
m_cam_look_at_smooth = this->smoothVector3(precedingLookAt, m_cam_look_at, m_cct_dt, App::gfx_camera_speed->getFloat());

m_cam_look_at_last = m_cam_look_at;
m_cam_look_at_smooth_last = m_cam_look_at_smooth;
Expand Down Expand Up @@ -1068,8 +1062,6 @@ void CameraManager::UpdateCameraBehaviorVehicle()
m_cam_target_pitch = -asin(dir.dotProduct(Vector3::UNIT_Y));
}

m_cam_ratio = 1.0f / (m_cct_dt * 4.0f);

m_cam_dist_min = std::min(m_cct_player_actor->getMinimalCameraRadius() * 2.0f, 33.0f);

m_cam_look_at = m_cct_player_actor->getPosition();
Expand Down Expand Up @@ -1185,8 +1177,6 @@ bool CameraManager::CameraBehaviorVehicleSplineMouseMoved( const OIS::MouseEven
{
const OIS::MouseState ms = _arg.state;

m_cam_ratio = 1.0f / (m_cct_dt * 4.0f);

if (RoR::App::GetInputEngine()->isKeyDown(OIS::KC_LCONTROL) && ms.buttonDown(OIS::MB_Right))
{
Real splinePosDiff = ms.X.rel * std::max(0.00005f, m_splinecam_spline_len * 0.0000001f);
Expand Down Expand Up @@ -1390,6 +1380,16 @@ void CameraManager::switchDirectlyToBehavior(CameraBehaviors new_behavior, int i
}
}

float CameraManager::smoothFloat(float current, float target, float dt, float speed) const
{
return current + (target - current) * speed * dt;
}

Ogre::Vector3 CameraManager::smoothVector3(const Ogre::Vector3& current, const Ogre::Vector3& target, float dt, float speed) const
{
return current + (target - current) * speed * dt;
}

std::string RoR::ToLocalizedString(CameraManager::CameraBehaviors behavior)
{
switch (behavior)
Expand Down
8 changes: 5 additions & 3 deletions source/main/gfx/camera/CameraManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ class CameraManager
void ResetCurrentBehavior();
void DeactivateCurrentBehavior();
void UpdateCameraBehaviorStatic();
bool CameraBehaviorStaticMouseMoved(const OIS::MouseEvent& _arg);
void UpdateCameraBehaviorFree();
void UpdateCameraBehaviorFixed();
void UpdateCameraBehaviorVehicle();
Expand All @@ -104,6 +103,10 @@ class CameraManager
void CameraBehaviorVehicleSplineUpdateSplineDisplay();
void CreateCameraNode();

// Helper functions
float smoothFloat(float current, float target, float dt, float speed) const;
Ogre::Vector3 smoothVector3(const Ogre::Vector3& current, const Ogre::Vector3& target, float dt, float speed) const;

Ogre::Camera* m_camera;
Ogre::SceneNode* m_camera_node;

Expand All @@ -124,15 +127,14 @@ class CameraManager
float m_cam_dist;
float m_cam_dist_min;
float m_cam_dist_max;
float m_cam_ratio;
Ogre::Vector3 m_cam_look_at;
bool m_cam_limit_movement;
Ogre::Vector3 m_cam_look_at_last;
Ogre::Vector3 m_cam_look_at_smooth;
Ogre::Vector3 m_cam_look_at_smooth_last;
// Static cam attributes
bool m_staticcam_force_update;
float m_staticcam_fov_exponent;
float m_staticcam_fov_exp_current = 1.f; //!< Smoothed value; target is cvar `gfx_static_cam_fox_exp`
Ogre::Radian m_staticcam_previous_fov;
Ogre::Vector3 m_staticcam_look_at;
Ogre::Vector3 m_staticcam_position;
Expand Down
2 changes: 2 additions & 0 deletions source/main/gui/panels/GUI_TopMenubar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2464,6 +2464,8 @@ void TopMenubar::DrawCameraContextSensitiveBox()
// * `gfx_fov_external` (int) ~ FOV of exterior cameras (3rd person, free cam, freefixed cam), adjustable by hotkeys EV_COMMON_FOV_{LESS/MORE/RESET}.
// -------------------------------------------------------------------------------------------------

DrawGFloatSlider(App::gfx_camera_speed, _LC("TopMenubar", "Speed"), 1.0f, 10.0f);

switch (App::GetCameraManager()->GetCurrentBehavior())
{
case CameraManager::CAMERA_BEHAVIOR_STATIC:
Expand Down
1 change: 1 addition & 0 deletions source/main/system/CVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ void Console::cVarSetupBuiltins()
App::gfx_skidmarks_mode = this->cVarCreate("gfx_skidmarks_mode", "Skidmarks", CVAR_ARCHIVE | CVAR_TYPE_INT, "0");
App::gfx_sight_range = this->cVarCreate("gfx_sight_range", "SightRange", CVAR_ARCHIVE | CVAR_TYPE_INT, "5000");
App::gfx_camera_height = this->cVarCreate("gfx_camera_height", "Static camera height", CVAR_ARCHIVE | CVAR_TYPE_INT, "5");
App::gfx_camera_speed = this->cVarCreate("gfx_camera_speed", "Camera smoothing speed", CVAR_ARCHIVE | CVAR_TYPE_FLOAT, "4.0");
App::gfx_fov_external = this->cVarCreate("gfx_fov_external", "", CVAR_TYPE_INT, "60");
App::gfx_fov_external_default= this->cVarCreate("gfx_fov_external_default","FOV External", CVAR_ARCHIVE | CVAR_TYPE_INT, "60");
App::gfx_fov_internal = this->cVarCreate("gfx_fov_internal", "", CVAR_TYPE_INT, "75");
Expand Down

0 comments on commit 8fdf59d

Please sign in to comment.