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

315 lines
10 KiB

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