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

271 lines
9.0 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.exposure = std::exp2(-camera->get_exposure_tween().interpolate(alpha));
  99. // Create render queue
  100. render::queue queue;
  101. // Get camera culling volume
  102. ctx.camera_culling_volume = camera->get_culling_mask();
  103. if (!ctx.camera_culling_volume)
  104. ctx.camera_culling_volume = &camera->get_world_bounds();
  105. // Queue render operations for each visible scene object
  106. for (const scene::object_base* object: *objects)
  107. {
  108. // Skip inactive objects
  109. if (!object->is_active())
  110. continue;
  111. // Process object
  112. process_object(ctx, queue, object);
  113. }
  114. // Pass render context to the camera's compositor
  115. compositor->composite(ctx, queue);
  116. }
  117. }
  118. void renderer::set_billboard_vao(gl::vertex_array* vao)
  119. {
  120. billboard_op.vertex_array = vao;
  121. }
  122. void renderer::process_object(const render::context& ctx, render::queue& queue, const scene::object_base* object) const
  123. {
  124. std::size_t type = object->get_object_type_id();
  125. if (type == scene::model_instance::object_type_id)
  126. process_model_instance(ctx, queue, static_cast<const scene::model_instance*>(object));
  127. else if (type == scene::billboard::object_type_id)
  128. process_billboard(ctx, queue, static_cast<const scene::billboard*>(object));
  129. else if (type == scene::lod_group::object_type_id)
  130. process_lod_group(ctx, queue, static_cast<const scene::lod_group*>(object));
  131. else if (type == scene::text::object_type_id)
  132. process_text(ctx, queue, static_cast<const scene::text*>(object));
  133. }
  134. void renderer::process_model_instance(const render::context& ctx, render::queue& queue, const scene::model_instance* model_instance) const
  135. {
  136. const model* model = model_instance->get_model();
  137. if (!model)
  138. return;
  139. // Get object culling volume
  140. const geom::bounding_volume<float>* object_culling_volume = model_instance->get_culling_mask();
  141. if (!object_culling_volume)
  142. object_culling_volume = &model_instance->get_world_bounds();
  143. // Perform view-frustum culling
  144. if (!ctx.camera_culling_volume->intersects(*object_culling_volume))
  145. return;
  146. const std::vector<material*>* instance_materials = model_instance->get_materials();
  147. const std::vector<model_group*>* groups = model->get_groups();
  148. render::operation operation;
  149. operation.transform = math::matrix_cast(model_instance->get_transform_tween().interpolate(ctx.alpha));
  150. operation.depth = ctx.clip_near.signed_distance(math::resize<3>(operation.transform[3]));
  151. operation.vertex_array = model->get_vertex_array();
  152. operation.instance_count = model_instance->get_instance_count();
  153. // Skinning parameters
  154. operation.bone_count = model_instance->get_pose().size();
  155. if (operation.bone_count)
  156. {
  157. operation.skinning_palette = skinning_palette;
  158. ::matrix_palette(model->get_skeleton().inverse_bind_pose, model_instance->get_pose(), skinning_palette);
  159. }
  160. else
  161. {
  162. operation.skinning_palette = nullptr;
  163. }
  164. for (model_group* group: *groups)
  165. {
  166. // Determine operation material
  167. operation.material = group->get_material();
  168. if ((*instance_materials)[group->get_index()])
  169. {
  170. // Override model group material with the instance's material
  171. operation.material = (*instance_materials)[group->get_index()];
  172. }
  173. operation.drawing_mode = group->get_drawing_mode();
  174. operation.start_index = group->get_start_index();
  175. operation.index_count = group->get_index_count();
  176. queue.push_back(operation);
  177. }
  178. }
  179. void renderer::process_billboard(const render::context& ctx, render::queue& queue, const scene::billboard* billboard) const
  180. {
  181. // Get object culling volume
  182. const geom::bounding_volume<float>* object_culling_volume = billboard->get_culling_mask();
  183. if (!object_culling_volume)
  184. object_culling_volume = &billboard->get_world_bounds();
  185. // Perform view-frustum culling
  186. if (!ctx.camera_culling_volume->intersects(*object_culling_volume))
  187. return;
  188. math::transform<float> billboard_transform = billboard->get_transform_tween().interpolate(ctx.alpha);
  189. billboard_op.material = billboard->get_material();
  190. billboard_op.depth = ctx.clip_near.signed_distance(math::resize<3>(billboard_transform.translation));
  191. // Align billboard
  192. if (billboard->get_billboard_type() == scene::billboard_type::spherical)
  193. {
  194. billboard_transform.rotation = math::normalize(math::look_rotation(ctx.camera_forward, ctx.camera_up) * billboard_transform.rotation);
  195. }
  196. else if (billboard->get_billboard_type() == scene::billboard_type::cylindrical)
  197. {
  198. const float3& alignment_axis = billboard->get_alignment_axis();
  199. float3 look = math::normalize(geom::project_on_plane(billboard_transform.translation - ctx.camera_transform.translation, {0.0f, 0.0f, 0.0f}, alignment_axis));
  200. float3 right = math::normalize(math::cross(alignment_axis, look));
  201. look = math::cross(right, alignment_axis);
  202. float3 up = math::cross(look, right);
  203. billboard_transform.rotation = math::normalize(math::look_rotation(look, up) * billboard_transform.rotation);
  204. }
  205. billboard_op.transform = math::matrix_cast(billboard_transform);
  206. queue.push_back(billboard_op);
  207. }
  208. void renderer::process_lod_group(const render::context& ctx, render::queue& queue, const scene::lod_group* lod_group) const
  209. {
  210. // Select level of detail
  211. std::size_t level = lod_group->select_lod(*ctx.camera);
  212. // Process all objects in the group with the selected level of detail
  213. const std::list<scene::object_base*>& objects = lod_group->get_objects(level);
  214. for (const scene::object_base* object: objects)
  215. {
  216. process_object(ctx, queue, object);
  217. }
  218. }
  219. void renderer::process_text(const render::context& ctx, render::queue& queue, const scene::text* text) const
  220. {
  221. // Get object culling volume
  222. const geom::bounding_volume<float>* object_culling_volume = text->get_culling_mask();
  223. if (!object_culling_volume)
  224. object_culling_volume = &text->get_world_bounds();
  225. // Perform view-frustum culling
  226. if (!ctx.camera_culling_volume->intersects(*object_culling_volume))
  227. return;
  228. text->render(ctx, queue);
  229. }
  230. } // namespace render