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

481 lines
15 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 "renderer/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-type.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 "renderer/vertex-attributes.hpp"
  34. #include "renderer/render-context.hpp"
  35. #include "renderer/model.hpp"
  36. #include "renderer/material.hpp"
  37. #include "scene/camera.hpp"
  38. #include "utility/fundamental-types.hpp"
  39. #include "color/color.hpp"
  40. #include "astro/illuminance.hpp"
  41. #include "math/interpolation.hpp"
  42. #include "geom/cartesian.hpp"
  43. #include "geom/spherical.hpp"
  44. #include "physics/orbit/orbit.hpp"
  45. #include "physics/light/photometry.hpp"
  46. #include <cmath>
  47. #include <stdexcept>
  48. #include <glad/glad.h>
  49. #include <iostream>
  50. sky_pass::sky_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffer, resource_manager* resource_manager):
  51. render_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. clouds_model(nullptr),
  66. clouds_model_vao(nullptr),
  67. cloud_material(nullptr),
  68. cloud_shader_program(nullptr),
  69. time_tween(nullptr),
  70. observer_altitude_tween(0.0f, math::lerp<float, float>),
  71. sun_position_tween(float3{1.0f, 0.0f, 0.0f}, math::lerp<float3, float>),
  72. sun_color_outer_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp<float3, float>),
  73. sun_color_inner_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp<float3, float>),
  74. topocentric_frame_translation({0, 0, 0}, math::lerp<float3, float>),
  75. topocentric_frame_rotation(math::quaternion<float>::identity(), math::nlerp<float>)
  76. {}
  77. sky_pass::~sky_pass()
  78. {}
  79. void sky_pass::render(render_context* context) const
  80. {
  81. rasterizer->use_framebuffer(*framebuffer);
  82. glDisable(GL_BLEND);
  83. glDisable(GL_DEPTH_TEST);
  84. glDepthMask(GL_FALSE);
  85. glEnable(GL_CULL_FACE);
  86. glCullFace(GL_BACK);
  87. auto viewport = framebuffer->get_dimensions();
  88. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  89. float time = static_cast<float>((*time_tween)[context->alpha]);
  90. float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
  91. const scene::camera& camera = *context->camera;
  92. float clip_near = camera.get_clip_near_tween().interpolate(context->alpha);
  93. float clip_far = camera.get_clip_far_tween().interpolate(context->alpha);
  94. float3 model_scale = float3{1.0f, 1.0f, 1.0f} * (clip_near + clip_far) * 0.5f;
  95. float4x4 model = math::scale(math::identity4x4<float>, model_scale);
  96. float4x4 view = math::resize<4, 4>(math::resize<3, 3>(camera.get_view_tween().interpolate(context->alpha)));
  97. float4x4 model_view = view * model;
  98. float4x4 projection = camera.get_projection_tween().interpolate(context->alpha);
  99. float4x4 view_projection = projection * view;
  100. float4x4 model_view_projection = projection * model_view;
  101. float exposure = std::exp2(camera.get_exposure_tween().interpolate(context->alpha));
  102. // Interpolate observer altitude
  103. float observer_altitude = observer_altitude_tween.interpolate(context->alpha);
  104. // Construct tweened inertial to topocentric frame
  105. physics::frame<float> topocentric_frame =
  106. {
  107. topocentric_frame_translation.interpolate(context->alpha),
  108. topocentric_frame_rotation.interpolate(context->alpha)
  109. };
  110. // Get topocentric space direction to sun
  111. float3 sun_position = sun_position_tween.interpolate(context->alpha);
  112. float3 sun_direction = math::normalize(sun_position);
  113. // Interpolate sun color
  114. float3 sun_color_outer = sun_color_outer_tween.interpolate(context->alpha);
  115. float3 sun_color_inner = sun_color_inner_tween.interpolate(context->alpha);
  116. // Draw atmosphere
  117. if (sky_model)
  118. {
  119. rasterizer->use_program(*sky_shader_program);
  120. // Upload shader parameters
  121. if (model_view_projection_input)
  122. model_view_projection_input->upload(model_view_projection);
  123. if (mouse_input)
  124. mouse_input->upload(mouse_position);
  125. if (resolution_input)
  126. resolution_input->upload(resolution);
  127. if (time_input)
  128. time_input->upload(time);
  129. if (exposure_input)
  130. exposure_input->upload(exposure);
  131. if (observer_altitude_input)
  132. observer_altitude_input->upload(observer_altitude);
  133. if (sun_direction_input)
  134. sun_direction_input->upload(sun_direction);
  135. if (sun_angular_radius_input)
  136. sun_angular_radius_input->upload(sun_angular_radius);
  137. if (sun_color_input)
  138. sun_color_input->upload(sun_color_outer);
  139. if (scale_height_rm_input)
  140. scale_height_rm_input->upload(scale_height_rm);
  141. if (rayleigh_scattering_input)
  142. rayleigh_scattering_input->upload(rayleigh_scattering);
  143. if (mie_scattering_input)
  144. mie_scattering_input->upload(mie_scattering);
  145. if (mie_anisotropy_input)
  146. mie_anisotropy_input->upload(mie_anisotropy);
  147. if (atmosphere_radii_input)
  148. atmosphere_radii_input->upload(atmosphere_radii);
  149. sky_material->upload(context->alpha);
  150. rasterizer->draw_arrays(*sky_model_vao, sky_model_drawing_mode, sky_model_start_index, sky_model_index_count);
  151. }
  152. // Draw clouds
  153. if (clouds_model)
  154. {
  155. rasterizer->use_program(*cloud_shader_program);
  156. if (cloud_model_view_projection_input)
  157. cloud_model_view_projection_input->upload(model_view_projection);
  158. if (cloud_sun_direction_input)
  159. cloud_sun_direction_input->upload(sun_direction);
  160. if (cloud_sun_color_input)
  161. cloud_sun_color_input->upload(sun_color_inner);
  162. if (cloud_camera_position_input)
  163. cloud_camera_position_input->upload(context->camera_transform.translation);
  164. if (cloud_camera_exposure_input)
  165. cloud_camera_exposure_input->upload(exposure);
  166. cloud_material->upload(context->alpha);
  167. rasterizer->draw_arrays(*clouds_model_vao, clouds_model_drawing_mode, clouds_model_start_index, clouds_model_index_count);
  168. }
  169. glEnable(GL_BLEND);
  170. //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  171. glBlendFunc(GL_ONE, GL_ONE);
  172. // Draw stars
  173. if (stars_model)
  174. {
  175. float star_distance = (clip_near + clip_far) * 0.5f;
  176. model = math::resize<4, 4>(math::matrix_cast<float>(topocentric_frame.rotation));
  177. model = math::scale(model, {star_distance, star_distance, star_distance});
  178. model_view = view * model;
  179. rasterizer->use_program(*star_shader_program);
  180. if (star_model_view_input)
  181. star_model_view_input->upload(model_view);
  182. if (star_projection_input)
  183. star_projection_input->upload(projection);
  184. if (star_distance_input)
  185. star_distance_input->upload(star_distance);
  186. if (star_exposure_input)
  187. star_exposure_input->upload(exposure);
  188. star_material->upload(context->alpha);
  189. rasterizer->draw_arrays(*stars_model_vao, stars_model_drawing_mode, stars_model_start_index, stars_model_index_count);
  190. }
  191. // Draw moon model
  192. /*
  193. float3 moon_position = {0, 0, 0};
  194. if (moon_position.y >= -moon_angular_radius)
  195. {
  196. float moon_distance = (clip_near + clip_far) * 0.5f;
  197. float moon_radius = moon_angular_radius * moon_distance;
  198. math::transform<float> moon_transform;
  199. moon_transform.translation = moon_position * -moon_distance;
  200. moon_transform.rotation = math::quaternion<float>::identity();
  201. moon_transform.scale = {moon_radius, moon_radius, moon_radius};
  202. model = math::matrix_cast(moon_transform);
  203. model_view = view * model;
  204. model_view_projection = projection * model_view;
  205. float3x3 normal_model = math::transpose(math::inverse(math::resize<3, 3>(model)));
  206. rasterizer->use_program(*moon_shader_program);
  207. if (moon_model_view_projection_input)
  208. moon_model_view_projection_input->upload(model_view_projection);
  209. if (moon_normal_model_input)
  210. moon_normal_model_input->upload(normal_model);
  211. if (moon_moon_position_input)
  212. moon_moon_position_input->upload(moon_position);
  213. if (moon_sun_position_input)
  214. moon_sun_position_input->upload(sun_position);
  215. moon_material->upload(context->alpha);
  216. rasterizer->draw_arrays(*moon_model_vao, moon_model_drawing_mode, moon_model_start_index, moon_model_index_count);
  217. }
  218. */
  219. }
  220. void sky_pass::set_sky_model(const model* model)
  221. {
  222. sky_model = model;
  223. if (sky_model)
  224. {
  225. sky_model_vao = model->get_vertex_array();
  226. const std::vector<model_group*>& groups = *model->get_groups();
  227. for (model_group* group: groups)
  228. {
  229. sky_material = group->get_material();
  230. sky_model_drawing_mode = group->get_drawing_mode();
  231. sky_model_start_index = group->get_start_index();
  232. sky_model_index_count = group->get_index_count();
  233. }
  234. if (sky_material)
  235. {
  236. sky_shader_program = sky_material->get_shader_program();
  237. if (sky_shader_program)
  238. {
  239. model_view_projection_input = sky_shader_program->get_input("model_view_projection");
  240. mouse_input = sky_shader_program->get_input("mouse");
  241. resolution_input = sky_shader_program->get_input("resolution");
  242. time_input = sky_shader_program->get_input("time");
  243. exposure_input = sky_shader_program->get_input("camera.exposure");
  244. observer_altitude_input = sky_shader_program->get_input("observer_altitude");
  245. sun_direction_input = sky_shader_program->get_input("sun_direction");
  246. sun_color_input = sky_shader_program->get_input("sun_color");
  247. sun_angular_radius_input = sky_shader_program->get_input("sun_angular_radius");
  248. scale_height_rm_input = sky_shader_program->get_input("scale_height_rm");
  249. rayleigh_scattering_input = sky_shader_program->get_input("rayleigh_scattering");
  250. mie_scattering_input = sky_shader_program->get_input("mie_scattering");
  251. mie_anisotropy_input = sky_shader_program->get_input("mie_anisotropy");
  252. atmosphere_radii_input = sky_shader_program->get_input("atmosphere_radii");
  253. }
  254. }
  255. }
  256. else
  257. {
  258. sky_model_vao = nullptr;
  259. }
  260. }
  261. void sky_pass::set_moon_model(const model* model)
  262. {
  263. moon_model = model;
  264. if (moon_model)
  265. {
  266. moon_model_vao = model->get_vertex_array();
  267. const std::vector<model_group*>& groups = *model->get_groups();
  268. for (model_group* group: groups)
  269. {
  270. moon_material = group->get_material();
  271. moon_model_drawing_mode = group->get_drawing_mode();
  272. moon_model_start_index = group->get_start_index();
  273. moon_model_index_count = group->get_index_count();
  274. }
  275. if (moon_material)
  276. {
  277. moon_shader_program = moon_material->get_shader_program();
  278. if (moon_shader_program)
  279. {
  280. moon_model_view_projection_input = moon_shader_program->get_input("model_view_projection");
  281. moon_normal_model_input = moon_shader_program->get_input("normal_model");
  282. moon_moon_position_input = moon_shader_program->get_input("moon_position");
  283. moon_sun_position_input = moon_shader_program->get_input("sun_position");
  284. }
  285. }
  286. }
  287. else
  288. {
  289. moon_model = nullptr;
  290. }
  291. }
  292. void sky_pass::set_stars_model(const model* model)
  293. {
  294. stars_model = model;
  295. if (stars_model)
  296. {
  297. stars_model_vao = model->get_vertex_array();
  298. const std::vector<model_group*>& groups = *model->get_groups();
  299. for (model_group* group: groups)
  300. {
  301. star_material = group->get_material();
  302. stars_model_drawing_mode = group->get_drawing_mode();
  303. stars_model_start_index = group->get_start_index();
  304. stars_model_index_count = group->get_index_count();
  305. }
  306. if (star_material)
  307. {
  308. star_shader_program = star_material->get_shader_program();
  309. if (star_shader_program)
  310. {
  311. star_model_view_input = star_shader_program->get_input("model_view");
  312. star_projection_input = star_shader_program->get_input("projection");
  313. star_distance_input = star_shader_program->get_input("star_distance");
  314. star_exposure_input = star_shader_program->get_input("camera.exposure");
  315. }
  316. }
  317. }
  318. else
  319. {
  320. stars_model = nullptr;
  321. }
  322. }
  323. void sky_pass::set_clouds_model(const model* model)
  324. {
  325. clouds_model = model;
  326. if (clouds_model)
  327. {
  328. clouds_model_vao = model->get_vertex_array();
  329. const std::vector<model_group*>& groups = *model->get_groups();
  330. for (model_group* group: groups)
  331. {
  332. cloud_material = group->get_material();
  333. clouds_model_drawing_mode = group->get_drawing_mode();
  334. clouds_model_start_index = group->get_start_index();
  335. clouds_model_index_count = group->get_index_count();
  336. }
  337. if (cloud_material)
  338. {
  339. cloud_shader_program = cloud_material->get_shader_program();
  340. if (cloud_shader_program)
  341. {
  342. cloud_model_view_projection_input = cloud_shader_program->get_input("model_view_projection");
  343. cloud_sun_direction_input = cloud_shader_program->get_input("sun_direction");
  344. cloud_sun_color_input = cloud_shader_program->get_input("sun_color");
  345. cloud_camera_position_input = cloud_shader_program->get_input("camera.position");
  346. cloud_camera_exposure_input = cloud_shader_program->get_input("camera.exposure");
  347. }
  348. }
  349. }
  350. else
  351. {
  352. clouds_model = nullptr;
  353. }
  354. }
  355. void sky_pass::update_tweens()
  356. {
  357. observer_altitude_tween.update();
  358. sun_position_tween.update();
  359. sun_color_outer_tween.update();
  360. sun_color_inner_tween.update();
  361. topocentric_frame_translation.update();
  362. topocentric_frame_rotation.update();
  363. }
  364. void sky_pass::set_time_tween(const tween<double>* time)
  365. {
  366. this->time_tween = time;
  367. }
  368. void sky_pass::set_topocentric_frame(const physics::frame<float>& frame)
  369. {
  370. topocentric_frame_translation[1] = frame.translation;
  371. topocentric_frame_rotation[1] = frame.rotation;
  372. }
  373. void sky_pass::set_sun_position(const float3& position)
  374. {
  375. sun_position_tween[1] = position;
  376. }
  377. void sky_pass::set_sun_color(const float3& color_outer, const float3& color_inner)
  378. {
  379. sun_color_outer_tween[1] = color_outer;
  380. sun_color_inner_tween[1] = color_inner;
  381. }
  382. void sky_pass::set_sun_angular_radius(float radius)
  383. {
  384. sun_angular_radius = radius;
  385. }
  386. void sky_pass::set_observer_altitude(float altitude)
  387. {
  388. observer_altitude_tween[1] = altitude;
  389. }
  390. void sky_pass::set_scale_heights(float rayleigh, float mie)
  391. {
  392. scale_height_rm = {rayleigh, mie};
  393. }
  394. void sky_pass::set_scattering_coefficients(const float3& r, const float3& m)
  395. {
  396. rayleigh_scattering = r;
  397. mie_scattering = m;
  398. }
  399. void sky_pass::set_mie_anisotropy(float g)
  400. {
  401. mie_anisotropy = {g, g * g};
  402. }
  403. void sky_pass::set_atmosphere_radii(float inner, float outer)
  404. {
  405. atmosphere_radii.x = inner;
  406. atmosphere_radii.y = outer;
  407. atmosphere_radii.z = outer * outer;
  408. }
  409. void sky_pass::handle_event(const mouse_moved_event& event)
  410. {
  411. mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};
  412. }