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

525 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 "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. blue_noise_map(nullptr),
  62. sky_gradient(nullptr),
  63. sky_gradient2(nullptr),
  64. observer_location{0.0f, 0.0f, 0.0f},
  65. time_tween(nullptr),
  66. time_of_day_tween(0.0, math::lerp<float, float>),
  67. julian_day_tween(0.0, math::lerp<float, float>),
  68. horizon_color_tween(float3{0.0f, 0.0f, 0.0f}, math::lerp<float3, float>),
  69. zenith_color_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp<float3, float>),
  70. sun_color_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp<float3, float>),
  71. topocentric_frame_translation({0, 0, 0}, math::lerp<float3, float>),
  72. topocentric_frame_rotation(math::quaternion<float>::identity(), math::nlerp<float>),
  73. sun_object(nullptr)
  74. {
  75. // Load star catalog
  76. string_table* star_catalog = resource_manager->load<string_table>("stars.csv");
  77. // Allocate star catalog vertex data
  78. star_count = 0;
  79. if (star_catalog->size() > 0)
  80. star_count = star_catalog->size() - 1;
  81. std::size_t star_vertex_size = 6;
  82. std::size_t star_vertex_stride = star_vertex_size * sizeof(float);
  83. float* star_vertex_data = new float[star_count * star_vertex_size];
  84. float* star_vertex = star_vertex_data;
  85. // Build star catalog vertex data
  86. for (std::size_t i = 1; i < star_catalog->size(); ++i)
  87. {
  88. const string_table_row& catalog_row = (*star_catalog)[i];
  89. double ra = 0.0;
  90. double dec = 0.0;
  91. double vmag = 0.0;
  92. double bv_color = 0.0;
  93. // Parse star catalog entry
  94. try
  95. {
  96. ra = std::stod(catalog_row[1]);
  97. dec = std::stod(catalog_row[2]);
  98. vmag = std::stod(catalog_row[3]);
  99. bv_color = std::stod(catalog_row[4]);
  100. }
  101. catch (const std::exception& e)
  102. {}
  103. // Convert right ascension and declination from degrees to radians
  104. ra = math::wrap_radians(math::radians(ra));
  105. dec = math::wrap_radians(math::radians(dec));
  106. // Transform spherical equatorial coordinates to rectangular equatorial coordinates
  107. double3 position_bci = geom::spherical::to_cartesian(double3{1.0, dec, ra});
  108. // Transform coordinates from equatorial space to inertial space
  109. physics::frame<double> bci_to_inertial = physics::orbit::inertial::to_bci({0, 0, 0}, 0.0, math::radians(23.4393)).inverse();
  110. double3 position_inertial = bci_to_inertial * position_bci;
  111. // Convert color index to color temperature
  112. double cct = color::index::bv_to_cct(bv_color);
  113. // Calculate XYZ color from color temperature
  114. double3 color_xyz = color::cct::to_xyz(cct);
  115. // Transform XYZ color to ACEScg colorspace
  116. double3 color_acescg = color::xyz::to_acescg(color_xyz);
  117. // Convert apparent magnitude to irradiance W/m2
  118. double vmag_lux = astro::vmag_to_lux(vmag);
  119. // Convert irradiance to illuminance (using luminous efficiency of sun)
  120. double illuminance = physics::light::watts_to_lumens<double>(vmag_lux, 0.13);
  121. // Scale color by illuminance
  122. double3 scaled_color = color_acescg * illuminance;
  123. // Build vertex
  124. *(star_vertex++) = static_cast<float>(position_inertial.x);
  125. *(star_vertex++) = static_cast<float>(position_inertial.y);
  126. *(star_vertex++) = static_cast<float>(position_inertial.z);
  127. *(star_vertex++) = static_cast<float>(scaled_color.x);
  128. *(star_vertex++) = static_cast<float>(scaled_color.y);
  129. *(star_vertex++) = static_cast<float>(scaled_color.z);
  130. }
  131. // Unload star catalog
  132. resource_manager->unload("stars.csv");
  133. // Create star catalog VBO
  134. star_catalog_vbo = new gl::vertex_buffer(star_count * star_vertex_stride, star_vertex_data);
  135. // Create star catalog VAO
  136. star_catalog_vao = new gl::vertex_array();
  137. // Bind star catalog vertex attributes
  138. std::size_t vao_offset = 0;
  139. star_catalog_vao->bind_attribute(VERTEX_POSITION_LOCATION, *star_catalog_vbo, 3, gl::vertex_attribute_type::float_32, star_vertex_stride, 0);
  140. vao_offset += 3;
  141. 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);
  142. // Free star catalog vertex data
  143. delete[] star_vertex_data;
  144. // Load star shader
  145. star_shader_program = resource_manager->load<gl::shader_program>("star.glsl");
  146. star_model_view_input = star_shader_program->get_input("model_view");
  147. star_projection_input = star_shader_program->get_input("projection");
  148. star_distance_input = star_shader_program->get_input("star_distance");
  149. star_exposure_input = star_shader_program->get_input("camera.exposure");
  150. }
  151. sky_pass::~sky_pass()
  152. {}
  153. void sky_pass::render(render_context* context) const
  154. {
  155. rasterizer->use_framebuffer(*framebuffer);
  156. glDisable(GL_BLEND);
  157. glDisable(GL_DEPTH_TEST);
  158. glDepthMask(GL_FALSE);
  159. glEnable(GL_CULL_FACE);
  160. glCullFace(GL_BACK);
  161. auto viewport = framebuffer->get_dimensions();
  162. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  163. float time = (*time_tween)[context->alpha];
  164. float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
  165. const scene::camera& camera = *context->camera;
  166. float clip_near = camera.get_clip_near_tween().interpolate(context->alpha);
  167. float clip_far = camera.get_clip_far_tween().interpolate(context->alpha);
  168. float3 model_scale = float3{1.0f, 1.0f, 1.0f} * (clip_near + clip_far) * 0.5f;
  169. float4x4 model = math::scale(math::identity4x4<float>, model_scale);
  170. float4x4 view = math::resize<4, 4>(math::resize<3, 3>(camera.get_view_tween().interpolate(context->alpha)));
  171. float4x4 model_view = view * model;
  172. float4x4 projection = camera.get_projection_tween().interpolate(context->alpha);
  173. float4x4 view_projection = projection * view;
  174. float4x4 model_view_projection = projection * model_view;
  175. float exposure = std::exp2(camera.get_exposure_tween().interpolate(context->alpha));
  176. float time_of_day = time_of_day_tween.interpolate(context->alpha);
  177. float julian_day = julian_day_tween.interpolate(context->alpha);
  178. float3 horizon_color = horizon_color_tween.interpolate(context->alpha);
  179. float3 zenith_color = zenith_color_tween.interpolate(context->alpha);
  180. float3 sun_color = sun_color_tween.interpolate(context->alpha);
  181. // Construct tweened inertial to topocentric frame
  182. physics::frame<float> topocentric_frame =
  183. {
  184. topocentric_frame_translation.interpolate(context->alpha),
  185. topocentric_frame_rotation.interpolate(context->alpha)
  186. };
  187. // Get topocentric space sun position
  188. float3 sun_position = {0, 0, 0};
  189. if (sun_object != nullptr)
  190. {
  191. sun_position = math::normalize(sun_object->get_transform_tween().interpolate(context->alpha).translation);
  192. }
  193. // Get topocentric space moon position
  194. float3 moon_position = {0, 0, 0};
  195. // Draw sky model
  196. {
  197. rasterizer->use_program(*sky_shader_program);
  198. // Upload shader parameters
  199. if (model_view_projection_input)
  200. model_view_projection_input->upload(model_view_projection);
  201. if (horizon_color_input)
  202. horizon_color_input->upload(horizon_color);
  203. if (zenith_color_input)
  204. zenith_color_input->upload(zenith_color);
  205. if (sun_color_input)
  206. sun_color_input->upload(sun_color);
  207. if (mouse_input)
  208. mouse_input->upload(mouse_position);
  209. if (resolution_input)
  210. resolution_input->upload(resolution);
  211. if (time_input)
  212. time_input->upload(time);
  213. if (time_of_day_input)
  214. time_of_day_input->upload(time_of_day);
  215. if (blue_noise_map_input)
  216. blue_noise_map_input->upload(blue_noise_map);
  217. if (sky_gradient_input && sky_gradient)
  218. sky_gradient_input->upload(sky_gradient);
  219. if (sky_gradient2_input && sky_gradient2)
  220. sky_gradient2_input->upload(sky_gradient2);
  221. if (observer_location_input)
  222. observer_location_input->upload(observer_location);
  223. if (sun_position_input)
  224. sun_position_input->upload(sun_position);
  225. if (moon_position_input)
  226. moon_position_input->upload(moon_position);
  227. if (julian_day_input)
  228. julian_day_input->upload(julian_day);
  229. if (cos_moon_angular_radius_input)
  230. cos_moon_angular_radius_input->upload(cos_moon_angular_radius);
  231. if (cos_sun_angular_radius_input)
  232. cos_sun_angular_radius_input->upload(cos_sun_angular_radius);
  233. if (exposure_input)
  234. exposure_input->upload(exposure);
  235. sky_material->upload(context->alpha);
  236. rasterizer->draw_arrays(*sky_model_vao, sky_model_drawing_mode, sky_model_start_index, sky_model_index_count);
  237. }
  238. glEnable(GL_BLEND);
  239. //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  240. glBlendFunc(GL_ONE, GL_ONE);
  241. // Draw moon model
  242. if (moon_position.y >= -moon_angular_radius)
  243. {
  244. float moon_distance = (clip_near + clip_far) * 0.5f;
  245. float moon_radius = moon_angular_radius * moon_distance;
  246. math::transform<float> moon_transform;
  247. moon_transform.translation = moon_position * -moon_distance;
  248. moon_transform.rotation = math::quaternion<float>::identity();
  249. moon_transform.scale = {moon_radius, moon_radius, moon_radius};
  250. model = math::matrix_cast(moon_transform);
  251. model_view = view * model;
  252. model_view_projection = projection * model_view;
  253. float3x3 normal_model = math::transpose(math::inverse(math::resize<3, 3>(model)));
  254. rasterizer->use_program(*moon_shader_program);
  255. if (moon_model_view_projection_input)
  256. moon_model_view_projection_input->upload(model_view_projection);
  257. if (moon_normal_model_input)
  258. moon_normal_model_input->upload(normal_model);
  259. if (moon_moon_position_input)
  260. moon_moon_position_input->upload(moon_position);
  261. if (moon_sun_position_input)
  262. moon_sun_position_input->upload(sun_position);
  263. moon_material->upload(context->alpha);
  264. rasterizer->draw_arrays(*moon_model_vao, moon_model_drawing_mode, moon_model_start_index, moon_model_index_count);
  265. }
  266. // Draw stars
  267. {
  268. float star_distance = (clip_near + clip_far) * 0.5f;
  269. double lat = math::radians(1.0);
  270. double lst = time_of_day / 24.0f * math::two_pi<float>;
  271. //std::cout << "lst: " << lst << std::endl;
  272. /*
  273. double3x3 equatorial_to_horizontal = coordinates::rectangular::equatorial::to_horizontal(lat, lst);
  274. const double3x3 horizontal_to_local = coordinates::rectangular::rotate_x(-math::half_pi<double>) * coordinates::rectangular::rotate_z(-math::half_pi<double>);
  275. double3x3 rotation = horizontal_to_local * equatorial_to_horizontal;
  276. model = math::type_cast<float>(math::scale(math::resize<4, 4>(rotation), double3{star_distance, star_distance, star_distance}));;
  277. */
  278. //math::transform<float> star_transform;
  279. //star_transform.translation = {0.0, 0.0, 0.0};
  280. //star_transform.rotation = math::normalize(rotation_x * rotation_y);
  281. //star_transform.rotation = math::normalize(math::type_cast<float>(math::quaternion_cast(rotation)));
  282. //star_transform.rotation = math::identity_quaternion<float>;
  283. //star_transform.scale = {star_distance, star_distance, star_distance};
  284. //model = math::matrix_cast(star_transform);
  285. model = topocentric_frame.matrix();
  286. model = math::scale(model, {star_distance, star_distance, star_distance});
  287. model_view = view * model;
  288. rasterizer->use_program(*star_shader_program);
  289. if (star_model_view_input)
  290. star_model_view_input->upload(model_view);
  291. if (star_projection_input)
  292. star_projection_input->upload(projection);
  293. if (star_distance_input)
  294. star_distance_input->upload(star_distance);
  295. if (star_exposure_input)
  296. star_exposure_input->upload(exposure);
  297. rasterizer->draw_arrays(*star_catalog_vao, gl::drawing_mode::points, 0, star_count);
  298. }
  299. }
  300. void sky_pass::set_sky_model(const model* model)
  301. {
  302. sky_model = model;
  303. if (sky_model)
  304. {
  305. sky_model_vao = model->get_vertex_array();
  306. const std::vector<model_group*>& groups = *model->get_groups();
  307. for (model_group* group: groups)
  308. {
  309. sky_material = group->get_material();
  310. sky_model_drawing_mode = group->get_drawing_mode();
  311. sky_model_start_index = group->get_start_index();
  312. sky_model_index_count = group->get_index_count();
  313. }
  314. if (sky_material)
  315. {
  316. sky_shader_program = sky_material->get_shader_program();
  317. if (sky_shader_program)
  318. {
  319. model_view_projection_input = sky_shader_program->get_input("model_view_projection");
  320. horizon_color_input = sky_shader_program->get_input("horizon_color");
  321. zenith_color_input = sky_shader_program->get_input("zenith_color");
  322. sun_color_input = sky_shader_program->get_input("sun_color");
  323. mouse_input = sky_shader_program->get_input("mouse");
  324. resolution_input = sky_shader_program->get_input("resolution");
  325. time_input = sky_shader_program->get_input("time");
  326. time_of_day_input = sky_shader_program->get_input("time_of_day");
  327. blue_noise_map_input = sky_shader_program->get_input("blue_noise_map");
  328. sky_gradient_input = sky_shader_program->get_input("sky_gradient");
  329. sky_gradient2_input = sky_shader_program->get_input("sky_gradient2");
  330. observer_location_input = sky_shader_program->get_input("observer_location");
  331. sun_position_input = sky_shader_program->get_input("sun_position");
  332. moon_position_input = sky_shader_program->get_input("moon_position");
  333. julian_day_input = sky_shader_program->get_input("julian_day");
  334. cos_moon_angular_radius_input = sky_shader_program->get_input("cos_moon_angular_radius");
  335. cos_sun_angular_radius_input = sky_shader_program->get_input("cos_sun_angular_radius");
  336. exposure_input = sky_shader_program->get_input("camera.exposure");
  337. }
  338. }
  339. }
  340. else
  341. {
  342. sky_model_vao = nullptr;
  343. }
  344. }
  345. void sky_pass::set_moon_model(const model* model)
  346. {
  347. moon_model = model;
  348. if (moon_model)
  349. {
  350. moon_model_vao = model->get_vertex_array();
  351. const std::vector<model_group*>& groups = *model->get_groups();
  352. for (model_group* group: groups)
  353. {
  354. moon_material = group->get_material();
  355. moon_model_drawing_mode = group->get_drawing_mode();
  356. moon_model_start_index = group->get_start_index();
  357. moon_model_index_count = group->get_index_count();
  358. }
  359. if (moon_material)
  360. {
  361. moon_shader_program = moon_material->get_shader_program();
  362. if (moon_shader_program)
  363. {
  364. moon_model_view_projection_input = moon_shader_program->get_input("model_view_projection");
  365. moon_normal_model_input = moon_shader_program->get_input("normal_model");
  366. moon_moon_position_input = moon_shader_program->get_input("moon_position");
  367. moon_sun_position_input = moon_shader_program->get_input("sun_position");
  368. }
  369. }
  370. }
  371. else
  372. {
  373. moon_model = nullptr;
  374. }
  375. }
  376. void sky_pass::update_tweens()
  377. {
  378. julian_day_tween.update();
  379. time_of_day_tween.update();
  380. horizon_color_tween.update();
  381. zenith_color_tween.update();
  382. topocentric_frame_translation.update();
  383. topocentric_frame_rotation.update();
  384. sun_color_tween.update();
  385. }
  386. void sky_pass::set_time_of_day(float time)
  387. {
  388. time_of_day_tween[1] = time;
  389. }
  390. void sky_pass::set_time_tween(const tween<double>* time)
  391. {
  392. this->time_tween = time;
  393. }
  394. void sky_pass::set_blue_noise_map(const gl::texture_2d* texture)
  395. {
  396. blue_noise_map = texture;
  397. }
  398. void sky_pass::set_sky_gradient(const gl::texture_2d* texture, const gl::texture_2d* texture2)
  399. {
  400. sky_gradient = texture;
  401. sky_gradient2 = texture2;
  402. }
  403. void sky_pass::set_julian_day(float jd)
  404. {
  405. julian_day_tween[1] = jd;
  406. }
  407. void sky_pass::set_observer_location(float altitude, float latitude, float longitude)
  408. {
  409. observer_location = {altitude, latitude, longitude};
  410. }
  411. void sky_pass::set_moon_angular_radius(float radius)
  412. {
  413. moon_angular_radius = radius;
  414. cos_moon_angular_radius = std::cos(moon_angular_radius);
  415. }
  416. void sky_pass::set_sun_angular_radius(float radius)
  417. {
  418. sun_angular_radius = radius;
  419. cos_sun_angular_radius = std::cos(sun_angular_radius);
  420. }
  421. void sky_pass::set_topocentric_frame(const physics::frame<float>& frame)
  422. {
  423. topocentric_frame_translation[1] = frame.translation;
  424. topocentric_frame_rotation[1] = frame.rotation;
  425. }
  426. void sky_pass::set_sun_object(const scene::object_base* object)
  427. {
  428. sun_object = object;
  429. }
  430. void sky_pass::set_horizon_color(const float3& color)
  431. {
  432. horizon_color_tween[1] = color;
  433. }
  434. void sky_pass::set_zenith_color(const float3& color)
  435. {
  436. zenith_color_tween[1] = color;
  437. }
  438. void sky_pass::set_sun_color(const float3& color)
  439. {
  440. sun_color_tween[1] = color;
  441. }
  442. void sky_pass::handle_event(const mouse_moved_event& event)
  443. {
  444. mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};
  445. }