From be3b67fdd0d0420ffc1e97a6535159c63a97fdc8 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 16 Oct 2021 18:53:53 +0300 Subject: [PATCH] Add a day-night cycle to the desertpillars demo --- demos/desertpillars/source/desertpillars.cpp | 10 +++++++++- demos/desertpillars/source/desertpillars.h | 5 +++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/demos/desertpillars/source/desertpillars.cpp b/demos/desertpillars/source/desertpillars.cpp index 84583551..513d8174 100644 --- a/demos/desertpillars/source/desertpillars.cpp +++ b/demos/desertpillars/source/desertpillars.cpp @@ -29,6 +29,8 @@ DesertPillars::DesertPillars(int, char **): sphere_morph(0.0f), sphere_frozen(false), sphere_stopped(false), + sun(resources.get("Sun.light")), + sun_angle(Geometry::Angle::from_turns(0.1f)), camera_stopped(false) { window.set_title("Desert Pillars"); @@ -43,7 +45,6 @@ DesertPillars::DesertPillars(int, char **): global_env->set_fixed_position(GL::Vector3(0.0f, 0.0f, 0.0f)); global_env->set_debug_name("Global environment"); - GL::DirectionalLight &sun = resources.get("Sun.light"); sky = make_unique(*global_env, sun); sky->set_debug_name("Sky"); @@ -78,6 +79,10 @@ DesertPillars::DesertPillars(int, char **): view.set_content(sequence.get()); view.set_camera(&camera); + const GL::Vector3 &sun_direction = sun.get_direction(); + sun_node = normalize(GL::Vector3(sun_direction.y, -sun_direction.x, 0.0f)); + sun_axis = normalize(cross(sun_direction, sun_node)); + const GL::Vector3 &cam_pos = camera.get_position(); camera_distance = cam_pos.norm(); camera_angle = Geometry::atan2(cam_pos.y, cam_pos.x); @@ -118,6 +123,9 @@ void DesertPillars::tick() camera.set_look_direction(-cam_dir); } + sun_angle += Geometry::Angle::from_degrees(4*dt/Time::sec); + sun.set_direction(GL::Matrix::rotation(sun_angle, sun_axis)*-sun_node); + display.tick(); view.render(); } diff --git a/demos/desertpillars/source/desertpillars.h b/demos/desertpillars/source/desertpillars.h index c6242ad3..41707381 100644 --- a/demos/desertpillars/source/desertpillars.h +++ b/demos/desertpillars/source/desertpillars.h @@ -78,6 +78,11 @@ private: bool sphere_frozen; bool sphere_stopped; + Msp::GL::DirectionalLight &sun; + Msp::GL::Vector3 sun_node; + Msp::GL::Vector3 sun_axis; + Msp::Geometry::Angle sun_angle; + float camera_distance; Msp::Geometry::Angle camera_base_height; Msp::Geometry::Angle camera_angle; -- 2.43.0