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

198 lines
6.4 KiB

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