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

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