From 7cb41956b7fec23ad281da4e27a56a3915347e6d Mon Sep 17 00:00:00 2001 From: Alexander Zubov Date: Fri, 1 May 2026 02:51:02 +0200 Subject: [PATCH] Add DrawGridEx function --- src/raylib.h | 1 + src/rmodels.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/raylib.h b/src/raylib.h index e8164073f..bac6518db 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1586,6 +1586,7 @@ RLAPI void DrawCapsuleWires(Vector3 startPos, Vector3 endPos, float radius, int RLAPI void DrawPlane(Vector3 centerPos, Vector2 size, Color color); // Draw a plane XZ RLAPI void DrawRay(Ray ray, Color color); // Draw a ray line RLAPI void DrawGrid(int slices, float spacing); // Draw a grid (centered at (0, 0, 0)) +RLAPI void DrawGridEx(int slices, float spacing, Vector3 color, Vector3 position, Vector3 normal, Vector3 tangent); // Draw a grid with extended parameters //------------------------------------------------------------------------------------ // Model 3d Loading and Drawing Functions (Module: models) diff --git a/src/rmodels.c b/src/rmodels.c index eb7f30cb8..dfb7ffb78 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -1098,6 +1098,45 @@ void DrawGrid(int slices, float spacing) rlEnd(); } +// Draw a grid with extended parameters +void DrawGridEx(int slices, float spacing, Vector3 color, Vector3 position, Vector3 normal, Vector3 tangent) +{ + int halfSlices = slices / 2; + + // Build basis vectors + Vector3 n = Vector3Normalize(normal); + Vector3 t = Vector3Normalize(tangent); + Vector3 b = Vector3Normalize(Vector3CrossProduct(n, t)); + t = Vector3CrossProduct(n, b); // Ensure normal and tangent vectors are orthogonal + + rlBegin(RL_LINES); + rlColor3f(color.x, color.y, color.z); + + for (int i = -halfSlices; i <= halfSlices; i++) + { + float f = (float)i * spacing; + + Vector3 t1 = Vector3Add(position, + Vector3Add(Vector3Scale(t, -halfSlices * spacing), + Vector3Scale(b, f))); + Vector3 t2 = Vector3Add(position, + Vector3Add(Vector3Scale(t, halfSlices * spacing), + Vector3Scale(b, f))); + Vector3 b1 = Vector3Add(position, + Vector3Add(Vector3Scale(b, -halfSlices * spacing), + Vector3Scale(t, f))); + Vector3 b2 = Vector3Add(position, + Vector3Add(Vector3Scale(b, halfSlices * spacing), + Vector3Scale(t, f))); + + rlVertex3f(t1.x, t1.y, t1.z); + rlVertex3f(t2.x, t2.y, t2.z); + rlVertex3f(b1.x, b1.y, b1.z); + rlVertex3f(b2.x, b2.y, b2.z); + } + rlEnd(); +} + // Load model from files (mesh and material) Model LoadModel(const char *fileName) {