]> git.tdb.fi Git - libs/gl.git/blobdiff - demos/desertpillars/source/desertpillars.cpp
Add a day-night cycle to the desertpillars demo
[libs/gl.git] / demos / desertpillars / source / desertpillars.cpp
index 5f3b29b1375fdfd6c99283fbd60779e9724bb655..513d81743d700dec32bc734eea0ff63357ea4a49 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/fs/dir.h>
+#include <msp/gl/directionallight.h>
 #include <msp/gl/sequencebuilder.h>
 #include <msp/gl/renderer.h>
 #include <msp/gl/tests.h>
@@ -24,46 +25,64 @@ DesertPillars::DesertPillars(int, char **):
        keyboard(window),
        view(window, gl_ctx),
        camera(resources.get<GL::Camera>("Camera.camera")),
-       sphere(resources.get<GL::Object>("sphere_phong.object")),
+       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)),
        camera_stopped(false)
 {
        window.set_title("Desert Pillars");
        window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &DesertPillars::exit), 0));
        keyboard.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &DesertPillars::key_press), false));
 
-       const GL::Light &sun = resources.get<GL::Light>("Sun.light");
-       sky = make_unique<GL::Sky>(content, sun);
+       env_seq = make_unique<GL::Sequence>();
+       env_seq->set_clear_enabled(true);
+       env_seq->set_debug_name("Environment sequence");
 
-       unsigned shadow_size = 8192;
-       shadow_seq = make_unique<GL::Sequence>(shadow_size, shadow_size);
+       global_env = make_unique<GL::EnvironmentMap>(32, GL::RGB16F, 2, content, *env_seq);
+       global_env->set_fixed_position(GL::Vector3(0.0f, 0.0f, 0.0f));
+       global_env->set_debug_name("Global environment");
+
+       sky = make_unique<GL::Sky>(*global_env, sun);
+       sky->set_debug_name("Sky");
+
+       shadow_seq = make_unique<GL::Sequence>();
+       shadow_seq->set_clear_enabled(true);
+       shadow_seq->set_debug_name("Shadow sequence");
        GL::Sequence::Step *step = &shadow_seq->add_step("shadow", content);
-       step->set_depth_test(&GL::DepthTest::lequal());
+       step->set_depth_test(GL::LEQUAL);
 
-       shadow_map = make_unique<GL::ShadowMap>(shadow_size, *sky, sun, *shadow_seq);
+       shadow_map = make_unique<GL::ShadowMap>(8192, *sky, sun, *shadow_seq);
+       shadow_map->set_debug_name("Shadow map");
        shadow_map->set_target(GL::Vector3(0.0f, 0.0f, 0.0f), 20.0f);
 
        GL::SequenceBuilder seq_bld(resources.get<GL::SequenceTemplate>("Desert.seq"));
        seq_bld.set_renderable("content", *shadow_map);
+       seq_bld.set_debug_name("Main sequence");
        sequence.reset(seq_bld.build(view));
 
-       unsigned env_size = 256;
-       env_seq = make_unique<GL::Sequence>(env_size, env_size);
        step = &env_seq->add_step("", *shadow_map);
        step->set_lighting(&resources.get<GL::Lighting>("Desert.lightn"));
-       step->set_depth_test(&GL::DepthTest::lequal());
+       step->set_depth_test(GL::LEQUAL);
 
-       env_map = make_unique<GL::EnvironmentMap>(256, GL::RGB16F, sphere, *env_seq);
+       env_map = make_unique<GL::EnvironmentMap>(256, GL::RGB16F, 7, sphere, *env_seq);
+       env_map->set_debug_name("Environment map");
        sphere.set_matrix(GL::Matrix::translation(GL::Vector3(0.0f, 0.0f, 3.3f)));
 
        content.add(resources.get<GL::Scene>("Background.scene"));
        content.add(*env_map);
 
+       camera.set_debug_name("Main camera");
+
        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);
@@ -104,6 +123,9 @@ void DesertPillars::tick()
                camera.set_look_direction(-cam_dir);
        }
 
+       sun_angle += Geometry::Angle<float>::from_degrees(4*dt/Time::sec);
+       sun.set_direction(GL::Matrix::rotation(sun_angle, sun_axis)*-sun_node);
+
        display.tick();
        view.render();
 }