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

362 lines
12 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 "gl/rasterizer.hpp"
  22. #include "gl/framebuffer.hpp"
  23. #include "gl/shader-program.hpp"
  24. #include "gl/shader-input.hpp"
  25. #include "gl/vertex-buffer.hpp"
  26. #include "gl/vertex-array.hpp"
  27. #include "gl/vertex-attribute-type.hpp"
  28. #include "gl/drawing-mode.hpp"
  29. #include "gl/texture-2d.hpp"
  30. #include "gl/texture-wrapping.hpp"
  31. #include "gl/texture-filter.hpp"
  32. #include "renderer/vertex-attributes.hpp"
  33. #include "renderer/render-context.hpp"
  34. #include "renderer/model.hpp"
  35. #include "renderer/material.hpp"
  36. #include "scene/camera.hpp"
  37. #include "utility/fundamental-types.hpp"
  38. #include "math/interpolation.hpp"
  39. #include <cmath>
  40. #include <glad/glad.h>
  41. sky_pass::sky_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffer, resource_manager* resource_manager):
  42. render_pass(rasterizer, framebuffer),
  43. mouse_position({0.0f, 0.0f}),
  44. sky_model(nullptr),
  45. sky_material(nullptr),
  46. sky_model_vao(nullptr),
  47. sky_shader_program(nullptr),
  48. moon_model(nullptr),
  49. moon_material(nullptr),
  50. moon_model_vao(nullptr),
  51. moon_shader_program(nullptr),
  52. blue_noise_map(nullptr),
  53. sky_gradient(nullptr),
  54. sky_gradient2(nullptr),
  55. observer_location{0.0f, 0.0f, 0.0f},
  56. time_tween(nullptr),
  57. time_of_day_tween(0.0, math::lerp<float, float>),
  58. julian_day_tween(0.0, math::lerp<float, float>),
  59. sun_position_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp<float3, float>),
  60. sun_az_el_tween(float2{0.0f, 0.0f}, math::lerp<float2, float>),
  61. moon_position_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp<float3, float>),
  62. moon_az_el_tween(float2{0.0f, 0.0f}, math::lerp<float2, float>),
  63. horizon_color_tween(float3{0.0f, 0.0f, 0.0f}, math::lerp<float3, float>),
  64. zenith_color_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp<float3, float>)
  65. {}
  66. sky_pass::~sky_pass()
  67. {}
  68. void sky_pass::render(render_context* context) const
  69. {
  70. rasterizer->use_framebuffer(*framebuffer);
  71. glDisable(GL_BLEND);
  72. glDisable(GL_DEPTH_TEST);
  73. glDepthMask(GL_FALSE);
  74. glEnable(GL_CULL_FACE);
  75. glCullFace(GL_BACK);
  76. auto viewport = framebuffer->get_dimensions();
  77. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  78. float time = (*time_tween)[context->alpha];
  79. float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
  80. const scene::camera& camera = *context->camera;
  81. float clip_near = camera.get_clip_near_tween().interpolate(context->alpha);
  82. float clip_far = camera.get_clip_far_tween().interpolate(context->alpha);
  83. float3 model_scale = float3{1.0f, 1.0f, 1.0f} * (clip_near + clip_far) * 0.5f;
  84. float4x4 model = math::scale(math::identity4x4<float>, model_scale);
  85. float4x4 view = math::resize<4, 4>(math::resize<3, 3>(camera.get_view_tween().interpolate(context->alpha)));
  86. float4x4 model_view = view * model;
  87. float4x4 projection = camera.get_projection_tween().interpolate(context->alpha);
  88. float4x4 model_view_projection = projection * model_view;
  89. float exposure = std::exp2(camera.get_exposure_tween().interpolate(context->alpha));
  90. float time_of_day = time_of_day_tween.interpolate(context->alpha);
  91. float julian_day = julian_day_tween.interpolate(context->alpha);
  92. float3 sun_position = sun_position_tween.interpolate(context->alpha);
  93. float2 sun_az_el = sun_az_el_tween.interpolate(context->alpha);
  94. float3 moon_position = moon_position_tween.interpolate(context->alpha);
  95. float2 moon_az_el = moon_az_el_tween.interpolate(context->alpha);
  96. float3 horizon_color = horizon_color_tween.interpolate(context->alpha);
  97. float3 zenith_color = zenith_color_tween.interpolate(context->alpha);
  98. // Draw sky model
  99. {
  100. rasterizer->use_program(*sky_shader_program);
  101. // Upload shader parameters
  102. if (model_view_projection_input)
  103. model_view_projection_input->upload(model_view_projection);
  104. if (horizon_color_input)
  105. horizon_color_input->upload(horizon_color);
  106. if (zenith_color_input)
  107. zenith_color_input->upload(zenith_color);
  108. if (mouse_input)
  109. mouse_input->upload(mouse_position);
  110. if (resolution_input)
  111. resolution_input->upload(resolution);
  112. if (time_input)
  113. time_input->upload(time);
  114. if (time_of_day_input)
  115. time_of_day_input->upload(time_of_day);
  116. if (blue_noise_map_input)
  117. blue_noise_map_input->upload(blue_noise_map);
  118. if (sky_gradient_input && sky_gradient)
  119. sky_gradient_input->upload(sky_gradient);
  120. if (sky_gradient2_input && sky_gradient2)
  121. sky_gradient2_input->upload(sky_gradient2);
  122. if (observer_location_input)
  123. observer_location_input->upload(observer_location);
  124. if (sun_position_input)
  125. sun_position_input->upload(sun_position);
  126. if (sun_az_el_input)
  127. sun_az_el_input->upload(sun_az_el);
  128. if (moon_position_input)
  129. moon_position_input->upload(moon_position);
  130. if (moon_az_el_input)
  131. moon_az_el_input->upload(moon_az_el);
  132. if (julian_day_input)
  133. julian_day_input->upload(julian_day);
  134. if (cos_moon_angular_radius_input)
  135. cos_moon_angular_radius_input->upload(cos_moon_angular_radius);
  136. if (cos_sun_angular_radius_input)
  137. cos_sun_angular_radius_input->upload(cos_sun_angular_radius);
  138. if (exposure_input)
  139. exposure_input->upload(exposure);
  140. sky_material->upload(context->alpha);
  141. rasterizer->draw_arrays(*sky_model_vao, sky_model_drawing_mode, sky_model_start_index, sky_model_index_count);
  142. }
  143. // Draw moon model
  144. if (moon_az_el[1] >= -moon_angular_radius)
  145. {
  146. glEnable(GL_BLEND);
  147. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  148. float moon_distance = (clip_near + clip_far) * 0.5f;
  149. float moon_radius = moon_angular_radius * moon_distance;
  150. math::transform<float> moon_transform;
  151. moon_transform.translation = moon_position * -moon_distance;
  152. moon_transform.rotation = moon_rotation;
  153. moon_transform.scale = {moon_radius, moon_radius, moon_radius};
  154. model = math::matrix_cast(moon_transform);
  155. model_view = view * model;
  156. model_view_projection = projection * model_view;
  157. float3x3 normal_model = math::transpose(math::inverse(math::resize<3, 3>(model)));
  158. rasterizer->use_program(*moon_shader_program);
  159. if (moon_model_view_projection_input)
  160. moon_model_view_projection_input->upload(model_view_projection);
  161. if (moon_normal_model_input)
  162. moon_normal_model_input->upload(normal_model);
  163. if (moon_moon_position_input)
  164. moon_moon_position_input->upload(moon_position);
  165. if (moon_sun_position_input)
  166. moon_sun_position_input->upload(sun_position);
  167. moon_material->upload(context->alpha);
  168. rasterizer->draw_arrays(*moon_model_vao, moon_model_drawing_mode, moon_model_start_index, moon_model_index_count);
  169. }
  170. }
  171. void sky_pass::set_sky_model(const model* model)
  172. {
  173. sky_model = model;
  174. if (sky_model)
  175. {
  176. sky_model_vao = model->get_vertex_array();
  177. const std::vector<model_group*>& groups = *model->get_groups();
  178. for (model_group* group: groups)
  179. {
  180. sky_material = group->get_material();
  181. sky_model_drawing_mode = group->get_drawing_mode();
  182. sky_model_start_index = group->get_start_index();
  183. sky_model_index_count = group->get_index_count();
  184. }
  185. if (sky_material)
  186. {
  187. sky_shader_program = sky_material->get_shader_program();
  188. if (sky_shader_program)
  189. {
  190. model_view_projection_input = sky_shader_program->get_input("model_view_projection");
  191. horizon_color_input = sky_shader_program->get_input("horizon_color");
  192. zenith_color_input = sky_shader_program->get_input("zenith_color");
  193. mouse_input = sky_shader_program->get_input("mouse");
  194. resolution_input = sky_shader_program->get_input("resolution");
  195. time_input = sky_shader_program->get_input("time");
  196. time_of_day_input = sky_shader_program->get_input("time_of_day");
  197. blue_noise_map_input = sky_shader_program->get_input("blue_noise_map");
  198. sky_gradient_input = sky_shader_program->get_input("sky_gradient");
  199. sky_gradient2_input = sky_shader_program->get_input("sky_gradient2");
  200. observer_location_input = sky_shader_program->get_input("observer_location");
  201. sun_position_input = sky_shader_program->get_input("sun_position");
  202. sun_az_el_input = sky_shader_program->get_input("sun_az_el");
  203. moon_position_input = sky_shader_program->get_input("moon_position");
  204. moon_az_el_input = sky_shader_program->get_input("moon_az_el");
  205. julian_day_input = sky_shader_program->get_input("julian_day");
  206. cos_moon_angular_radius_input = sky_shader_program->get_input("cos_moon_angular_radius");
  207. cos_sun_angular_radius_input = sky_shader_program->get_input("cos_sun_angular_radius");
  208. exposure_input = sky_shader_program->get_input("camera.exposure");
  209. }
  210. }
  211. }
  212. else
  213. {
  214. sky_model_vao = nullptr;
  215. }
  216. }
  217. void sky_pass::set_moon_model(const model* model)
  218. {
  219. moon_model = model;
  220. if (moon_model)
  221. {
  222. moon_model_vao = model->get_vertex_array();
  223. const std::vector<model_group*>& groups = *model->get_groups();
  224. for (model_group* group: groups)
  225. {
  226. moon_material = group->get_material();
  227. moon_model_drawing_mode = group->get_drawing_mode();
  228. moon_model_start_index = group->get_start_index();
  229. moon_model_index_count = group->get_index_count();
  230. }
  231. if (moon_material)
  232. {
  233. moon_shader_program = moon_material->get_shader_program();
  234. if (moon_shader_program)
  235. {
  236. moon_model_view_projection_input = moon_shader_program->get_input("model_view_projection");
  237. moon_normal_model_input = moon_shader_program->get_input("normal_model");
  238. moon_moon_position_input = moon_shader_program->get_input("moon_position");
  239. moon_sun_position_input = moon_shader_program->get_input("sun_position");
  240. }
  241. }
  242. }
  243. else
  244. {
  245. moon_model = nullptr;
  246. }
  247. }
  248. void sky_pass::update_tweens()
  249. {
  250. julian_day_tween.update();
  251. sun_position_tween.update();
  252. sun_az_el_tween.update();
  253. moon_position_tween.update();
  254. moon_az_el_tween.update();
  255. time_of_day_tween.update();
  256. horizon_color_tween.update();
  257. zenith_color_tween.update();
  258. }
  259. void sky_pass::set_time_of_day(float time)
  260. {
  261. time_of_day_tween[1] = time;
  262. }
  263. void sky_pass::set_time_tween(const tween<double>* time)
  264. {
  265. this->time_tween = time;
  266. }
  267. void sky_pass::set_blue_noise_map(const gl::texture_2d* texture)
  268. {
  269. blue_noise_map = texture;
  270. }
  271. void sky_pass::set_sky_gradient(const gl::texture_2d* texture, const gl::texture_2d* texture2)
  272. {
  273. sky_gradient = texture;
  274. sky_gradient2 = texture2;
  275. }
  276. void sky_pass::set_julian_day(float jd)
  277. {
  278. julian_day_tween[1] = jd;
  279. }
  280. void sky_pass::set_observer_location(float altitude, float latitude, float longitude)
  281. {
  282. observer_location = {altitude, latitude, longitude};
  283. }
  284. void sky_pass::set_sun_coordinates(const float3& position, const float2& az_el)
  285. {
  286. sun_position_tween[1] = position;
  287. sun_az_el_tween[1] = az_el;
  288. }
  289. void sky_pass::set_moon_coordinates(const float3& position, const float2& az_el)
  290. {
  291. moon_position_tween[1] = position;
  292. moon_az_el_tween[1] = az_el;
  293. }
  294. void sky_pass::set_moon_rotation(const math::quaternion<float>& rotation)
  295. {
  296. moon_rotation = rotation;
  297. }
  298. void sky_pass::set_moon_angular_radius(float radius)
  299. {
  300. moon_angular_radius = radius;
  301. cos_moon_angular_radius = std::cos(moon_angular_radius);
  302. }
  303. void sky_pass::set_sun_angular_radius(float radius)
  304. {
  305. sun_angular_radius = radius;
  306. cos_sun_angular_radius = std::cos(sun_angular_radius);
  307. }
  308. void sky_pass::set_horizon_color(const float3& color)
  309. {
  310. horizon_color_tween[1] = color;
  311. }
  312. void sky_pass::set_zenith_color(const float3& color)
  313. {
  314. zenith_color_tween[1] = color;
  315. }
  316. void sky_pass::handle_event(const mouse_moved_event& event)
  317. {
  318. mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};
  319. }