mounted_knight_renderer_base.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include "mounted_knight_renderer_base.h"
  2. #include "../equipment/equipment_registry.h"
  3. #include "../equipment/weapons/shield_renderer.h"
  4. #include "../equipment/weapons/sword_renderer.h"
  5. #include "../humanoid/humanoid_math.h"
  6. #include "../humanoid/humanoid_specs.h"
  7. #include "../humanoid/mounted_pose_controller.h"
  8. #include "../palette.h"
  9. #include "../../game/core/component.h"
  10. #include "../../game/core/entity.h"
  11. #include "../../game/systems/nation_id.h"
  12. #include "mounted_knight_pose.h"
  13. #include "renderer_constants.h"
  14. #include <QVector3D>
  15. #include <algorithm>
  16. #include <cmath>
  17. #include <utility>
  18. namespace Render::GL {
  19. namespace {
  20. constexpr QVector3D k_default_proportion_scale{0.80F, 0.88F, 0.88F};
  21. }
  22. MountedKnightRendererBase::MountedKnightRendererBase(
  23. MountedKnightRendererConfig config)
  24. : m_config(std::move(config)) {
  25. m_config.has_sword =
  26. m_config.has_sword && !m_config.sword_equipment_id.empty();
  27. if (!m_config.has_sword) {
  28. m_config.sword_equipment_id.clear();
  29. }
  30. m_config.has_cavalry_shield =
  31. m_config.has_cavalry_shield && !m_config.shield_equipment_id.empty();
  32. if (!m_config.has_cavalry_shield) {
  33. m_config.shield_equipment_id.clear();
  34. }
  35. m_horseRenderer.set_attachments(m_config.horse_attachments);
  36. }
  37. auto MountedKnightRendererBase::get_proportion_scaling() const -> QVector3D {
  38. return k_default_proportion_scale;
  39. }
  40. auto MountedKnightRendererBase::get_mount_scale() const -> float {
  41. return m_config.mount_scale;
  42. }
  43. void MountedKnightRendererBase::adjust_variation(
  44. const DrawContext &, uint32_t, VariationParams &variation) const {
  45. variation.height_scale = 0.88F;
  46. variation.bulk_scale = 0.76F;
  47. variation.stance_width = 0.60F;
  48. variation.arm_swing_amp = 0.45F;
  49. variation.walk_speed_mult = 1.0F;
  50. variation.posture_slump = 0.0F;
  51. variation.shoulder_tilt = 0.0F;
  52. }
  53. void MountedKnightRendererBase::get_variant(const DrawContext &ctx,
  54. uint32_t seed,
  55. HumanoidVariant &v) const {
  56. QVector3D const team_tint = resolve_team_tint(ctx);
  57. v.palette = make_humanoid_palette(team_tint, seed);
  58. }
  59. void MountedKnightRendererBase::apply_riding_animation(
  60. MountedPoseController &mounted_controller, MountedAttachmentFrame &mount,
  61. const HumanoidAnimationContext &anim_ctx, HumanoidPose &pose,
  62. const HorseDimensions &dims, const ReinState &reins) const {
  63. (void)pose;
  64. const AnimationInputs &anim = anim_ctx.inputs;
  65. float const speed_norm = anim_ctx.locomotion_normalized_speed();
  66. float const speed_lean = 0.0F;
  67. float const forward_lean =
  68. (dims.seat_forward_offset * 0.08F + speed_lean) / 0.15F;
  69. MountedPoseController::MountedRiderPoseRequest pose_request;
  70. pose_request.dims = dims;
  71. pose_request.forwardBias = forward_lean;
  72. pose_request.reinSlackLeft = reins.slack;
  73. pose_request.reinSlackRight = reins.slack;
  74. pose_request.reinTensionLeft = reins.tension;
  75. pose_request.reinTensionRight = reins.tension;
  76. pose_request.leftHandOnReins = !m_config.has_cavalry_shield;
  77. pose_request.rightHandOnReins = true;
  78. pose_request.clearanceForward = 1.15F;
  79. pose_request.clearanceUp = 1.05F;
  80. pose_request.seatPose = (speed_norm > 0.55F)
  81. ? MountedPoseController::MountedSeatPose::Forward
  82. : MountedPoseController::MountedSeatPose::Neutral;
  83. pose_request.torsoCompression =
  84. std::clamp(0.18F + anim_ctx.variation.posture_slump * 0.9F, 0.0F, 0.55F);
  85. pose_request.torsoTwist = anim_ctx.variation.shoulder_tilt * 3.0F;
  86. pose_request.shoulderDip =
  87. std::clamp(anim_ctx.variation.shoulder_tilt * 0.6F +
  88. (m_config.has_cavalry_shield ? 0.18F : 0.08F),
  89. -0.4F, 0.4F);
  90. if (m_config.has_cavalry_shield) {
  91. pose_request.shieldPose = MountedPoseController::MountedShieldPose::Guard;
  92. }
  93. if (anim.is_attacking && anim.is_melee) {
  94. pose_request.weaponPose =
  95. MountedPoseController::MountedWeaponPose::SwordStrike;
  96. pose_request.actionPhase =
  97. std::fmod(anim.time * MOUNTED_KNIGHT_INV_ATTACK_CYCLE_TIME, 1.0F);
  98. pose_request.rightHandOnReins = false;
  99. if (m_config.has_cavalry_shield) {
  100. pose_request.shieldPose =
  101. MountedPoseController::MountedShieldPose::Stowed;
  102. }
  103. } else {
  104. pose_request.weaponPose =
  105. m_config.has_sword ? MountedPoseController::MountedWeaponPose::SwordIdle
  106. : MountedPoseController::MountedWeaponPose::None;
  107. pose_request.rightHandOnReins = !m_config.has_sword;
  108. }
  109. mounted_controller.apply_pose(mount, pose_request);
  110. }
  111. void MountedKnightRendererBase::draw_equipment(
  112. const DrawContext &ctx, const HumanoidVariant &v, const HumanoidPose &pose,
  113. const HumanoidAnimationContext &anim_ctx, ISubmitter &out) const {
  114. auto &registry = EquipmentRegistry::instance();
  115. uint32_t horse_seed = 0U;
  116. if (ctx.entity != nullptr) {
  117. horse_seed = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(ctx.entity) &
  118. 0xFFFFFFFFU);
  119. }
  120. float const sword_length =
  121. 0.82F + (hash_01(horse_seed ^ 0xABCDU) - 0.5F) * 0.12F;
  122. float const sword_width =
  123. 0.042F + (hash_01(horse_seed ^ 0x7777U) - 0.5F) * 0.008F;
  124. if (m_config.has_sword && !m_config.sword_equipment_id.empty()) {
  125. auto sword =
  126. registry.get(EquipmentCategory::Weapon, m_config.sword_equipment_id);
  127. if (sword) {
  128. SwordRenderConfig sword_config;
  129. sword_config.metal_color = m_config.metal_color;
  130. sword_config.sword_length = sword_length;
  131. sword_config.sword_width = sword_width;
  132. if (auto *sword_renderer = dynamic_cast<SwordRenderer *>(sword.get())) {
  133. sword_renderer->set_config(sword_config);
  134. }
  135. sword->render(ctx, pose.body_frames, v.palette, anim_ctx, out);
  136. }
  137. }
  138. if (m_config.has_cavalry_shield && !m_config.shield_equipment_id.empty()) {
  139. auto shield =
  140. registry.get(EquipmentCategory::Weapon, m_config.shield_equipment_id);
  141. if (shield) {
  142. shield->render(ctx, pose.body_frames, v.palette, anim_ctx, out);
  143. }
  144. }
  145. if (m_config.has_shoulder && !m_config.shoulder_equipment_id.empty()) {
  146. auto shoulder_cover =
  147. registry.get(EquipmentCategory::Armor, m_config.shoulder_equipment_id);
  148. if (shoulder_cover) {
  149. shoulder_cover->render(ctx, pose.body_frames, v.palette, anim_ctx, out);
  150. }
  151. }
  152. }
  153. void MountedKnightRendererBase::draw_helmet(const DrawContext &ctx,
  154. const HumanoidVariant &v,
  155. const HumanoidPose &pose,
  156. ISubmitter &out) const {
  157. if (m_config.helmet_equipment_id.empty()) {
  158. return;
  159. }
  160. auto &registry = EquipmentRegistry::instance();
  161. auto helmet =
  162. registry.get(EquipmentCategory::Helmet, m_config.helmet_equipment_id);
  163. if (helmet) {
  164. HumanoidAnimationContext anim_ctx{};
  165. BodyFrames frames = pose.body_frames;
  166. if (ctx.entity != nullptr) {
  167. auto *move = ctx.entity->get_component<Engine::Core::MovementComponent>();
  168. if (move != nullptr) {
  169. float speed_sq = move->vx * move->vx + move->vz * move->vz;
  170. if (speed_sq > 0.0001F && m_config.helmet_offset_moving > 0.0F) {
  171. frames.head.origin +=
  172. frames.head.forward * m_config.helmet_offset_moving;
  173. }
  174. }
  175. }
  176. helmet->render(ctx, frames, v.palette, anim_ctx, out);
  177. }
  178. }
  179. void MountedKnightRendererBase::draw_armor(const DrawContext &ctx,
  180. const HumanoidVariant &v,
  181. const HumanoidPose &pose,
  182. const HumanoidAnimationContext &anim,
  183. ISubmitter &out) const {
  184. if (m_config.armor_equipment_id.empty()) {
  185. return;
  186. }
  187. auto &registry = EquipmentRegistry::instance();
  188. auto armor =
  189. registry.get(EquipmentCategory::Armor, m_config.armor_equipment_id);
  190. if (armor) {
  191. armor->render(ctx, pose.body_frames, v.palette, anim, out);
  192. }
  193. }
  194. auto MountedKnightRendererBase::resolve_shader_key(const DrawContext &ctx) const
  195. -> QString {
  196. std::string nation;
  197. if (ctx.entity != nullptr) {
  198. if (auto *unit = ctx.entity->get_component<Engine::Core::UnitComponent>()) {
  199. nation = Game::Systems::nationIDToString(unit->nation_id);
  200. }
  201. }
  202. if (!nation.empty()) {
  203. return QString::fromStdString(std::string("horse_swordsman_") + nation);
  204. }
  205. return QStringLiteral("horse_swordsman");
  206. }
  207. } // namespace Render::GL