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

337 lines
11 KiB

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