1 #include <msp/geometry/hypersphere.h>
2 #include <msp/geometry/ray.h>
15 Sky::Sky(Renderable &r, Light &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")),
29 shdata.uniform("n_steps", 50);
31 set_planet(Planet::earth());
32 set_view_height(5.0f);
35 void Sky::set_planet(const 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);
51 void Sky::set_view_height(float h)
54 shdata.uniform("view_height", h);
57 Color Sky::get_transmittance(const Vector3 &look_dir)
59 Vector3 pos(0.0f, 0.0f, view_height);
60 Vector3 planet_center(0.0f, 0.0f, -planet.planet_radius);
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))
68 Geometry::HyperSphere<float, 3> space(planet.planet_radius+planet.atmosphere_thickness);
69 if(!space.get_intersections(ray, &intersection, 1))
72 float step_size = intersection.distance/50;
74 Color path_extinction(0.0f);
75 for(unsigned i=0; i<50; ++i)
77 Vector3 from_center = pos-planet_center;
78 float height = from_center.norm()-planet.planet_radius;
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);
85 path_extinction = path_extinction+(rayleigh_scatter+mie_scatter+mie_absorb+ozone_absorb)*step_size;
86 pos += look_dir*step_size;
89 return Color(exp(-path_extinction.r), exp(-path_extinction.g), exp(-path_extinction.b));
92 void Sky::setup_frame(Renderer &renderer)
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))));
103 Renderer::Push push(renderer);
105 if(transmittance_lookup_dirty)
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);
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);
118 renderable.setup_frame(renderer);
121 void Sky::finish_frame()
126 renderable.finish_frame();
130 void Sky::render(Renderer &renderer, Tag tag) const
132 renderable.render(renderer, tag);
134 Renderer::Push push(renderer);
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);
141 void Sky::set_debug_name(const string &name)
144 transmittance_lookup.set_debug_name(name+" [RT:transmittance]");
145 distant.set_debug_name(name+" [RT:distant]");
146 shdata.set_debug_name(name+" [UBO]");
153 Sky::Planet::Planet():
154 rayleigh_scatter(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),
166 Sky::Planet Sky::Planet::earth()
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;