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

327 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. model = math::scale(math::translate(math::identity4x4<float>, moon_position * -moon_distance), float3{moon_radius, moon_radius, moon_radius});
  135. model_view = view * model;
  136. model_view_projection = projection * model_view;
  137. float3x3 normal_model = math::transpose(math::inverse(math::resize<3, 3>(model)));
  138. rasterizer->use_program(*moon_shader_program);
  139. if (moon_model_view_projection_input)
  140. moon_model_view_projection_input->upload(model_view_projection);
  141. if (moon_normal_model_input)
  142. moon_normal_model_input->upload(normal_model);
  143. if (moon_moon_position_input)
  144. moon_moon_position_input->upload(moon_position);
  145. if (moon_sun_position_input)
  146. moon_sun_position_input->upload(sun_position);
  147. moon_material->upload(context->alpha);
  148. rasterizer->draw_arrays(*moon_model_vao, moon_model_drawing_mode, moon_model_start_index, moon_model_index_count);
  149. }
  150. }
  151. void sky_pass::set_sky_model(const model* model)
  152. {
  153. sky_model = model;
  154. if (sky_model)
  155. {
  156. sky_model_vao = model->get_vertex_array();
  157. const std::vector<model_group*>& groups = *model->get_groups();
  158. for (model_group* group: groups)
  159. {
  160. sky_material = group->get_material();
  161. sky_model_drawing_mode = group->get_drawing_mode();
  162. sky_model_start_index = group->get_start_index();
  163. sky_model_index_count = group->get_index_count();
  164. }
  165. if (sky_material)
  166. {
  167. sky_shader_program = sky_material->get_shader_program();
  168. if (sky_shader_program)
  169. {
  170. model_view_projection_input = sky_shader_program->get_input("model_view_projection");
  171. horizon_color_input = sky_shader_program->get_input("horizon_color");
  172. zenith_color_input = sky_shader_program->get_input("zenith_color");
  173. mouse_input = sky_shader_program->get_input("mouse");
  174. resolution_input = sky_shader_program->get_input("resolution");
  175. time_input = sky_shader_program->get_input("time");
  176. time_of_day_input = sky_shader_program->get_input("time_of_day");
  177. blue_noise_map_input = sky_shader_program->get_input("blue_noise_map");
  178. observer_location_input = sky_shader_program->get_input("observer_location");
  179. sun_position_input = sky_shader_program->get_input("sun_position");
  180. sun_az_el_input = sky_shader_program->get_input("sun_az_el");
  181. moon_position_input = sky_shader_program->get_input("moon_position");
  182. moon_az_el_input = sky_shader_program->get_input("moon_az_el");
  183. julian_day_input = sky_shader_program->get_input("julian_day");
  184. cos_moon_angular_radius_input = sky_shader_program->get_input("cos_moon_angular_radius");
  185. cos_sun_angular_radius_input = sky_shader_program->get_input("cos_sun_angular_radius");
  186. }
  187. }
  188. }
  189. else
  190. {
  191. sky_model_vao = nullptr;
  192. }
  193. }
  194. void sky_pass::set_moon_model(const model* model)
  195. {
  196. moon_model = model;
  197. if (moon_model)
  198. {
  199. moon_model_vao = model->get_vertex_array();
  200. const std::vector<model_group*>& groups = *model->get_groups();
  201. for (model_group* group: groups)
  202. {
  203. moon_material = group->get_material();
  204. moon_model_drawing_mode = group->get_drawing_mode();
  205. moon_model_start_index = group->get_start_index();
  206. moon_model_index_count = group->get_index_count();
  207. }
  208. if (moon_material)
  209. {
  210. moon_shader_program = moon_material->get_shader_program();
  211. if (moon_shader_program)
  212. {
  213. moon_model_view_projection_input = moon_shader_program->get_input("model_view_projection");
  214. moon_normal_model_input = moon_shader_program->get_input("normal_model");
  215. moon_moon_position_input = moon_shader_program->get_input("moon_position");
  216. moon_sun_position_input = moon_shader_program->get_input("sun_position");
  217. }
  218. }
  219. }
  220. else
  221. {
  222. moon_model = nullptr;
  223. }
  224. }
  225. void sky_pass::update_tweens()
  226. {
  227. julian_day_tween.update();
  228. sun_position_tween.update();
  229. sun_az_el_tween.update();
  230. moon_position_tween.update();
  231. moon_az_el_tween.update();
  232. time_of_day_tween.update();
  233. horizon_color_tween.update();
  234. zenith_color_tween.update();
  235. }
  236. void sky_pass::set_time_of_day(float time)
  237. {
  238. time_of_day_tween[1] = time;
  239. }
  240. void sky_pass::set_time_tween(const tween<double>* time)
  241. {
  242. this->time_tween = time;
  243. }
  244. void sky_pass::set_blue_noise_map(const texture_2d* texture)
  245. {
  246. blue_noise_map = texture;
  247. }
  248. void sky_pass::set_julian_day(float jd)
  249. {
  250. julian_day_tween[1] = jd;
  251. }
  252. void sky_pass::set_observer_location(float latitude, float longitude, float altitude)
  253. {
  254. observer_location = {latitude, longitude, altitude};
  255. }
  256. void sky_pass::set_sun_coordinates(const float3& position, const float2& az_el)
  257. {
  258. sun_position_tween[1] = position;
  259. sun_az_el_tween[1] = az_el;
  260. }
  261. void sky_pass::set_moon_coordinates(const float3& position, const float2& az_el)
  262. {
  263. moon_position_tween[1] = position;
  264. moon_az_el_tween[1] = az_el;
  265. }
  266. void sky_pass::set_moon_angular_radius(float radius)
  267. {
  268. moon_angular_radius = radius;
  269. cos_moon_angular_radius = std::cos(moon_angular_radius);
  270. }
  271. void sky_pass::set_sun_angular_radius(float radius)
  272. {
  273. sun_angular_radius = radius;
  274. cos_sun_angular_radius = std::cos(sun_angular_radius);
  275. }
  276. void sky_pass::set_horizon_color(const float3& color)
  277. {
  278. horizon_color_tween[1] = color;
  279. }
  280. void sky_pass::set_zenith_color(const float3& color)
  281. {
  282. zenith_color_tween[1] = color;
  283. }
  284. void sky_pass::handle_event(const mouse_moved_event& event)
  285. {
  286. mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};
  287. }