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

467 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_material(nullptr),
  59. moon_model_vao(nullptr),
  60. moon_shader_program(nullptr),
  61. time_tween(nullptr),
  62. observer_altitude_tween(0.0f, math::lerp<float, float>),
  63. sun_position_tween(float3{1.0f, 0.0f, 0.0f}, math::lerp<float3, float>),
  64. sun_color_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp<float3, float>),
  65. topocentric_frame_translation({0, 0, 0}, math::lerp<float3, float>),
  66. topocentric_frame_rotation(math::quaternion<float>::identity(), math::nlerp<float>)
  67. {
  68. // Load star catalog
  69. string_table* star_catalog = resource_manager->load<string_table>("stars.csv");
  70. // Allocate star catalog vertex data
  71. star_count = 0;
  72. if (star_catalog->size() > 0)
  73. star_count = star_catalog->size() - 1;
  74. std::size_t star_vertex_size = 6;
  75. std::size_t star_vertex_stride = star_vertex_size * sizeof(float);
  76. float* star_vertex_data = new float[star_count * star_vertex_size];
  77. float* star_vertex = star_vertex_data;
  78. // Build star catalog vertex data
  79. for (std::size_t i = 1; i < star_catalog->size(); ++i)
  80. {
  81. const string_table_row& catalog_row = (*star_catalog)[i];
  82. double ra = 0.0;
  83. double dec = 0.0;
  84. double vmag = 0.0;
  85. double bv_color = 0.0;
  86. // Parse star catalog entry
  87. try
  88. {
  89. ra = std::stod(catalog_row[1]);
  90. dec = std::stod(catalog_row[2]);
  91. vmag = std::stod(catalog_row[3]);
  92. bv_color = std::stod(catalog_row[4]);
  93. }
  94. catch (const std::exception& e)
  95. {
  96. continue;
  97. }
  98. // Convert right ascension and declination from degrees to radians
  99. ra = math::wrap_radians(math::radians(ra));
  100. dec = math::wrap_radians(math::radians(dec));
  101. // Transform spherical equatorial coordinates to rectangular equatorial coordinates
  102. double3 position_bci = geom::spherical::to_cartesian(double3{1.0, dec, ra});
  103. // Transform coordinates from equatorial space to inertial space
  104. physics::frame<double> bci_to_inertial = physics::orbit::inertial::to_bci({0, 0, 0}, 0.0, math::radians(23.4393)).inverse();
  105. double3 position_inertial = bci_to_inertial * position_bci;
  106. // Convert color index to color temperature
  107. double cct = color::index::bv_to_cct(bv_color);
  108. // Calculate XYZ color from color temperature
  109. double3 color_xyz = color::cct::to_xyz(cct);
  110. // Transform XYZ color to ACEScg colorspace
  111. double3 color_acescg = color::xyz::to_acescg(color_xyz);
  112. // Convert apparent magnitude to irradiance (W/m^2)
  113. double vmag_irradiance = std::pow(10.0, 0.4 * (-vmag - 19.0 + 0.4));
  114. // Convert irradiance to illuminance
  115. double vmag_illuminance = vmag_irradiance * (683.0 * 0.14);
  116. // Scale color by illuminance
  117. double3 scaled_color = color_acescg * vmag_illuminance;
  118. // Build vertex
  119. *(star_vertex++) = static_cast<float>(position_inertial.x);
  120. *(star_vertex++) = static_cast<float>(position_inertial.y);
  121. *(star_vertex++) = static_cast<float>(position_inertial.z);
  122. *(star_vertex++) = static_cast<float>(scaled_color.x);
  123. *(star_vertex++) = static_cast<float>(scaled_color.y);
  124. *(star_vertex++) = static_cast<float>(scaled_color.z);
  125. }
  126. // Unload star catalog
  127. resource_manager->unload("stars.csv");
  128. // Create star catalog VBO
  129. star_catalog_vbo = new gl::vertex_buffer(star_count * star_vertex_stride, star_vertex_data);
  130. // Create star catalog VAO
  131. star_catalog_vao = new gl::vertex_array();
  132. // Bind star catalog vertex attributes
  133. std::size_t vao_offset = 0;
  134. star_catalog_vao->bind_attribute(VERTEX_POSITION_LOCATION, *star_catalog_vbo, 3, gl::vertex_attribute_type::float_32, star_vertex_stride, 0);
  135. vao_offset += 3;
  136. star_catalog_vao->bind_attribute(VERTEX_COLOR_LOCATION, *star_catalog_vbo, 3, gl::vertex_attribute_type::float_32, star_vertex_stride, sizeof(float) * vao_offset);
  137. // Free star catalog vertex data
  138. delete[] star_vertex_data;
  139. // Load star shader
  140. star_shader_program = resource_manager->load<gl::shader_program>("star.glsl");
  141. star_model_view_input = star_shader_program->get_input("model_view");
  142. star_projection_input = star_shader_program->get_input("projection");
  143. star_distance_input = star_shader_program->get_input("star_distance");
  144. star_exposure_input = star_shader_program->get_input("camera.exposure");
  145. }
  146. sky_pass::~sky_pass()
  147. {}
  148. void sky_pass::render(render_context* context) const
  149. {
  150. rasterizer->use_framebuffer(*framebuffer);
  151. glDisable(GL_BLEND);
  152. glDisable(GL_DEPTH_TEST);
  153. glDepthMask(GL_FALSE);
  154. glEnable(GL_CULL_FACE);
  155. glCullFace(GL_BACK);
  156. auto viewport = framebuffer->get_dimensions();
  157. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  158. float time = (*time_tween)[context->alpha];
  159. float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
  160. const scene::camera& camera = *context->camera;
  161. float clip_near = camera.get_clip_near_tween().interpolate(context->alpha);
  162. float clip_far = camera.get_clip_far_tween().interpolate(context->alpha);
  163. float3 model_scale = float3{1.0f, 1.0f, 1.0f} * (clip_near + clip_far) * 0.5f;
  164. float4x4 model = math::scale(math::identity4x4<float>, model_scale);
  165. float4x4 view = math::resize<4, 4>(math::resize<3, 3>(camera.get_view_tween().interpolate(context->alpha)));
  166. float4x4 model_view = view * model;
  167. float4x4 projection = camera.get_projection_tween().interpolate(context->alpha);
  168. float4x4 view_projection = projection * view;
  169. float4x4 model_view_projection = projection * model_view;
  170. float exposure = std::exp2(camera.get_exposure_tween().interpolate(context->alpha));
  171. // Interpolate observer altitude
  172. float observer_altitude = observer_altitude_tween.interpolate(context->alpha);
  173. // Construct tweened inertial to topocentric frame
  174. physics::frame<float> topocentric_frame =
  175. {
  176. topocentric_frame_translation.interpolate(context->alpha),
  177. topocentric_frame_rotation.interpolate(context->alpha)
  178. };
  179. // Get topocentric space direction to sun
  180. float3 sun_position = sun_position_tween.interpolate(context->alpha);
  181. float3 sun_direction = math::normalize(sun_position);
  182. // Interpolate sun color
  183. float3 sun_color = sun_color_tween.interpolate(context->alpha);
  184. // Draw sky model
  185. {
  186. rasterizer->use_program(*sky_shader_program);
  187. // Upload shader parameters
  188. if (model_view_projection_input)
  189. model_view_projection_input->upload(model_view_projection);
  190. if (mouse_input)
  191. mouse_input->upload(mouse_position);
  192. if (resolution_input)
  193. resolution_input->upload(resolution);
  194. if (time_input)
  195. time_input->upload(time);
  196. if (exposure_input)
  197. exposure_input->upload(exposure);
  198. if (observer_altitude_input)
  199. observer_altitude_input->upload(observer_altitude);
  200. if (sun_direction_input)
  201. sun_direction_input->upload(sun_direction);
  202. if (sun_angular_radius_input)
  203. sun_angular_radius_input->upload(sun_angular_radius);
  204. if (sun_color_input)
  205. sun_color_input->upload(sun_color);
  206. if (scale_height_rm_input)
  207. scale_height_rm_input->upload(scale_height_rm);
  208. if (rayleigh_scattering_input)
  209. rayleigh_scattering_input->upload(rayleigh_scattering);
  210. if (mie_scattering_input)
  211. mie_scattering_input->upload(mie_scattering);
  212. if (mie_anisotropy_input)
  213. mie_anisotropy_input->upload(mie_anisotropy);
  214. if (atmosphere_radii_input)
  215. atmosphere_radii_input->upload(atmosphere_radii);
  216. sky_material->upload(context->alpha);
  217. rasterizer->draw_arrays(*sky_model_vao, sky_model_drawing_mode, sky_model_start_index, sky_model_index_count);
  218. }
  219. glEnable(GL_BLEND);
  220. //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  221. glBlendFunc(GL_ONE, GL_ONE);
  222. // Draw moon model
  223. /*
  224. float3 moon_position = {0, 0, 0};
  225. if (moon_position.y >= -moon_angular_radius)
  226. {
  227. float moon_distance = (clip_near + clip_far) * 0.5f;
  228. float moon_radius = moon_angular_radius * moon_distance;
  229. math::transform<float> moon_transform;
  230. moon_transform.translation = moon_position * -moon_distance;
  231. moon_transform.rotation = math::quaternion<float>::identity();
  232. moon_transform.scale = {moon_radius, moon_radius, moon_radius};
  233. model = math::matrix_cast(moon_transform);
  234. model_view = view * model;
  235. model_view_projection = projection * model_view;
  236. float3x3 normal_model = math::transpose(math::inverse(math::resize<3, 3>(model)));
  237. rasterizer->use_program(*moon_shader_program);
  238. if (moon_model_view_projection_input)
  239. moon_model_view_projection_input->upload(model_view_projection);
  240. if (moon_normal_model_input)
  241. moon_normal_model_input->upload(normal_model);
  242. if (moon_moon_position_input)
  243. moon_moon_position_input->upload(moon_position);
  244. if (moon_sun_position_input)
  245. moon_sun_position_input->upload(sun_position);
  246. moon_material->upload(context->alpha);
  247. rasterizer->draw_arrays(*moon_model_vao, moon_model_drawing_mode, moon_model_start_index, moon_model_index_count);
  248. }
  249. */
  250. // Draw stars
  251. {
  252. float star_distance = (clip_near + clip_far) * 0.5f;
  253. model = math::resize<4, 4>(math::matrix_cast<float>(topocentric_frame.rotation));
  254. model = math::scale(model, {star_distance, star_distance, star_distance});
  255. model_view = view * model;
  256. rasterizer->use_program(*star_shader_program);
  257. if (star_model_view_input)
  258. star_model_view_input->upload(model_view);
  259. if (star_projection_input)
  260. star_projection_input->upload(projection);
  261. if (star_distance_input)
  262. star_distance_input->upload(star_distance);
  263. if (star_exposure_input)
  264. star_exposure_input->upload(exposure);
  265. rasterizer->draw_arrays(*star_catalog_vao, gl::drawing_mode::points, 0, star_count);
  266. }
  267. }
  268. void sky_pass::set_sky_model(const model* model)
  269. {
  270. sky_model = model;
  271. if (sky_model)
  272. {
  273. sky_model_vao = model->get_vertex_array();
  274. const std::vector<model_group*>& groups = *model->get_groups();
  275. for (model_group* group: groups)
  276. {
  277. sky_material = group->get_material();
  278. sky_model_drawing_mode = group->get_drawing_mode();
  279. sky_model_start_index = group->get_start_index();
  280. sky_model_index_count = group->get_index_count();
  281. }
  282. if (sky_material)
  283. {
  284. sky_shader_program = sky_material->get_shader_program();
  285. if (sky_shader_program)
  286. {
  287. model_view_projection_input = sky_shader_program->get_input("model_view_projection");
  288. mouse_input = sky_shader_program->get_input("mouse");
  289. resolution_input = sky_shader_program->get_input("resolution");
  290. time_input = sky_shader_program->get_input("time");
  291. exposure_input = sky_shader_program->get_input("camera.exposure");
  292. observer_altitude_input = sky_shader_program->get_input("observer_altitude");
  293. sun_direction_input = sky_shader_program->get_input("sun_direction");
  294. sun_color_input = sky_shader_program->get_input("sun_color");
  295. sun_angular_radius_input = sky_shader_program->get_input("sun_angular_radius");
  296. scale_height_rm_input = sky_shader_program->get_input("scale_height_rm");
  297. rayleigh_scattering_input = sky_shader_program->get_input("rayleigh_scattering");
  298. mie_scattering_input = sky_shader_program->get_input("mie_scattering");
  299. mie_anisotropy_input = sky_shader_program->get_input("mie_anisotropy");
  300. atmosphere_radii_input = sky_shader_program->get_input("atmosphere_radii");
  301. }
  302. }
  303. }
  304. else
  305. {
  306. sky_model_vao = nullptr;
  307. }
  308. }
  309. void sky_pass::set_moon_model(const model* model)
  310. {
  311. moon_model = model;
  312. if (moon_model)
  313. {
  314. moon_model_vao = model->get_vertex_array();
  315. const std::vector<model_group*>& groups = *model->get_groups();
  316. for (model_group* group: groups)
  317. {
  318. moon_material = group->get_material();
  319. moon_model_drawing_mode = group->get_drawing_mode();
  320. moon_model_start_index = group->get_start_index();
  321. moon_model_index_count = group->get_index_count();
  322. }
  323. if (moon_material)
  324. {
  325. moon_shader_program = moon_material->get_shader_program();
  326. if (moon_shader_program)
  327. {
  328. moon_model_view_projection_input = moon_shader_program->get_input("model_view_projection");
  329. moon_normal_model_input = moon_shader_program->get_input("normal_model");
  330. moon_moon_position_input = moon_shader_program->get_input("moon_position");
  331. moon_sun_position_input = moon_shader_program->get_input("sun_position");
  332. }
  333. }
  334. }
  335. else
  336. {
  337. moon_model = nullptr;
  338. }
  339. }
  340. void sky_pass::update_tweens()
  341. {
  342. observer_altitude_tween.update();
  343. sun_position_tween.update();
  344. sun_color_tween.update();
  345. topocentric_frame_translation.update();
  346. topocentric_frame_rotation.update();
  347. }
  348. void sky_pass::set_time_tween(const tween<double>* time)
  349. {
  350. this->time_tween = time;
  351. }
  352. void sky_pass::set_topocentric_frame(const physics::frame<float>& frame)
  353. {
  354. topocentric_frame_translation[1] = frame.translation;
  355. topocentric_frame_rotation[1] = frame.rotation;
  356. }
  357. void sky_pass::set_sun_position(const float3& position)
  358. {
  359. sun_position_tween[1] = position;
  360. }
  361. void sky_pass::set_sun_color(const float3& color)
  362. {
  363. sun_color_tween[1] = color;
  364. }
  365. void sky_pass::set_sun_angular_radius(float radius)
  366. {
  367. sun_angular_radius = radius;
  368. }
  369. void sky_pass::set_observer_altitude(float altitude)
  370. {
  371. observer_altitude_tween[1] = altitude;
  372. }
  373. void sky_pass::set_scale_heights(float rayleigh, float mie)
  374. {
  375. scale_height_rm = {rayleigh, mie};
  376. }
  377. void sky_pass::set_scattering_coefficients(const float3& r, const float3& m)
  378. {
  379. rayleigh_scattering = r;
  380. mie_scattering = m;
  381. }
  382. void sky_pass::set_mie_anisotropy(float g)
  383. {
  384. mie_anisotropy = {g, g * g};
  385. }
  386. void sky_pass::set_atmosphere_radii(float inner, float outer)
  387. {
  388. atmosphere_radii.x = inner;
  389. atmosphere_radii.y = outer;
  390. atmosphere_radii.z = outer * outer;
  391. }
  392. void sky_pass::handle_event(const mouse_moved_event& event)
  393. {
  394. mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};
  395. }