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

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