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

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