]> git.tdb.fi Git - libs/gl.git/blob - demos/desertpillars/source/desertpillars.cpp
Create a Device class to hold the graphics context
[libs/gl.git] / demos / desertpillars / source / desertpillars.cpp
1 #include <msp/fs/dir.h>
2 #include <msp/gl/directionallight.h>
3 #include <msp/gl/lighting.h>
4 #include <msp/gl/sequencebuilder.h>
5 #include <msp/gl/pointlight.h>
6 #include <msp/gl/renderer.h>
7 #include <msp/gl/tests.h>
8 #include <msp/input/keys.h>
9 #include <msp/time/utils.h>
10 #include "desertpillars.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 DesertPillars::Options::Options()
16 {
17         wnd_opts.width = 1920;
18         wnd_opts.height = 1080;
19         gl_opts.gl_version_major = Graphics::GLOptions::LATEST_VERSION;
20         gl_opts.core_profile = true;
21 }
22
23
24 DesertPillars::DesertPillars(int, char **):
25         window(display, opts.wnd_opts),
26         gl_device(window, opts.gl_opts),
27         keyboard(window),
28         resources(&res_mgr),
29         view(window),
30         camera(resources.get<GL::Camera>("Camera.camera")),
31         lighting(resources.get<GL::Lighting>("Desert.lightn")),
32         sphere(resources.get<GL::Object>("Sphere.object")),
33         sphere_morph(0.0f),
34         sphere_frozen(false),
35         sphere_stopped(false),
36         sun(resources.get<GL::DirectionalLight>("Sun.light")),
37         sun_angle(Geometry::Angle<float>::from_turns(0.1f)),
38         wisp(resources.get<GL::PointLight>("Wisp.light")),
39         flare(resources.get<GL::Object>("Flare.object"), lighting.find_light_index(wisp)),
40         camera_stopped(false)
41 {
42         window.set_title("Desert Pillars");
43         window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &DesertPillars::exit), 0));
44         keyboard.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &DesertPillars::key_press), false));
45
46         GL::SequenceBuilder seq_bld(resources.get<GL::SequenceTemplate>("Desert.seq"));
47         seq_bld.set_renderable("content", content);
48         seq_bld.set_debug_name("Main sequence");
49         sequence.reset(seq_bld.build(view));
50
51         GL::SequenceBuilder env_bld(resources.get<GL::SequenceTemplate>("Desert_environment.seq"));
52         env_bld.set_renderable("content", *sequence->get_steps().front().get_renderable());
53         env_bld.set_debug_name("Environment sequence");
54         env_seq.reset(env_bld.build());
55
56         env_map = make_unique<GL::EnvironmentMap>(256, GL::RGB16F, 7, sphere, *env_seq);
57         env_map->set_debug_name("Environment map");
58         sphere.set_matrix(GL::Matrix::translation(GL::Vector3(0.0f, 0.0f, 3.3f)));
59
60         content.add(resources.get<GL::Scene>("Background.scene"));
61         content.add(*env_map);
62         content.add(flare);
63
64         camera.set_debug_name("Main camera");
65
66         view.set_content(sequence.get());
67         view.set_camera(&camera);
68
69         const GL::Vector3 &sun_direction = sun.get_direction();
70         sun_node = normalize(GL::Vector3(sun_direction.y, -sun_direction.x, 0.0f));
71         sun_axis = normalize(cross(sun_direction, sun_node));
72
73         wisp_base_color = wisp.get_color();
74
75         const GL::Vector3 &cam_pos = camera.get_position();
76         camera_distance = cam_pos.norm();
77         camera_angle = Geometry::atan2(cam_pos.y, cam_pos.x);
78         camera_base_height = Geometry::atan2(cam_pos.z, cam_pos.slice<2>(0).norm());
79 }
80
81 int DesertPillars::main()
82 {
83         window.show();
84         return Application::main();
85 }
86
87 void DesertPillars::tick()
88 {
89         Time::TimeStamp t = Time::now();
90         Time::TimeDelta dt = (last_tick ? t-last_tick : Time::zero);
91         last_tick = t;
92
93         if(!sphere_frozen)
94         {
95                 sphere_morph += dt/Time::sec;
96                 sphere.set_morph(sphere_morph);
97         }
98
99         if(!sphere_stopped)
100         {
101                 sphere_angle += Geometry::Angle<float>::from_degrees(36*dt/Time::sec);
102                 sphere.set_matrix(GL::Matrix().translate(GL::Vector3(0.0f, 0.0f, 3.3f))
103                         .rotate(sphere_angle, GL::Vector3(0.0f, 0.0f, 1.0f)).rotate(sphere_angle*0.5f, GL::Vector3(1.0f, 0.0f, 0.0f)));
104         }
105
106         if(!camera_stopped)
107         {
108                 camera_angle += Geometry::Angle<float>::from_degrees(12*dt/Time::sec);
109                 Geometry::Angle<float> cam_height = camera_base_height+Geometry::Angle<float>::from_degrees(15.0f*cos(camera_angle*1.5f));
110                 GL::Vector3 cam_dir(cos(camera_angle)*cos(cam_height), sin(camera_angle)*cos(cam_height), sin(cam_height));
111                 camera.set_position(GL::Vector3(0.0f, 0.0f, 2.0f)+cam_dir*camera_distance);
112                 camera.set_look_direction(-cam_dir);
113         }
114
115         sun_angle += Geometry::Angle<float>::from_degrees(4*dt/Time::sec);
116         sun.set_direction(GL::Matrix::rotation(sun_angle, sun_axis)*-sun_node);
117
118         wisp_angle += Geometry::Angle<float>::from_degrees(6*dt/Time::sec);
119         float r = 3.1f+1.1f*cos(4.0f*wisp_angle);
120         GL::Vector3 p(cos(wisp_angle)*r, sin(wisp_angle)*r, 3.6f+0.4f*sin(1.6f*wisp_angle));
121         wisp.set_position(p);
122         float twilight = 0.8f-min(max(sin(sun_angle), -0.05f), 0.2f)*4.0f;
123         wisp.set_color(wisp_base_color*((3.0f-2.0f*twilight)*twilight*twilight));
124         flare.set_matrix(GL::Matrix::translation(p));
125
126         display.tick();
127         res_mgr.tick();
128         view.render();
129 }
130
131 void DesertPillars::key_press(unsigned key)
132 {
133         if(key==Input::KEY_ESC)
134                 exit(0);
135         else if(key==Input::KEY_SPACE)
136                 camera_stopped = !camera_stopped;
137         else if(key==Input::KEY_F)
138                 sphere_frozen = !sphere_frozen;
139         else if(key==Input::KEY_S)
140                 sphere_stopped = !sphere_stopped;
141 }
142
143
144 DesertPillars::Resources::Resources(GL::ResourceManager *rm)
145 {
146         FS::Path base_dir = FS::get_sys_data_dir()/"demos"/"desertpillars"/"data";
147         source.add_directory(base_dir);
148         source.add_directory(base_dir/"textures");
149         source.add_directory(base_dir/"exported");
150         add_source(source);
151
152         set_resource_manager(rm);
153 }
154
155
156 void DesertPillars::MorphSphere::set_morph(float m)
157 {
158         shdata.uniform("morph", m);
159 }
160
161 void DesertPillars::MorphSphere::setup_render(GL::Renderer &renderer, GL::Tag tag) const
162 {
163         ObjectInstance::setup_render(renderer, tag);
164         renderer.add_shader_data(shdata);
165 }
166
167
168 DesertPillars::LightFlare::LightFlare(const GL::Object &o, unsigned i):
169         ObjectInstance(o)
170 {
171         shdata.uniform("flare_light_index", static_cast<int>(i));
172 }
173
174 void DesertPillars::LightFlare::setup_render(GL::Renderer &renderer, GL::Tag tag) const
175 {
176         ObjectInstance::setup_render(renderer, tag);
177         renderer.add_shader_data(shdata);
178 }