archer_renderer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #include "archer_renderer.h"
  2. #include "../../../../game/core/component.h"
  3. #include "../../../../game/core/entity.h"
  4. #include "../../../../game/systems/nation_id.h"
  5. #include "../../../equipment/armor/cloak_renderer.h"
  6. #include "../../../equipment/equipment_registry.h"
  7. #include "../../../equipment/weapons/bow_renderer.h"
  8. #include "../../../equipment/weapons/quiver_renderer.h"
  9. #include "../../../geom/math_utils.h"
  10. #include "../../../geom/transforms.h"
  11. #include "../../../gl/backend.h"
  12. #include "../../../gl/primitives.h"
  13. #include "../../../gl/render_constants.h"
  14. #include "../../../gl/shader.h"
  15. #include "../../../humanoid/humanoid_math.h"
  16. #include "../../../humanoid/humanoid_specs.h"
  17. #include "../../../humanoid/pose_controller.h"
  18. #include "../../../humanoid/rig.h"
  19. #include "../../../humanoid/style_palette.h"
  20. #include "../../../palette.h"
  21. #include "../../../scene_renderer.h"
  22. #include "../../../submitter.h"
  23. #include "../../registry.h"
  24. #include "../../renderer_constants.h"
  25. #include "archer_style.h"
  26. #include <QMatrix4x4>
  27. #include <QString>
  28. #include <QVector3D>
  29. #include <cmath>
  30. #include <cstdint>
  31. #include <numbers>
  32. #include <optional>
  33. #include <qmatrix4x4.h>
  34. #include <qstringliteral.h>
  35. #include <qvectornd.h>
  36. #include <string>
  37. #include <string_view>
  38. #include <unordered_map>
  39. namespace Render::GL::Roman {
  40. namespace {
  41. constexpr std::string_view k_default_style_key = "default";
  42. constexpr std::string_view k_attachment_headwrap = "carthage_headwrap";
  43. constexpr float k_kneel_depth_multiplier = 1.125F;
  44. constexpr float k_lean_amount_multiplier = 0.83F;
  45. auto style_registry() -> std::unordered_map<std::string, ArcherStyleConfig> & {
  46. static std::unordered_map<std::string, ArcherStyleConfig> styles;
  47. return styles;
  48. }
  49. void ensure_archer_styles_registered() {
  50. static const bool registered = []() {
  51. register_roman_archer_style();
  52. return true;
  53. }();
  54. (void)registered;
  55. }
  56. constexpr float k_team_mix_weight = 0.65F;
  57. constexpr float k_style_mix_weight = 0.35F;
  58. } // namespace
  59. void register_archer_style(const std::string &nation_id,
  60. const ArcherStyleConfig &style) {
  61. style_registry()[nation_id] = style;
  62. }
  63. using Render::Geom::clamp01;
  64. using Render::Geom::clamp_f;
  65. using Render::Geom::cone_from_to;
  66. using Render::Geom::cylinder_between;
  67. using Render::Geom::sphere_at;
  68. using Render::GL::Humanoid::mix_palette_color;
  69. using Render::GL::Humanoid::saturate_color;
  70. class ArcherRenderer : public HumanoidRendererBase {
  71. public:
  72. auto get_proportion_scaling() const -> QVector3D override {
  73. return {0.78F, 1.01F, 0.96F};
  74. }
  75. void get_variant(const DrawContext &ctx, uint32_t seed,
  76. HumanoidVariant &v) const override {
  77. QVector3D const team_tint = resolve_team_tint(ctx);
  78. v.palette = make_humanoid_palette(team_tint, seed);
  79. auto const &style = resolve_style(ctx);
  80. apply_palette_overrides(style, team_tint, v);
  81. }
  82. void customize_pose(const DrawContext &,
  83. const HumanoidAnimationContext &anim_ctx, uint32_t seed,
  84. HumanoidPose &pose) const override {
  85. using HP = HumanProportions;
  86. const AnimationInputs &anim = anim_ctx.inputs;
  87. HumanoidPoseController controller(pose, anim_ctx);
  88. float const arm_height_jitter = (hash_01(seed ^ 0xABCDU) - 0.5F) * 0.03F;
  89. float const arm_asymmetry = (hash_01(seed ^ 0xDEF0U) - 0.5F) * 0.04F;
  90. float const bow_x = 0.0F;
  91. if (anim.is_in_hold_mode || anim.is_exiting_hold) {
  92. float const t =
  93. anim.is_in_hold_mode ? 1.0F : (1.0F - anim.hold_exit_progress);
  94. controller.kneel(t * k_kneel_depth_multiplier);
  95. controller.lean(QVector3D(0.0F, 0.0F, 1.0F),
  96. t * k_lean_amount_multiplier);
  97. QVector3D const hold_hand_r(
  98. bow_x + 0.03F, controller.get_shoulder_y(false) + 0.30F, 0.55F);
  99. QVector3D const hold_hand_l(
  100. bow_x - 0.02F, controller.get_shoulder_y(true) + 0.12F, 0.55F);
  101. QVector3D const normal_hand_r(bow_x + 0.03F - arm_asymmetry,
  102. HP::SHOULDER_Y + 0.05F + arm_height_jitter,
  103. 0.55F);
  104. QVector3D const normal_hand_l(
  105. bow_x - 0.02F + arm_asymmetry * 0.5F,
  106. HP::SHOULDER_Y + 0.12F + arm_height_jitter * 0.8F, 0.50F);
  107. QVector3D const blended_hand_r =
  108. normal_hand_r * (1.0F - t) + hold_hand_r * t;
  109. QVector3D const blended_hand_l =
  110. normal_hand_l * (1.0F - t) + hold_hand_l * t;
  111. controller.place_hand_at(false, blended_hand_r);
  112. controller.place_hand_at(true, blended_hand_l);
  113. } else {
  114. QVector3D const idle_hand_r(bow_x + 0.03F - arm_asymmetry,
  115. HP::SHOULDER_Y + 0.05F + arm_height_jitter,
  116. 0.55F);
  117. QVector3D const idle_hand_l(
  118. bow_x - 0.05F + arm_asymmetry * 0.5F,
  119. HP::SHOULDER_Y + 0.14F + arm_height_jitter * 0.8F, 0.48F);
  120. controller.place_hand_at(false, idle_hand_r);
  121. controller.place_hand_at(true, idle_hand_l);
  122. }
  123. if (anim.is_attacking && !anim.is_in_hold_mode) {
  124. float const attack_phase =
  125. std::fmod(anim_ctx.attack_phase * ARCHER_INV_ATTACK_CYCLE_TIME, 1.0F);
  126. if (anim.is_melee) {
  127. controller.melee_strike(attack_phase);
  128. } else {
  129. controller.aim_bow(attack_phase);
  130. }
  131. }
  132. }
  133. void add_attachments(const DrawContext &ctx, const HumanoidVariant &v,
  134. const HumanoidPose &pose,
  135. const HumanoidAnimationContext &anim_ctx,
  136. ISubmitter &out) const override {
  137. using HP = HumanProportions;
  138. auto const &style = resolve_style(ctx);
  139. QVector3D team_tint = resolve_team_tint(ctx);
  140. auto tint = [&](float k) {
  141. return QVector3D(clamp01(team_tint.x() * k), clamp01(team_tint.y() * k),
  142. clamp01(team_tint.z() * k));
  143. };
  144. QVector3D const fletch = tint(0.9F);
  145. auto &registry = EquipmentRegistry::instance();
  146. if (style.show_cape) {
  147. auto cloak = registry.get(EquipmentCategory::Armor, "cloak_carthage");
  148. if (cloak) {
  149. CloakConfig cloak_config;
  150. if (style.cape_color) {
  151. cloak_config.primary_color = *style.cape_color;
  152. } else {
  153. cloak_config.primary_color = QVector3D(0.70F, 0.15F, 0.18F);
  154. }
  155. cloak_config.trim_color = v.palette.metal;
  156. if (auto *cloak_renderer = dynamic_cast<CloakRenderer *>(cloak.get())) {
  157. cloak_renderer->set_config(cloak_config);
  158. }
  159. cloak->render(ctx, pose.body_frames, v.palette, anim_ctx, out);
  160. }
  161. }
  162. auto quiver = registry.get(EquipmentCategory::Weapon, "quiver");
  163. if (quiver) {
  164. QuiverRenderConfig quiver_config;
  165. quiver_config.fletching_color = fletch;
  166. quiver_config.quiver_radius = HP::HEAD_RADIUS * 0.45F;
  167. auto *quiver_renderer = dynamic_cast<QuiverRenderer *>(quiver.get());
  168. if (quiver_renderer) {
  169. quiver_renderer->set_config(quiver_config);
  170. }
  171. quiver->render(ctx, pose.body_frames, v.palette, anim_ctx, out);
  172. }
  173. auto bow = registry.get(EquipmentCategory::Weapon, "bow_roman");
  174. if (bow) {
  175. BowRenderConfig bow_config;
  176. bow_config.string_color = QVector3D(0.30F, 0.30F, 0.32F);
  177. bow_config.metal_color =
  178. Render::Geom::clamp_vec_01(v.palette.metal * 1.15F);
  179. bow_config.fletching_color = fletch;
  180. bow_config.bow_top_y = HP::SHOULDER_Y + 0.55F;
  181. bow_config.bow_bot_y = HP::WAIST_Y - 0.25F;
  182. bow_config.bow_x = 0.0F;
  183. bow_config.arrow_visibility = ArrowVisibility::IdleAndAttackCycle;
  184. if (style.bow_string_color) {
  185. bow_config.string_color = saturate_color(*style.bow_string_color);
  186. }
  187. if (style.fletching_color) {
  188. bow_config.fletching_color = saturate_color(*style.fletching_color);
  189. }
  190. auto *bow_renderer = dynamic_cast<BowRenderer *>(bow.get());
  191. if (bow_renderer) {
  192. bow_renderer->set_config(bow_config);
  193. }
  194. bow->render(ctx, pose.body_frames, v.palette, anim_ctx, out);
  195. }
  196. }
  197. void draw_helmet(const DrawContext &ctx, const HumanoidVariant &v,
  198. const HumanoidPose &pose, ISubmitter &out) const override {
  199. using HP = HumanProportions;
  200. auto const &style = resolve_style(ctx);
  201. auto &registry = EquipmentRegistry::instance();
  202. HumanoidAnimationContext anim_ctx{};
  203. if (!style.show_helmet) {
  204. if (style.attachment_profile == std::string(k_attachment_headwrap)) {
  205. auto headwrap = registry.get(EquipmentCategory::Helmet, "headwrap");
  206. if (headwrap) {
  207. headwrap->render(ctx, pose.body_frames, v.palette, anim_ctx, out);
  208. }
  209. }
  210. return;
  211. }
  212. auto helmet = registry.get(EquipmentCategory::Helmet, "roman_light");
  213. if (helmet) {
  214. helmet->render(ctx, pose.body_frames, v.palette, anim_ctx, out);
  215. }
  216. }
  217. void draw_armor(const DrawContext &ctx, const HumanoidVariant &v,
  218. const HumanoidPose &pose,
  219. const HumanoidAnimationContext &anim,
  220. ISubmitter &out) const override {
  221. auto &registry = EquipmentRegistry::instance();
  222. if (resolve_style(ctx).show_armor) {
  223. auto armor = registry.get(EquipmentCategory::Armor, "roman_light_armor");
  224. if (armor) {
  225. armor->render(ctx, pose.body_frames, v.palette, anim, out);
  226. }
  227. }
  228. auto greaves = registry.get(EquipmentCategory::Armor, "roman_greaves");
  229. if (greaves) {
  230. greaves->render(ctx, pose.body_frames, v.palette, anim, out);
  231. }
  232. }
  233. private:
  234. auto
  235. resolve_style(const DrawContext &ctx) const -> const ArcherStyleConfig & {
  236. ensure_archer_styles_registered();
  237. auto &styles = style_registry();
  238. std::string nation_id;
  239. if (ctx.entity != nullptr) {
  240. if (auto *unit =
  241. ctx.entity->get_component<Engine::Core::UnitComponent>()) {
  242. nation_id = Game::Systems::nation_id_to_string(unit->nation_id);
  243. }
  244. }
  245. if (!nation_id.empty()) {
  246. auto it = styles.find(nation_id);
  247. if (it != styles.end()) {
  248. return it->second;
  249. }
  250. }
  251. auto fallback = styles.find(std::string(k_default_style_key));
  252. if (fallback != styles.end()) {
  253. return fallback->second;
  254. }
  255. static const ArcherStyleConfig default_style{};
  256. return default_style;
  257. }
  258. public:
  259. auto resolve_shader_key(const DrawContext &ctx) const -> QString {
  260. const ArcherStyleConfig &style = resolve_style(ctx);
  261. if (!style.shader_id.empty()) {
  262. return QString::fromStdString(style.shader_id);
  263. }
  264. return QStringLiteral("archer");
  265. }
  266. private:
  267. void apply_palette_overrides(const ArcherStyleConfig &style,
  268. const QVector3D &team_tint,
  269. HumanoidVariant &variant) const {
  270. auto apply_color = [&](const std::optional<QVector3D> &override_color,
  271. QVector3D &target) {
  272. target = mix_palette_color(target, override_color, team_tint,
  273. k_team_mix_weight, k_style_mix_weight);
  274. };
  275. apply_color(style.cloth_color, variant.palette.cloth);
  276. apply_color(style.leather_color, variant.palette.leather);
  277. apply_color(style.leather_dark_color, variant.palette.leather_dark);
  278. apply_color(style.metal_color, variant.palette.metal);
  279. apply_color(style.wood_color, variant.palette.wood);
  280. }
  281. };
  282. void register_archer_renderer(Render::GL::EntityRendererRegistry &registry) {
  283. ensure_archer_styles_registered();
  284. static ArcherRenderer const renderer;
  285. registry.register_renderer(
  286. "troops/roman/archer", [](const DrawContext &ctx, ISubmitter &out) {
  287. static ArcherRenderer const static_renderer;
  288. Shader *archer_shader = nullptr;
  289. if (ctx.backend != nullptr) {
  290. QString shader_key = static_renderer.resolve_shader_key(ctx);
  291. archer_shader = ctx.backend->shader(shader_key);
  292. if (archer_shader == nullptr) {
  293. archer_shader = ctx.backend->shader(QStringLiteral("archer"));
  294. }
  295. }
  296. auto *scene_renderer = dynamic_cast<Renderer *>(&out);
  297. if ((scene_renderer != nullptr) && (archer_shader != nullptr)) {
  298. scene_renderer->set_current_shader(archer_shader);
  299. }
  300. static_renderer.render(ctx, out);
  301. if (scene_renderer != nullptr) {
  302. scene_renderer->set_current_shader(nullptr);
  303. }
  304. });
  305. }
  306. } // namespace Render::GL::Roman