From 470df18aa2e8f9dfb982995775424286df3c9c56 Mon Sep 17 00:00:00 2001 From: George Ibrahim Aziz <103423847+georgeibrahim1@users.noreply.github.com> Date: Tue, 10 Mar 2026 01:44:11 +0200 Subject: [PATCH 1/8] feat: implement beginShape kind parameter supporting POINTS, LINES, TRIANGLES, TRIANGLE_STRIP, TRIANGLE_FAN with fill and stroke --- L5.lua | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 124 insertions(+), 1 deletion(-) diff --git a/L5.lua b/L5.lua index 66b0d30..b59ddc6 100644 --- a/L5.lua +++ b/L5.lua @@ -824,6 +824,15 @@ function defaults() SIZEALL = "sizeall" NO = "no" HAND = "hand" + -- beginShape kinds + POINTS = "points" + LINES = "lines" + TRIANGLES = "triangles" + TRIANGLE_FAN = "fan" + TRIANGLE_STRIP = "strip" + QUADS = "quads" + QUAD_STRIP = "quad_strip" + TESS = "tess" -- global user vars - can be read by user but shouldn't be altered by user key = "" --default, overriden with key presses detected in love.update(dt) @@ -893,6 +902,7 @@ function define_env_globals() L5_env.pixelsLoaded = false -- custom shape drawing L5_env.vertices = {} + L5_env.kind = nil -- custom texture mesh L5_env.currentTexture = nil L5_env.useTexture = false @@ -2301,10 +2311,11 @@ function textureWrap(_mode) end end -function beginShape() +function beginShape(_kind) -- reset custom shape vertices table L5_env.vertices = {} L5_env.useTexture = false + L5_env.kind = _kind end function vertex(_x, _y, _u, _v) @@ -2328,6 +2339,118 @@ function endShape(_close) -- no vertices, early exit if #L5_env.vertices == 0 then return end + -- draw points + if L5_env.kind == POINTS then + local r, g, b, a = love.graphics.getColor() + love.graphics.setColor(unpack(L5_env.stroke_color)) + for i = 1 , #L5_env.vertices , 2 do + love.graphics.points(L5_env.vertices[i] , L5_env.vertices[i+1]) + end + love.graphics.setColor(r, g, b, a) + return + end + + -- draw unconnected lines + if L5_env.kind == LINES then + local r, g, b, a = love.graphics.getColor() + love.graphics.setColor(unpack(L5_env.stroke_color)) + for i = 1, #L5_env.vertices - 2, 4 do + love.graphics.line( + L5_env.vertices[i], L5_env.vertices[i+1], L5_env.vertices[i+2], L5_env.vertices[i+3] + ) + end + love.graphics.setColor(r, g, b, a) + return + end + + -- draw seperated triangles + if L5_env.kind == TRIANGLES then + local verts = L5_env.vertices + if type(verts[1]) == "number" then + local converted = {} + for i = 1 , #verts , 2 do + converted[#converted+1] = {verts[i] , verts[i+1]} + end + verts = converted + end + + if L5_env.fill_mode == "fill" then + local mesh = love.graphics.newMesh(verts, TRIANGLES) + love.graphics.draw(mesh) + end + + local r, g, b, a = love.graphics.getColor() + love.graphics.setColor(unpack(L5_env.stroke_color)) + for i = 1, #verts, 3 do + local v1, v2, v3 = verts[i], verts[i+1], verts[i+2] + if(v1 == nil or v2 == nil or v3 == nil) then break end + love.graphics.line(v1[1],v1[2], v2[1],v2[2]) + love.graphics.line(v2[1],v2[2], v3[1],v3[2]) + love.graphics.line(v3[1],v3[2], v1[1],v1[2]) + end + love.graphics.setColor(r, g, b, a) + return + end + + + -- draw triangle strip + if L5_env.kind == TRIANGLE_STRIP then + local verts = L5_env.vertices + if type(verts[1]) == "number" then + local converted = {} + for i = 1 , #verts , 2 do + converted[#converted+1] = {verts[i] , verts[i+1]} + end + verts = converted + end + + if L5_env.fill_mode == "fill" then + local mesh = love.graphics.newMesh(verts, TRIANGLE_STRIP) + love.graphics.draw(mesh) + end + + local r, g, b, a = love.graphics.getColor() + love.graphics.setColor(unpack(L5_env.stroke_color)) + for i = 1, #verts-2, 1 do + local v1, v2, v3 = verts[i], verts[i+1], verts[i+2] + if(v1 == nil or v2 == nil or v3 == nil) then break end + love.graphics.line(v1[1],v1[2], v2[1],v2[2]) + love.graphics.line(v2[1],v2[2], v3[1],v3[2]) + love.graphics.line(v3[1],v3[2], v1[1],v1[2]) + end + love.graphics.setColor(r, g, b, a) + return + end + + -- draw triangles centered around the first vertex + if L5_env.kind == TRIANGLE_FAN then + local verts = L5_env.vertices + if type(verts[1]) == "number" then + local converted = {} + for i = 1 , #verts , 2 do + converted[#converted+1] = {verts[i] , verts[i+1]} + end + verts = converted + end + + if L5_env.fill_mode == "fill" then + local mesh = love.graphics.newMesh(verts, TRIANGLE_FAN) + love.graphics.draw(mesh) + end + + local r, g, b, a = love.graphics.getColor() + love.graphics.setColor(unpack(L5_env.stroke_color)) + for i = 2, #verts-1, 1 do + local v1, v2, v3 = verts[1], verts[i], verts[i+1] + if(v1 == nil or v2 == nil or v3 == nil) then break end + love.graphics.line(v1[1],v1[2], v2[1],v2[2]) + love.graphics.line(v2[1],v2[2], v3[1],v3[2]) + love.graphics.line(v3[1],v3[2], v1[1],v1[2]) + end + love.graphics.setColor(r, g, b, a) + return + end + -- if texture() triangulate fan mesh - convex assumed if L5_env.useTexture and L5_env.currentTexture then local mesh = love.graphics.newMesh(L5_env.vertices, "fan") From 780658a6f28127e9ff8870901f25b40e7886a054 Mon Sep 17 00:00:00 2001 From: George Ibrahim Aziz <103423847+georgeibrahim1@users.noreply.github.com> Date: Tue, 10 Mar 2026 01:44:40 +0200 Subject: [PATCH 2/8] feat: add examples for beginShape with LINES, POINTS, TRIANGLE_FAN, TRIANGLE_STRIP, and TRIANGLES --- examples/beginShapeLines.lua | 14 ++++++++++++++ examples/beginShapePoints.lua | 14 ++++++++++++++ examples/beginShapeTriangleFan.lua | 24 ++++++++++++++++++++++++ examples/beginShapeTriangleStrip.lua | 23 +++++++++++++++++++++++ examples/beginShapeTriangles.lua | 23 +++++++++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 examples/beginShapeLines.lua create mode 100644 examples/beginShapePoints.lua create mode 100644 examples/beginShapeTriangleFan.lua create mode 100644 examples/beginShapeTriangleStrip.lua create mode 100644 examples/beginShapeTriangles.lua diff --git a/examples/beginShapeLines.lua b/examples/beginShapeLines.lua new file mode 100644 index 0000000..0a54493 --- /dev/null +++ b/examples/beginShapeLines.lua @@ -0,0 +1,14 @@ +require("L5") + +function setup() + size(400, 400) + windowTitle("beginShape(LINES) example") + + fill(0) + beginShape(LINES) + for i=0,10 do + vertex(random(width),random(height)) + end + endShape() + describe("custom shape with beginShape(LINES) function, vertices and endShape()") +end \ No newline at end of file diff --git a/examples/beginShapePoints.lua b/examples/beginShapePoints.lua new file mode 100644 index 0000000..249d138 --- /dev/null +++ b/examples/beginShapePoints.lua @@ -0,0 +1,14 @@ +require("L5") + +function setup() + size(400, 400) + windowTitle("beginShape(POINTS) example") + + fill(0) + beginShape() + for i=0,10 do + vertex(random(width),random(height)) + end + endShape() + describe("custom shape with beginShape(POINTS) function, vertices and endShape()") +end \ No newline at end of file diff --git a/examples/beginShapeTriangleFan.lua b/examples/beginShapeTriangleFan.lua new file mode 100644 index 0000000..7d84bec --- /dev/null +++ b/examples/beginShapeTriangleFan.lua @@ -0,0 +1,24 @@ +require("L5") + +function setup() + size(400, 400) + windowTitle("beginShape(TRIANGLE_FAN) example") + + fill(150, 200, 255) + stroke(51) + strokeWeight(5) + + beginShape(TRIANGLE_FAN) + vertex(200, 200) -- center + vertex(200, 50) -- top + vertex(320, 110) + vertex(370, 250) + vertex(280, 370) + vertex(120, 370) + vertex(30, 250) + vertex(80, 110) + vertex(200, 50) + endShape() + + describe("octagon drawn with beginShape(TRIANGLE_FAN) from a center point, with stroke") +end diff --git a/examples/beginShapeTriangleStrip.lua b/examples/beginShapeTriangleStrip.lua new file mode 100644 index 0000000..a8d7c45 --- /dev/null +++ b/examples/beginShapeTriangleStrip.lua @@ -0,0 +1,23 @@ +require("L5") + +function setup() + size(400, 400) + windowTitle("beginShape(TRIANGLE_STRIP) example") + + fill(255, 180, 100) + stroke(51) + strokeWeight(2) + + beginShape(TRIANGLE_STRIP) + vertex(50, 100) + vertex(50, 300) + vertex(150, 50) + vertex(150, 300) + vertex(250, 100) + vertex(250, 300) + vertex(350, 50) + vertex(350, 300) + endShape() + + describe("ribbon of triangles drawn with beginShape(TRIANGLE_STRIP), with stroke") +end diff --git a/examples/beginShapeTriangles.lua b/examples/beginShapeTriangles.lua new file mode 100644 index 0000000..fbb4d67 --- /dev/null +++ b/examples/beginShapeTriangles.lua @@ -0,0 +1,23 @@ +require("L5") + +function setup() + size(400, 400) + windowTitle("beginShape(TRIANGLES) example") + + fill(0) + beginShape(TRIANGLES); + + -- Left triangle + vertex(30, 75); + vertex(40, 20); + vertex(50, 75); + + -- Right triangle + vertex(60, 20); + vertex(70, 75); + vertex(80, 20); + + endShape(); + + describe("custom shape with beginShape(TRIANGLES) function, vertices and endShape()") +end \ No newline at end of file From 1a5fd915390a97f86df8b4e4d8328a7a671cf1ca Mon Sep 17 00:00:00 2001 From: George Ibrahim Aziz <103423847+georgeibrahim1@users.noreply.github.com> Date: Tue, 10 Mar 2026 02:10:53 +0200 Subject: [PATCH 3/8] fix: specify POINTS kind in beginShape for example --- examples/beginShapePoints.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/beginShapePoints.lua b/examples/beginShapePoints.lua index 249d138..4810cb9 100644 --- a/examples/beginShapePoints.lua +++ b/examples/beginShapePoints.lua @@ -5,7 +5,7 @@ function setup() windowTitle("beginShape(POINTS) example") fill(0) - beginShape() + beginShape(POINTS) for i=0,10 do vertex(random(width),random(height)) end From 2cf45877035f91db5057da9904acae99e91c0cc2 Mon Sep 17 00:00:00 2001 From: George Ibrahim Aziz <103423847+georgeibrahim1@users.noreply.github.com> Date: Tue, 10 Mar 2026 02:36:20 +0200 Subject: [PATCH 4/8] feat: add texture support for TRIANGLES, TRIANGLE_STRIP, and TRIANGLE_FAN in endShape function --- L5.lua | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/L5.lua b/L5.lua index b59ddc6..599f290 100644 --- a/L5.lua +++ b/L5.lua @@ -2365,6 +2365,8 @@ function endShape(_close) -- draw seperated triangles if L5_env.kind == TRIANGLES then + + local verts = L5_env.vertices if type(verts[1]) == "number" then local converted = {} @@ -2373,6 +2375,15 @@ function endShape(_close) end verts = converted end + + if L5_env.useTexture and L5_env.currentTexture then + local mesh = love.graphics.newMesh(verts, TRIANGLES) + mesh:setTexture(L5_env.currentTexture) + L5_env.currentTexture:setWrap(L5_env.textureWrap, L5_env.textureWrap) + love.graphics.draw(mesh) + return + end + if L5_env.fill_mode == "fill" then local mesh = love.graphics.newMesh(verts, TRIANGLES) @@ -2404,6 +2415,14 @@ function endShape(_close) verts = converted end + if L5_env.useTexture and L5_env.currentTexture then + local mesh = love.graphics.newMesh(verts, TRIANGLE_STRIP) + mesh:setTexture(L5_env.currentTexture) + L5_env.currentTexture:setWrap(L5_env.textureWrap, L5_env.textureWrap) + love.graphics.draw(mesh) + return + end + if L5_env.fill_mode == "fill" then local mesh = love.graphics.newMesh(verts, TRIANGLE_STRIP) love.graphics.draw(mesh) @@ -2424,6 +2443,7 @@ function endShape(_close) -- draw triangles centered around the first vertex if L5_env.kind == TRIANGLE_FAN then + local verts = L5_env.vertices if type(verts[1]) == "number" then local converted = {} @@ -2432,7 +2452,14 @@ function endShape(_close) end verts = converted end - + + if L5_env.useTexture and L5_env.currentTexture then + local mesh = love.graphics.newMesh(verts, TRIANGLE_FAN) + mesh:setTexture(L5_env.currentTexture) + L5_env.currentTexture:setWrap(L5_env.textureWrap, L5_env.textureWrap) + love.graphics.draw(mesh) + return + end if L5_env.fill_mode == "fill" then local mesh = love.graphics.newMesh(verts, TRIANGLE_FAN) love.graphics.draw(mesh) From b41c0516c97af2e98043a2613ccdda4e2183de6c Mon Sep 17 00:00:00 2001 From: George Ibrahim Aziz <103423847+georgeibrahim1@users.noreply.github.com> Date: Tue, 10 Mar 2026 02:36:49 +0200 Subject: [PATCH 5/8] feat: add example for wrapping a mesh texture in beginShape(TRIANGELS) --- examples/beginShapeTrianglsWithTexture.lua | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 examples/beginShapeTrianglsWithTexture.lua diff --git a/examples/beginShapeTrianglsWithTexture.lua b/examples/beginShapeTrianglsWithTexture.lua new file mode 100644 index 0000000..a5883ac --- /dev/null +++ b/examples/beginShapeTrianglsWithTexture.lua @@ -0,0 +1,23 @@ +require("L5") + +function setup() + size(400, 400) + windowTitle("texture() example") + textureMode(NORMAL) + + cat = loadImage("examples/assets/cat.png") + + beginShape(TRIANGLES) + texture(cat) + vertex(50, 100, 0, 0.2) + vertex(50, 300, 0, 1) + vertex(150, 50, 0.33, 0) + vertex(150, 300, 0.33, 1) + vertex(250, 100, 0.67, 0.2) + vertex(250, 300, 0.67, 1) + vertex(350, 50, 1, 0) + vertex(350, 300, 1, 1) + endShape() + + describe("Wrapping a mesh texture around a custom shape with coordinates specified with u,v") +end \ No newline at end of file From e626eefead692c70b4b0363c2cfbd8ad7a5f200b Mon Sep 17 00:00:00 2001 From: George Ibrahim Aziz <103423847+georgeibrahim1@users.noreply.github.com> Date: Tue, 10 Mar 2026 02:43:02 +0200 Subject: [PATCH 6/8] TODO: add QUADS, QUAD_STRIP, TESS support for kind parameter in beginShape --- TODO.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.txt b/TODO.txt index 3947983..bb72caf 100644 --- a/TODO.txt +++ b/TODO.txt @@ -57,7 +57,7 @@ * [X] ellipseMode doesn't seem to work? * [X] imageMode(): add CORNERS mode * [X] add custom shape creation via createShape() (processing) / beginShape() (p5.js), vertex() and endShape() (using love2d polygon - pass vertices as table?) -* [ ] improve beginShape to get feature parity with p5-processing with `kind`? at the very least, basic shape drawing should match basic examples on reference page. +* [ ] Add QUADS, QUAD_STRIP, TESS support for kind parameter in beginShape ## Filter From 1d654ddfda58bc013fcbac578e86ab70be770d90 Mon Sep 17 00:00:00 2001 From: George Ibrahim Aziz <103423847+georgeibrahim1@users.noreply.github.com> Date: Thu, 12 Mar 2026 23:37:09 +0200 Subject: [PATCH 7/8] feat: add error validation for beginShape --- L5.lua | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/L5.lua b/L5.lua index 599f290..ed24bd2 100644 --- a/L5.lua +++ b/L5.lua @@ -830,9 +830,6 @@ function defaults() TRIANGLES = "triangles" TRIANGLE_FAN = "fan" TRIANGLE_STRIP = "strip" - QUADS = "quads" - QUAD_STRIP = "quad_strip" - TESS = "tess" -- global user vars - can be read by user but shouldn't be altered by user key = "" --default, overriden with key presses detected in love.update(dt) @@ -903,6 +900,11 @@ function define_env_globals() -- custom shape drawing L5_env.vertices = {} L5_env.kind = nil + L5_env.shapeKinds = {[POINTS] = true, [LINES] = true, [TRIANGLES]=true, [TRIANGLE_FAN]=true, [TRIANGLE_STRIP]=true} + L5_env.mesh = love.graphics.newMesh( + {{"VertexPosition", "float", 2}}, + 4096, "triangles", "dynamic" + ) -- reusable mesh for non-texture shapes -- custom texture mesh L5_env.currentTexture = nil L5_env.useTexture = false @@ -2311,7 +2313,23 @@ function textureWrap(_mode) end end -function beginShape(_kind) +function beginShape(...) + + local n = select('#' , ...) + if(n > 1) then + error("beginShape(kind) accepts at most one argument", 2) + end + + local _kind = select(1, ...) + + if n == 0 then + _kind = nil + elseif _kind == nil then + error("This kind is not defined (undefined variable passed)") + elseif not L5_env.shapeKinds[_kind] then -- if any other type is passed + error("Invalid kind: " .. tostring(_kind)) + end + -- reset custom shape vertices table L5_env.vertices = {} L5_env.useTexture = false @@ -2335,6 +2353,18 @@ function vertex(_x, _y, _u, _v) end end +-- helper function to convert flat {x,y,x,y...} to {{x,y},{x,y}...} +function toVertTable(verts) + if type(verts[1]) == "number" then + local converted = {} + for i = 1, #verts, 2 do + converted[#converted+1] = {verts[i], verts[i+1]} + end + return converted + end + return verts +end + function endShape(_close) -- no vertices, early exit if #L5_env.vertices == 0 then return end From 605e33a6dc1ced42e6a975237922377092060708 Mon Sep 17 00:00:00 2001 From: George Ibrahim Aziz <103423847+georgeibrahim1@users.noreply.github.com> Date: Thu, 12 Mar 2026 23:49:50 +0200 Subject: [PATCH 8/8] feat: optimize drawing non-texture shapes in beginShape --- L5.lua | 251 +++++++++++++++++++++++++-------------------------------- 1 file changed, 109 insertions(+), 142 deletions(-) diff --git a/L5.lua b/L5.lua index ed24bd2..e3c4973 100644 --- a/L5.lua +++ b/L5.lua @@ -2353,198 +2353,165 @@ function vertex(_x, _y, _u, _v) end end --- helper function to convert flat {x,y,x,y...} to {{x,y},{x,y}...} -function toVertTable(verts) - if type(verts[1]) == "number" then - local converted = {} - for i = 1, #verts, 2 do - converted[#converted+1] = {verts[i], verts[i+1]} - end - return converted - end - return verts -end - function endShape(_close) -- no vertices, early exit if #L5_env.vertices == 0 then return end + -- helper function to convert flat {x,y,x,y...} to {{x,y},{x,y}...} + local function toVertTable(verts) + if type(verts[1]) == "number" then + local converted = {} + for i = 1, #verts, 2 do + converted[#converted+1] = {verts[i], verts[i+1]} + end + return converted + end + return verts + end + -- draw points if L5_env.kind == POINTS then local r, g, b, a = love.graphics.getColor() love.graphics.setColor(unpack(L5_env.stroke_color)) - for i = 1 , #L5_env.vertices , 2 do - love.graphics.points(L5_env.vertices[i] , L5_env.vertices[i+1]) + for i = 1, #L5_env.vertices, 2 do + love.graphics.points(L5_env.vertices[i], L5_env.vertices[i+1]) end love.graphics.setColor(r, g, b, a) - return - end -- draw unconnected lines - if L5_env.kind == LINES then + elseif L5_env.kind == LINES then local r, g, b, a = love.graphics.getColor() love.graphics.setColor(unpack(L5_env.stroke_color)) for i = 1, #L5_env.vertices - 2, 4 do love.graphics.line( - L5_env.vertices[i], L5_env.vertices[i+1], L5_env.vertices[i+2], L5_env.vertices[i+3] + L5_env.vertices[i], L5_env.vertices[i+1], + L5_env.vertices[i+2], L5_env.vertices[i+3] ) end love.graphics.setColor(r, g, b, a) - return - end - - -- draw seperated triangles - if L5_env.kind == TRIANGLES then - - local verts = L5_env.vertices - if type(verts[1]) == "number" then - local converted = {} - for i = 1 , #verts , 2 do - converted[#converted+1] = {verts[i] , verts[i+1]} - end - verts = converted - end - + -- draw separated triangles + elseif L5_env.kind == TRIANGLES then + local verts = toVertTable(L5_env.vertices) if L5_env.useTexture and L5_env.currentTexture then local mesh = love.graphics.newMesh(verts, TRIANGLES) mesh:setTexture(L5_env.currentTexture) L5_env.currentTexture:setWrap(L5_env.textureWrap, L5_env.textureWrap) love.graphics.draw(mesh) - return - end - - - if L5_env.fill_mode == "fill" then - local mesh = love.graphics.newMesh(verts, TRIANGLES) - love.graphics.draw(mesh) - end - - local r, g, b, a = love.graphics.getColor() - love.graphics.setColor(unpack(L5_env.stroke_color)) - for i = 1, #verts, 3 do - local v1, v2, v3 = verts[i], verts[i+1], verts[i+2] - if(v1 == nil or v2 == nil or v3 == nil) then break end - love.graphics.line(v1[1],v1[2], v2[1],v2[2]) - love.graphics.line(v2[1],v2[2], v3[1],v3[2]) - love.graphics.line(v3[1],v3[2], v1[1],v1[2]) - end - love.graphics.setColor(r, g, b, a) - return - end - - - -- draw triangle strip - if L5_env.kind == TRIANGLE_STRIP then - local verts = L5_env.vertices - if type(verts[1]) == "number" then - local converted = {} - for i = 1 , #verts , 2 do - converted[#converted+1] = {verts[i] , verts[i+1]} + else + if L5_env.fill_mode == "fill" then + L5_env.mesh:setVertices(verts, 1, #verts) + L5_env.mesh:setDrawMode("triangles") + L5_env.mesh:setDrawRange(1, #verts) + love.graphics.draw(L5_env.mesh) + end + local r, g, b, a = love.graphics.getColor() + love.graphics.setColor(unpack(L5_env.stroke_color)) + for i = 1, #verts, 3 do + local v1, v2, v3 = verts[i], verts[i+1], verts[i+2] + if v1 == nil or v2 == nil or v3 == nil then break end + love.graphics.line(v1[1],v1[2], v2[1],v2[2]) + love.graphics.line(v2[1],v2[2], v3[1],v3[2]) + love.graphics.line(v3[1],v3[2], v1[1],v1[2]) end - verts = converted + love.graphics.setColor(r, g, b, a) end + -- draw triangle strip + elseif L5_env.kind == TRIANGLE_STRIP then + local verts = toVertTable(L5_env.vertices) if L5_env.useTexture and L5_env.currentTexture then local mesh = love.graphics.newMesh(verts, TRIANGLE_STRIP) mesh:setTexture(L5_env.currentTexture) L5_env.currentTexture:setWrap(L5_env.textureWrap, L5_env.textureWrap) love.graphics.draw(mesh) - return - end - - if L5_env.fill_mode == "fill" then - local mesh = love.graphics.newMesh(verts, TRIANGLE_STRIP) - love.graphics.draw(mesh) - end - - local r, g, b, a = love.graphics.getColor() - love.graphics.setColor(unpack(L5_env.stroke_color)) - for i = 1, #verts-2, 1 do - local v1, v2, v3 = verts[i], verts[i+1], verts[i+2] - if(v1 == nil or v2 == nil or v3 == nil) then break end - love.graphics.line(v1[1],v1[2], v2[1],v2[2]) - love.graphics.line(v2[1],v2[2], v3[1],v3[2]) - love.graphics.line(v3[1],v3[2], v1[1],v1[2]) + else + if L5_env.fill_mode == "fill" then + L5_env.mesh:setVertices(verts, 1, #verts) + L5_env.mesh:setDrawMode("strip") + L5_env.mesh:setDrawRange(1, #verts) + love.graphics.draw(L5_env.mesh) + end + local r, g, b, a = love.graphics.getColor() + love.graphics.setColor(unpack(L5_env.stroke_color)) + for i = 1, #verts-2 do + local v1, v2, v3 = verts[i], verts[i+1], verts[i+2] + if v1 == nil or v2 == nil or v3 == nil then break end + love.graphics.line(v1[1],v1[2], v2[1],v2[2]) + love.graphics.line(v2[1],v2[2], v3[1],v3[2]) + love.graphics.line(v3[1],v3[2], v1[1],v1[2]) + end + love.graphics.setColor(r, g, b, a) end - love.graphics.setColor(r, g, b, a) - return - end -- draw triangles centered around the first vertex - if L5_env.kind == TRIANGLE_FAN then - - local verts = L5_env.vertices - if type(verts[1]) == "number" then - local converted = {} - for i = 1 , #verts , 2 do - converted[#converted+1] = {verts[i] , verts[i+1]} - end - verts = converted - end - + elseif L5_env.kind == TRIANGLE_FAN then + local verts = toVertTable(L5_env.vertices) if L5_env.useTexture and L5_env.currentTexture then local mesh = love.graphics.newMesh(verts, TRIANGLE_FAN) mesh:setTexture(L5_env.currentTexture) L5_env.currentTexture:setWrap(L5_env.textureWrap, L5_env.textureWrap) love.graphics.draw(mesh) - return - end - if L5_env.fill_mode == "fill" then - local mesh = love.graphics.newMesh(verts, TRIANGLE_FAN) - love.graphics.draw(mesh) - end - - local r, g, b, a = love.graphics.getColor() - love.graphics.setColor(unpack(L5_env.stroke_color)) - for i = 2, #verts-1, 1 do - local v1, v2, v3 = verts[1], verts[i], verts[i+1] - if(v1 == nil or v2 == nil or v3 == nil) then break end - love.graphics.line(v1[1],v1[2], v2[1],v2[2]) - love.graphics.line(v2[1],v2[2], v3[1],v3[2]) - love.graphics.line(v3[1],v3[2], v1[1],v1[2]) + else + if L5_env.fill_mode == "fill" then + L5_env.mesh:setVertices(verts, 1, #verts) + L5_env.mesh:setDrawMode("fan") + L5_env.mesh:setDrawRange(1, #verts) + love.graphics.draw(L5_env.mesh) + end + local r, g, b, a = love.graphics.getColor() + love.graphics.setColor(unpack(L5_env.stroke_color)) + for i = 2, #verts-1 do + local v1, v2, v3 = verts[1], verts[i], verts[i+1] + if v1 == nil or v2 == nil or v3 == nil then break end + love.graphics.line(v1[1],v1[2], v2[1],v2[2]) + love.graphics.line(v2[1],v2[2], v3[1],v3[2]) + love.graphics.line(v3[1],v3[2], v1[1],v1[2]) + end + love.graphics.setColor(r, g, b, a) end - love.graphics.setColor(r, g, b, a) - return - end + -- polygon fallback (kind == nil) -- if texture() triangulate fan mesh - convex assumed - if L5_env.useTexture and L5_env.currentTexture then - local mesh = love.graphics.newMesh(L5_env.vertices, "fan") - mesh:setTexture(L5_env.currentTexture) - L5_env.currentTexture:setWrap(L5_env.textureWrap, L5_env.textureWrap) - love.graphics.draw(mesh) else - -- triangulate handles concave shapes but errors on self-intersecting polygons - if L5_env.fill_mode == "fill" then - local ok, triangles = pcall(love.math.triangulate, L5_env.vertices) - if ok then - local meshVerts = {} - for _, tri in ipairs(triangles) do - for i = 1, 6, 2 do - meshVerts[#meshVerts+1] = {tri[i], tri[i+1]} + if L5_env.useTexture and L5_env.currentTexture then + local mesh = love.graphics.newMesh(L5_env.vertices, "fan") + mesh:setTexture(L5_env.currentTexture) + L5_env.currentTexture:setWrap(L5_env.textureWrap, L5_env.textureWrap) + love.graphics.draw(mesh) + else + -- triangulate handles concave shapes but errors on self-intersecting polygons + if L5_env.fill_mode == "fill" then + local ok, triangles = pcall(love.math.triangulate, L5_env.vertices) + if ok then + local meshVerts = {} + for _, tri in ipairs(triangles) do + for i = 1, 6, 2 do + meshVerts[#meshVerts+1] = {tri[i], tri[i+1]} + end end + L5_env.mesh:setVertices(meshVerts, 1, #meshVerts) + L5_env.mesh:setDrawMode("triangles") + L5_env.mesh:setDrawRange(1, #meshVerts) + love.graphics.draw(L5_env.mesh) + else + love.graphics.polygon("fill", L5_env.vertices) end - local mesh = love.graphics.newMesh(meshVerts, "triangles") - love.graphics.draw(mesh) + end + local r, g, b, a = love.graphics.getColor() + love.graphics.setColor(unpack(L5_env.stroke_color)) + if _close == CLOSE then + local verts = L5_env.vertices + -- draw all segments + love.graphics.line(verts[1], verts[2], unpack(verts, 3, #verts)) + -- close by drawing back to start + love.graphics.line(verts[#verts-1], verts[#verts], verts[1], verts[2]) else - love.graphics.polygon("fill", L5_env.vertices) + -- continuous lines, doesn't close the last two segments aka OPEN + love.graphics.line(L5_env.vertices) end + love.graphics.setColor(r, g, b, a) end - local r, g, b, a = love.graphics.getColor() - love.graphics.setColor(unpack(L5_env.stroke_color)) - if _close == CLOSE then - local verts = L5_env.vertices - -- draw all segments - love.graphics.line(verts[1], verts[2], - unpack(verts, 3, #verts)) - -- close by drawing back to start - love.graphics.line(verts[#verts-1], verts[#verts], verts[1], verts[2]) - else - -- continuous lines, doesn't close the last two segments aka OPEN - love.graphics.line(L5_env.vertices) - end - love.graphics.setColor(r, g, b, a) end end