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

201 lines
6.4 KiB

  1. /*
  2. * Copyright (C) 2023 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 "render/passes/ground-pass.hpp"
  20. #include "resources/resource-manager.hpp"
  21. #include "gl/rasterizer.hpp"
  22. #include "gl/framebuffer.hpp"
  23. #include "gl/shader-program.hpp"
  24. #include "gl/shader-input.hpp"
  25. #include "gl/vertex-buffer.hpp"
  26. #include "gl/vertex-array.hpp"
  27. #include "gl/vertex-attribute.hpp"
  28. #include "gl/drawing-mode.hpp"
  29. #include "gl/texture-2d.hpp"
  30. #include "gl/texture-wrapping.hpp"
  31. #include "gl/texture-filter.hpp"
  32. #include "render/vertex-attribute.hpp"
  33. #include "render/context.hpp"
  34. #include "render/model.hpp"
  35. #include "render/material.hpp"
  36. #include "scene/camera.hpp"
  37. #include "scene/collection.hpp"
  38. #include "scene/directional-light.hpp"
  39. #include "scene/ambient-light.hpp"
  40. #include "utility/fundamental-types.hpp"
  41. #include "color/color.hpp"
  42. #include "math/interpolation.hpp"
  43. #include <cmath>
  44. #include <stdexcept>
  45. #include <glad/glad.h>
  46. namespace render {
  47. ground_pass::ground_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffer, resource_manager* resource_manager):
  48. pass(rasterizer, framebuffer),
  49. ground_model(nullptr),
  50. ground_model_vao(nullptr),
  51. ground_material(nullptr),
  52. shader_program(nullptr)
  53. {}
  54. ground_pass::~ground_pass()
  55. {}
  56. void ground_pass::render(const render::context& ctx, render::queue& queue) const
  57. {
  58. if (!ground_model)
  59. return;
  60. rasterizer->use_framebuffer(*framebuffer);
  61. glDisable(GL_BLEND);
  62. glEnable(GL_DEPTH_TEST);
  63. glDepthFunc(GL_ALWAYS);
  64. glDepthMask(GL_TRUE);
  65. glDepthRange(-1.0f, 1.0f);
  66. glEnable(GL_CULL_FACE);
  67. glCullFace(GL_BACK);
  68. glDisable(GL_STENCIL_TEST);
  69. glStencilMask(0);
  70. auto viewport = framebuffer->get_dimensions();
  71. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  72. float2 resolution = {static_cast<float>(std::get<0>(viewport)), static_cast<float>(std::get<1>(viewport))};
  73. const scene::camera& camera = *ctx.camera;
  74. float clip_near = camera.get_clip_near_tween().interpolate(ctx.alpha);
  75. float clip_far = camera.get_clip_far_tween().interpolate(ctx.alpha);
  76. float3 model_scale = float3{1.0f, 1.0f, 1.0f} * (clip_near + clip_far) * 0.5f;
  77. float4x4 model = math::scale(math::matrix4<float>::identity(), model_scale);
  78. float4x4 view = float4x4(float3x3(ctx.view));
  79. float4x4 model_view = view * model;
  80. const float4x4& projection = ctx.projection;
  81. const float4x4& view_projection = ctx.view_projection;
  82. float4x4 model_view_projection = projection * model_view;
  83. float3 ambient_light_color = {0.0f, 0.0f, 0.0f};
  84. float3 directional_light_color = {0.0f, 0.0f, 0.0f};
  85. float3 directional_light_direction = {0.0f, 0.0f, 0.0f};
  86. // Collect lights
  87. const std::list<scene::object_base*>* lights = ctx.collection->get_objects(scene::light::object_type_id);
  88. for (const scene::object_base* object: *lights)
  89. {
  90. // Skip inactive lights
  91. if (!object->is_active())
  92. continue;
  93. const scene::light* light = static_cast<const scene::light*>(object);
  94. switch (light->get_light_type())
  95. {
  96. // Add ambient light
  97. case scene::light_type::ambient:
  98. {
  99. // Pre-expose light
  100. ambient_light_color = light->get_scaled_color_tween().interpolate(ctx.alpha) * ctx.exposure;
  101. break;
  102. }
  103. // Add directional light
  104. case scene::light_type::directional:
  105. {
  106. const scene::directional_light* directional_light = static_cast<const scene::directional_light*>(light);
  107. // Pre-expose light
  108. float3 light_color = light->get_scaled_color_tween().interpolate(ctx.alpha) * ctx.exposure;
  109. if (light_color.x() < directional_light_color.x())
  110. break;
  111. directional_light_color = light_color;
  112. directional_light_direction = static_cast<const scene::directional_light*>(light)->get_direction_tween().interpolate(ctx.alpha);
  113. break;
  114. }
  115. default:
  116. break;
  117. }
  118. }
  119. // Draw ground
  120. rasterizer->use_program(*shader_program);
  121. if (model_view_projection_input)
  122. model_view_projection_input->upload(model_view_projection);
  123. if (view_projection_input)
  124. view_projection_input->upload(view_projection);
  125. if (camera_position_input)
  126. camera_position_input->upload(ctx.camera_transform.translation);
  127. if (directional_light_colors_input)
  128. directional_light_colors_input->upload(0, &directional_light_color, 1);
  129. if (directional_light_directions_input)
  130. directional_light_directions_input->upload(0, &directional_light_direction, 1);
  131. if (ambient_light_colors_input)
  132. ambient_light_colors_input->upload(0, &ambient_light_color, 1);
  133. ground_material->upload(ctx.alpha);
  134. rasterizer->draw_arrays(*ground_model_vao, ground_model_drawing_mode, ground_model_start_index, ground_model_index_count);
  135. }
  136. void ground_pass::set_ground_model(const model* model)
  137. {
  138. ground_model = model;
  139. if (ground_model)
  140. {
  141. ground_model_vao = model->get_vertex_array();
  142. const std::vector<model_group*>& groups = *model->get_groups();
  143. for (model_group* group: groups)
  144. {
  145. ground_material = group->get_material();
  146. ground_model_drawing_mode = group->get_drawing_mode();
  147. ground_model_start_index = group->get_start_index();
  148. ground_model_index_count = group->get_index_count();
  149. }
  150. if (ground_material)
  151. {
  152. shader_program = ground_material->get_shader_program();
  153. if (shader_program)
  154. {
  155. model_view_projection_input = shader_program->get_input("model_view_projection");
  156. view_projection_input = shader_program->get_input("view_projection");
  157. camera_position_input = shader_program->get_input("camera.position");
  158. directional_light_colors_input = shader_program->get_input("directional_light_colors");
  159. directional_light_directions_input = shader_program->get_input("directional_light_directions");
  160. ambient_light_colors_input = shader_program->get_input("ambient_light_colors");
  161. }
  162. }
  163. }
  164. else
  165. {
  166. ground_model_vao = nullptr;
  167. }
  168. }
  169. } // namespace render