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

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