💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

659 lines
24 KiB

3 years ago
3 years ago
  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "render/passes/sky-pass.hpp"
  20. #include "resources/resource-manager.hpp"
  21. #include "resources/string-table.hpp"
  22. #include "gl/rasterizer.hpp"
  23. #include "gl/framebuffer.hpp"
  24. #include "gl/shader-program.hpp"
  25. #include "gl/shader-input.hpp"
  26. #include "gl/vertex-buffer.hpp"
  27. #include "gl/vertex-array.hpp"
  28. #include "gl/vertex-attribute.hpp"
  29. #include "gl/drawing-mode.hpp"
  30. #include "gl/texture-2d.hpp"
  31. #include "gl/texture-wrapping.hpp"
  32. #include "gl/texture-filter.hpp"
  33. #include "render/vertex-attribute.hpp"
  34. #include "render/context.hpp"
  35. #include "render/model.hpp"
  36. #include "render/material.hpp"
  37. #include "scene/camera.hpp"
  38. #include "utility/fundamental-types.hpp"
  39. #include "color/color.hpp"
  40. #include "math/interpolation.hpp"
  41. #include "geom/cartesian.hpp"
  42. #include "geom/spherical.hpp"
  43. #include "physics/orbit/orbit.hpp"
  44. #include "physics/light/photometry.hpp"
  45. #include <cmath>
  46. #include <stdexcept>
  47. #include <glad/glad.h>
  48. #include <iostream>
  49. namespace render {
  50. sky_pass::sky_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffer, resource_manager* resource_manager):
  51. pass(rasterizer, framebuffer),
  52. mouse_position({0.0f, 0.0f}),
  53. sky_model(nullptr),
  54. sky_material(nullptr),
  55. sky_model_vao(nullptr),
  56. sky_shader_program(nullptr),
  57. moon_model(nullptr),
  58. moon_model_vao(nullptr),
  59. moon_material(nullptr),
  60. moon_shader_program(nullptr),
  61. stars_model(nullptr),
  62. stars_model_vao(nullptr),
  63. star_material(nullptr),
  64. star_shader_program(nullptr),
  65. observer_position_tween({0, 0, 0}, math::lerp<float3, float>),
  66. sun_position_tween(float3{1.0f, 0.0f, 0.0f}, math::lerp<float3, float>),
  67. sun_luminance_tween(float3{0.0f, 0.0f, 0.0f}, math::lerp<float3, float>),
  68. sun_illuminance_tween(float3{0.0f, 0.0f, 0.0f}, math::lerp<float3, float>),
  69. icrf_to_eus_translation({0, 0, 0}, math::lerp<float3, float>),
  70. icrf_to_eus_rotation(math::quaternion<float>::identity(), math::nlerp<float>),
  71. moon_position_tween(float3{0, 0, 0}, math::lerp<float3, float>),
  72. moon_rotation_tween(math::quaternion<float>::identity(), math::nlerp<float>),
  73. moon_angular_radius_tween(0.0f, math::lerp<float, float>),
  74. moon_sunlight_direction_tween(float3{0, 0, 0}, math::lerp<float3, float>),
  75. moon_sunlight_illuminance_tween(float3{0, 0, 0}, math::lerp<float3, float>),
  76. moon_planetlight_direction_tween(float3{0, 0, 0}, math::lerp<float3, float>),
  77. moon_planetlight_illuminance_tween(float3{0, 0, 0}, math::lerp<float3, float>),
  78. moon_illuminance_tween(float3{0.0f, 0.0f, 0.0f}, math::lerp<float3, float>),
  79. render_transmittance_lut(false),
  80. magnification(1.0f)
  81. {
  82. // Build quad VBO and VAO
  83. const float quad_vertex_data[] =
  84. {
  85. -1.0f, 1.0f, 0.0f,
  86. -1.0f, -1.0f, 0.0f,
  87. 1.0f, 1.0f, 0.0f,
  88. 1.0f, 1.0f, 0.0f,
  89. -1.0f, -1.0f, 0.0f,
  90. 1.0f, -1.0f, 0.0f
  91. };
  92. std::size_t quad_vertex_size = 3;
  93. std::size_t quad_vertex_stride = sizeof(float) * quad_vertex_size;
  94. std::size_t quad_vertex_count = 6;
  95. quad_vbo = new gl::vertex_buffer(sizeof(float) * quad_vertex_size * quad_vertex_count, quad_vertex_data);
  96. quad_vao = new gl::vertex_array();
  97. gl::vertex_attribute quad_position_attribute;
  98. quad_position_attribute.buffer = quad_vbo;
  99. quad_position_attribute.offset = 0;
  100. quad_position_attribute.stride = quad_vertex_stride;
  101. quad_position_attribute.type = gl::vertex_attribute_type::float_32;
  102. quad_position_attribute.components = 3;
  103. quad_vao->bind(render::vertex_attribute::position, quad_position_attribute);
  104. // Create transmittance LUT texture and framebuffer (32F color, no depth)
  105. transmittance_lut_texture = new gl::texture_2d(256, 64, gl::pixel_type::float_32, gl::pixel_format::rgb);
  106. transmittance_lut_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  107. transmittance_lut_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  108. transmittance_lut_texture->set_max_anisotropy(0.0f);
  109. transmittance_lut_framebuffer = new gl::framebuffer({transmittance_lut_texture->get_width(), transmittance_lut_texture->get_height()});
  110. transmittance_lut_framebuffer->attach(gl::framebuffer_attachment_type::color, transmittance_lut_texture);
  111. transmittance_lut_resolution = {static_cast<float>(transmittance_lut_texture->get_width()), static_cast<float>(transmittance_lut_texture->get_height())};
  112. // Load transmittance LUT shader
  113. transmittance_shader_program = resource_manager->load<gl::shader_program>("transmittance-lut.glsl");
  114. transmittance_atmosphere_radii_input = transmittance_shader_program->get_input("atmosphere_radii");
  115. transmittance_rayleigh_parameters_input = transmittance_shader_program->get_input("rayleigh_parameters");
  116. transmittance_mie_parameters_input = transmittance_shader_program->get_input("mie_parameters");
  117. transmittance_ozone_distribution_input = transmittance_shader_program->get_input("ozone_distribution");
  118. transmittance_ozone_absorption_input = transmittance_shader_program->get_input("ozone_absorption");
  119. transmittance_resolution_input = transmittance_shader_program->get_input("resolution");
  120. // Create sky LUT texture and framebuffer (32F color, no depth)
  121. int sky_lut_width = 200;
  122. int sky_lut_height = 100;
  123. sky_lut_resolution = {static_cast<float>(sky_lut_width), static_cast<float>(sky_lut_height)};
  124. sky_lut_texture = new gl::texture_2d(sky_lut_width, sky_lut_height, gl::pixel_type::float_32, gl::pixel_format::rgb);
  125. sky_lut_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  126. sky_lut_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  127. sky_lut_texture->set_max_anisotropy(0.0f);
  128. sky_lut_framebuffer = new gl::framebuffer(sky_lut_width, sky_lut_height);
  129. sky_lut_framebuffer->attach(gl::framebuffer_attachment_type::color, sky_lut_texture);
  130. // Load sky LUT shader
  131. sky_lut_shader_program = resource_manager->load<gl::shader_program>("sky-illuminance-lut.glsl");
  132. sky_lut_light_direction_input = sky_lut_shader_program->get_input("light_direction");
  133. sky_lut_light_illuminance_input = sky_lut_shader_program->get_input("light_illuminance");
  134. sky_lut_atmosphere_radii_input = sky_lut_shader_program->get_input("atmosphere_radii");
  135. sky_lut_observer_position_input = sky_lut_shader_program->get_input("observer_position");
  136. sky_lut_rayleigh_parameters_input = sky_lut_shader_program->get_input("rayleigh_parameters");
  137. sky_lut_mie_parameters_input = sky_lut_shader_program->get_input("mie_parameters");
  138. sky_lut_ozone_distribution_input = sky_lut_shader_program->get_input("ozone_distribution");
  139. sky_lut_ozone_absorption_input = sky_lut_shader_program->get_input("ozone_absorption");
  140. sky_lut_airglow_illuminance_input = sky_lut_shader_program->get_input("airglow_illuminance");
  141. sky_lut_resolution_input = sky_lut_shader_program->get_input("resolution");
  142. sky_lut_transmittance_lut_input = sky_lut_shader_program->get_input("transmittance_lut");
  143. sky_lut_transmittance_lut_resolution_input = sky_lut_shader_program->get_input("transmittance_lut_resolution");
  144. }
  145. sky_pass::~sky_pass()
  146. {
  147. delete sky_lut_framebuffer;
  148. delete sky_lut_texture;
  149. delete transmittance_lut_framebuffer;
  150. delete transmittance_lut_texture;
  151. delete quad_vao;
  152. delete quad_vbo;
  153. }
  154. void sky_pass::render(const render::context& ctx, render::queue& queue) const
  155. {
  156. glDisable(GL_BLEND);
  157. glDisable(GL_DEPTH_TEST);
  158. glDepthMask(GL_FALSE);
  159. glEnable(GL_CULL_FACE);
  160. glCullFace(GL_BACK);
  161. // Render transmittance LUT if transmittance parameters have been altered.
  162. if (render_transmittance_lut)
  163. {
  164. // Render transmittance LUT
  165. rasterizer->set_viewport(0, 0, transmittance_lut_texture->get_width(), transmittance_lut_texture->get_height());
  166. rasterizer->use_framebuffer(*transmittance_lut_framebuffer);
  167. rasterizer->use_program(*transmittance_shader_program);
  168. transmittance_atmosphere_radii_input->upload(atmosphere_radii);
  169. transmittance_rayleigh_parameters_input->upload(rayleigh_parameters);
  170. transmittance_mie_parameters_input->upload(mie_parameters);
  171. transmittance_ozone_distribution_input->upload(ozone_distribution);
  172. transmittance_ozone_absorption_input->upload(ozone_absorption);
  173. if (transmittance_resolution_input)
  174. transmittance_resolution_input->upload(transmittance_lut_resolution);
  175. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  176. // Don't render transmittance LUT next frame unless parameters have changed.
  177. render_transmittance_lut = false;
  178. }
  179. // Construct matrices
  180. const scene::camera& camera = *ctx.camera;
  181. float clip_near = camera.get_clip_near_tween().interpolate(ctx.alpha);
  182. float clip_far = camera.get_clip_far_tween().interpolate(ctx.alpha);
  183. float3 model_scale = float3{1.0f, 1.0f, 1.0f} * (clip_near + clip_far) * 0.5f;
  184. float4x4 model = math::scale(math::matrix4<float>::identity(), model_scale);
  185. float4x4 view = float4x4(float3x3(ctx.view));
  186. float4x4 model_view = view * model;
  187. const float4x4& projection = ctx.projection;
  188. float4x4 view_projection = projection * view;
  189. float4x4 model_view_projection = projection * model_view;
  190. // Interpolate observer position
  191. float3 observer_position = observer_position_tween.interpolate(ctx.alpha);
  192. // Construct tweened ICRF to EUS transformation
  193. math::transformation::se3<float> icrf_to_eus =
  194. {
  195. icrf_to_eus_translation.interpolate(ctx.alpha),
  196. icrf_to_eus_rotation.interpolate(ctx.alpha)
  197. };
  198. // Get EUS direction to sun
  199. float3 sun_position = sun_position_tween.interpolate(ctx.alpha);
  200. float3 sun_direction = math::normalize(sun_position);
  201. // Interpolate and expose sun luminance and illuminance
  202. float3 sun_luminance = sun_luminance_tween.interpolate(ctx.alpha) * ctx.exposure;
  203. float3 sun_illuminance = sun_illuminance_tween.interpolate(ctx.alpha) * ctx.exposure;
  204. float3 moon_position = moon_position_tween.interpolate(ctx.alpha);
  205. float3 moon_direction = math::normalize(moon_position);
  206. float3 moon_illuminance = moon_illuminance_tween.interpolate(ctx.alpha) * ctx.exposure;
  207. float moon_angular_radius = moon_angular_radius_tween.interpolate(ctx.alpha) * magnification;
  208. float sun_y = color::aces::ap1<float>.luminance(sun_transmitted_illuminance);
  209. float moon_y = color::aces::ap1<float>.luminance(moon_transmitted_illuminance);
  210. float3 dominant_light_direction = (sun_y > moon_y) ? sun_direction : moon_direction;
  211. float3 dominant_light_illuminance = (sun_y > moon_y) ? sun_illuminance : moon_illuminance;
  212. if (moon_y > sun_y)
  213. sun_luminance *= 0.0f;
  214. // Render sky illuminance LUT
  215. auto sky_lut_viewport = sky_lut_framebuffer->get_dimensions();
  216. rasterizer->set_viewport(0, 0, std::get<0>(sky_lut_viewport), std::get<1>(sky_lut_viewport));
  217. rasterizer->use_framebuffer(*sky_lut_framebuffer);
  218. rasterizer->use_program(*sky_lut_shader_program);
  219. sky_lut_light_direction_input->upload(dominant_light_direction);
  220. sky_lut_light_illuminance_input->upload(dominant_light_illuminance);
  221. sky_lut_atmosphere_radii_input->upload(atmosphere_radii);
  222. sky_lut_observer_position_input->upload(observer_position);
  223. sky_lut_rayleigh_parameters_input->upload(rayleigh_parameters);
  224. sky_lut_mie_parameters_input->upload(mie_parameters);
  225. sky_lut_ozone_distribution_input->upload(ozone_distribution);
  226. sky_lut_ozone_absorption_input->upload(ozone_absorption);
  227. sky_lut_airglow_illuminance_input->upload(airglow_illuminance * ctx.exposure);
  228. if (sky_lut_resolution_input)
  229. sky_lut_resolution_input->upload(sky_lut_resolution);
  230. sky_lut_transmittance_lut_input->upload(transmittance_lut_texture);
  231. if (sky_lut_transmittance_lut_resolution_input)
  232. sky_lut_transmittance_lut_resolution_input->upload(transmittance_lut_resolution);
  233. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  234. rasterizer->use_framebuffer(*framebuffer);
  235. auto viewport = framebuffer->get_dimensions();
  236. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  237. float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
  238. // Draw atmosphere
  239. if (sky_model)
  240. {
  241. rasterizer->use_program(*sky_shader_program);
  242. // Upload shader parameters
  243. if (model_view_projection_input)
  244. model_view_projection_input->upload(model_view_projection);
  245. if (mouse_input)
  246. mouse_input->upload(mouse_position);
  247. if (resolution_input)
  248. resolution_input->upload(resolution);
  249. if (light_direction_input)
  250. light_direction_input->upload(dominant_light_direction);
  251. if (sun_luminance_input)
  252. sun_luminance_input->upload(sun_luminance);
  253. if (sun_angular_radius_input)
  254. sun_angular_radius_input->upload(sun_angular_radius * magnification);
  255. if (atmosphere_radii_input)
  256. atmosphere_radii_input->upload(atmosphere_radii);
  257. if (observer_position_input)
  258. observer_position_input->upload(observer_position);
  259. if (sky_illuminance_lut_input)
  260. sky_illuminance_lut_input->upload(sky_lut_texture);
  261. if (sky_illuminance_lut_resolution_input)
  262. sky_illuminance_lut_resolution_input->upload(sky_lut_resolution);
  263. sky_material->upload(ctx.alpha);
  264. rasterizer->draw_arrays(*sky_model_vao, sky_model_drawing_mode, sky_model_start_index, sky_model_index_count);
  265. }
  266. glEnable(GL_BLEND);
  267. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  268. //glBlendFunc(GL_ONE, GL_ONE);
  269. // Draw stars
  270. if (stars_model)
  271. {
  272. float star_distance = (clip_near + clip_far) * 0.5f;
  273. model = float4x4(math::matrix_cast<float>(icrf_to_eus.r));
  274. model = math::scale(model, {star_distance, star_distance, star_distance});
  275. model_view = view * model;
  276. rasterizer->use_program(*star_shader_program);
  277. if (star_model_view_input)
  278. star_model_view_input->upload(model_view);
  279. if (star_projection_input)
  280. star_projection_input->upload(projection);
  281. if (star_distance_input)
  282. star_distance_input->upload(star_distance);
  283. if (star_exposure_input)
  284. star_exposure_input->upload(ctx.exposure);
  285. star_material->upload(ctx.alpha);
  286. rasterizer->draw_arrays(*stars_model_vao, stars_model_drawing_mode, stars_model_start_index, stars_model_index_count);
  287. }
  288. // Draw moon model
  289. //if (moon_position.y() >= -moon_angular_radius)
  290. {
  291. float moon_distance = (clip_near + clip_far) * 0.5f;
  292. float moon_radius = moon_angular_radius * moon_distance;
  293. math::transform<float> moon_transform;
  294. moon_transform.translation = math::normalize(moon_position) * moon_distance;
  295. moon_transform.rotation = moon_rotation_tween.interpolate(ctx.alpha);
  296. moon_transform.scale = {moon_radius, moon_radius, moon_radius};
  297. model = math::matrix_cast(moon_transform);
  298. float3x3 normal_model = math::transpose(math::inverse(float3x3(model)));
  299. rasterizer->use_program(*moon_shader_program);
  300. if (moon_model_input)
  301. moon_model_input->upload(model);
  302. if (moon_view_projection_input)
  303. moon_view_projection_input->upload(view_projection);
  304. if (moon_normal_model_input)
  305. moon_normal_model_input->upload(normal_model);
  306. if (moon_camera_position_input)
  307. moon_camera_position_input->upload(ctx.camera_transform.translation);
  308. if (moon_sunlight_direction_input)
  309. moon_sunlight_direction_input->upload(math::normalize(moon_sunlight_direction_tween.interpolate(ctx.alpha)));
  310. if (moon_sunlight_illuminance_input)
  311. moon_sunlight_illuminance_input->upload(moon_sunlight_illuminance_tween.interpolate(ctx.alpha) * ctx.exposure);
  312. if (moon_planetlight_direction_input)
  313. moon_planetlight_direction_input->upload(math::normalize(moon_planetlight_direction_tween.interpolate(ctx.alpha)));
  314. if (moon_planetlight_illuminance_input)
  315. moon_planetlight_illuminance_input->upload(moon_planetlight_illuminance_tween.interpolate(ctx.alpha) * ctx.exposure);
  316. moon_material->upload(ctx.alpha);
  317. rasterizer->draw_arrays(*moon_model_vao, moon_model_drawing_mode, moon_model_start_index, moon_model_index_count);
  318. }
  319. }
  320. void sky_pass::set_sky_model(const model* model)
  321. {
  322. sky_model = model;
  323. if (sky_model)
  324. {
  325. sky_model_vao = model->get_vertex_array();
  326. const std::vector<model_group*>& groups = *model->get_groups();
  327. for (model_group* group: groups)
  328. {
  329. sky_material = group->get_material();
  330. sky_model_drawing_mode = group->get_drawing_mode();
  331. sky_model_start_index = group->get_start_index();
  332. sky_model_index_count = group->get_index_count();
  333. }
  334. if (sky_material)
  335. {
  336. sky_shader_program = sky_material->get_shader_program();
  337. if (sky_shader_program)
  338. {
  339. model_view_projection_input = sky_shader_program->get_input("model_view_projection");
  340. mouse_input = sky_shader_program->get_input("mouse");
  341. resolution_input = sky_shader_program->get_input("resolution");
  342. light_direction_input = sky_shader_program->get_input("light_direction");
  343. sun_luminance_input = sky_shader_program->get_input("sun_luminance");
  344. sun_angular_radius_input = sky_shader_program->get_input("sun_angular_radius");
  345. atmosphere_radii_input = sky_shader_program->get_input("atmosphere_radii");
  346. observer_position_input = sky_shader_program->get_input("observer_position");
  347. sky_illuminance_lut_input = sky_shader_program->get_input("sky_illuminance_lut");
  348. sky_illuminance_lut_resolution_input = sky_shader_program->get_input("sky_illuminance_lut_resolution");
  349. }
  350. }
  351. }
  352. else
  353. {
  354. sky_model_vao = nullptr;
  355. }
  356. }
  357. void sky_pass::set_moon_model(const model* model)
  358. {
  359. moon_model = model;
  360. if (moon_model)
  361. {
  362. moon_model_vao = model->get_vertex_array();
  363. const std::vector<model_group*>& groups = *model->get_groups();
  364. for (model_group* group: groups)
  365. {
  366. moon_material = group->get_material();
  367. moon_model_drawing_mode = group->get_drawing_mode();
  368. moon_model_start_index = group->get_start_index();
  369. moon_model_index_count = group->get_index_count();
  370. }
  371. if (moon_material)
  372. {
  373. moon_shader_program = moon_material->get_shader_program();
  374. if (moon_shader_program)
  375. {
  376. moon_model_input = moon_shader_program->get_input("model");
  377. moon_view_projection_input = moon_shader_program->get_input("view_projection");
  378. moon_normal_model_input = moon_shader_program->get_input("normal_model");
  379. moon_camera_position_input = moon_shader_program->get_input("camera_position");
  380. moon_sunlight_direction_input = moon_shader_program->get_input("sunlight_direction");
  381. moon_sunlight_illuminance_input = moon_shader_program->get_input("sunlight_illuminance");
  382. moon_planetlight_direction_input = moon_shader_program->get_input("planetlight_direction");
  383. moon_planetlight_illuminance_input = moon_shader_program->get_input("planetlight_illuminance");
  384. }
  385. }
  386. }
  387. else
  388. {
  389. moon_model = nullptr;
  390. }
  391. }
  392. void sky_pass::set_stars_model(const model* model)
  393. {
  394. stars_model = model;
  395. if (stars_model)
  396. {
  397. stars_model_vao = model->get_vertex_array();
  398. const std::vector<model_group*>& groups = *model->get_groups();
  399. for (model_group* group: groups)
  400. {
  401. star_material = group->get_material();
  402. stars_model_drawing_mode = group->get_drawing_mode();
  403. stars_model_start_index = group->get_start_index();
  404. stars_model_index_count = group->get_index_count();
  405. }
  406. if (star_material)
  407. {
  408. star_shader_program = star_material->get_shader_program();
  409. if (star_shader_program)
  410. {
  411. star_model_view_input = star_shader_program->get_input("model_view");
  412. star_projection_input = star_shader_program->get_input("projection");
  413. star_distance_input = star_shader_program->get_input("star_distance");
  414. star_exposure_input = star_shader_program->get_input("camera.exposure");
  415. }
  416. }
  417. }
  418. else
  419. {
  420. stars_model = nullptr;
  421. }
  422. }
  423. void sky_pass::update_tweens()
  424. {
  425. observer_position_tween.update();
  426. sun_position_tween.update();
  427. sun_luminance_tween.update();
  428. sun_illuminance_tween.update();
  429. icrf_to_eus_translation.update();
  430. icrf_to_eus_rotation.update();
  431. moon_position_tween.update();
  432. moon_rotation_tween.update();
  433. moon_angular_radius_tween.update();
  434. moon_sunlight_direction_tween.update();
  435. moon_sunlight_illuminance_tween.update();
  436. moon_planetlight_direction_tween.update();
  437. moon_planetlight_illuminance_tween.update();
  438. moon_illuminance_tween.update();
  439. }
  440. void sky_pass::set_magnification(float magnification)
  441. {
  442. this->magnification = magnification;
  443. }
  444. void sky_pass::set_icrf_to_eus(const math::transformation::se3<float>& transformation)
  445. {
  446. icrf_to_eus_translation[1] = transformation.t;
  447. icrf_to_eus_rotation[1] = transformation.r;
  448. }
  449. void sky_pass::set_sun_position(const float3& position)
  450. {
  451. sun_position_tween[1] = position;
  452. }
  453. void sky_pass::set_sun_illuminance(const float3& illuminance, const float3& transmitted_illuminance)
  454. {
  455. sun_illuminance_tween[1] = illuminance;
  456. sun_transmitted_illuminance = transmitted_illuminance;
  457. }
  458. void sky_pass::set_sun_luminance(const float3& luminance)
  459. {
  460. sun_luminance_tween[1] = luminance;
  461. }
  462. void sky_pass::set_sun_angular_radius(float radius)
  463. {
  464. sun_angular_radius = radius;
  465. }
  466. void sky_pass::set_planet_radius(float radius)
  467. {
  468. atmosphere_radii.x() = radius;
  469. atmosphere_radii.y() = atmosphere_radii.x() + atmosphere_upper_limit;
  470. atmosphere_radii.z() = atmosphere_radii.y() * atmosphere_radii.y();
  471. observer_position_tween[1] = {0.0f, atmosphere_radii.x() + observer_elevation, 0.0f};
  472. // Trigger transmittance LUT render
  473. render_transmittance_lut = true;
  474. }
  475. void sky_pass::set_atmosphere_upper_limit(float limit)
  476. {
  477. atmosphere_upper_limit = limit;
  478. atmosphere_radii.y() = atmosphere_radii.x() + atmosphere_upper_limit;
  479. atmosphere_radii.z() = atmosphere_radii.y() * atmosphere_radii.y();
  480. // Trigger transmittance LUT render
  481. render_transmittance_lut = true;
  482. }
  483. void sky_pass::set_observer_elevation(float elevation)
  484. {
  485. observer_elevation = elevation;
  486. observer_position_tween[1] = {0.0f, atmosphere_radii.x() + observer_elevation, 0.0f};
  487. }
  488. void sky_pass::set_rayleigh_parameters(float scale_height, const float3& scattering)
  489. {
  490. rayleigh_parameters =
  491. {
  492. -1.0f / scale_height,
  493. scattering.x(),
  494. scattering.y(),
  495. scattering.z()
  496. };
  497. // Trigger transmittance LUT render
  498. render_transmittance_lut = true;
  499. }
  500. void sky_pass::set_mie_parameters(float scale_height, float scattering, float extinction, float anisotropy)
  501. {
  502. mie_parameters =
  503. {
  504. -1.0f / scale_height,
  505. scattering,
  506. extinction,
  507. anisotropy
  508. };
  509. // Trigger transmittance LUT render
  510. render_transmittance_lut = true;
  511. }
  512. void sky_pass::set_ozone_parameters(float lower_limit, float upper_limit, float mode, const float3& absorption)
  513. {
  514. ozone_distribution =
  515. {
  516. 1.0f / (lower_limit - mode),
  517. 1.0f / (upper_limit - mode),
  518. mode
  519. };
  520. ozone_absorption = absorption;
  521. // Trigger transmittance LUT render
  522. render_transmittance_lut = true;
  523. }
  524. void sky_pass::set_airglow_illuminance(const float3& illuminance)
  525. {
  526. airglow_illuminance = illuminance;
  527. }
  528. void sky_pass::set_moon_position(const float3& position)
  529. {
  530. moon_position_tween[1] = position;
  531. }
  532. void sky_pass::set_moon_rotation(const math::quaternion<float>& rotation)
  533. {
  534. moon_rotation_tween[1] = rotation;
  535. }
  536. void sky_pass::set_moon_angular_radius(float angular_radius)
  537. {
  538. moon_angular_radius_tween[1] = angular_radius;
  539. }
  540. void sky_pass::set_moon_sunlight_direction(const float3& direction)
  541. {
  542. moon_sunlight_direction_tween[1] = direction;
  543. }
  544. void sky_pass::set_moon_sunlight_illuminance(const float3& illuminance)
  545. {
  546. moon_sunlight_illuminance_tween[1] = illuminance;
  547. }
  548. void sky_pass::set_moon_planetlight_direction(const float3& direction)
  549. {
  550. moon_planetlight_direction_tween[1] = direction;
  551. }
  552. void sky_pass::set_moon_planetlight_illuminance(const float3& illuminance)
  553. {
  554. moon_planetlight_illuminance_tween[1] = illuminance;
  555. }
  556. void sky_pass::set_moon_illuminance(const float3& illuminance, const float3& transmitted_illuminance)
  557. {
  558. moon_illuminance_tween[1] = illuminance;
  559. moon_transmitted_illuminance = transmitted_illuminance;
  560. }
  561. void sky_pass::set_transmittance_lut_resolution(std::uint16_t width, std::uint16_t height)
  562. {
  563. transmittance_lut_texture->resize(width, height, nullptr);
  564. transmittance_lut_framebuffer->resize({transmittance_lut_texture->get_width(), transmittance_lut_texture->get_height()});
  565. transmittance_lut_resolution = {static_cast<float>(width), static_cast<float>(height)};
  566. // Trigger transmittance LUT render
  567. render_transmittance_lut = true;
  568. }
  569. void sky_pass::handle_event(const mouse_moved_event& event)
  570. {
  571. mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};
  572. }
  573. } // namespace render