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

222 lines
7.7 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 "renderer/renderer.hpp"
  20. #include "renderer/render-context.hpp"
  21. #include "renderer/compositor.hpp"
  22. #include "scene/collection.hpp"
  23. #include "scene/camera.hpp"
  24. #include "scene/model-instance.hpp"
  25. #include "scene/billboard.hpp"
  26. #include "scene/lod-group.hpp"
  27. #include "renderer/model.hpp"
  28. #include "rasterizer/drawing-mode.hpp"
  29. #include "math/math.hpp"
  30. #include "geom/projection.hpp"
  31. #include "configuration.hpp"
  32. #include <functional>
  33. #include <set>
  34. renderer::renderer()
  35. {
  36. // Setup billboard render operation
  37. billboard_op.pose = nullptr;
  38. billboard_op.drawing_mode = drawing_mode::triangles;
  39. billboard_op.vertex_array = nullptr;
  40. billboard_op.start_index = 0;
  41. billboard_op.index_count = 6;
  42. billboard_op.instance_count = 0;
  43. }
  44. void renderer::render(float alpha, const scene::collection& collection) const
  45. {
  46. // Get list of all objects in the collection
  47. const std::list<scene::object_base*>* objects = collection.get_objects();
  48. // Build list of cameras to be sorted
  49. const std::list<scene::object_base*>* cameras = collection.get_objects(scene::camera::object_type_id);
  50. std::list<scene::camera*> sorted_cameras;
  51. for (scene::object_base* object: *cameras)
  52. {
  53. sorted_cameras.push_back(static_cast<scene::camera*>(object));
  54. }
  55. // Sort cameras according to their respective compositing indices
  56. sorted_cameras.sort
  57. (
  58. [](const scene::camera* a, const scene::camera* b) -> bool
  59. {
  60. return a->get_composite_index() < b->get_composite_index();
  61. }
  62. );
  63. // Process cameras in order
  64. for (const scene::camera* camera: sorted_cameras)
  65. {
  66. // Skip inactive cameras
  67. if (!camera->is_active())
  68. {
  69. continue;
  70. }
  71. // Skip cameras with no compositors
  72. const compositor* compositor = camera->get_compositor();
  73. if (!compositor)
  74. {
  75. continue;
  76. }
  77. // Setup render context
  78. render_context context;
  79. context.camera = camera;
  80. context.camera_transform = camera->get_transform_tween().interpolate(alpha);
  81. context.camera_forward = context.camera_transform.rotation * global_forward;
  82. context.camera_up = context.camera_transform.rotation * global_up;
  83. context.clip_near = camera->get_view_frustum().get_near(); ///< TODO: tween this
  84. context.collection = &collection;
  85. context.alpha = alpha;
  86. // Get camera culling volume
  87. context.camera_culling_volume = camera->get_culling_mask();
  88. if (!context.camera_culling_volume)
  89. context.camera_culling_volume = &camera->get_bounds();
  90. // Generate render operations for each visible scene object
  91. for (const scene::object_base* object: *objects)
  92. {
  93. // Skip inactive objects
  94. if (!object->is_active())
  95. continue;
  96. // Process object
  97. process_object(context, object);
  98. }
  99. // Pass render context to the camera's compositor
  100. compositor->composite(&context);
  101. }
  102. }
  103. void renderer::set_billboard_vao(vertex_array* vao)
  104. {
  105. billboard_op.vertex_array = vao;
  106. }
  107. void renderer::process_object(render_context& context, const scene::object_base* object) const
  108. {
  109. std::size_t type = object->get_object_type_id();
  110. if (type == scene::model_instance::object_type_id)
  111. process_model_instance(context, static_cast<const scene::model_instance*>(object));
  112. else if (type == scene::billboard::object_type_id)
  113. process_billboard(context, static_cast<const scene::billboard*>(object));
  114. else if (type == scene::lod_group::object_type_id)
  115. process_lod_group(context, static_cast<const scene::lod_group*>(object));
  116. }
  117. void renderer::process_model_instance(render_context& context, const scene::model_instance* model_instance) const
  118. {
  119. const model* model = model_instance->get_model();
  120. if (!model)
  121. return;
  122. // Get object culling volume
  123. const geom::bounding_volume<float>* object_culling_volume = model_instance->get_culling_mask();
  124. if (!object_culling_volume)
  125. object_culling_volume = &model_instance->get_bounds();
  126. // Perform view-frustum culling
  127. if (!context.camera_culling_volume->intersects(*object_culling_volume))
  128. return;
  129. const std::vector<material*>* instance_materials = model_instance->get_materials();
  130. const std::vector<model_group*>* groups = model->get_groups();
  131. for (model_group* group: *groups)
  132. {
  133. render_operation operation;
  134. // Determine operation material
  135. operation.material = group->get_material();
  136. if ((*instance_materials)[group->get_index()])
  137. {
  138. // Override model group material with the instance's material
  139. operation.material = (*instance_materials)[group->get_index()];
  140. }
  141. operation.pose = model_instance->get_pose();
  142. operation.vertex_array = model->get_vertex_array();
  143. operation.drawing_mode = group->get_drawing_mode();
  144. operation.start_index = group->get_start_index();
  145. operation.index_count = group->get_index_count();
  146. operation.transform = math::matrix_cast(model_instance->get_transform_tween().interpolate(context.alpha));
  147. operation.depth = context.clip_near.signed_distance(math::resize<3>(operation.transform[3]));
  148. operation.instance_count = model_instance->get_instance_count();
  149. context.operations.push_back(operation);
  150. }
  151. }
  152. void renderer::process_billboard(render_context& context, const scene::billboard* billboard) const
  153. {
  154. // Get object culling volume
  155. const geom::bounding_volume<float>* object_culling_volume = billboard->get_culling_mask();
  156. if (!object_culling_volume)
  157. object_culling_volume = &billboard->get_bounds();
  158. // Perform view-frustum culling
  159. if (!context.camera_culling_volume->intersects(*object_culling_volume))
  160. return;
  161. math::transform<float> billboard_transform = billboard->get_transform_tween().interpolate(context.alpha);
  162. billboard_op.material = billboard->get_material();
  163. billboard_op.depth = context.clip_near.signed_distance(math::resize<3>(billboard_transform.translation));
  164. // Align billboard
  165. if (billboard->get_billboard_type() == scene::billboard_type::spherical)
  166. {
  167. billboard_transform.rotation = math::normalize(math::look_rotation(context.camera_forward, context.camera_up) * billboard_transform.rotation);
  168. }
  169. else if (billboard->get_billboard_type() == scene::billboard_type::cylindrical)
  170. {
  171. const float3& alignment_axis = billboard->get_alignment_axis();
  172. float3 look = math::normalize(geom::project_on_plane(billboard_transform.translation - context.camera_transform.translation, {0.0f, 0.0f, 0.0f}, alignment_axis));
  173. float3 right = math::normalize(math::cross(alignment_axis, look));
  174. look = math::cross(right, alignment_axis);
  175. float3 up = math::cross(look, right);
  176. billboard_transform.rotation = math::normalize(math::look_rotation(look, up) * billboard_transform.rotation);
  177. }
  178. billboard_op.transform = math::matrix_cast(billboard_transform);
  179. context.operations.push_back(billboard_op);
  180. }
  181. void renderer::process_lod_group(render_context& context, const scene::lod_group* lod_group) const
  182. {
  183. // Select level of detail
  184. std::size_t level = lod_group->select_lod(*context.camera);
  185. // Process all objects in the group with the selected level of detail
  186. const std::list<scene::object_base*>& objects = lod_group->get_objects(level);
  187. for (const scene::object_base* object: objects)
  188. {
  189. process_object(context, object);
  190. }
  191. }