elephant_renderer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "elephant_renderer.h"
  2. #include "../../../../game/core/component.h"
  3. #include "../../../../game/visuals/team_colors.h"
  4. #include "../../../elephant/rig.h"
  5. #include "../../../geom/math_utils.h"
  6. #include "../../../geom/transforms.h"
  7. #include "../../../gl/humanoid/animation/animation_inputs.h"
  8. #include "../../../gl/humanoid/humanoid_types.h"
  9. #include "../../../gl/primitives.h"
  10. #include "../../../gl/resources.h"
  11. #include "../../../scene_renderer.h"
  12. #include "../../../submitter.h"
  13. #include "../../registry.h"
  14. #include <QMatrix4x4>
  15. #include <QVector3D>
  16. #include <cmath>
  17. namespace Render::GL::Carthage {
  18. namespace {
  19. using Render::Geom::clamp01;
  20. using Render::Geom::clamp_vec_01;
  21. using Render::Geom::cylinder_between;
  22. struct CarthageElephantPalette {
  23. QVector3D fabric_purple{0.45F, 0.18F, 0.55F};
  24. QVector3D fabric_gold{0.85F, 0.70F, 0.35F};
  25. QVector3D metal_bronze{0.70F, 0.50F, 0.28F};
  26. QVector3D metal_gold{0.85F, 0.72F, 0.40F};
  27. QVector3D wood_cedar{0.52F, 0.35F, 0.22F};
  28. QVector3D wood_dark{0.38F, 0.25F, 0.15F};
  29. QVector3D leather{0.48F, 0.35F, 0.22F};
  30. QVector3D rope{0.58F, 0.50F, 0.38F};
  31. QVector3D team{0.8F, 0.9F, 1.0F};
  32. };
  33. inline auto make_palette(const QVector3D &team) -> CarthageElephantPalette {
  34. CarthageElephantPalette p;
  35. p.team = clamp_vec_01(team);
  36. return p;
  37. }
  38. inline void draw_box(ISubmitter &out, Mesh *unit, Texture *white,
  39. const QMatrix4x4 &model, const QVector3D &pos,
  40. const QVector3D &size, const QVector3D &color) {
  41. QMatrix4x4 m = model;
  42. m.translate(pos);
  43. m.scale(size);
  44. out.mesh(unit, m, color, white, 1.0F);
  45. }
  46. inline void draw_cyl(ISubmitter &out, const QMatrix4x4 &model,
  47. const QVector3D &a, const QVector3D &b, float r,
  48. const QVector3D &color, Texture *white) {
  49. out.mesh(get_unit_cylinder(), model * cylinder_between(a, b, r), color, white,
  50. 1.0F);
  51. }
  52. class CarthageElephantRenderer : public ElephantRendererBase {
  53. public:
  54. CarthageElephantRenderer() = default;
  55. protected:
  56. };
  57. } // namespace
  58. void register_elephant_renderer(EntityRendererRegistry &registry) {
  59. registry.register_renderer(
  60. "troops/carthage/elephant", [](const DrawContext &p, ISubmitter &out) {
  61. static CarthageElephantRenderer const static_renderer;
  62. if (p.entity == nullptr) {
  63. return;
  64. }
  65. QVector3D team_color{0.4F, 0.2F, 0.6F};
  66. if (auto *r =
  67. p.entity->get_component<Engine::Core::RenderableComponent>()) {
  68. team_color = QVector3D(r->color[0], r->color[1], r->color[2]);
  69. }
  70. uint32_t seed = 0U;
  71. seed = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(p.entity) &
  72. 0xFFFFFFFFU);
  73. QVector3D const fabric_base(0.45F, 0.18F, 0.55F);
  74. QVector3D const metal_base(0.70F, 0.50F, 0.28F);
  75. ElephantProfile profile = get_or_create_cached_elephant_profile(
  76. seed, fabric_base, metal_base);
  77. AnimationInputs anim = sample_anim_state(p);
  78. if (p.entity != nullptr) {
  79. if (auto *combat_state =
  80. p.entity
  81. ->get_component<Engine::Core::CombatStateComponent>()) {
  82. if (combat_state->animation_state !=
  83. Engine::Core::CombatAnimationState::Idle) {
  84. anim.is_attacking = true;
  85. anim.is_melee = true;
  86. }
  87. }
  88. if (auto *attack =
  89. p.entity->get_component<Engine::Core::AttackComponent>()) {
  90. if (attack->in_melee_lock) {
  91. anim.is_attacking = true;
  92. anim.is_melee = true;
  93. }
  94. }
  95. }
  96. static_renderer.render(p, anim, profile, nullptr, nullptr, out);
  97. });
  98. }
  99. } // namespace Render::GL::Carthage