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