From bf51b4ae1d10b2065200f4ae33b9abdde0e6baed Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Wed, 28 Apr 2021 22:41:16 +0800 Subject: [PATCH] Fix sky pass and time tweening issues --- src/animation/frame-scheduler.cpp | 10 +++++----- src/game/bootloader.cpp | 10 ++++++---- src/renderer/passes/material-pass.cpp | 2 +- src/renderer/passes/sky-pass.cpp | 15 ++++++++++++--- src/renderer/passes/sky-pass.hpp | 4 ++-- 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/animation/frame-scheduler.cpp b/src/animation/frame-scheduler.cpp index 291eab0..42140f8 100644 --- a/src/animation/frame-scheduler.cpp +++ b/src/animation/frame-scheduler.cpp @@ -67,6 +67,10 @@ void frame_scheduler::reset() void frame_scheduler::tick() { + frame_end = std::chrono::high_resolution_clock::now(); + frame_duration = static_cast(std::chrono::duration_cast(frame_end - frame_start).count()) / 1000000.0; + frame_start = frame_end; + accumulator += std::min(max_frame_duration, frame_duration); while (accumulator >= update_timestep) @@ -75,10 +79,6 @@ void frame_scheduler::tick() elapsed_time += update_timestep; accumulator -= update_timestep; } - + render_callback(accumulator * update_rate); - - frame_end = std::chrono::high_resolution_clock::now(); - frame_duration = static_cast(std::chrono::duration_cast(frame_end - frame_start).count()) / 1000000.0; - frame_start = frame_end; } diff --git a/src/game/bootloader.cpp b/src/game/bootloader.cpp index 5ff77af..2f89992 100644 --- a/src/game/bootloader.cpp +++ b/src/game/bootloader.cpp @@ -1236,14 +1236,17 @@ void setup_callbacks(game_context* ctx) ( [ctx](double t, double dt) { - (*ctx->time_tween)[1] = t; - + // Update tweens + ctx->time_tween->update(); ctx->overworld_sky_pass->update_tweens(); ctx->overworld_scene->update_tweens(); ctx->underworld_scene->update_tweens(); ctx->ui_scene->update_tweens(); ctx->focal_point_tween->update(); ctx->underworld_final_pass->get_material()->update_tweens(); + + // Set time tween time + (*ctx->time_tween)[1] = t; ctx->timeline->advance(dt); @@ -1286,8 +1289,7 @@ void setup_callbacks(game_context* ctx) ctx->menu_controls->update(); ctx->camera_controls->update(); - // Update tweens - ctx->time_tween->update(); + } ); diff --git a/src/renderer/passes/material-pass.cpp b/src/renderer/passes/material-pass.cpp index 9b84278..0cddda5 100644 --- a/src/renderer/passes/material-pass.cpp +++ b/src/renderer/passes/material-pass.cpp @@ -122,7 +122,7 @@ void material_pass::render(render_context* context) const float2 resolution = {static_cast(std::get<0>(viewport)), static_cast(std::get<1>(viewport))}; - float time = (time_tween) ? time_tween->interpolate(context->alpha) : 0.0f; + float time = time_tween->interpolate(context->alpha); const float3& camera_position = context->camera_transform.translation; float3 focal_point = (focal_point_tween) ? focal_point_tween->interpolate(context->alpha) : float3{0, 0, 0}; float4x4 view = context->camera->get_view_tween().interpolate(context->alpha); diff --git a/src/renderer/passes/sky-pass.cpp b/src/renderer/passes/sky-pass.cpp index e41300c..c0ea2fe 100644 --- a/src/renderer/passes/sky-pass.cpp +++ b/src/renderer/passes/sky-pass.cpp @@ -36,9 +36,9 @@ #include "renderer/material.hpp" #include "scene/camera.hpp" #include "utility/fundamental-types.hpp" +#include "math/interpolation.hpp" #include #include -#include sky_pass::sky_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffer, resource_manager* resource_manager): render_pass(rasterizer, framebuffer), @@ -52,7 +52,16 @@ sky_pass::sky_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffe moon_model_vao(nullptr), moon_shader_program(nullptr), blue_noise_map(nullptr), - observer_location{0.0f, 0.0f, 0.0f} + observer_location{0.0f, 0.0f, 0.0f}, + time_tween(nullptr), + time_of_day_tween(0.0, math::lerp), + julian_day_tween(0.0, math::lerp), + sun_position_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp), + sun_az_el_tween(float2{0.0f, 0.0f}, math::lerp), + moon_position_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp), + moon_az_el_tween(float2{0.0f, 0.0f}, math::lerp), + horizon_color_tween(float3{0.0f, 0.0f, 0.0f}, math::lerp), + zenith_color_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp) {} sky_pass::~sky_pass() @@ -71,7 +80,7 @@ void sky_pass::render(render_context* context) const auto viewport = framebuffer->get_dimensions(); rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport)); - float time = (time_tween) ? time_tween->interpolate(context->alpha) : 0.0f; + float time = time_tween->interpolate(context->alpha); float2 resolution = {static_cast(std::get<0>(viewport)), static_cast(std::get<1>(viewport))}; const scene::camera& camera = *context->camera; diff --git a/src/renderer/passes/sky-pass.hpp b/src/renderer/passes/sky-pass.hpp index 3cbcd3b..c8add96 100644 --- a/src/renderer/passes/sky-pass.hpp +++ b/src/renderer/passes/sky-pass.hpp @@ -111,10 +111,10 @@ private: const gl::texture_2d* blue_noise_map; float2 mouse_position; - const tween* time_tween; float3 observer_location; - tween time_of_day_tween; + const tween* time_tween; + tween time_of_day_tween; tween julian_day_tween; tween sun_position_tween; tween sun_az_el_tween;