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

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