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

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