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

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