]> git.tdb.fi Git - libs/gl.git/blobdiff - demos/desertpillars/source/desertpillars.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / demos / desertpillars / source / desertpillars.cpp
index c138d65a6cdb428718212ebb6e2d112ebc04afea..bfb25f30c6b5c3564dee3746955ace47ddf8bf51 100644 (file)
@@ -1,7 +1,9 @@
 #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>
 #include <msp/time/utils.h>
 #include "desertpillars.h"
@@ -13,58 +15,60 @@ DesertPillars::Options::Options()
 {
        wnd_opts.width = 1920;
        wnd_opts.height = 1080;
-       gl_opts.gl_version_major = Graphics::GLOptions::LATEST_VERSION;
-       gl_opts.core_profile = true;
 }
 
 
 DesertPillars::DesertPillars(int, char **):
        window(display, opts.wnd_opts),
-       gl_ctx(window, opts.gl_opts),
+       gl_device(window),
        keyboard(window),
-       view(window, gl_ctx),
-       camera(resources.get<GL::Camera>("Camera.camera")),
-       sphere(resources.get<GL::Object>("sphere_phong.object")),
+       resources(&res_mgr),
+       view(window),
+       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");
        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>(resources, content, sun);
-
-       unsigned shadow_size = 8192;
-       shadow_seq = make_unique<GL::Sequence>(shadow_size, shadow_size);
-       GL::Sequence::Step *step = &shadow_seq->add_step("shadow", content);
-       step->set_depth_test(&GL::DepthTest::lequal());
-
-       shadow_map = make_unique<GL::ShadowMap>(resources, shadow_size, *sky, sun, *shadow_seq);
-       shadow_map->set_darkness(0.9f);
-       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_renderable("content", content);
+       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());
+       GL::SequenceBuilder env_bld(resources.get<GL::SequenceTemplate>("Desert_environment.seq"));
+       env_bld.set_renderable("content", *sequence->get_steps().front().get_renderable());
+       env_bld.set_debug_name("Environment sequence");
+       env_seq.reset(env_bld.build());
 
-       env_map = make_unique<GL::EnvironmentMap>(resources, 256, sphere, *env_seq);
+       env_map = make_unique<GL::EnvironmentMap>(256, GL::RGBA16F, 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);
+       content.add(flare);
+
+       camera.copy_parameters(resources.get<GL::Camera>("Camera.camera"));
+       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));
+
+       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);
@@ -105,7 +109,19 @@ 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);
+
+       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();
+       res_mgr.tick();
        view.render();
 }
 
@@ -122,13 +138,15 @@ void DesertPillars::key_press(unsigned key)
 }
 
 
-DesertPillars::Resources::Resources()
+DesertPillars::Resources::Resources(GL::ResourceManager *rm)
 {
        FS::Path base_dir = FS::get_sys_data_dir()/"demos"/"desertpillars"/"data";
        source.add_directory(base_dir);
        source.add_directory(base_dir/"textures");
        source.add_directory(base_dir/"exported");
        add_source(source);
+
+       set_resource_manager(rm);
 }
 
 
@@ -142,3 +160,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);
+}