Skip to content

Commit

Permalink
Improvements to the cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
QuestionableM committed Dec 14, 2023
1 parent a1b1727 commit 3d1cc64
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
36 changes: 27 additions & 9 deletions Code/DynamicSun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ inline static float lerp_float(float a, float b, float f)

const float GetLightIntensity(float time_of_day)
{
constexpr float c_fSunriseTime = 0.3f;
constexpr float c_fSunriseTime = 0.27f;
constexpr float c_fSunsetTime = 0.75f;
constexpr float c_fSunsetTimeReversed = 1.0f - c_fSunsetTime;
constexpr float c_fSunriseOffset = 0.27f;
constexpr float c_fSunriseOffset = 0.265f;
constexpr float c_fSunsetOffset = 0.23f;

if (time_of_day <= c_fSunriseTime)
Expand Down Expand Up @@ -57,7 +57,7 @@ DirectX::XMVECTOR DynamicSun::GetRotationQuat(float time_of_day)
else
{
const float v_range_start = time_of_day - 0.5f;
const float v_range_end = 1.0f - 0.77f;
const float v_range_end = 1.0f - 0.75f;

const float v_distance = 1.0f - std::fmaxf((v_range_end - v_range_start) / v_range_end, 0.0f);
v_lerp_factor = 0.5f + (v_distance / 2.0f);
Expand All @@ -70,6 +70,17 @@ DirectX::XMVECTOR DynamicSun::GetRotationQuat(float time_of_day)
return ComputeQuatFromAngle(v_rotation_radians);
}

const static DirectX::FXMVECTOR v_right_dir = { 1.0f, 0.0f, 0.0f, 0.0f };
const static DirectX::FXMVECTOR v_up_dir = { 0.0f, 1.0f, 0.0f, 0.0f };

void DynamicSun::UpdateLightDirection(DirectX::FXMVECTOR& dir, DirectX::FXMVECTOR& quat, float intensity)
{
DirectX::FXMVECTOR v_shadow_dir = DirectX::XMVectorScale(
DirectX::XMVector3Normalize(DirectX::XMVector3Rotate(dir, quat)), intensity);

DirectX::XMStoreFloat3(&DynamicSun::GetLightDirection(), v_shadow_dir);
}

__int64 DynamicSun::Update(void* device)
{
const float delta_time = ms_deltaTimeTimer.Update();
Expand All @@ -80,14 +91,21 @@ __int64 DynamicSun::Update(void* device)
if (v_game_instance)
{
const float v_light_intensity = GetLightIntensity(v_game_instance->time_of_day);
DynamicSun::UpdateLightDirection(v_right_dir, GetRotationQuat(v_game_instance->time_of_day), v_light_intensity);
}
else
{
ms_fMainMenuTime = std::fmod(ms_fMainMenuTime + delta_time * 0.015625f, 1.0f);

const float v_rot = lerp_float(0.0f, DirectX::XM_2PI, ms_fMainMenuTime);

DirectX::FXMVECTOR v_rot_quat = GetRotationQuat(v_game_instance->time_of_day);
DirectX::FXMVECTOR v_shadow_dir = DirectX::XMVectorScale(
DirectX::XMVector3Normalize(
DirectX::XMVector3Rotate({ 1.0f, 0.0f, 0.0f, 0.0f }, v_rot_quat)
), v_light_intensity);
DirectX::FXMVECTOR v_right_rotation = DirectX::XMQuaternionRotationAxis(
{ -1.0f, 0.0f, 0.0f, 0.0f }, DirectX::XMConvertToRadians(70.0f) - std::abs(ms_fSunAngle));
DirectX::FXMVECTOR v_roll_rotation = DirectX::XMQuaternionRotationAxis(
{ 0.0f, 0.0f, 1.0f, 0.0f }, v_rot);
DirectX::FXMVECTOR v_quat = DirectX::XMQuaternionMultiply(v_right_rotation, v_roll_rotation);

DirectX::XMStoreFloat3(&v_curLightDir, v_shadow_dir);
DynamicSun::UpdateLightDirection(v_up_dir, v_quat, 1.0f);
}

return DynamicSun::o_PresentFunction(device);
Expand Down
2 changes: 2 additions & 0 deletions Code/DynamicSun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ class DynamicSun
static DirectX::XMFLOAT3& GetLightDirection();

static DirectX::XMVECTOR GetRotationQuat(float time_of_day);
static void UpdateLightDirection(DirectX::FXMVECTOR& dir, DirectX::FXMVECTOR& quat, float intensity);
static __int64 Update(void* device);

inline static float ms_fSunAngle = 0.32f;

private:
inline static Timer ms_deltaTimeTimer;
inline static float ms_fLightIntensity = 0.0f;
inline static float ms_fMainMenuTime = 0.5f;
};
9 changes: 7 additions & 2 deletions Code/GraphicsOptionsMenu.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "GraphicsOptionsMenu.hpp"

#include "OptionsItemSlider.hpp"
#include "GameInstanceData.hpp"
#include "GameSettings.hpp"
#include "DynamicSun.hpp"


#include "Utils/Console.hpp"
#include "Utils/Memory.hpp"

Expand Down Expand Up @@ -55,7 +55,12 @@ __int64 GraphicsOptionsMenu::h_CreateWidgets(GraphicsOptionsMenu* self)
OptionsItemSlider* v_new_slider = new OptionsItemSlider(
v_new_option, "Sun Angle", "SunAngle", v_min_sun_angle, v_max_sun_angle, 20,
[](std::size_t value) -> void {
DynamicSun::ms_fSunAngle = *reinterpret_cast<float*>(&value);
const float v_flt_val = *reinterpret_cast<float*>(&value);

if (GameInstanceData::GetInstance())
DynamicSun::ms_fSunAngle = v_flt_val;
else
DynamicSun::ms_fSunAngle = std::abs(v_flt_val);
}
);

Expand Down
7 changes: 3 additions & 4 deletions Code/GraphicsOptionsMenu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ static_assert(sizeof(OptionsSubMenuBase) == 0x168, "OptionsSubMenuBase: Incorrec

struct GraphicsOptionsMenu;

template<typename T>
struct CustomHasher
struct VoidHasher
{
inline std::size_t operator()(const T* ptr) const noexcept
inline std::size_t operator()(const void* ptr) const noexcept
{
return reinterpret_cast<std::size_t>(ptr);
}
Expand All @@ -45,7 +44,7 @@ struct GraphicsOptionsMenu : public OptionsSubMenuBase
using Destructor = void (*)(GraphicsOptionsMenu*, char);

inline static std::unordered_map<const GraphicsOptionsMenu*,
std::vector<OptionsItemBase*>, CustomHasher<void>> ms_customItems = {};
std::vector<OptionsItemBase*>, VoidHasher> ms_customItems = {};

/* 0x0168 */ MyGUI::Button* some_button;
/* 0x0170 */ std::shared_ptr<OptionsItemDropDown> shader_quality_dropdown;
Expand Down

0 comments on commit 3d1cc64

Please sign in to comment.