Browse Source

Consolidate duplicate utility functions into shared modules

Co-authored-by: djeada <[email protected]>
copilot-swe-agent[bot] 1 month ago
parent
commit
15c3971b37

+ 1 - 2
render/entity/archer_renderer.cpp

@@ -17,6 +17,7 @@
 #include "../scene_renderer.h"
 #include "../submitter.h"
 #include "registry.h"
+#include "renderer_constants.h"
 
 #include <QMatrix4x4>
 #include <QString>
@@ -34,8 +35,6 @@ using Render::Geom::coneFromTo;
 using Render::Geom::cylinderBetween;
 using Render::Geom::sphereAt;
 
-static constexpr std::size_t MAX_EXTRAS_CACHE_SIZE = 10000;
-
 struct ArcherExtras {
   QVector3D stringCol;
   QVector3D fletch;

+ 1 - 4
render/entity/barracks_renderer.cpp

@@ -17,6 +17,7 @@ namespace {
 using Render::Geom::clamp01;
 using Render::Geom::clampVec01;
 using Render::Geom::cylinderBetween;
+using Render::Geom::lerp;
 using Render::Geom::sphereAt;
 
 struct BuildingProportions {
@@ -73,10 +74,6 @@ static inline BarracksPalette makePalette(const QVector3D &team) {
   return p;
 }
 
-static inline QVector3D lerp(const QVector3D &a, const QVector3D &b, float t) {
-  return a * (1.0f - t) + b * t;
-}
-
 static inline void drawCylinder(ISubmitter &out, const QMatrix4x4 &model,
                                 const QVector3D &a, const QVector3D &b,
                                 float radius, const QVector3D &color,

+ 5 - 24
render/entity/knight_renderer.cpp

@@ -17,6 +17,7 @@
 #include "../scene_renderer.h"
 #include "../submitter.h"
 #include "registry.h"
+#include "renderer_constants.h"
 #include <unordered_map>
 
 #include <QMatrix4x4>
@@ -32,32 +33,12 @@ using Render::Geom::clamp01;
 using Render::Geom::clampf;
 using Render::Geom::coneFromTo;
 using Render::Geom::cylinderBetween;
+using Render::Geom::easeInOutCubic;
+using Render::Geom::lerp;
+using Render::Geom::nlerp;
+using Render::Geom::smoothstep;
 using Render::Geom::sphereAt;
 
-static constexpr std::size_t MAX_EXTRAS_CACHE_SIZE = 10000;
-
-static inline float easeInOutCubic(float t) {
-  t = clamp01(t);
-  return t < 0.5f ? 4.0f * t * t * t
-                  : 1.0f - std::pow(-2.0f * t + 2.0f, 3.0f) / 2.0f;
-}
-
-static inline float smoothstep(float a, float b, float x) {
-  x = clamp01((x - a) / (b - a));
-  return x * x * (3.0f - 2.0f * x);
-}
-
-static inline float lerp(float a, float b, float t) {
-  return a * (1.0f - t) + b * t;
-}
-
-static inline QVector3D nlerp(const QVector3D &a, const QVector3D &b, float t) {
-  QVector3D v = a * (1.0f - t) + b * t;
-  if (v.lengthSquared() > 1e-6f)
-    v.normalize();
-  return v;
-}
-
 struct KnightExtras {
   QVector3D metalColor;
   QVector3D shieldColor;

+ 5 - 24
render/entity/mounted_knight_renderer.cpp

@@ -18,6 +18,7 @@
 #include "../submitter.h"
 #include "horse_renderer.h"
 #include "registry.h"
+#include "renderer_constants.h"
 #include <unordered_map>
 
 #include <QMatrix4x4>
@@ -33,32 +34,12 @@ using Render::Geom::clamp01;
 using Render::Geom::clampf;
 using Render::Geom::coneFromTo;
 using Render::Geom::cylinderBetween;
+using Render::Geom::easeInOutCubic;
+using Render::Geom::lerp;
+using Render::Geom::nlerp;
+using Render::Geom::smoothstep;
 using Render::Geom::sphereAt;
 
-static constexpr std::size_t MAX_EXTRAS_CACHE_SIZE = 10000;
-
-static inline float easeInOutCubic(float t) {
-  t = clamp01(t);
-  return t < 0.5f ? 4.0f * t * t * t
-                  : 1.0f - std::pow(-2.0f * t + 2.0f, 3.0f) / 2.0f;
-}
-
-static inline float smoothstep(float a, float b, float x) {
-  x = clamp01((x - a) / (b - a));
-  return x * x * (3.0f - 2.0f * x);
-}
-
-static inline float lerp(float a, float b, float t) {
-  return a * (1.0f - t) + b * t;
-}
-
-static inline QVector3D nlerp(const QVector3D &a, const QVector3D &b, float t) {
-  QVector3D v = a * (1.0f - t) + b * t;
-  if (v.lengthSquared() > 1e-6f)
-    v.normalize();
-  return v;
-}
-
 struct MountedKnightExtras {
   QVector3D metalColor;
   HorseProfile horseProfile;

+ 11 - 0
render/entity/renderer_constants.h

@@ -0,0 +1,11 @@
+#pragma once
+
+#include <cstddef>
+
+namespace Render::GL {
+
+// Maximum size for extras caches in entity renderers
+// This prevents unbounded memory growth in renderer caches
+static constexpr std::size_t MAX_EXTRAS_CACHE_SIZE = 10000;
+
+} // namespace Render::GL

+ 4 - 17
render/entity/spearman_renderer.cpp

@@ -17,6 +17,7 @@
 #include "../scene_renderer.h"
 #include "../submitter.h"
 #include "registry.h"
+#include "renderer_constants.h"
 
 #include <QMatrix4x4>
 #include <QString>
@@ -32,25 +33,11 @@ using Render::Geom::clamp01;
 using Render::Geom::clampf;
 using Render::Geom::coneFromTo;
 using Render::Geom::cylinderBetween;
+using Render::Geom::easeInOutCubic;
+using Render::Geom::lerp;
+using Render::Geom::smoothstep;
 using Render::Geom::sphereAt;
 
-static constexpr std::size_t MAX_EXTRAS_CACHE_SIZE = 10000;
-
-static inline float easeInOutCubic(float t) {
-  t = clamp01(t);
-  return t < 0.5f ? 4.0f * t * t * t
-                  : 1.0f - std::pow(-2.0f * t + 2.0f, 3.0f) / 2.0f;
-}
-
-static inline float smoothstep(float a, float b, float x) {
-  x = clamp01((x - a) / (b - a));
-  return x * x * (3.0f - 2.0f * x);
-}
-
-static inline float lerp(float a, float b, float t) {
-  return a * (1.0f - t) + b * t;
-}
-
 struct SpearmanExtras {
   QVector3D spearShaftColor;
   QVector3D spearheadColor;

+ 29 - 0
render/geom/math_utils.h

@@ -2,6 +2,7 @@
 
 #include <QVector3D>
 #include <algorithm>
+#include <cmath>
 
 namespace Render::Geom {
 
@@ -20,4 +21,32 @@ inline QVector3D clampVec(const QVector3D &c, float minVal, float maxVal) {
                    clampf(c.z(), minVal, maxVal));
 }
 
+// Interpolation and easing functions
+
+inline float lerp(float a, float b, float t) {
+  return a * (1.0f - t) + b * t;
+}
+
+inline QVector3D lerp(const QVector3D &a, const QVector3D &b, float t) {
+  return a * (1.0f - t) + b * t;
+}
+
+inline float easeInOutCubic(float t) {
+  t = clamp01(t);
+  return t < 0.5f ? 4.0f * t * t * t
+                  : 1.0f - std::pow(-2.0f * t + 2.0f, 3.0f) / 2.0f;
+}
+
+inline float smoothstep(float a, float b, float x) {
+  x = clamp01((x - a) / (b - a));
+  return x * x * (3.0f - 2.0f * x);
+}
+
+inline QVector3D nlerp(const QVector3D &a, const QVector3D &b, float t) {
+  QVector3D v = a * (1.0f - t) + b * t;
+  if (v.lengthSquared() > 1e-6f)
+    v.normalize();
+  return v;
+}
+
 } // namespace Render::Geom