]> git.tdb.fi Git - libs/gl.git/blob - source/effects/sky.cpp
Fix a bug in Renderer::resolve_multisample
[libs/gl.git] / source / effects / sky.cpp
1 #include <msp/geometry/hypersphere.h>
2 #include <msp/geometry/ray.h>
3 #include "light.h"
4 #include "mesh.h"
5 #include "renderer.h"
6 #include "resources.h"
7 #include "texture2d.h"
8 #include "sky.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 Sky::Sky(Renderable &r, Light &s):
16         Effect(r),
17         sun(s),
18         transmittance_lookup(128, 64, (COLOR_ATTACHMENT,RGB16F)),
19         transmittance_shprog(Resources::get_global().get<Program>("_sky_transmittance.glsl.shader")),
20         transmittance_lookup_dirty(true),
21         distant(256, 128, (COLOR_ATTACHMENT,RGB16F)),
22         distant_shprog(Resources::get_global().get<Program>("_sky_distant.glsl.shader")),
23         fullscreen_mesh(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
24         backdrop_shprog(Resources::get_global().get<Program>("_sky_backdrop.glsl.shader")),
25         sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
26         wrap_sampler(Resources::get_global().get<Sampler>("_linear_clamp_v.samp")),
27         rendered(false)
28 {
29         shdata.uniform("n_steps", 50);
30
31         set_planet(Planet::earth());
32         set_view_height(5.0f);
33 }
34
35 void Sky::set_planet(const Planet &p)
36 {
37         planet = p;
38         shdata.uniform("events.rayleigh_scatter", planet.rayleigh_scatter.r, planet.rayleigh_scatter.g, planet.rayleigh_scatter.b);
39         shdata.uniform("events.mie_scatter", planet.mie_scatter.r, planet.mie_scatter.g, planet.mie_scatter.b);
40         shdata.uniform("events.mie_absorb", planet.mie_absorb.r, planet.mie_absorb.g, planet.mie_absorb.b);
41         shdata.uniform("events.ozone_absorb", planet.ozone_absorb.r, planet.ozone_absorb.g, planet.ozone_absorb.b);
42         shdata.uniform("rayleigh_density_decay", planet.rayleigh_density_decay);
43         shdata.uniform("mie_density_decay", planet.mie_density_decay);
44         shdata.uniform("ozone_band_center", planet.ozone_band_center);
45         shdata.uniform("ozone_band_extent", planet.ozone_band_extent);
46         shdata.uniform("atmosphere_thickness", planet.atmosphere_thickness);
47         shdata.uniform("planet_radius", planet.planet_radius);
48         shdata.uniform("ground_albedo", planet.ground_albedo.r, planet.ground_albedo.g, planet.ground_albedo.b);
49 }
50
51 void Sky::set_view_height(float h)
52 {
53         view_height = h;
54         shdata.uniform("view_height", h);
55 }
56
57 Color Sky::get_transmittance(const Vector3 &look_dir)
58 {
59         Vector3 pos(0.0f, 0.0f, view_height);
60         Vector3 planet_center(0.0f, 0.0f, -planet.planet_radius);
61
62         Geometry::Ray<float, 3> ray(pos-planet_center, look_dir);
63         Geometry::HyperSphere<float, 3> surface(planet.planet_radius);
64         Geometry::SurfacePoint<float, 3> intersection;
65         if(surface.get_intersections(ray, &intersection, 1))
66                 return Color(0.0f);
67
68         Geometry::HyperSphere<float, 3> space(planet.planet_radius+planet.atmosphere_thickness);
69         if(!space.get_intersections(ray, &intersection, 1))
70                 return Color(1.0f);
71
72         float step_size = intersection.distance/50;
73
74         Color path_extinction(0.0f);
75         for(unsigned i=0; i<50; ++i)
76         {
77                 Vector3 from_center = pos-planet_center;
78                 float height = from_center.norm()-planet.planet_radius;
79
80                 Color rayleigh_scatter = planet.rayleigh_scatter*exp(height/planet.rayleigh_density_decay);
81                 Color mie_scatter = planet.mie_scatter*exp(height/planet.mie_density_decay);
82                 Color mie_absorb = planet.mie_absorb*exp(height/planet.mie_density_decay);
83                 Color ozone_absorb = planet.ozone_absorb*max(1.0f-abs(height-planet.ozone_band_center)/planet.ozone_band_extent, 0.0f);
84
85                 path_extinction = path_extinction+(rayleigh_scatter+mie_scatter+mie_absorb+ozone_absorb)*step_size;
86                 pos += look_dir*step_size;
87         }
88
89         return Color(exp(-path_extinction.r), exp(-path_extinction.g), exp(-path_extinction.b));
90 }
91
92 void Sky::setup_frame(Renderer &renderer)
93 {
94         if(rendered)
95                 return;
96
97         rendered = true;
98
99         shdata.uniform("light_color", sun.get_color());
100         shdata.uniform("light_dir", sun.get_position().slice<3>(0));
101         sun.set_transmittance(get_transmittance(normalize(sun.get_position().slice<3>(0))));
102
103         Renderer::Push push(renderer);
104
105         if(transmittance_lookup_dirty)
106         {
107                 transmittance_lookup_dirty = false;
108                 renderer.set_framebuffer(&transmittance_lookup.get_framebuffer());
109                 renderer.set_shader_program(&transmittance_shprog, &shdata);
110                 fullscreen_mesh.draw(renderer);
111         }
112
113         renderer.set_framebuffer(&distant.get_framebuffer());
114         renderer.set_shader_program(&distant_shprog, &shdata);
115         renderer.set_texture("transmittance_lookup", &transmittance_lookup.get_target_texture(0), &sampler);
116         fullscreen_mesh.draw(renderer);
117
118         renderable.setup_frame(renderer);
119 }
120
121 void Sky::finish_frame()
122 {
123         if(rendered)
124         {
125                 rendered = false;
126                 renderable.finish_frame();
127         }
128 }
129
130 void Sky::render(Renderer &renderer, Tag tag) const
131 {
132         renderable.render(renderer, tag);
133
134         Renderer::Push push(renderer);
135
136         renderer.set_shader_program(&backdrop_shprog, &shdata);
137         renderer.set_texture("distant", &distant.get_target_texture(0), &wrap_sampler);
138         fullscreen_mesh.draw(renderer);
139 }
140
141 void Sky::set_debug_name(const string &name)
142 {
143 #ifdef DEBUG
144         transmittance_lookup.set_debug_name(name+" [RT:transmittance]");
145         distant.set_debug_name(name+" [RT:distant]");
146         shdata.set_debug_name(name+" [UBO]");
147 #else
148         (void)name;
149 #endif
150 }
151
152
153 Sky::Planet::Planet():
154         rayleigh_scatter(0.0f),
155         mie_scatter(0.0f),
156         mie_absorb(0.0f),
157         ozone_absorb(0.0f),
158         rayleigh_density_decay(1e3f),
159         mie_density_decay(1e3f),
160         ozone_band_center(1e4f),
161         ozone_band_extent(1e2f),
162         atmosphere_thickness(2e4f),
163         planet_radius(1e6f)
164 { }
165
166 Sky::Planet Sky::Planet::earth()
167 {
168         Planet planet;
169         planet.rayleigh_scatter = Color(5.802e-6f, 13.558e-6f, 33.1e-6f);
170         planet.mie_scatter = Color(3.996e-6f, 3.996e-6f, 3.996e-6f);
171         planet.mie_absorb = Color(4.4e-6f, 4.4e-6f, 4.4e-6f);
172         planet.ozone_absorb = Color(0.65e-6f, 1.881e-6f, 0.085e-6f);
173         planet.rayleigh_density_decay = -8e3f;
174         planet.mie_density_decay = -1.2e3f;
175         planet.ozone_band_center = 25e3f;
176         planet.ozone_band_extent = 15e3f;
177         planet.atmosphere_thickness = 1e5f;
178         planet.planet_radius = 6.36e6f;
179         return planet;
180 }
181
182 } // namespace GL
183 } // namespace Msp