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

491 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 "math/interpolation.hpp"
  41. #include "astro/astro.hpp"
  42. #include <cmath>
  43. #include <stdexcept>
  44. #include <glad/glad.h>
  45. #include <iostream>
  46. sky_pass::sky_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffer, resource_manager* resource_manager):
  47. render_pass(rasterizer, framebuffer),
  48. mouse_position({0.0f, 0.0f}),
  49. sky_model(nullptr),
  50. sky_material(nullptr),
  51. sky_model_vao(nullptr),
  52. sky_shader_program(nullptr),
  53. moon_model(nullptr),
  54. moon_material(nullptr),
  55. moon_model_vao(nullptr),
  56. moon_shader_program(nullptr),
  57. blue_noise_map(nullptr),
  58. sky_gradient(nullptr),
  59. sky_gradient2(nullptr),
  60. observer_location{0.0f, 0.0f, 0.0f},
  61. time_tween(nullptr),
  62. time_of_day_tween(0.0, math::lerp<float, float>),
  63. julian_day_tween(0.0, math::lerp<float, float>),
  64. sun_position_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp<float3, float>),
  65. sun_az_el_tween(float2{0.0f, 0.0f}, math::lerp<float2, float>),
  66. moon_position_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp<float3, float>),
  67. moon_az_el_tween(float2{0.0f, 0.0f}, math::lerp<float2, 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. {
  71. // Load star catalog
  72. string_table* star_catalog = resource_manager->load<string_table>("stars.csv");
  73. // Allocate star catalog vertex data
  74. star_count = 0;
  75. if (star_catalog->size() > 0)
  76. star_count = star_catalog->size() - 1;
  77. std::size_t star_vertex_size = 6;
  78. std::size_t star_vertex_stride = star_vertex_size * sizeof(float);
  79. float* star_vertex_data = new float[star_count * star_vertex_size];
  80. float* star_vertex = star_vertex_data;
  81. // Build star catalog vertex data
  82. for (std::size_t i = 1; i < star_catalog->size(); ++i)
  83. {
  84. const string_table_row& catalog_row = (*star_catalog)[i];
  85. double ra = 0.0;
  86. double dec = 0.0;
  87. double vmag = 0.0;
  88. double bv_color = 0.0;
  89. // Parse star catalog entry
  90. try
  91. {
  92. ra = std::stod(catalog_row[1]);
  93. dec = std::stod(catalog_row[2]);
  94. vmag = std::stod(catalog_row[3]);
  95. bv_color = std::stod(catalog_row[4]);
  96. }
  97. catch (const std::exception& e)
  98. {}
  99. // Convert right ascension and declination from degrees to radians
  100. ra = math::wrap_radians(math::radians(ra));
  101. dec = math::wrap_radians(math::radians(dec));
  102. // Transform spherical coordinates to rectangular (Cartesian) coordinates
  103. double3 spherical = {1.0, dec, ra};
  104. double3 rectangular = astro::spherical_to_rectangular(spherical);
  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>(rectangular.x);
  119. *(star_vertex++) = static_cast<float>(rectangular.y);
  120. *(star_vertex++) = static_cast<float>(rectangular.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. math::quaternion<float> rotation_y = math::angle_axis(time_of_day / 24.0f * -math::two_pi<float>, {0, 1, 0});
  254. math::quaternion<float> rotation_x = math::angle_axis(-math::half_pi<float> + math::radians(30.0f), {1, 0, 0});
  255. math::transform<float> star_transform;
  256. star_transform.translation = {0.0, 0.0, 0.0};
  257. //star_transform.rotation = math::normalize(rotation_x * rotation_y);
  258. star_transform.rotation = math::normalize(rotation_x * rotation_y * math::angle_axis(math::radians(-90.0f), {1, 0, 0}));
  259. //star_transform.rotation = math::identity_quaternion<float>;
  260. star_transform.scale = {star_distance, star_distance, star_distance};
  261. model = math::matrix_cast(star_transform);
  262. model_view = view * model;
  263. rasterizer->use_program(*star_shader_program);
  264. if (star_model_view_input)
  265. star_model_view_input->upload(model_view);
  266. if (star_projection_input)
  267. star_projection_input->upload(projection);
  268. if (star_distance_input)
  269. star_distance_input->upload(star_distance);
  270. if (star_exposure_input)
  271. star_exposure_input->upload(exposure);
  272. rasterizer->draw_arrays(*star_catalog_vao, gl::drawing_mode::points, 0, star_count);
  273. }
  274. }
  275. void sky_pass::set_sky_model(const model* model)
  276. {
  277. sky_model = model;
  278. if (sky_model)
  279. {
  280. sky_model_vao = model->get_vertex_array();
  281. const std::vector<model_group*>& groups = *model->get_groups();
  282. for (model_group* group: groups)
  283. {
  284. sky_material = group->get_material();
  285. sky_model_drawing_mode = group->get_drawing_mode();
  286. sky_model_start_index = group->get_start_index();
  287. sky_model_index_count = group->get_index_count();
  288. }
  289. if (sky_material)
  290. {
  291. sky_shader_program = sky_material->get_shader_program();
  292. if (sky_shader_program)
  293. {
  294. model_view_projection_input = sky_shader_program->get_input("model_view_projection");
  295. horizon_color_input = sky_shader_program->get_input("horizon_color");
  296. zenith_color_input = sky_shader_program->get_input("zenith_color");
  297. mouse_input = sky_shader_program->get_input("mouse");
  298. resolution_input = sky_shader_program->get_input("resolution");
  299. time_input = sky_shader_program->get_input("time");
  300. time_of_day_input = sky_shader_program->get_input("time_of_day");
  301. blue_noise_map_input = sky_shader_program->get_input("blue_noise_map");
  302. sky_gradient_input = sky_shader_program->get_input("sky_gradient");
  303. sky_gradient2_input = sky_shader_program->get_input("sky_gradient2");
  304. observer_location_input = sky_shader_program->get_input("observer_location");
  305. sun_position_input = sky_shader_program->get_input("sun_position");
  306. sun_az_el_input = sky_shader_program->get_input("sun_az_el");
  307. moon_position_input = sky_shader_program->get_input("moon_position");
  308. moon_az_el_input = sky_shader_program->get_input("moon_az_el");
  309. julian_day_input = sky_shader_program->get_input("julian_day");
  310. cos_moon_angular_radius_input = sky_shader_program->get_input("cos_moon_angular_radius");
  311. cos_sun_angular_radius_input = sky_shader_program->get_input("cos_sun_angular_radius");
  312. exposure_input = sky_shader_program->get_input("camera.exposure");
  313. }
  314. }
  315. }
  316. else
  317. {
  318. sky_model_vao = nullptr;
  319. }
  320. }
  321. void sky_pass::set_moon_model(const model* model)
  322. {
  323. moon_model = model;
  324. if (moon_model)
  325. {
  326. moon_model_vao = model->get_vertex_array();
  327. const std::vector<model_group*>& groups = *model->get_groups();
  328. for (model_group* group: groups)
  329. {
  330. moon_material = group->get_material();
  331. moon_model_drawing_mode = group->get_drawing_mode();
  332. moon_model_start_index = group->get_start_index();
  333. moon_model_index_count = group->get_index_count();
  334. }
  335. if (moon_material)
  336. {
  337. moon_shader_program = moon_material->get_shader_program();
  338. if (moon_shader_program)
  339. {
  340. moon_model_view_projection_input = moon_shader_program->get_input("model_view_projection");
  341. moon_normal_model_input = moon_shader_program->get_input("normal_model");
  342. moon_moon_position_input = moon_shader_program->get_input("moon_position");
  343. moon_sun_position_input = moon_shader_program->get_input("sun_position");
  344. }
  345. }
  346. }
  347. else
  348. {
  349. moon_model = nullptr;
  350. }
  351. }
  352. void sky_pass::update_tweens()
  353. {
  354. julian_day_tween.update();
  355. sun_position_tween.update();
  356. sun_az_el_tween.update();
  357. moon_position_tween.update();
  358. moon_az_el_tween.update();
  359. time_of_day_tween.update();
  360. horizon_color_tween.update();
  361. zenith_color_tween.update();
  362. }
  363. void sky_pass::set_time_of_day(float time)
  364. {
  365. time_of_day_tween[1] = time;
  366. }
  367. void sky_pass::set_time_tween(const tween<double>* time)
  368. {
  369. this->time_tween = time;
  370. }
  371. void sky_pass::set_blue_noise_map(const gl::texture_2d* texture)
  372. {
  373. blue_noise_map = texture;
  374. }
  375. void sky_pass::set_sky_gradient(const gl::texture_2d* texture, const gl::texture_2d* texture2)
  376. {
  377. sky_gradient = texture;
  378. sky_gradient2 = texture2;
  379. }
  380. void sky_pass::set_julian_day(float jd)
  381. {
  382. julian_day_tween[1] = jd;
  383. }
  384. void sky_pass::set_observer_location(float altitude, float latitude, float longitude)
  385. {
  386. observer_location = {altitude, latitude, longitude};
  387. }
  388. void sky_pass::set_sun_coordinates(const float3& position, const float2& az_el)
  389. {
  390. sun_position_tween[1] = position;
  391. sun_az_el_tween[1] = az_el;
  392. }
  393. void sky_pass::set_moon_coordinates(const float3& position, const float2& az_el)
  394. {
  395. moon_position_tween[1] = position;
  396. moon_az_el_tween[1] = az_el;
  397. }
  398. void sky_pass::set_moon_rotation(const math::quaternion<float>& rotation)
  399. {
  400. moon_rotation = rotation;
  401. }
  402. void sky_pass::set_moon_angular_radius(float radius)
  403. {
  404. moon_angular_radius = radius;
  405. cos_moon_angular_radius = std::cos(moon_angular_radius);
  406. }
  407. void sky_pass::set_sun_angular_radius(float radius)
  408. {
  409. sun_angular_radius = radius;
  410. cos_sun_angular_radius = std::cos(sun_angular_radius);
  411. }
  412. void sky_pass::set_horizon_color(const float3& color)
  413. {
  414. horizon_color_tween[1] = color;
  415. }
  416. void sky_pass::set_zenith_color(const float3& color)
  417. {
  418. zenith_color_tween[1] = color;
  419. }
  420. void sky_pass::handle_event(const mouse_moved_event& event)
  421. {
  422. mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};
  423. }