💿🐜 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.

546 lines
18 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. namespace render {
  49. sky_pass::sky_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffer, resource_manager* resource_manager):
  50. pass(rasterizer, framebuffer),
  51. mouse_position({0.0f, 0.0f}),
  52. sky_model(nullptr),
  53. sky_material(nullptr),
  54. sky_model_vao(nullptr),
  55. sky_shader_program(nullptr),
  56. moon_model(nullptr),
  57. moon_model_vao(nullptr),
  58. moon_material(nullptr),
  59. moon_shader_program(nullptr),
  60. stars_model(nullptr),
  61. stars_model_vao(nullptr),
  62. star_material(nullptr),
  63. star_shader_program(nullptr),
  64. clouds_model(nullptr),
  65. clouds_model_vao(nullptr),
  66. cloud_material(nullptr),
  67. cloud_shader_program(nullptr),
  68. observer_altitude_tween(0.0f, math::lerp<float, float>),
  69. sun_position_tween(float3{1.0f, 0.0f, 0.0f}, math::lerp<float3, float>),
  70. sun_luminance_tween(float3{0.0f, 0.0f, 0.0f}, math::lerp<float3, float>),
  71. sun_illuminance_tween(float3{0.0f, 0.0f, 0.0f}, math::lerp<float3, float>),
  72. icrf_to_eus_translation({0, 0, 0}, math::lerp<float3, float>),
  73. icrf_to_eus_rotation(math::quaternion<float>::identity, math::nlerp<float>),
  74. moon_position_tween(float3{0, 0, 0}, math::lerp<float3, float>),
  75. moon_rotation_tween(math::quaternion<float>::identity, math::nlerp<float>),
  76. moon_angular_radius_tween(0.0f, math::lerp<float, float>),
  77. moon_sunlight_direction_tween(float3{0, 0, 0}, math::lerp<float3, float>),
  78. moon_sunlight_illuminance_tween(float3{0, 0, 0}, math::lerp<float3, float>),
  79. moon_planetlight_direction_tween(float3{0, 0, 0}, math::lerp<float3, float>),
  80. moon_planetlight_illuminance_tween(float3{0, 0, 0}, math::lerp<float3, float>),
  81. magnification(1.0f)
  82. {}
  83. sky_pass::~sky_pass()
  84. {}
  85. void sky_pass::render(const render::context& ctx, render::queue& queue) const
  86. {
  87. rasterizer->use_framebuffer(*framebuffer);
  88. glDisable(GL_BLEND);
  89. glDisable(GL_DEPTH_TEST);
  90. glDepthMask(GL_FALSE);
  91. glEnable(GL_CULL_FACE);
  92. glCullFace(GL_BACK);
  93. auto viewport = framebuffer->get_dimensions();
  94. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  95. float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
  96. const scene::camera& camera = *ctx.camera;
  97. float clip_near = camera.get_clip_near_tween().interpolate(ctx.alpha);
  98. float clip_far = camera.get_clip_far_tween().interpolate(ctx.alpha);
  99. float3 model_scale = float3{1.0f, 1.0f, 1.0f} * (clip_near + clip_far) * 0.5f;
  100. float4x4 model = math::scale(math::matrix4<float>::identity, model_scale);
  101. float4x4 view = math::resize<4, 4>(math::resize<3, 3>(camera.get_view_tween().interpolate(ctx.alpha)));
  102. float4x4 model_view = view * model;
  103. float4x4 projection = camera.get_projection_tween().interpolate(ctx.alpha);
  104. float4x4 view_projection = projection * view;
  105. float4x4 model_view_projection = projection * model_view;
  106. // Interpolate observer altitude
  107. float observer_altitude = observer_altitude_tween.interpolate(ctx.alpha);
  108. // Construct tweened ICRF to EUS transformation
  109. math::transformation::se3<float> icrf_to_eus =
  110. {
  111. icrf_to_eus_translation.interpolate(ctx.alpha),
  112. icrf_to_eus_rotation.interpolate(ctx.alpha)
  113. };
  114. // Get EUS direction to sun
  115. float3 sun_position = sun_position_tween.interpolate(ctx.alpha);
  116. float3 sun_direction = math::normalize(sun_position);
  117. // Interpolate and expose sun luminance and illuminance
  118. float3 sun_luminance = sun_luminance_tween.interpolate(ctx.alpha) * ctx.exposure;
  119. float3 sun_illuminance = sun_illuminance_tween.interpolate(ctx.alpha) * ctx.exposure;
  120. // Draw atmosphere
  121. if (sky_model)
  122. {
  123. rasterizer->use_program(*sky_shader_program);
  124. // Upload shader parameters
  125. if (model_view_projection_input)
  126. model_view_projection_input->upload(model_view_projection);
  127. if (mouse_input)
  128. mouse_input->upload(mouse_position);
  129. if (resolution_input)
  130. resolution_input->upload(resolution);
  131. if (time_input)
  132. time_input->upload(ctx.t);
  133. if (exposure_input)
  134. exposure_input->upload(ctx.exposure);
  135. if (observer_altitude_input)
  136. observer_altitude_input->upload(observer_altitude);
  137. if (sun_direction_input)
  138. sun_direction_input->upload(sun_direction);
  139. if (sun_angular_radius_input)
  140. sun_angular_radius_input->upload(sun_angular_radius * magnification);
  141. if (sun_luminance_input)
  142. sun_luminance_input->upload(sun_luminance);
  143. if (sun_illuminance_input)
  144. sun_illuminance_input->upload(sun_illuminance);
  145. if (scale_height_rm_input)
  146. scale_height_rm_input->upload(scale_height_rm);
  147. if (rayleigh_scattering_input)
  148. rayleigh_scattering_input->upload(rayleigh_scattering);
  149. if (mie_scattering_input)
  150. mie_scattering_input->upload(mie_scattering);
  151. if (mie_anisotropy_input)
  152. mie_anisotropy_input->upload(mie_anisotropy);
  153. if (atmosphere_radii_input)
  154. atmosphere_radii_input->upload(atmosphere_radii);
  155. sky_material->upload(ctx.alpha);
  156. rasterizer->draw_arrays(*sky_model_vao, sky_model_drawing_mode, sky_model_start_index, sky_model_index_count);
  157. }
  158. // Draw clouds
  159. if (clouds_model)
  160. {
  161. rasterizer->use_program(*cloud_shader_program);
  162. if (cloud_model_view_projection_input)
  163. cloud_model_view_projection_input->upload(model_view_projection);
  164. if (cloud_sun_direction_input)
  165. cloud_sun_direction_input->upload(sun_direction);
  166. if (cloud_sun_illuminance_input)
  167. cloud_sun_illuminance_input->upload(sun_illuminance);
  168. if (cloud_camera_position_input)
  169. cloud_camera_position_input->upload(ctx.camera_transform.translation);
  170. if (cloud_camera_exposure_input)
  171. cloud_camera_exposure_input->upload(ctx.exposure);
  172. cloud_material->upload(ctx.alpha);
  173. rasterizer->draw_arrays(*clouds_model_vao, clouds_model_drawing_mode, clouds_model_start_index, clouds_model_index_count);
  174. }
  175. glEnable(GL_BLEND);
  176. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  177. //glBlendFunc(GL_ONE, GL_ONE);
  178. // Draw stars
  179. if (stars_model)
  180. {
  181. float star_distance = (clip_near + clip_far) * 0.5f;
  182. model = math::resize<4, 4>(math::matrix_cast<float>(icrf_to_eus.r));
  183. model = math::scale(model, {star_distance, star_distance, star_distance});
  184. model_view = view * model;
  185. rasterizer->use_program(*star_shader_program);
  186. if (star_model_view_input)
  187. star_model_view_input->upload(model_view);
  188. if (star_projection_input)
  189. star_projection_input->upload(projection);
  190. if (star_distance_input)
  191. star_distance_input->upload(star_distance);
  192. if (star_exposure_input)
  193. star_exposure_input->upload(ctx.exposure);
  194. star_material->upload(ctx.alpha);
  195. rasterizer->draw_arrays(*stars_model_vao, stars_model_drawing_mode, stars_model_start_index, stars_model_index_count);
  196. }
  197. // Draw moon model
  198. float3 moon_position = moon_position_tween.interpolate(ctx.alpha);
  199. float moon_angular_radius = moon_angular_radius_tween.interpolate(ctx.alpha) * magnification;
  200. //if (moon_position.y >= -moon_angular_radius)
  201. {
  202. float moon_distance = (clip_near + clip_far) * 0.5f;
  203. float moon_radius = moon_angular_radius * moon_distance;
  204. math::transform<float> moon_transform;
  205. moon_transform.translation = math::normalize(moon_position) * moon_distance;
  206. moon_transform.rotation = moon_rotation_tween.interpolate(ctx.alpha);
  207. moon_transform.scale = {moon_radius, moon_radius, moon_radius};
  208. model = math::matrix_cast(moon_transform);
  209. float3x3 normal_model = math::transpose(math::inverse(math::resize<3, 3>(model)));
  210. rasterizer->use_program(*moon_shader_program);
  211. if (moon_model_input)
  212. moon_model_input->upload(model);
  213. if (moon_view_projection_input)
  214. moon_view_projection_input->upload(view_projection);
  215. if (moon_normal_model_input)
  216. moon_normal_model_input->upload(normal_model);
  217. if (moon_camera_position_input)
  218. moon_camera_position_input->upload(ctx.camera_transform.translation);
  219. if (moon_sunlight_direction_input)
  220. moon_sunlight_direction_input->upload(math::normalize(moon_sunlight_direction_tween.interpolate(ctx.alpha)));
  221. if (moon_sunlight_illuminance_input)
  222. moon_sunlight_illuminance_input->upload(moon_sunlight_illuminance_tween.interpolate(ctx.alpha) * ctx.exposure);
  223. if (moon_planetlight_direction_input)
  224. moon_planetlight_direction_input->upload(math::normalize(moon_planetlight_direction_tween.interpolate(ctx.alpha)));
  225. if (moon_planetlight_illuminance_input)
  226. moon_planetlight_illuminance_input->upload(moon_planetlight_illuminance_tween.interpolate(ctx.alpha) * ctx.exposure);
  227. moon_material->upload(ctx.alpha);
  228. rasterizer->draw_arrays(*moon_model_vao, moon_model_drawing_mode, moon_model_start_index, moon_model_index_count);
  229. }
  230. }
  231. void sky_pass::set_sky_model(const model* model)
  232. {
  233. sky_model = model;
  234. if (sky_model)
  235. {
  236. sky_model_vao = model->get_vertex_array();
  237. const std::vector<model_group*>& groups = *model->get_groups();
  238. for (model_group* group: groups)
  239. {
  240. sky_material = group->get_material();
  241. sky_model_drawing_mode = group->get_drawing_mode();
  242. sky_model_start_index = group->get_start_index();
  243. sky_model_index_count = group->get_index_count();
  244. }
  245. if (sky_material)
  246. {
  247. sky_shader_program = sky_material->get_shader_program();
  248. if (sky_shader_program)
  249. {
  250. model_view_projection_input = sky_shader_program->get_input("model_view_projection");
  251. mouse_input = sky_shader_program->get_input("mouse");
  252. resolution_input = sky_shader_program->get_input("resolution");
  253. time_input = sky_shader_program->get_input("time");
  254. exposure_input = sky_shader_program->get_input("camera.exposure");
  255. observer_altitude_input = sky_shader_program->get_input("observer_altitude");
  256. sun_direction_input = sky_shader_program->get_input("sun_direction");
  257. sun_luminance_input = sky_shader_program->get_input("sun_luminance");
  258. sun_illuminance_input = sky_shader_program->get_input("sun_illuminance");
  259. sun_angular_radius_input = sky_shader_program->get_input("sun_angular_radius");
  260. scale_height_rm_input = sky_shader_program->get_input("scale_height_rm");
  261. rayleigh_scattering_input = sky_shader_program->get_input("rayleigh_scattering");
  262. mie_scattering_input = sky_shader_program->get_input("mie_scattering");
  263. mie_anisotropy_input = sky_shader_program->get_input("mie_anisotropy");
  264. atmosphere_radii_input = sky_shader_program->get_input("atmosphere_radii");
  265. }
  266. }
  267. }
  268. else
  269. {
  270. sky_model_vao = nullptr;
  271. }
  272. }
  273. void sky_pass::set_moon_model(const model* model)
  274. {
  275. moon_model = model;
  276. if (moon_model)
  277. {
  278. moon_model_vao = model->get_vertex_array();
  279. const std::vector<model_group*>& groups = *model->get_groups();
  280. for (model_group* group: groups)
  281. {
  282. moon_material = group->get_material();
  283. moon_model_drawing_mode = group->get_drawing_mode();
  284. moon_model_start_index = group->get_start_index();
  285. moon_model_index_count = group->get_index_count();
  286. }
  287. if (moon_material)
  288. {
  289. moon_shader_program = moon_material->get_shader_program();
  290. if (moon_shader_program)
  291. {
  292. moon_model_input = moon_shader_program->get_input("model");
  293. moon_view_projection_input = moon_shader_program->get_input("view_projection");
  294. moon_normal_model_input = moon_shader_program->get_input("normal_model");
  295. moon_camera_position_input = moon_shader_program->get_input("camera_position");
  296. moon_sunlight_direction_input = moon_shader_program->get_input("sunlight_direction");
  297. moon_sunlight_illuminance_input = moon_shader_program->get_input("sunlight_illuminance");
  298. moon_planetlight_direction_input = moon_shader_program->get_input("planetlight_direction");
  299. moon_planetlight_illuminance_input = moon_shader_program->get_input("planetlight_illuminance");
  300. }
  301. }
  302. }
  303. else
  304. {
  305. moon_model = nullptr;
  306. }
  307. }
  308. void sky_pass::set_stars_model(const model* model)
  309. {
  310. stars_model = model;
  311. if (stars_model)
  312. {
  313. stars_model_vao = model->get_vertex_array();
  314. const std::vector<model_group*>& groups = *model->get_groups();
  315. for (model_group* group: groups)
  316. {
  317. star_material = group->get_material();
  318. stars_model_drawing_mode = group->get_drawing_mode();
  319. stars_model_start_index = group->get_start_index();
  320. stars_model_index_count = group->get_index_count();
  321. }
  322. if (star_material)
  323. {
  324. star_shader_program = star_material->get_shader_program();
  325. if (star_shader_program)
  326. {
  327. star_model_view_input = star_shader_program->get_input("model_view");
  328. star_projection_input = star_shader_program->get_input("projection");
  329. star_distance_input = star_shader_program->get_input("star_distance");
  330. star_exposure_input = star_shader_program->get_input("camera.exposure");
  331. }
  332. }
  333. }
  334. else
  335. {
  336. stars_model = nullptr;
  337. }
  338. }
  339. void sky_pass::set_clouds_model(const model* model)
  340. {
  341. clouds_model = model;
  342. if (clouds_model)
  343. {
  344. clouds_model_vao = model->get_vertex_array();
  345. const std::vector<model_group*>& groups = *model->get_groups();
  346. for (model_group* group: groups)
  347. {
  348. cloud_material = group->get_material();
  349. clouds_model_drawing_mode = group->get_drawing_mode();
  350. clouds_model_start_index = group->get_start_index();
  351. clouds_model_index_count = group->get_index_count();
  352. }
  353. if (cloud_material)
  354. {
  355. cloud_shader_program = cloud_material->get_shader_program();
  356. if (cloud_shader_program)
  357. {
  358. cloud_model_view_projection_input = cloud_shader_program->get_input("model_view_projection");
  359. cloud_sun_direction_input = cloud_shader_program->get_input("sun_direction");
  360. cloud_sun_illuminance_input = cloud_shader_program->get_input("sun_illuminance");
  361. cloud_camera_position_input = cloud_shader_program->get_input("camera.position");
  362. cloud_camera_exposure_input = cloud_shader_program->get_input("camera.exposure");
  363. }
  364. }
  365. }
  366. else
  367. {
  368. clouds_model = nullptr;
  369. }
  370. }
  371. void sky_pass::update_tweens()
  372. {
  373. observer_altitude_tween.update();
  374. sun_position_tween.update();
  375. sun_luminance_tween.update();
  376. sun_illuminance_tween.update();
  377. icrf_to_eus_translation.update();
  378. icrf_to_eus_rotation.update();
  379. moon_position_tween.update();
  380. moon_rotation_tween.update();
  381. moon_angular_radius_tween.update();
  382. moon_sunlight_direction_tween.update();
  383. moon_sunlight_illuminance_tween.update();
  384. moon_planetlight_direction_tween.update();
  385. moon_planetlight_illuminance_tween.update();
  386. }
  387. void sky_pass::set_magnification(float magnification)
  388. {
  389. this->magnification = magnification;
  390. }
  391. void sky_pass::set_icrf_to_eus(const math::transformation::se3<float>& transformation)
  392. {
  393. icrf_to_eus_translation[1] = transformation.t;
  394. icrf_to_eus_rotation[1] = transformation.r;
  395. }
  396. void sky_pass::set_sun_position(const float3& position)
  397. {
  398. sun_position_tween[1] = position;
  399. }
  400. void sky_pass::set_sun_illuminance(const float3& illuminance)
  401. {
  402. sun_illuminance_tween[1] = illuminance;
  403. }
  404. void sky_pass::set_sun_luminance(const float3& luminance)
  405. {
  406. sun_luminance_tween[1] = luminance;
  407. }
  408. void sky_pass::set_sun_angular_radius(float radius)
  409. {
  410. sun_angular_radius = radius;
  411. }
  412. void sky_pass::set_observer_altitude(float altitude)
  413. {
  414. observer_altitude_tween[1] = altitude;
  415. }
  416. void sky_pass::set_scale_heights(float rayleigh, float mie)
  417. {
  418. scale_height_rm = {rayleigh, mie};
  419. }
  420. void sky_pass::set_scattering_coefficients(const float3& r, const float3& m)
  421. {
  422. rayleigh_scattering = r;
  423. mie_scattering = m;
  424. }
  425. void sky_pass::set_mie_anisotropy(float g)
  426. {
  427. mie_anisotropy = {g, g * g};
  428. }
  429. void sky_pass::set_atmosphere_radii(float inner, float outer)
  430. {
  431. atmosphere_radii.x = inner;
  432. atmosphere_radii.y = outer;
  433. atmosphere_radii.z = outer * outer;
  434. }
  435. void sky_pass::set_moon_position(const float3& position)
  436. {
  437. moon_position_tween[1] = position;
  438. }
  439. void sky_pass::set_moon_rotation(const math::quaternion<float>& rotation)
  440. {
  441. moon_rotation_tween[1] = rotation;
  442. }
  443. void sky_pass::set_moon_angular_radius(float angular_radius)
  444. {
  445. moon_angular_radius_tween[1] = angular_radius;
  446. }
  447. void sky_pass::set_moon_sunlight_direction(const float3& direction)
  448. {
  449. moon_sunlight_direction_tween[1] = direction;
  450. }
  451. void sky_pass::set_moon_sunlight_illuminance(const float3& illuminance)
  452. {
  453. moon_sunlight_illuminance_tween[1] = illuminance;
  454. }
  455. void sky_pass::set_moon_planetlight_direction(const float3& direction)
  456. {
  457. moon_planetlight_direction_tween[1] = direction;
  458. }
  459. void sky_pass::set_moon_planetlight_illuminance(const float3& illuminance)
  460. {
  461. moon_planetlight_illuminance_tween[1] = illuminance;
  462. }
  463. void sky_pass::handle_event(const mouse_moved_event& event)
  464. {
  465. mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};
  466. }
  467. } // namespace render