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

245 lines
7.8 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 "scene/camera.hpp"
  36. #include "scene/scene.hpp"
  37. #include "scene/scene.hpp"
  38. #include "utility/fundamental-types.hpp"
  39. #include <cmath>
  40. #include <glad/glad.h>
  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_model_vao(nullptr),
  46. blue_noise_map(nullptr),
  47. observer_coordinates{0.0f, 0.0f}
  48. {
  49. shader_program = resource_manager->load<::shader_program>("sky.glsl");
  50. model_view_projection_input = shader_program->get_input("model_view_projection");
  51. sky_gradient_input = shader_program->get_input("sky_gradient");
  52. mouse_input = shader_program->get_input("mouse");
  53. resolution_input = shader_program->get_input("resolution");
  54. time_input = shader_program->get_input("time");
  55. time_of_day_input = shader_program->get_input("time_of_day");
  56. blue_noise_map_input = shader_program->get_input("blue_noise_map");
  57. observer_coordinates_input = shader_program->get_input("observer_coordinates");
  58. sun_position_input = shader_program->get_input("sun_position");
  59. sun_az_el_input = shader_program->get_input("sun_az_el");
  60. moon_position_input = shader_program->get_input("moon_position");
  61. moon_az_el_input = shader_program->get_input("moon_az_el");
  62. julian_day_input = shader_program->get_input("julian_day");
  63. const float vertex_data[] =
  64. {
  65. -1.0f, 1.0f, 0.0f,
  66. -1.0f, -1.0f, 0.0f,
  67. 1.0f, 1.0f, 0.0f,
  68. 1.0f, 1.0f, 0.0f,
  69. -1.0f, -1.0f, 0.0f,
  70. 1.0f, -1.0f, 0.0f
  71. };
  72. std::size_t vertex_size = 3;
  73. std::size_t vertex_stride = sizeof(float) * vertex_size;
  74. std::size_t vertex_count = 6;
  75. quad_vbo = new vertex_buffer(sizeof(float) * vertex_size * vertex_count, vertex_data);
  76. quad_vao = new vertex_array();
  77. quad_vao->bind_attribute(VERTEX_POSITION_LOCATION, *quad_vbo, 3, vertex_attribute_type::float_32, vertex_stride, 0);
  78. sky_gradient[0] = {1.0, 0.0f, 0.0f, 0.0f};
  79. sky_gradient[1] = {0.0, 1.0f, 0.0f, 0.333f};
  80. sky_gradient[2] = {0.0, 0.0f, 1.0f, 0.667f};
  81. sky_gradient[3] = {1.0, 1.0f, 0.0f, 1.0f};
  82. }
  83. sky_pass::~sky_pass()
  84. {
  85. delete quad_vao;
  86. delete quad_vbo;
  87. }
  88. void sky_pass::render(render_context* context) const
  89. {
  90. if (!sky_model_vao)
  91. return;
  92. rasterizer->use_framebuffer(*framebuffer);
  93. glDisable(GL_BLEND);
  94. glDisable(GL_DEPTH_TEST);
  95. glDepthMask(GL_FALSE);
  96. glEnable(GL_CULL_FACE);
  97. glCullFace(GL_BACK);
  98. auto viewport = framebuffer->get_dimensions();
  99. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  100. float time = (time_tween) ? time_tween->interpolate(context->alpha) : 0.0f;
  101. float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
  102. const ::camera& camera = *context->camera;
  103. float clip_near = camera.get_clip_near_tween().interpolate(context->alpha);
  104. float clip_far = camera.get_clip_far_tween().interpolate(context->alpha);
  105. float3 model_scale = float3{1.0f, 1.0f, 1.0f} * (clip_near + clip_far) * 0.5f;
  106. float4x4 model = math::scale(math::identity4x4<float>, model_scale);
  107. float4x4 model_view = math::resize<4, 4>(math::resize<3, 3>(camera.get_view_tween().interpolate(context->alpha))) * model;
  108. float4x4 projection = camera.get_projection_tween().interpolate(context->alpha);
  109. float4x4 model_view_projection = projection * model_view;
  110. float time_of_day = time_of_day_tween.interpolate(context->alpha);
  111. float julian_day = julian_day_tween.interpolate(context->alpha);
  112. float3 sun_position = sun_position_tween.interpolate(context->alpha);
  113. float2 sun_az_el = sun_az_el_tween.interpolate(context->alpha);
  114. float3 moon_position = moon_position_tween.interpolate(context->alpha);
  115. float2 moon_az_el = moon_az_el_tween.interpolate(context->alpha);
  116. // Change shader program
  117. rasterizer->use_program(*shader_program);
  118. // Upload shader parameters
  119. if (model_view_projection_input)
  120. model_view_projection_input->upload(model_view_projection);
  121. if (sky_gradient_input)
  122. sky_gradient_input->upload(0, &sky_gradient[0], 4);
  123. if (mouse_input)
  124. mouse_input->upload(mouse_position);
  125. if (resolution_input)
  126. resolution_input->upload(resolution);
  127. if (time_input)
  128. time_input->upload(time);
  129. if (time_of_day_input)
  130. time_of_day_input->upload(time_of_day);
  131. if (blue_noise_map_input)
  132. blue_noise_map_input->upload(blue_noise_map);
  133. if (observer_coordinates_input)
  134. observer_coordinates_input->upload(observer_coordinates);
  135. if (sun_position_input)
  136. sun_position_input->upload(sun_position);
  137. if (sun_az_el_input)
  138. sun_az_el_input->upload(sun_az_el);
  139. if (moon_position_input)
  140. moon_position_input->upload(moon_position);
  141. if (moon_az_el_input)
  142. moon_az_el_input->upload(moon_az_el);
  143. if (julian_day_input)
  144. julian_day_input->upload(julian_day);
  145. // Draw sky model
  146. rasterizer->draw_arrays(*sky_model_vao, sky_model_drawing_mode, sky_model_start_index, sky_model_index_count);
  147. }
  148. void sky_pass::set_sky_model(const model* model)
  149. {
  150. sky_model = model;
  151. if (sky_model)
  152. {
  153. sky_model_vao = model->get_vertex_array();
  154. const std::vector<model_group*>& groups = *model->get_groups();
  155. for (model_group* group: groups)
  156. {
  157. sky_model_drawing_mode = group->get_drawing_mode();
  158. sky_model_start_index = group->get_start_index();
  159. sky_model_index_count = group->get_index_count();
  160. }
  161. }
  162. else
  163. {
  164. sky_model_vao = nullptr;
  165. }
  166. }
  167. void sky_pass::update_tweens()
  168. {
  169. julian_day_tween.update();
  170. sun_position_tween.update();
  171. sun_az_el_tween.update();
  172. moon_position_tween.update();
  173. moon_az_el_tween.update();
  174. time_of_day_tween.update();
  175. }
  176. void sky_pass::set_sky_gradient(const std::array<float4, 4>& gradient)
  177. {
  178. sky_gradient = gradient;
  179. }
  180. void sky_pass::set_time_of_day(float time)
  181. {
  182. time_of_day_tween[1] = time;
  183. }
  184. void sky_pass::set_time_tween(const tween<double>* time)
  185. {
  186. this->time_tween = time;
  187. }
  188. void sky_pass::set_blue_noise_map(const texture_2d* texture)
  189. {
  190. blue_noise_map = texture;
  191. }
  192. void sky_pass::set_julian_day(float jd)
  193. {
  194. julian_day_tween[1] = jd;
  195. }
  196. void sky_pass::set_observer_coordinates(const float2& coordinates)
  197. {
  198. observer_coordinates = coordinates;
  199. }
  200. void sky_pass::set_sun_coordinates(const float3& position, const float2& az_el)
  201. {
  202. sun_position_tween[1] = position;
  203. sun_az_el_tween[1] = az_el;
  204. }
  205. void sky_pass::set_moon_coordinates(const float3& position, const float2& az_el)
  206. {
  207. moon_position_tween[1] = position;
  208. moon_az_el_tween[1] = az_el;
  209. }
  210. void sky_pass::handle_event(const mouse_moved_event& event)
  211. {
  212. mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};
  213. }