work_apron_renderer.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "work_apron_renderer.h"
  2. #include "../../geom/math_utils.h"
  3. #include "../../geom/transforms.h"
  4. #include "../../gl/primitives.h"
  5. #include "../../humanoid/humanoid_specs.h"
  6. #include "../../submitter.h"
  7. #include <QMatrix4x4>
  8. #include <QVector3D>
  9. #include <cmath>
  10. #include <numbers>
  11. namespace Render::GL {
  12. using Render::Geom::cylinder_between;
  13. using Render::Geom::sphere_at;
  14. WorkApronRenderer::WorkApronRenderer(const WorkApronConfig &config)
  15. : m_config(config) {}
  16. void WorkApronRenderer::render(const DrawContext &ctx, const BodyFrames &frames,
  17. const HumanoidPalette &,
  18. const HumanoidAnimationContext &,
  19. ISubmitter &submitter) {
  20. renderApronBody(ctx, frames.torso, frames.waist, submitter);
  21. if (m_config.include_straps) {
  22. renderStraps(ctx, frames.torso, frames, submitter);
  23. }
  24. if (m_config.include_pockets) {
  25. renderPockets(ctx, frames.waist, submitter);
  26. }
  27. }
  28. void WorkApronRenderer::renderApronBody(const DrawContext &ctx,
  29. const AttachmentFrame &torso,
  30. const AttachmentFrame &waist,
  31. ISubmitter &submitter) {
  32. using HP = HumanProportions;
  33. if (torso.radius <= 0.0F || waist.radius <= 0.0F) {
  34. return;
  35. }
  36. const QVector3D &origin = waist.origin;
  37. const QVector3D &right = waist.right;
  38. const QVector3D &up = waist.up;
  39. const QVector3D &forward = waist.forward;
  40. float const waist_r = waist.radius * m_config.apron_width;
  41. float const waist_d =
  42. (waist.depth > 0.0F) ? waist.depth * 0.85F : waist.radius * 0.75F;
  43. float const y_top = origin.y() + 0.05F;
  44. float const y_bottom = origin.y() - m_config.apron_length;
  45. constexpr int segs = 14;
  46. constexpr float pi = std::numbers::pi_v<float>;
  47. QVector3D const apron_color = m_config.leather_color;
  48. QVector3D const apron_dark = apron_color * 0.80F;
  49. for (int ring = 0; ring < 6; ++ring) {
  50. float const t = float(ring) / 5.0F;
  51. float const y = y_top - t * (y_top - y_bottom);
  52. float const flare = 1.0F + t * 0.15F;
  53. float const w = waist_r * flare;
  54. float const d = waist_d * flare;
  55. float const thickness = 0.018F + t * 0.004F;
  56. QVector3D const color = apron_color * (1.0F - t * 0.12F);
  57. for (int i = 0; i < segs; ++i) {
  58. float const angle_start = (float(i) / segs - 0.25F) * pi;
  59. float const angle_end = (float(i + 1) / segs - 0.25F) * pi;
  60. if (angle_start < -pi * 0.5F || angle_start > pi * 0.5F) {
  61. continue;
  62. }
  63. QVector3D const p1 = origin + right * (w * std::sin(angle_start)) +
  64. forward * (d * std::cos(angle_start)) +
  65. up * (y - origin.y());
  66. QVector3D const p2 = origin + right * (w * std::sin(angle_end)) +
  67. forward * (d * std::cos(angle_end)) +
  68. up * (y - origin.y());
  69. submitter.mesh(get_unit_cylinder(),
  70. cylinder_between(ctx.model, p1, p2, thickness), color,
  71. nullptr, 1.0F);
  72. }
  73. }
  74. for (int edge = 0; edge < 2; ++edge) {
  75. float const side_angle = (edge == 0) ? -pi * 0.25F : pi * 0.25F;
  76. for (int i = 0; i < 6; ++i) {
  77. float const t = float(i) / 5.0F;
  78. float const y = y_top - t * (y_top - y_bottom);
  79. float const flare = 1.0F + t * 0.15F;
  80. float const w = waist_r * flare;
  81. float const d = waist_d * flare;
  82. QVector3D const pos = origin + right * (w * std::sin(side_angle)) +
  83. forward * (d * std::cos(side_angle)) +
  84. up * (y - origin.y());
  85. submitter.mesh(get_unit_sphere(), sphere_at(ctx.model, pos, 0.020F),
  86. apron_dark, nullptr, 1.0F);
  87. }
  88. }
  89. }
  90. void WorkApronRenderer::renderStraps(const DrawContext &ctx,
  91. const AttachmentFrame &torso,
  92. const BodyFrames &frames,
  93. ISubmitter &submitter) {
  94. if (torso.radius <= 0.0F) {
  95. return;
  96. }
  97. QVector3D const strap_color = m_config.strap_color;
  98. QVector3D const shoulder_l = frames.shoulder_l.origin;
  99. QVector3D const shoulder_r = frames.shoulder_r.origin;
  100. QVector3D const torso_front =
  101. torso.origin + torso.forward * torso.radius * 0.80F;
  102. QVector3D const torso_back =
  103. torso.origin - torso.forward * torso.radius * 0.65F;
  104. QVector3D const chest_l =
  105. torso_front + torso.right * torso.radius * 0.30F + torso.up * 0.08F;
  106. QVector3D const chest_r =
  107. torso_front - torso.right * torso.radius * 0.30F + torso.up * 0.08F;
  108. QVector3D const back_l =
  109. torso_back + torso.right * torso.radius * 0.20F - torso.up * 0.02F;
  110. QVector3D const back_r =
  111. torso_back - torso.right * torso.radius * 0.20F - torso.up * 0.02F;
  112. submitter.mesh(get_unit_cylinder(),
  113. cylinder_between(ctx.model, chest_l, back_l, 0.020F),
  114. strap_color, nullptr, 1.0F);
  115. submitter.mesh(get_unit_cylinder(),
  116. cylinder_between(ctx.model, chest_r, back_r, 0.020F),
  117. strap_color, nullptr, 1.0F);
  118. submitter.mesh(get_unit_cylinder(),
  119. cylinder_between(ctx.model, back_l, back_r, 0.018F),
  120. strap_color, nullptr, 1.0F);
  121. }
  122. void WorkApronRenderer::renderPockets(const DrawContext &ctx,
  123. const AttachmentFrame &waist,
  124. ISubmitter &submitter) {
  125. if (waist.radius <= 0.0F) {
  126. return;
  127. }
  128. QVector3D const pocket_color = m_config.leather_color * 0.88F;
  129. constexpr float pi = std::numbers::pi_v<float>;
  130. for (int side = -1; side <= 1; side += 2) {
  131. float const pocket_angle = float(side) * 0.12F * pi;
  132. float const pocket_x = waist.radius * 0.55F * std::sin(pocket_angle);
  133. float const pocket_z = waist.radius * 0.45F * std::cos(pocket_angle);
  134. QVector3D const pocket_center = waist.origin + waist.right * pocket_x +
  135. waist.forward * pocket_z - waist.up * 0.12F;
  136. for (int i = 0; i < 3; ++i) {
  137. for (int j = 0; j < 3; ++j) {
  138. float const x_off = (float(i) - 1.0F) * 0.018F;
  139. float const y_off = (float(j) - 1.0F) * 0.022F;
  140. QVector3D const pos = pocket_center +
  141. waist.right * x_off * float(side) +
  142. waist.up * y_off;
  143. float const r = 0.012F - float(i + j) * 0.0005F;
  144. submitter.mesh(get_unit_sphere(), sphere_at(ctx.model, pos, r),
  145. pocket_color, nullptr, 1.0F);
  146. }
  147. }
  148. }
  149. }
  150. } // namespace Render::GL