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

414 lines
13 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_model_vao(nullptr),
  59. moon_material(nullptr),
  60. moon_shader_program(nullptr),
  61. stars_model(nullptr),
  62. stars_model_vao(nullptr),
  63. star_material(nullptr),
  64. star_shader_program(nullptr),
  65. time_tween(nullptr),
  66. observer_altitude_tween(0.0f, math::lerp<float, float>),
  67. sun_position_tween(float3{1.0f, 0.0f, 0.0f}, math::lerp<float3, float>),
  68. sun_color_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp<float3, float>),
  69. topocentric_frame_translation({0, 0, 0}, math::lerp<float3, float>),
  70. topocentric_frame_rotation(math::quaternion<float>::identity(), math::nlerp<float>)
  71. {}
  72. sky_pass::~sky_pass()
  73. {}
  74. void sky_pass::render(render_context* context) const
  75. {
  76. rasterizer->use_framebuffer(*framebuffer);
  77. glDisable(GL_BLEND);
  78. glDisable(GL_DEPTH_TEST);
  79. glDepthMask(GL_FALSE);
  80. glEnable(GL_CULL_FACE);
  81. glCullFace(GL_BACK);
  82. auto viewport = framebuffer->get_dimensions();
  83. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  84. float time = (*time_tween)[context->alpha];
  85. float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
  86. const scene::camera& camera = *context->camera;
  87. float clip_near = camera.get_clip_near_tween().interpolate(context->alpha);
  88. float clip_far = camera.get_clip_far_tween().interpolate(context->alpha);
  89. float3 model_scale = float3{1.0f, 1.0f, 1.0f} * (clip_near + clip_far) * 0.5f;
  90. float4x4 model = math::scale(math::identity4x4<float>, model_scale);
  91. float4x4 view = math::resize<4, 4>(math::resize<3, 3>(camera.get_view_tween().interpolate(context->alpha)));
  92. float4x4 model_view = view * model;
  93. float4x4 projection = camera.get_projection_tween().interpolate(context->alpha);
  94. float4x4 view_projection = projection * view;
  95. float4x4 model_view_projection = projection * model_view;
  96. float exposure = std::exp2(camera.get_exposure_tween().interpolate(context->alpha));
  97. // Interpolate observer altitude
  98. float observer_altitude = observer_altitude_tween.interpolate(context->alpha);
  99. // Construct tweened inertial to topocentric frame
  100. physics::frame<float> topocentric_frame =
  101. {
  102. topocentric_frame_translation.interpolate(context->alpha),
  103. topocentric_frame_rotation.interpolate(context->alpha)
  104. };
  105. // Get topocentric space direction to sun
  106. float3 sun_position = sun_position_tween.interpolate(context->alpha);
  107. float3 sun_direction = math::normalize(sun_position);
  108. // Interpolate sun color
  109. float3 sun_color = sun_color_tween.interpolate(context->alpha);
  110. // Draw sky model
  111. {
  112. rasterizer->use_program(*sky_shader_program);
  113. // Upload shader parameters
  114. if (model_view_projection_input)
  115. model_view_projection_input->upload(model_view_projection);
  116. if (mouse_input)
  117. mouse_input->upload(mouse_position);
  118. if (resolution_input)
  119. resolution_input->upload(resolution);
  120. if (time_input)
  121. time_input->upload(time);
  122. if (exposure_input)
  123. exposure_input->upload(exposure);
  124. if (observer_altitude_input)
  125. observer_altitude_input->upload(observer_altitude);
  126. if (sun_direction_input)
  127. sun_direction_input->upload(sun_direction);
  128. if (sun_angular_radius_input)
  129. sun_angular_radius_input->upload(sun_angular_radius);
  130. if (sun_color_input)
  131. sun_color_input->upload(sun_color);
  132. if (scale_height_rm_input)
  133. scale_height_rm_input->upload(scale_height_rm);
  134. if (rayleigh_scattering_input)
  135. rayleigh_scattering_input->upload(rayleigh_scattering);
  136. if (mie_scattering_input)
  137. mie_scattering_input->upload(mie_scattering);
  138. if (mie_anisotropy_input)
  139. mie_anisotropy_input->upload(mie_anisotropy);
  140. if (atmosphere_radii_input)
  141. atmosphere_radii_input->upload(atmosphere_radii);
  142. sky_material->upload(context->alpha);
  143. rasterizer->draw_arrays(*sky_model_vao, sky_model_drawing_mode, sky_model_start_index, sky_model_index_count);
  144. }
  145. glEnable(GL_BLEND);
  146. //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  147. glBlendFunc(GL_ONE, GL_ONE);
  148. // Draw stars
  149. if (stars_model)
  150. {
  151. float star_distance = (clip_near + clip_far) * 0.5f;
  152. model = math::resize<4, 4>(math::matrix_cast<float>(topocentric_frame.rotation));
  153. model = math::scale(model, {star_distance, star_distance, star_distance});
  154. model_view = view * model;
  155. rasterizer->use_program(*star_shader_program);
  156. if (star_model_view_input)
  157. star_model_view_input->upload(model_view);
  158. if (star_projection_input)
  159. star_projection_input->upload(projection);
  160. if (star_distance_input)
  161. star_distance_input->upload(star_distance);
  162. if (star_exposure_input)
  163. star_exposure_input->upload(exposure);
  164. star_material->upload(context->alpha);
  165. rasterizer->draw_arrays(*stars_model_vao, stars_model_drawing_mode, stars_model_start_index, stars_model_index_count);
  166. }
  167. // Draw moon model
  168. /*
  169. float3 moon_position = {0, 0, 0};
  170. if (moon_position.y >= -moon_angular_radius)
  171. {
  172. float moon_distance = (clip_near + clip_far) * 0.5f;
  173. float moon_radius = moon_angular_radius * moon_distance;
  174. math::transform<float> moon_transform;
  175. moon_transform.translation = moon_position * -moon_distance;
  176. moon_transform.rotation = math::quaternion<float>::identity();
  177. moon_transform.scale = {moon_radius, moon_radius, moon_radius};
  178. model = math::matrix_cast(moon_transform);
  179. model_view = view * model;
  180. model_view_projection = projection * model_view;
  181. float3x3 normal_model = math::transpose(math::inverse(math::resize<3, 3>(model)));
  182. rasterizer->use_program(*moon_shader_program);
  183. if (moon_model_view_projection_input)
  184. moon_model_view_projection_input->upload(model_view_projection);
  185. if (moon_normal_model_input)
  186. moon_normal_model_input->upload(normal_model);
  187. if (moon_moon_position_input)
  188. moon_moon_position_input->upload(moon_position);
  189. if (moon_sun_position_input)
  190. moon_sun_position_input->upload(sun_position);
  191. moon_material->upload(context->alpha);
  192. rasterizer->draw_arrays(*moon_model_vao, moon_model_drawing_mode, moon_model_start_index, moon_model_index_count);
  193. }
  194. */
  195. }
  196. void sky_pass::set_sky_model(const model* model)
  197. {
  198. sky_model = model;
  199. if (sky_model)
  200. {
  201. sky_model_vao = model->get_vertex_array();
  202. const std::vector<model_group*>& groups = *model->get_groups();
  203. for (model_group* group: groups)
  204. {
  205. sky_material = group->get_material();
  206. sky_model_drawing_mode = group->get_drawing_mode();
  207. sky_model_start_index = group->get_start_index();
  208. sky_model_index_count = group->get_index_count();
  209. }
  210. if (sky_material)
  211. {
  212. sky_shader_program = sky_material->get_shader_program();
  213. if (sky_shader_program)
  214. {
  215. model_view_projection_input = sky_shader_program->get_input("model_view_projection");
  216. mouse_input = sky_shader_program->get_input("mouse");
  217. resolution_input = sky_shader_program->get_input("resolution");
  218. time_input = sky_shader_program->get_input("time");
  219. exposure_input = sky_shader_program->get_input("camera.exposure");
  220. observer_altitude_input = sky_shader_program->get_input("observer_altitude");
  221. sun_direction_input = sky_shader_program->get_input("sun_direction");
  222. sun_color_input = sky_shader_program->get_input("sun_color");
  223. sun_angular_radius_input = sky_shader_program->get_input("sun_angular_radius");
  224. scale_height_rm_input = sky_shader_program->get_input("scale_height_rm");
  225. rayleigh_scattering_input = sky_shader_program->get_input("rayleigh_scattering");
  226. mie_scattering_input = sky_shader_program->get_input("mie_scattering");
  227. mie_anisotropy_input = sky_shader_program->get_input("mie_anisotropy");
  228. atmosphere_radii_input = sky_shader_program->get_input("atmosphere_radii");
  229. }
  230. }
  231. }
  232. else
  233. {
  234. sky_model_vao = nullptr;
  235. }
  236. }
  237. void sky_pass::set_moon_model(const model* model)
  238. {
  239. moon_model = model;
  240. if (moon_model)
  241. {
  242. moon_model_vao = model->get_vertex_array();
  243. const std::vector<model_group*>& groups = *model->get_groups();
  244. for (model_group* group: groups)
  245. {
  246. moon_material = group->get_material();
  247. moon_model_drawing_mode = group->get_drawing_mode();
  248. moon_model_start_index = group->get_start_index();
  249. moon_model_index_count = group->get_index_count();
  250. }
  251. if (moon_material)
  252. {
  253. moon_shader_program = moon_material->get_shader_program();
  254. if (moon_shader_program)
  255. {
  256. moon_model_view_projection_input = moon_shader_program->get_input("model_view_projection");
  257. moon_normal_model_input = moon_shader_program->get_input("normal_model");
  258. moon_moon_position_input = moon_shader_program->get_input("moon_position");
  259. moon_sun_position_input = moon_shader_program->get_input("sun_position");
  260. }
  261. }
  262. }
  263. else
  264. {
  265. moon_model = nullptr;
  266. }
  267. }
  268. void sky_pass::set_stars_model(const model* model)
  269. {
  270. stars_model = model;
  271. if (stars_model)
  272. {
  273. stars_model_vao = model->get_vertex_array();
  274. const std::vector<model_group*>& groups = *model->get_groups();
  275. for (model_group* group: groups)
  276. {
  277. star_material = group->get_material();
  278. stars_model_drawing_mode = group->get_drawing_mode();
  279. stars_model_start_index = group->get_start_index();
  280. stars_model_index_count = group->get_index_count();
  281. }
  282. if (star_material)
  283. {
  284. star_shader_program = star_material->get_shader_program();
  285. if (star_shader_program)
  286. {
  287. star_model_view_input = star_shader_program->get_input("model_view");
  288. star_projection_input = star_shader_program->get_input("projection");
  289. star_distance_input = star_shader_program->get_input("star_distance");
  290. star_exposure_input = star_shader_program->get_input("camera.exposure");
  291. }
  292. }
  293. }
  294. else
  295. {
  296. stars_model = nullptr;
  297. }
  298. }
  299. void sky_pass::update_tweens()
  300. {
  301. observer_altitude_tween.update();
  302. sun_position_tween.update();
  303. sun_color_tween.update();
  304. topocentric_frame_translation.update();
  305. topocentric_frame_rotation.update();
  306. }
  307. void sky_pass::set_time_tween(const tween<double>* time)
  308. {
  309. this->time_tween = time;
  310. }
  311. void sky_pass::set_topocentric_frame(const physics::frame<float>& frame)
  312. {
  313. topocentric_frame_translation[1] = frame.translation;
  314. topocentric_frame_rotation[1] = frame.rotation;
  315. }
  316. void sky_pass::set_sun_position(const float3& position)
  317. {
  318. sun_position_tween[1] = position;
  319. }
  320. void sky_pass::set_sun_color(const float3& color)
  321. {
  322. sun_color_tween[1] = color;
  323. }
  324. void sky_pass::set_sun_angular_radius(float radius)
  325. {
  326. sun_angular_radius = radius;
  327. }
  328. void sky_pass::set_observer_altitude(float altitude)
  329. {
  330. observer_altitude_tween[1] = altitude;
  331. }
  332. void sky_pass::set_scale_heights(float rayleigh, float mie)
  333. {
  334. scale_height_rm = {rayleigh, mie};
  335. }
  336. void sky_pass::set_scattering_coefficients(const float3& r, const float3& m)
  337. {
  338. rayleigh_scattering = r;
  339. mie_scattering = m;
  340. }
  341. void sky_pass::set_mie_anisotropy(float g)
  342. {
  343. mie_anisotropy = {g, g * g};
  344. }
  345. void sky_pass::set_atmosphere_radii(float inner, float outer)
  346. {
  347. atmosphere_radii.x = inner;
  348. atmosphere_radii.y = outer;
  349. atmosphere_radii.z = outer * outer;
  350. }
  351. void sky_pass::handle_event(const mouse_moved_event& event)
  352. {
  353. mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};
  354. }