diff --git a/L5.lua b/L5.lua index 66b0d30..599f290 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,145 @@ 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.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]} + end + 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) + 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.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]) + 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") 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 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..4810cb9 --- /dev/null +++ b/examples/beginShapePoints.lua @@ -0,0 +1,14 @@ +require("L5") + +function setup() + size(400, 400) + windowTitle("beginShape(POINTS) example") + + fill(0) + beginShape(POINTS) + 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 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