mounted_humanoid_renderer_base.cpp 4.8 KB

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