spearman_renderer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #include "spearman_renderer.h"
  2. #include "../../../../game/core/component.h"
  3. #include "../../../../game/systems/nation_id.h"
  4. #include "../../../equipment/equipment_registry.h"
  5. #include "../../../equipment/weapons/spear_renderer.h"
  6. #include "../../../geom/math_utils.h"
  7. #include "../../../geom/transforms.h"
  8. #include "../../../gl/backend.h"
  9. #include "../../../gl/primitives.h"
  10. #include "../../../gl/shader.h"
  11. #include "../../../humanoid/humanoid_math.h"
  12. #include "../../../humanoid/humanoid_specs.h"
  13. #include "../../../humanoid/pose_controller.h"
  14. #include "../../../humanoid/rig.h"
  15. #include "../../../humanoid/spear_pose_utils.h"
  16. #include "../../../humanoid/style_palette.h"
  17. #include "../../../palette.h"
  18. #include "../../../scene_renderer.h"
  19. #include "../../../submitter.h"
  20. #include "../../registry.h"
  21. #include "../../renderer_constants.h"
  22. #include "spearman_style.h"
  23. #include <QMatrix4x4>
  24. #include <QString>
  25. #include <QVector3D>
  26. #include <algorithm>
  27. #include <cmath>
  28. #include <cstdint>
  29. #include <optional>
  30. #include <qstringliteral.h>
  31. #include <qvectornd.h>
  32. #include <string>
  33. #include <string_view>
  34. #include <unordered_map>
  35. namespace Render::GL::Roman {
  36. namespace {
  37. constexpr std::string_view k_spearman_default_style_key = "default";
  38. constexpr float k_spearman_team_mix_weight = 0.6F;
  39. constexpr float k_spearman_style_mix_weight = 0.4F;
  40. constexpr float k_kneel_depth_multiplier = 0.875F;
  41. constexpr float k_lean_amount_multiplier = 0.67F;
  42. struct SpearmanShaderResourcePaths {
  43. QString vertex;
  44. QString fragment;
  45. };
  46. auto lookup_spearman_shader_resources(const QString &shader_key)
  47. -> std::optional<SpearmanShaderResourcePaths> {
  48. if (shader_key == QStringLiteral("spearman_carthage")) {
  49. return SpearmanShaderResourcePaths{
  50. QStringLiteral(":/assets/shaders/spearman_carthage.vert"),
  51. QStringLiteral(":/assets/shaders/spearman_carthage.frag")};
  52. }
  53. if (shader_key == QStringLiteral("spearman_roman_republic")) {
  54. return SpearmanShaderResourcePaths{
  55. QStringLiteral(":/assets/shaders/spearman_roman_republic.vert"),
  56. QStringLiteral(":/assets/shaders/spearman_roman_republic.frag")};
  57. }
  58. return std::nullopt;
  59. }
  60. auto spearman_style_registry()
  61. -> std::unordered_map<std::string, SpearmanStyleConfig> & {
  62. static std::unordered_map<std::string, SpearmanStyleConfig> styles;
  63. return styles;
  64. }
  65. void ensure_spearman_styles_registered() {
  66. static const bool registered = []() {
  67. register_roman_spearman_style();
  68. return true;
  69. }();
  70. (void)registered;
  71. }
  72. } // namespace
  73. void register_spearman_style(const std::string &nation_id,
  74. const SpearmanStyleConfig &style) {
  75. spearman_style_registry()[nation_id] = style;
  76. }
  77. using Render::Geom::clamp01;
  78. using Render::Geom::cone_from_to;
  79. using Render::Geom::cylinder_between;
  80. using Render::Geom::ease_in_out_cubic;
  81. using Render::Geom::lerp;
  82. using Render::Geom::smoothstep;
  83. using Render::Geom::sphere_at;
  84. using Render::GL::Humanoid::mix_palette_color;
  85. using Render::GL::Humanoid::saturate_color;
  86. struct SpearmanExtras {
  87. QVector3D spearShaftColor;
  88. QVector3D spearhead_color;
  89. float spear_length = 1.20F;
  90. float spear_shaft_radius = 0.020F;
  91. float spearhead_length = 0.18F;
  92. };
  93. class SpearmanRenderer : public HumanoidRendererBase {
  94. public:
  95. SpearmanRenderer() { cache_equipment(); }
  96. auto get_proportion_scaling() const -> QVector3D override {
  97. return {0.90F, 0.80F, 0.76F};
  98. }
  99. auto get_torso_scale() const -> float override { return 0.64F; }
  100. private:
  101. mutable std::unordered_map<uint32_t, SpearmanExtras> m_extrasCache;
  102. public:
  103. void get_variant(const DrawContext &ctx, uint32_t seed,
  104. HumanoidVariant &v) const override {
  105. QVector3D const team_tint = resolve_team_tint(ctx);
  106. v.palette = make_humanoid_palette(team_tint, seed);
  107. auto const &style = resolve_style(ctx);
  108. apply_palette_overrides(style, team_tint, v);
  109. }
  110. void customize_pose(const DrawContext &,
  111. const HumanoidAnimationContext &anim_ctx, uint32_t seed,
  112. HumanoidPose &pose) const override {
  113. using HP = HumanProportions;
  114. const AnimationInputs &anim = anim_ctx.inputs;
  115. HumanoidPoseController controller(pose, anim_ctx);
  116. float const arm_height_jitter = (hash_01(seed ^ 0xABCDU) - 0.5F) * 0.03F;
  117. float const arm_asymmetry = (hash_01(seed ^ 0xDEF0U) - 0.5F) * 0.04F;
  118. if (anim.is_in_hold_mode || anim.is_exiting_hold) {
  119. float const hold_t =
  120. anim.is_in_hold_mode ? 1.0F : (1.0F - anim.hold_exit_progress);
  121. if (anim.is_exiting_hold) {
  122. controller.kneel_transition(anim.hold_exit_progress, true);
  123. } else {
  124. controller.kneel(hold_t * k_kneel_depth_multiplier);
  125. }
  126. controller.lean(QVector3D(0.0F, 0.0F, 1.0F),
  127. hold_t * k_lean_amount_multiplier);
  128. if (anim.is_attacking && anim.is_melee && anim.is_in_hold_mode) {
  129. float const attack_phase = std::fmod(
  130. anim_ctx.attack_phase * SPEARMAN_INV_ATTACK_CYCLE_TIME, 1.0F);
  131. controller.spear_thrust_from_hold(attack_phase,
  132. hold_t * k_kneel_depth_multiplier);
  133. } else {
  134. float const lowered_shoulder_y = controller.get_shoulder_y(true);
  135. float const pelvis_y = controller.get_pelvis_y();
  136. QVector3D const hand_r_pos(0.18F * (1.0F - hold_t) + 0.22F * hold_t,
  137. lowered_shoulder_y * (1.0F - hold_t) +
  138. (pelvis_y + 0.05F) * hold_t,
  139. 0.15F * (1.0F - hold_t) + 0.20F * hold_t);
  140. float const offhand_along = lerp(-0.06F, -0.02F, hold_t);
  141. float const offhand_drop = 0.10F + 0.02F * hold_t;
  142. QVector3D const hand_l_pos =
  143. compute_offhand_spear_grip(pose, anim_ctx, hand_r_pos, false,
  144. offhand_along, offhand_drop, -0.08F);
  145. controller.place_hand_at(false, hand_r_pos);
  146. controller.place_hand_at(true, hand_l_pos);
  147. }
  148. } else if (anim.is_attacking && anim.is_melee && !anim.is_in_hold_mode) {
  149. float const attack_phase = std::fmod(
  150. anim_ctx.attack_phase * SPEARMAN_INV_ATTACK_CYCLE_TIME, 1.0F);
  151. controller.spear_thrust_variant(attack_phase, anim.attack_variant);
  152. } else {
  153. QVector3D const idle_hand_r(0.28F + arm_asymmetry,
  154. HP::SHOULDER_Y - 0.02F + arm_height_jitter,
  155. 0.30F);
  156. QVector3D const idle_hand_l = compute_offhand_spear_grip(
  157. pose, anim_ctx, idle_hand_r, false, -0.04F, 0.10F, -0.08F);
  158. controller.place_hand_at(false, idle_hand_r);
  159. controller.place_hand_at(true, idle_hand_l);
  160. }
  161. }
  162. void add_attachments(const DrawContext &ctx, const HumanoidVariant &v,
  163. const HumanoidPose &pose,
  164. const HumanoidAnimationContext &anim_ctx,
  165. ISubmitter &out) const override {
  166. const AnimationInputs &anim = anim_ctx.inputs;
  167. uint32_t const seed = reinterpret_cast<uintptr_t>(ctx.entity) & 0xFFFFFFFFU;
  168. auto const &style = resolve_style(ctx);
  169. QVector3D const team_tint = resolve_team_tint(ctx);
  170. SpearmanExtras extras;
  171. auto it = m_extrasCache.find(seed);
  172. if (it != m_extrasCache.end()) {
  173. extras = it->second;
  174. } else {
  175. extras = computeSpearmanExtras(seed, v);
  176. apply_extras_overrides(style, team_tint, v, extras);
  177. m_extrasCache[seed] = extras;
  178. if (m_extrasCache.size() > MAX_EXTRAS_CACHE_SIZE) {
  179. m_extrasCache.clear();
  180. }
  181. }
  182. apply_extras_overrides(style, team_tint, v, extras);
  183. bool const is_attacking = anim.is_attacking && anim.is_melee;
  184. if (m_cached_spear) {
  185. SpearRenderConfig spear_config;
  186. spear_config.shaft_color = extras.spearShaftColor;
  187. spear_config.spearhead_color = extras.spearhead_color;
  188. spear_config.spear_length = extras.spear_length;
  189. spear_config.shaft_radius = extras.spear_shaft_radius;
  190. spear_config.spearhead_length = extras.spearhead_length;
  191. auto *spear_renderer =
  192. dynamic_cast<SpearRenderer *>(m_cached_spear.get());
  193. if (spear_renderer) {
  194. spear_renderer->set_config(spear_config);
  195. }
  196. m_cached_spear->render(ctx, pose.body_frames, v.palette, anim_ctx, out);
  197. }
  198. }
  199. void draw_helmet(const DrawContext &ctx, const HumanoidVariant &v,
  200. const HumanoidPose &pose, ISubmitter &out) const override {
  201. if (m_cached_helmet) {
  202. HumanoidAnimationContext anim_ctx{};
  203. m_cached_helmet->render(ctx, pose.body_frames, v.palette, anim_ctx, out);
  204. }
  205. }
  206. void draw_armor(const DrawContext &ctx, const HumanoidVariant &v,
  207. const HumanoidPose &pose,
  208. const HumanoidAnimationContext &anim,
  209. ISubmitter &out) const override {
  210. if (m_cached_armor) {
  211. m_cached_armor->render(ctx, pose.body_frames, v.palette, anim, out);
  212. }
  213. if (m_cached_shoulder_cover) {
  214. m_cached_shoulder_cover->render(ctx, pose.body_frames, v.palette, anim,
  215. out);
  216. }
  217. if (m_cached_greaves) {
  218. m_cached_greaves->render(ctx, pose.body_frames, v.palette, anim, out);
  219. }
  220. }
  221. private:
  222. void cache_equipment() {
  223. auto &registry = EquipmentRegistry::instance();
  224. m_cached_spear = registry.get(EquipmentCategory::Weapon, "spear");
  225. m_cached_helmet = registry.get(EquipmentCategory::Helmet, "roman_heavy");
  226. m_cached_armor =
  227. registry.get(EquipmentCategory::Armor, "roman_light_armor");
  228. m_cached_shoulder_cover =
  229. registry.get(EquipmentCategory::Armor, "roman_shoulder_cover");
  230. m_cached_greaves = registry.get(EquipmentCategory::Armor, "roman_greaves");
  231. }
  232. mutable std::shared_ptr<IEquipmentRenderer> m_cached_spear;
  233. mutable std::shared_ptr<IEquipmentRenderer> m_cached_helmet;
  234. mutable std::shared_ptr<IEquipmentRenderer> m_cached_armor;
  235. mutable std::shared_ptr<IEquipmentRenderer> m_cached_shoulder_cover;
  236. mutable std::shared_ptr<IEquipmentRenderer> m_cached_greaves;
  237. static auto computeSpearmanExtras(uint32_t seed, const HumanoidVariant &v)
  238. -> SpearmanExtras {
  239. SpearmanExtras e;
  240. e.spearShaftColor = v.palette.leather * QVector3D(0.85F, 0.75F, 0.65F);
  241. e.spearhead_color = QVector3D(0.75F, 0.76F, 0.80F);
  242. e.spear_length = 1.15F + (hash_01(seed ^ 0xABCDU) - 0.5F) * 0.10F;
  243. e.spear_shaft_radius = 0.018F + (hash_01(seed ^ 0x7777U) - 0.5F) * 0.003F;
  244. e.spearhead_length = 0.16F + (hash_01(seed ^ 0xBEEFU) - 0.5F) * 0.04F;
  245. return e;
  246. }
  247. auto
  248. resolve_style(const DrawContext &ctx) const -> const SpearmanStyleConfig & {
  249. ensure_spearman_styles_registered();
  250. auto &styles = spearman_style_registry();
  251. std::string nation_id;
  252. if (ctx.entity != nullptr) {
  253. if (auto *unit =
  254. ctx.entity->get_component<Engine::Core::UnitComponent>()) {
  255. nation_id = Game::Systems::nation_id_to_string(unit->nation_id);
  256. }
  257. }
  258. if (!nation_id.empty()) {
  259. auto it = styles.find(nation_id);
  260. if (it != styles.end()) {
  261. return it->second;
  262. }
  263. }
  264. auto it_default = styles.find(std::string(k_spearman_default_style_key));
  265. if (it_default != styles.end()) {
  266. return it_default->second;
  267. }
  268. static const SpearmanStyleConfig k_empty{};
  269. return k_empty;
  270. }
  271. public:
  272. auto resolve_shader_key(const DrawContext &ctx) const -> QString {
  273. const SpearmanStyleConfig &style = resolve_style(ctx);
  274. if (!style.shader_id.empty()) {
  275. return QString::fromStdString(style.shader_id);
  276. }
  277. return QStringLiteral("spearman");
  278. }
  279. private:
  280. void apply_palette_overrides(const SpearmanStyleConfig &style,
  281. const QVector3D &team_tint,
  282. HumanoidVariant &variant) const {
  283. auto apply_color = [&](const std::optional<QVector3D> &override_color,
  284. QVector3D &target) {
  285. target = mix_palette_color(target, override_color, team_tint,
  286. k_spearman_team_mix_weight,
  287. k_spearman_style_mix_weight);
  288. };
  289. apply_color(style.cloth_color, variant.palette.cloth);
  290. apply_color(style.leather_color, variant.palette.leather);
  291. apply_color(style.leather_dark_color, variant.palette.leather_dark);
  292. apply_color(style.metal_color, variant.palette.metal);
  293. }
  294. void apply_extras_overrides(const SpearmanStyleConfig &style,
  295. const QVector3D &team_tint,
  296. [[maybe_unused]] const HumanoidVariant &variant,
  297. SpearmanExtras &extras) const {
  298. extras.spearShaftColor = saturate_color(extras.spearShaftColor);
  299. extras.spearhead_color = saturate_color(extras.spearhead_color);
  300. auto apply_color = [&](const std::optional<QVector3D> &override_color,
  301. QVector3D &target) {
  302. target = mix_palette_color(target, override_color, team_tint,
  303. k_spearman_team_mix_weight,
  304. k_spearman_style_mix_weight);
  305. };
  306. apply_color(style.spear_shaft_color, extras.spearShaftColor);
  307. apply_color(style.spearhead_color, extras.spearhead_color);
  308. if (style.spear_length_scale) {
  309. extras.spear_length =
  310. std::max(0.80F, extras.spear_length * *style.spear_length_scale);
  311. }
  312. }
  313. };
  314. void register_spearman_renderer(Render::GL::EntityRendererRegistry &registry) {
  315. ensure_spearman_styles_registered();
  316. static SpearmanRenderer const renderer;
  317. registry.register_renderer("troops/roman/spearman", [](const DrawContext &ctx,
  318. ISubmitter &out) {
  319. static SpearmanRenderer const static_renderer;
  320. Shader *spearman_shader = nullptr;
  321. auto acquireShader = [&](const QString &shader_key) -> Shader * {
  322. if (ctx.backend == nullptr || shader_key.isEmpty()) {
  323. return nullptr;
  324. }
  325. Shader *shader = ctx.backend->shader(shader_key);
  326. if (shader != nullptr) {
  327. return shader;
  328. }
  329. if (auto resources = lookup_spearman_shader_resources(shader_key)) {
  330. shader = ctx.backend->get_or_load_shader(shader_key, resources->vertex,
  331. resources->fragment);
  332. }
  333. return shader;
  334. };
  335. if (ctx.backend != nullptr) {
  336. QString shader_key = static_renderer.resolve_shader_key(ctx);
  337. spearman_shader = acquireShader(shader_key);
  338. if (spearman_shader == nullptr) {
  339. spearman_shader = acquireShader(QStringLiteral("spearman"));
  340. }
  341. }
  342. auto *scene_renderer = dynamic_cast<Renderer *>(&out);
  343. if ((scene_renderer != nullptr) && (spearman_shader != nullptr)) {
  344. scene_renderer->set_current_shader(spearman_shader);
  345. }
  346. static_renderer.render(ctx, out);
  347. if (scene_renderer != nullptr) {
  348. scene_renderer->set_current_shader(nullptr);
  349. }
  350. });
  351. }
  352. } // namespace Render::GL::Roman