From 27af359d1c8908d91abe32ed9986d9b0c3a97e54 Mon Sep 17 00:00:00 2001 From: Destructor17 <60874647+Destructor17@users.noreply.github.com> Date: Tue, 25 Feb 2025 14:39:00 +0300 Subject: [PATCH] [rlgl] Preserve texture on mode switching (#4364) * Fix textures on draw mode switch * formatting fix * ident --- examples/textures/textures_polygon.c | 6 +----- src/rlgl.h | 4 +++- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/examples/textures/textures_polygon.c b/examples/textures/textures_polygon.c index 759f2b420..729e25184 100644 --- a/examples/textures/textures_polygon.c +++ b/examples/textures/textures_polygon.c @@ -117,8 +117,7 @@ void DrawTexturePoly(Texture2D texture, Vector2 center, Vector2 *points, Vector2 { rlSetTexture(texture.id); - // Texturing is only supported on RL_QUADS - rlBegin(RL_QUADS); + rlBegin(RL_TRIANGLES); rlColor4ub(tint.r, tint.g, tint.b, tint.a); @@ -132,9 +131,6 @@ void DrawTexturePoly(Texture2D texture, Vector2 center, Vector2 *points, Vector2 rlTexCoord2f(texcoords[i + 1].x, texcoords[i + 1].y); rlVertex2f(points[i + 1].x + center.x, points[i + 1].y + center.y); - - rlTexCoord2f(texcoords[i + 1].x, texcoords[i + 1].y); - rlVertex2f(points[i + 1].x + center.x, points[i + 1].y + center.y); } rlEnd(); diff --git a/src/rlgl.h b/src/rlgl.h index c428f1887..24eb63996 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -1459,6 +1459,7 @@ void rlBegin(int mode) // NOTE: In all three cases, vertex are accumulated over default internal vertex buffer if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode != mode) { + int currentTexture = RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId; if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount > 0) { // Make sure current RLGL.currentBatch->draws[i].vertexCount is aligned a multiple of 4, @@ -1481,13 +1482,14 @@ void rlBegin(int mode) RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode = mode; RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount = 0; - RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId = RLGL.State.defaultTextureId; + RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId = currentTexture; } } // Finish vertex providing void rlEnd(void) { + rlSetTexture(RLGL.State.defaultTextureId); // NOTE: Depth increment is dependant on rlOrtho(): z-near and z-far values, // as well as depth buffer bit-depth (16bit or 24bit or 32bit) // Correct increment formula would be: depthInc = (zfar - znear)/pow(2, bits)