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

247 lines
8.2 KiB

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