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

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