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

235 lines
7.2 KiB

3 years ago
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/ambient-light.hpp"
  38. #include "scene/directional-light.hpp"
  39. #include "scene/scene.hpp"
  40. #include "utility/fundamental-types.hpp"
  41. #include <cmath>
  42. #include <glad/glad.h>
  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. sun_light(nullptr),
  47. moon_light(nullptr),
  48. time_of_day(0.0f),
  49. sky_model(nullptr),
  50. sky_model_vao(nullptr),
  51. blue_noise_map(nullptr)
  52. {
  53. shader_program = resource_manager->load<::shader_program>("sky.glsl");
  54. model_view_projection_input = shader_program->get_input("model_view_projection");
  55. sun_direction_input = shader_program->get_input("sun_direction");
  56. moon_direction_input = shader_program->get_input("moon_direction");
  57. sun_angular_radius_input = shader_program->get_input("sun_angular_radius");
  58. sun_angular_radius_input = shader_program->get_input("sun_angular_radius");
  59. sun_color_input = shader_program->get_input("sun_color");
  60. sky_gradient_input = shader_program->get_input("sky_gradient");
  61. mouse_input = shader_program->get_input("mouse");
  62. resolution_input = shader_program->get_input("resolution");
  63. time_input = shader_program->get_input("time");
  64. time_of_day_input = shader_program->get_input("time_of_day");
  65. blue_noise_map_input = shader_program->get_input("blue_noise_map");
  66. const float vertex_data[] =
  67. {
  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. -1.0f, -1.0f, 0.0f,
  73. 1.0f, -1.0f, 0.0f
  74. };
  75. std::size_t vertex_size = 3;
  76. std::size_t vertex_stride = sizeof(float) * vertex_size;
  77. std::size_t vertex_count = 6;
  78. quad_vbo = new vertex_buffer(sizeof(float) * vertex_size * vertex_count, vertex_data);
  79. quad_vao = new vertex_array();
  80. quad_vao->bind_attribute(VERTEX_POSITION_LOCATION, *quad_vbo, 3, vertex_attribute_type::float_32, vertex_stride, 0);
  81. sky_gradient[0] = {1.0, 0.0f, 0.0f, 0.0f};
  82. sky_gradient[1] = {0.0, 1.0f, 0.0f, 0.333f};
  83. sky_gradient[2] = {0.0, 0.0f, 1.0f, 0.667f};
  84. sky_gradient[3] = {1.0, 1.0f, 0.0f, 1.0f};
  85. }
  86. sky_pass::~sky_pass()
  87. {
  88. delete quad_vao;
  89. delete quad_vbo;
  90. }
  91. void sky_pass::render(render_context* context) const
  92. {
  93. if (!sky_model_vao)
  94. return;
  95. rasterizer->use_framebuffer(*framebuffer);
  96. glDisable(GL_BLEND);
  97. glDisable(GL_DEPTH_TEST);
  98. glDepthMask(GL_FALSE);
  99. glEnable(GL_CULL_FACE);
  100. glCullFace(GL_BACK);
  101. auto viewport = framebuffer->get_dimensions();
  102. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  103. float time = (time_tween) ? time_tween->interpolate(context->alpha) : 0.0f;
  104. float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
  105. float3 sun_direction = {0, -1, 0};
  106. if (sun_light)
  107. {
  108. sun_direction = sun_light->get_direction_tween().interpolate(context->alpha);
  109. }
  110. float3 moon_direction = {0, -1, 0};
  111. if (moon_light)
  112. {
  113. moon_direction = moon_light->get_direction_tween().interpolate(context->alpha);
  114. }
  115. const ::camera& camera = *context->camera;
  116. float clip_near = camera.get_clip_near_tween().interpolate(context->alpha);
  117. float clip_far = camera.get_clip_far_tween().interpolate(context->alpha);
  118. float3 model_scale = float3{1.0f, 1.0f, 1.0f} * (clip_near + clip_far) * 0.5f;
  119. float4x4 model = math::scale(math::identity4x4<float>, model_scale);
  120. float4x4 model_view = math::resize<4, 4>(math::resize<3, 3>(camera.get_view_tween().interpolate(context->alpha))) * model;
  121. float4x4 projection = camera.get_projection_tween().interpolate(context->alpha);
  122. float4x4 model_view_projection = projection * model_view;
  123. // Change shader program
  124. rasterizer->use_program(*shader_program);
  125. // Upload shader parameters
  126. if (model_view_projection_input)
  127. model_view_projection_input->upload(model_view_projection);
  128. if (sun_direction_input)
  129. sun_direction_input->upload(sun_direction);
  130. if (moon_direction_input)
  131. moon_direction_input->upload(moon_direction);
  132. if (sun_angular_radius_input)
  133. sun_angular_radius_input->upload(sun_angular_radius);
  134. if (sun_color_input)
  135. sun_color_input->upload(sun_color);
  136. if (sky_gradient_input)
  137. sky_gradient_input->upload(0, &sky_gradient[0], 4);
  138. if (mouse_input)
  139. mouse_input->upload(mouse_position);
  140. if (resolution_input)
  141. resolution_input->upload(resolution);
  142. if (time_input)
  143. time_input->upload(time);
  144. if (time_of_day_input)
  145. time_of_day_input->upload(time_of_day);
  146. if (blue_noise_map_input)
  147. blue_noise_map_input->upload(blue_noise_map);
  148. // Draw sky model
  149. rasterizer->draw_arrays(*sky_model_vao, sky_model_drawing_mode, sky_model_start_index, sky_model_index_count);
  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_model_drawing_mode = group->get_drawing_mode();
  161. sky_model_start_index = group->get_start_index();
  162. sky_model_index_count = group->get_index_count();
  163. }
  164. }
  165. else
  166. {
  167. sky_model_vao = nullptr;
  168. }
  169. }
  170. void sky_pass::set_sun_angular_radius(float angle)
  171. {
  172. sun_angular_radius = angle;
  173. }
  174. void sky_pass::set_sun_color(const float3& color)
  175. {
  176. sun_color = color;
  177. }
  178. void sky_pass::set_sun_light(const directional_light* light)
  179. {
  180. sun_light = light;
  181. }
  182. void sky_pass::set_moon_light(const directional_light* light)
  183. {
  184. moon_light = light;
  185. }
  186. void sky_pass::set_sky_gradient(const std::array<float4, 4>& gradient)
  187. {
  188. sky_gradient = gradient;
  189. }
  190. void sky_pass::set_time_of_day(float time)
  191. {
  192. time_of_day = time;
  193. }
  194. void sky_pass::set_time_tween(const tween<double>* time)
  195. {
  196. this->time_tween = time;
  197. }
  198. void sky_pass::set_blue_noise_map(const texture_2d* texture)
  199. {
  200. blue_noise_map = texture;
  201. }
  202. void sky_pass::handle_event(const mouse_moved_event& event)
  203. {
  204. mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};
  205. }