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

151 lines
4.8 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. {
  45. shader_program = resource_manager->load<::shader_program>("sky.glsl");
  46. matrix_input = shader_program->get_input("matrix");
  47. sun_direction_input = shader_program->get_input("sun_direction");
  48. sun_angular_radius_input = shader_program->get_input("sun_angular_radius");
  49. horizon_color_input = shader_program->get_input("horizon_color");
  50. zenith_color_input = shader_program->get_input("zenith_color");
  51. sun_angular_radius_input = shader_program->get_input("sun_angular_radius");
  52. sun_color_input = shader_program->get_input("sun_color");
  53. const float vertex_data[] =
  54. {
  55. -1.0f, 1.0f, 0.0f,
  56. -1.0f, -1.0f, 0.0f,
  57. 1.0f, 1.0f, 0.0f,
  58. 1.0f, 1.0f, 0.0f,
  59. -1.0f, -1.0f, 0.0f,
  60. 1.0f, -1.0f, 0.0f
  61. };
  62. std::size_t vertex_size = 3;
  63. std::size_t vertex_stride = sizeof(float) * vertex_size;
  64. std::size_t vertex_count = 6;
  65. quad_vbo = new vertex_buffer(sizeof(float) * vertex_size * vertex_count, vertex_data);
  66. quad_vao = new vertex_array();
  67. quad_vao->bind_attribute(VERTEX_POSITION_LOCATION, *quad_vbo, 3, vertex_attribute_type::float_32, vertex_stride, 0);
  68. }
  69. sky_pass::~sky_pass()
  70. {
  71. delete quad_vao;
  72. delete quad_vbo;
  73. }
  74. void sky_pass::render(render_context* context) const
  75. {
  76. rasterizer->use_framebuffer(*framebuffer);
  77. glDisable(GL_BLEND);
  78. glDisable(GL_DEPTH_TEST);
  79. glDepthMask(GL_FALSE);
  80. glDisable(GL_CULL_FACE);
  81. auto viewport = framebuffer->get_dimensions();
  82. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  83. // Find sun direction
  84. float3 sun_direction = {0, 0, -1};
  85. const std::list<scene_object_base*>* lights = context->scene->get_objects(light::object_type_id);
  86. for (const scene_object_base* object: *lights)
  87. {
  88. const ::light* light = static_cast<const ::light*>(object);
  89. if (light->get_light_type() == light_type::directional)
  90. {
  91. sun_direction = static_cast<const directional_light*>(light)->get_direction_tween().interpolate(context->alpha);
  92. break;
  93. }
  94. }
  95. // Calculate matrix
  96. float4x4 model_view = math::resize<4, 4>(math::resize<3, 3>(context->camera->get_view_tween().interpolate(context->alpha)));
  97. float4x4 inverse_projection = math::inverse(context->camera->get_projection_tween().interpolate(context->alpha));
  98. float4x4 matrix = math::inverse(model_view) * inverse_projection;
  99. // Change shader program
  100. rasterizer->use_program(*shader_program);
  101. // Upload shader parameters
  102. if (matrix_input)
  103. matrix_input->upload(matrix);
  104. if (sun_direction_input)
  105. sun_direction_input->upload(sun_direction);
  106. if (sun_angular_radius_input)
  107. sun_angular_radius_input->upload(sun_angular_radius);
  108. if (sun_color_input)
  109. sun_color_input->upload(sun_color);
  110. if (horizon_color_input)
  111. horizon_color_input->upload(horizon_color);
  112. if (zenith_color_input)
  113. zenith_color_input->upload(zenith_color);
  114. // Draw quad
  115. rasterizer->draw_arrays(*quad_vao, drawing_mode::triangles, 0, 6);
  116. }
  117. void sky_pass::set_sun_angular_radius(float angle)
  118. {
  119. sun_angular_radius = angle;
  120. }
  121. void sky_pass::set_sun_color(const float3& color)
  122. {
  123. sun_color = color;
  124. }
  125. void sky_pass::set_horizon_color(const float3& color)
  126. {
  127. horizon_color = color;
  128. }
  129. void sky_pass::set_zenith_color(const float3& color)
  130. {
  131. zenith_color = color;
  132. }