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

228 lines
7.1 KiB

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