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

181 lines
5.6 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. sky_palette(nullptr),
  45. mouse_position({0.0f, 0.0f}),
  46. sun_light(nullptr)
  47. {
  48. shader_program = resource_manager->load<::shader_program>("sky.glsl");
  49. matrix_input = shader_program->get_input("matrix");
  50. sun_direction_input = shader_program->get_input("sun_direction");
  51. sun_angular_radius_input = shader_program->get_input("sun_angular_radius");
  52. horizon_color_input = shader_program->get_input("horizon_color");
  53. zenith_color_input = shader_program->get_input("zenith_color");
  54. sun_angular_radius_input = shader_program->get_input("sun_angular_radius");
  55. sun_color_input = shader_program->get_input("sun_color");
  56. sky_palette_input = shader_program->get_input("sky_palette");
  57. mouse_input = shader_program->get_input("mouse");
  58. resolution_input = shader_program->get_input("resolution");
  59. time_input = shader_program->get_input("time");
  60. const float vertex_data[] =
  61. {
  62. -1.0f, 1.0f, 0.0f,
  63. -1.0f, -1.0f, 0.0f,
  64. 1.0f, 1.0f, 0.0f,
  65. 1.0f, 1.0f, 0.0f,
  66. -1.0f, -1.0f, 0.0f,
  67. 1.0f, -1.0f, 0.0f
  68. };
  69. std::size_t vertex_size = 3;
  70. std::size_t vertex_stride = sizeof(float) * vertex_size;
  71. std::size_t vertex_count = 6;
  72. quad_vbo = new vertex_buffer(sizeof(float) * vertex_size * vertex_count, vertex_data);
  73. quad_vao = new vertex_array();
  74. quad_vao->bind_attribute(VERTEX_POSITION_LOCATION, *quad_vbo, 3, vertex_attribute_type::float_32, vertex_stride, 0);
  75. }
  76. sky_pass::~sky_pass()
  77. {
  78. delete quad_vao;
  79. delete quad_vbo;
  80. }
  81. void sky_pass::render(render_context* context) const
  82. {
  83. rasterizer->use_framebuffer(*framebuffer);
  84. glDisable(GL_BLEND);
  85. glDisable(GL_DEPTH_TEST);
  86. glDepthMask(GL_FALSE);
  87. glDisable(GL_CULL_FACE);
  88. auto viewport = framebuffer->get_dimensions();
  89. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  90. float time = (time_tween) ? time_tween->interpolate(context->alpha) : 0.0f;
  91. float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
  92. float3 sun_direction = {0, -1, 0};
  93. if (sun_light)
  94. {
  95. sun_direction = sun_light->get_direction_tween().interpolate(context->alpha);
  96. }
  97. // Calculate matrix
  98. float4x4 model_view = math::resize<4, 4>(math::resize<3, 3>(context->camera->get_view_tween().interpolate(context->alpha)));
  99. float4x4 inverse_projection = math::inverse(context->camera->get_projection_tween().interpolate(context->alpha));
  100. float4x4 matrix = math::inverse(model_view) * inverse_projection;
  101. // Change shader program
  102. rasterizer->use_program(*shader_program);
  103. // Upload shader parameters
  104. if (matrix_input)
  105. matrix_input->upload(matrix);
  106. if (sun_direction_input)
  107. sun_direction_input->upload(sun_direction);
  108. if (sun_angular_radius_input)
  109. sun_angular_radius_input->upload(sun_angular_radius);
  110. if (sun_color_input)
  111. sun_color_input->upload(sun_color);
  112. if (horizon_color_input)
  113. horizon_color_input->upload(horizon_color);
  114. if (zenith_color_input)
  115. zenith_color_input->upload(zenith_color);
  116. if (sky_palette_input && sky_palette)
  117. sky_palette_input->upload(sky_palette);
  118. if (mouse_input)
  119. mouse_input->upload(mouse_position);
  120. if (resolution_input)
  121. resolution_input->upload(resolution);
  122. if (time_input)
  123. time_input->upload(time);
  124. // Draw quad
  125. rasterizer->draw_arrays(*quad_vao, drawing_mode::triangles, 0, 6);
  126. }
  127. void sky_pass::set_sun_angular_radius(float angle)
  128. {
  129. sun_angular_radius = angle;
  130. }
  131. void sky_pass::set_sun_color(const float3& color)
  132. {
  133. sun_color = color;
  134. }
  135. void sky_pass::set_horizon_color(const float3& color)
  136. {
  137. horizon_color = color;
  138. }
  139. void sky_pass::set_zenith_color(const float3& color)
  140. {
  141. zenith_color = color;
  142. }
  143. void sky_pass::set_sun_light(const directional_light* light)
  144. {
  145. sun_light = light;
  146. }
  147. void sky_pass::set_sky_palette(const texture_2d* texture)
  148. {
  149. sky_palette = texture;
  150. }
  151. void sky_pass::set_time_tween(const tween<double>* time)
  152. {
  153. this->time_tween = time;
  154. }
  155. void sky_pass::handle_event(const mouse_moved_event& event)
  156. {
  157. mouse_position = {static_cast<float>(event.x), static_cast<float>(event.y)};
  158. }