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

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