mounted_humanoid_renderer_base.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "mounted_humanoid_renderer_base.h"
  2. #include "../gl/camera.h"
  3. #include "../humanoid/humanoid_math.h"
  4. #include "../humanoid/humanoid_specs.h"
  5. #include "../palette.h"
  6. #include "../visibility_budget.h"
  7. #include "../../game/core/component.h"
  8. #include "../../game/core/entity.h"
  9. #include "mounted_knight_pose.h"
  10. #include <QVector3D>
  11. #include <algorithm>
  12. #include <cmath>
  13. namespace Render::GL {
  14. MountedHumanoidRendererBase::MountedHumanoidRendererBase() = default;
  15. auto MountedHumanoidRendererBase::get_scaled_horse_dimensions(
  16. uint32_t seed) const -> HorseDimensions {
  17. HorseDimensions dims = make_horse_dimensions(seed);
  18. scale_horse_dimensions(dims, get_mount_scale());
  19. return dims;
  20. }
  21. auto MountedHumanoidRendererBase::get_cached_horse_profile(
  22. uint32_t seed, const HumanoidVariant &v) const -> const HorseProfile & {
  23. auto it = m_profile_cache.find(seed);
  24. if (it != m_profile_cache.end()) {
  25. return it->second;
  26. }
  27. HorseDimensions dims = get_scaled_horse_dimensions(seed);
  28. HorseProfile profile =
  29. make_horse_profile(seed, v.palette.leather, v.palette.cloth);
  30. profile.dims = dims;
  31. m_profile_cache[seed] = profile;
  32. if (m_profile_cache.size() > MAX_PROFILE_CACHE_SIZE) {
  33. m_profile_cache.clear();
  34. }
  35. return m_profile_cache[seed];
  36. }
  37. auto MountedHumanoidRendererBase::resolve_entity_ground_offset(
  38. const DrawContext &ctx, Engine::Core::UnitComponent *unit_comp,
  39. Engine::Core::TransformComponent *transform_comp) const -> float {
  40. (void)unit_comp;
  41. uint32_t horse_seed = 0U;
  42. if (ctx.entity != nullptr) {
  43. horse_seed = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(ctx.entity) &
  44. 0xFFFFFFFFU);
  45. }
  46. HorseDimensions dims = get_scaled_horse_dimensions(horse_seed);
  47. float offset = -dims.barrel_center_y;
  48. if (transform_comp != nullptr) {
  49. offset *= transform_comp->scale.y;
  50. }
  51. return offset;
  52. }
  53. void MountedHumanoidRendererBase::customize_pose(
  54. const DrawContext &ctx, const HumanoidAnimationContext &anim_ctx,
  55. uint32_t seed, HumanoidPose &pose) const {
  56. const AnimationInputs &anim = anim_ctx.inputs;
  57. uint32_t horse_seed = seed;
  58. if (ctx.entity != nullptr) {
  59. horse_seed = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(ctx.entity) &
  60. 0xFFFFFFFFU);
  61. }
  62. HorseDimensions dims = get_scaled_horse_dimensions(horse_seed);
  63. HorseProfile mount_profile{};
  64. mount_profile.dims = dims;
  65. MountedAttachmentFrame mount = compute_mount_frame(mount_profile);
  66. tune_mounted_knight_frame(dims, mount);
  67. HorseMotionSample const motion =
  68. evaluate_horse_motion(mount_profile, anim, anim_ctx);
  69. apply_mount_vertical_offset(mount, motion.bob);
  70. m_last_pose = &pose;
  71. m_last_mount = mount;
  72. m_last_motion = motion;
  73. ReinState const reins = compute_rein_state(horse_seed, anim_ctx);
  74. m_last_rein_state = reins;
  75. m_has_last_reins = true;
  76. MountedPoseController mounted_controller(pose, anim_ctx);
  77. mounted_controller.mount_on_horse(mount);
  78. apply_riding_animation(mounted_controller, mount, anim_ctx, pose, dims,
  79. reins);
  80. applyMountedKnightLowerBody(dims, mount, anim_ctx, pose);
  81. mounted_controller.finalize_head_sync(mount, "customize_pose_final_sync");
  82. }
  83. void MountedHumanoidRendererBase::add_attachments(
  84. const DrawContext &ctx, const HumanoidVariant &v, const HumanoidPose &pose,
  85. const HumanoidAnimationContext &anim_ctx, ISubmitter &out) const {
  86. static uint64_t s_mounted_frame_counter = 0;
  87. ++s_mounted_frame_counter;
  88. uint64_t frame_id = s_mounted_frame_counter;
  89. uint32_t horse_seed = 0U;
  90. if (ctx.entity != nullptr) {
  91. horse_seed = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(ctx.entity) &
  92. 0xFFFFFFFFU);
  93. }
  94. const HorseProfile &profile_const = get_cached_horse_profile(horse_seed, v);
  95. HorseProfile &profile = const_cast<HorseProfile &>(profile_const);
  96. const bool is_current_pose = (m_last_pose == &pose);
  97. const MountedAttachmentFrame *mount_ptr =
  98. (is_current_pose) ? &m_last_mount : nullptr;
  99. const ReinState *rein_ptr =
  100. (is_current_pose && m_has_last_reins) ? &m_last_rein_state : nullptr;
  101. const HorseMotionSample *motion_ptr =
  102. (is_current_pose) ? &m_last_motion : nullptr;
  103. const AnimationInputs &anim = anim_ctx.inputs;
  104. HorseLOD horse_lod = HorseLOD::Full;
  105. if (ctx.camera != nullptr) {
  106. QVector3D const horse_world_pos =
  107. ctx.model.map(QVector3D(0.0F, 0.0F, 0.0F));
  108. float const distance =
  109. (horse_world_pos - ctx.camera->get_position()).length();
  110. horse_lod = calculate_horse_lod(distance);
  111. horse_lod = Render::VisibilityBudgetTracker::instance().request_horse_lod(
  112. horse_lod);
  113. }
  114. m_horseRenderer.render(ctx, anim, anim_ctx, profile, mount_ptr, rein_ptr,
  115. motion_ptr, out, horse_lod);
  116. m_last_pose = nullptr;
  117. m_has_last_reins = false;
  118. draw_equipment(ctx, v, pose, anim_ctx, out);
  119. }
  120. } // namespace Render::GL