]> git.tdb.fi Git - libs/gl.git/blobdiff - demos/desertpillars/source/desertpillars.cpp
Add a moving point light to the night phase of desertpillars
[libs/gl.git] / demos / desertpillars / source / desertpillars.cpp
index 513d81743d700dec32bc734eea0ff63357ea4a49..432c91663dbb4b7b53446a66b00be9ba2e391c0d 100644 (file)
@@ -1,6 +1,8 @@
 #include <msp/fs/dir.h>
 #include <msp/gl/directionallight.h>
+#include <msp/gl/lighting.h>
 #include <msp/gl/sequencebuilder.h>
+#include <msp/gl/pointlight.h>
 #include <msp/gl/renderer.h>
 #include <msp/gl/tests.h>
 #include <msp/input/keys.h>
@@ -25,12 +27,15 @@ DesertPillars::DesertPillars(int, char **):
        keyboard(window),
        view(window, gl_ctx),
        camera(resources.get<GL::Camera>("Camera.camera")),
+       lighting(resources.get<GL::Lighting>("Desert.lightn")),
        sphere(resources.get<GL::Object>("Sphere.object")),
        sphere_morph(0.0f),
        sphere_frozen(false),
        sphere_stopped(false),
        sun(resources.get<GL::DirectionalLight>("Sun.light")),
        sun_angle(Geometry::Angle<float>::from_turns(0.1f)),
+       wisp(resources.get<GL::PointLight>("Wisp.light")),
+       flare(resources.get<GL::Object>("Flare.object"), lighting.find_light_index(wisp)),
        camera_stopped(false)
 {
        window.set_title("Desert Pillars");
@@ -54,8 +59,16 @@ DesertPillars::DesertPillars(int, char **):
        GL::Sequence::Step *step = &shadow_seq->add_step("shadow", content);
        step->set_depth_test(GL::LEQUAL);
 
-       shadow_map = make_unique<GL::ShadowMap>(8192, *sky, sun, *shadow_seq);
+       shadow_seq_thsm = make_unique<GL::Sequence>();
+       shadow_seq_thsm->set_clear_enabled(true);
+       shadow_seq_thsm->set_debug_name("Shadow sequence");
+       step = &shadow_seq_thsm->add_step("shadow_thsm", content);
+       step->set_depth_test(GL::LEQUAL);
+
+       shadow_map = make_unique<GL::ShadowMap>(6144, 4096, *sky, lighting);
        shadow_map->set_debug_name("Shadow map");
+       shadow_map->add_light(sun, 4096, *shadow_seq);
+       shadow_map->add_light(resources.get<GL::PointLight>("Wisp.light"), 2048, *shadow_seq_thsm);
        shadow_map->set_target(GL::Vector3(0.0f, 0.0f, 0.0f), 20.0f);
 
        GL::SequenceBuilder seq_bld(resources.get<GL::SequenceTemplate>("Desert.seq"));
@@ -64,7 +77,7 @@ DesertPillars::DesertPillars(int, char **):
        sequence.reset(seq_bld.build(view));
 
        step = &env_seq->add_step("", *shadow_map);
-       step->set_lighting(&resources.get<GL::Lighting>("Desert.lightn"));
+       step->set_lighting(&lighting);
        step->set_depth_test(GL::LEQUAL);
 
        env_map = make_unique<GL::EnvironmentMap>(256, GL::RGB16F, 7, sphere, *env_seq);
@@ -73,6 +86,7 @@ DesertPillars::DesertPillars(int, char **):
 
        content.add(resources.get<GL::Scene>("Background.scene"));
        content.add(*env_map);
+       content.add(flare);
 
        camera.set_debug_name("Main camera");
 
@@ -83,6 +97,8 @@ DesertPillars::DesertPillars(int, char **):
        sun_node = normalize(GL::Vector3(sun_direction.y, -sun_direction.x, 0.0f));
        sun_axis = normalize(cross(sun_direction, sun_node));
 
+       wisp_base_color = wisp.get_color();
+
        const GL::Vector3 &cam_pos = camera.get_position();
        camera_distance = cam_pos.norm();
        camera_angle = Geometry::atan2(cam_pos.y, cam_pos.x);
@@ -126,6 +142,14 @@ void DesertPillars::tick()
        sun_angle += Geometry::Angle<float>::from_degrees(4*dt/Time::sec);
        sun.set_direction(GL::Matrix::rotation(sun_angle, sun_axis)*-sun_node);
 
+       wisp_angle += Geometry::Angle<float>::from_degrees(6*dt/Time::sec);
+       float r = 3.1f+1.1f*cos(4.0f*wisp_angle);
+       GL::Vector3 p(cos(wisp_angle)*r, sin(wisp_angle)*r, 3.6f+0.4f*sin(1.6f*wisp_angle));
+       wisp.set_position(p);
+       float twilight = 0.8f-min(max(sin(sun_angle), -0.05f), 0.2f)*4.0f;
+       wisp.set_color(wisp_base_color*((3.0f-2.0f*twilight)*twilight*twilight));
+       flare.set_matrix(GL::Matrix::translation(p));
+
        display.tick();
        view.render();
 }
@@ -163,3 +187,16 @@ void DesertPillars::MorphSphere::setup_render(GL::Renderer &renderer, GL::Tag ta
        ObjectInstance::setup_render(renderer, tag);
        renderer.add_shader_data(shdata);
 }
+
+
+DesertPillars::LightFlare::LightFlare(const GL::Object &o, unsigned i):
+       ObjectInstance(o)
+{
+       shdata.uniform("flare_light_index", static_cast<int>(i));
+}
+
+void DesertPillars::LightFlare::setup_render(GL::Renderer &renderer, GL::Tag tag) const
+{
+       ObjectInstance::setup_render(renderer, tag);
+       renderer.add_shader_data(shdata);
+}