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

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