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

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