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

169 lines
5.4 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 "scene/camera.hpp"
  35. #include "scene/scene.hpp"
  36. #include "scene/ambient-light.hpp"
  37. #include "scene/directional-light.hpp"
  38. #include "scene/scene.hpp"
  39. #include "utility/fundamental-types.hpp"
  40. #include <cmath>
  41. #include <glad/glad.h>
  42. sky_pass::sky_pass(::rasterizer* rasterizer, const ::framebuffer* framebuffer, resource_manager* resource_manager):
  43. render_pass(rasterizer, framebuffer),
  44. mouse_position({0.0f, 0.0f}),
  45. sun_light(nullptr)
  46. {
  47. shader_program = resource_manager->load<::shader_program>("sky.glsl");
  48. matrix_input = shader_program->get_input("matrix");
  49. sun_direction_input = shader_program->get_input("sun_direction");
  50. sun_angular_radius_input = shader_program->get_input("sun_angular_radius");
  51. sun_angular_radius_input = shader_program->get_input("sun_angular_radius");
  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. const float vertex_data[] =
  58. {
  59. -1.0f, 1.0f, 0.0f,
  60. -1.0f, -1.0f, 0.0f,
  61. 1.0f, 1.0f, 0.0f,
  62. 1.0f, 1.0f, 0.0f,
  63. -1.0f, -1.0f, 0.0f,
  64. 1.0f, -1.0f, 0.0f
  65. };
  66. std::size_t vertex_size = 3;
  67. std::size_t vertex_stride = sizeof(float) * vertex_size;
  68. std::size_t vertex_count = 6;
  69. quad_vbo = new vertex_buffer(sizeof(float) * vertex_size * vertex_count, vertex_data);
  70. quad_vao = new vertex_array();
  71. quad_vao->bind_attribute(VERTEX_POSITION_LOCATION, *quad_vbo, 3, vertex_attribute_type::float_32, vertex_stride, 0);
  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. delete quad_vao;
  80. delete quad_vbo;
  81. }
  82. void sky_pass::render(render_context* context) const
  83. {
  84. rasterizer->use_framebuffer(*framebuffer);
  85. glDisable(GL_BLEND);
  86. glDisable(GL_DEPTH_TEST);
  87. glDepthMask(GL_FALSE);
  88. glDisable(GL_CULL_FACE);
  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. float3 sun_direction = {0, -1, 0};
  94. if (sun_light)
  95. {
  96. sun_direction = sun_light->get_direction_tween().interpolate(context->alpha);
  97. }
  98. // Calculate matrix
  99. float4x4 model_view = math::resize<4, 4>(math::resize<3, 3>(context->camera->get_view_tween().interpolate(context->alpha)));
  100. float4x4 inverse_projection = math::inverse(context->camera->get_projection_tween().interpolate(context->alpha));
  101. float4x4 matrix = math::inverse(model_view) * inverse_projection;
  102. // Change shader program
  103. rasterizer->use_program(*shader_program);
  104. // Upload shader parameters
  105. if (matrix_input)
  106. matrix_input->upload(matrix);
  107. if (sun_direction_input)
  108. sun_direction_input->upload(sun_direction);
  109. if (sun_angular_radius_input)
  110. sun_angular_radius_input->upload(sun_angular_radius);
  111. if (sun_color_input)
  112. sun_color_input->upload(sun_color);
  113. if (sky_gradient_input)
  114. sky_gradient_input->upload(0, &sky_gradient[0], 4);
  115. if (mouse_input)
  116. mouse_input->upload(mouse_position);
  117. if (resolution_input)
  118. resolution_input->upload(resolution);
  119. if (time_input)
  120. time_input->upload(time);
  121. // Draw quad
  122. rasterizer->draw_arrays(*quad_vao, drawing_mode::triangles, 0, 6);
  123. }
  124. void sky_pass::set_sun_angular_radius(float angle)
  125. {
  126. sun_angular_radius = angle;
  127. }
  128. void sky_pass::set_sun_color(const float3& color)
  129. {
  130. sun_color = color;
  131. }
  132. void sky_pass::set_sun_light(const directional_light* light)
  133. {
  134. sun_light = light;
  135. }
  136. void sky_pass::set_sky_gradient(const std::array<float4, 4>& gradient)
  137. {
  138. sky_gradient = gradient;
  139. }
  140. void sky_pass::set_time_tween(const tween<double>* time)
  141. {
  142. this->time_tween = time;
  143. }
  144. void sky_pass::handle_event(const mouse_moved_event& event)
  145. {
  146. mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};
  147. }