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

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