Clumsy attempt to use a grid mesh to distort an image.
Main (part 1 of 3)
dothidden=false
function setup()
saveProjectInfo("Description","Distort picture using a grid mesh")
print("2 finger tap --- toggle full screen")
print("1 finger tap --- toggle control points")
print("Move Points to distort")
touches=0
gesture=0
-- pFIXBORDERS=false
FIXBORDERS = false -- Border points are movable
pnX=1 -- n columns
iparameter("nX",2,16,4)
pnY=1 -- n rows
iparameter("nY",2,16,4)
pstate=0 -- cumulate distortion
iparameter("Savestate",0,1,0)
iparameter("DoWave",0,1,0) -- apply saw effect
iparameter("HWave",0,2,0) -- horz 0, vert 1, both 2
iparameter("DoReset",0,1,0) -- reset to prior saved state
ImgW=0
ImgH=0
theMesh=mesh()
ActivePoint=nil
ActiveId=-1
CpGrid={} -- control points
CpRect={} -- grid cells
-- LOAD YOUR OWN HERE
-- local BaseImg = readImage("Planet Cute:Icon")
local BaseImg = CreateMyImage(600,600)
theMesh.texture=BaseImg
ImgW=BaseImg.width
ImgH=BaseImg.height
OrigW=ImgW
OrigH=ImgH
local Tw,Th,Zom=ResizeToFit(ImgW,ImgH,WIDTH,HEIGHT)
ImgW=Tw
ImgH=Th
CreateControlPoints(nX,nY)
CreateCells(nX,nY)
PW=WIDTH
PH=HEIGHT
end
function touched(touch)
if touch.state == BEGAN then
if touches == 0 then gesture=1 end
touches = touches + 1
end
if (not dothidden) and touch.state==BEGAN and touches==1 and ActiveId == -1 then
ActivePoint=nil
local found=false
for i=1,nX+1 do
if found == false then
for j=1,nY+1 do
if found == false then
local z = CpGrid[i][j]
local dd = (touch.x - z.c.x) ^ 2 + (touch.y - z.c.y) ^ 2
if dd < 300 then
found=true
if CpGrid[i][j].movable then ActivePoint = CpGrid[i][j] end
end
end --endif
end -- end j
end -- endif
end -- i
if ActivePoint ~= nil then ActiveId=touch.id end
end
if touch.state==MOVING and ActiveId==touch.id and ActivePoint ~= nil then
ActivePoint.c.x=touch.x
ActivePoint.c.y=touch.y
end
if touch.state==ENDED then
if ActivePoint==nil and touches == 1 and gesture==1 then
dothidden = (not dothidden)
gesture=0
end
if ActivePoint == nil and touches==2 and gesture==1 then
local tmode =displayMode()
if tmode==STANDARD then
displayMode(FULLSCREEN)
else
displayMode(STANDARD)
end
gesture=0
end
touches = touches - 1
if touches==0 then gesture=0 end
ActivePoint=nil
ActiveId=-1
end
end -- function
function ResetPoints()
for i=1,nX+1 do
for j=1,nY+1 do
local tobj = CpGrid[i][j]
local tx=tobj.i.x * ImgW
local ty=tobj.i.y * ImgH
if tobj.movable then
tobj.c.x=tx
tobj.c.y=ty
end
end
end
end
function draw()
-- Resize picture for orientation or FullScreen
if PW ~= WIDTH or PH~=HEIGHT then
resetSize()
PW=WIDTH
PH=HEIGHT
end
-- change of rows columns
if pnX ~= nX or pnY ~= nY then
InitGrids()
pnX=nX
pnY=nY
end
-- reset to last saved state
if DoReset==1 then
DoReset=0
ResetPoints()
end
-- apply saw to control points
if DoWave == 1 then
Dowave=0
local inc = 0.50
for i=1,nX+1 do
for j=1,nY+1 do
local tobj = CpGrid[i][j]
local tx=tobj.c.x
local ty=tobj.c.y
if HWave == 0 and nX > 3 and nY > 3 then -- horizontal
if i%2 == 0 and j > 1 then ty = ty + inc end
end
if HWave == 1 and nX > 3 and nY > 3 then -- vertical
if j%2 == 0 and i > 1 then tx = tx + inc end
end
if HWave == 2 and nX > 3 and nY > 3 then -- both
if i%2 == 0 then tx = tx + inc end
if j%2 == 0 then ty = ty + inc end
end
if tx < 0 then tx = 0 end
if tx > WIDTH then tx = WIDTH end
if ty < 0 then ty = 0 end
if ty > HEIGHT then ty = HEIGHT end
if tobj.movable then
tobj.c.x=tx
tobj.c.y=ty
end
end
end
DoWave=0
end
if Savestate ~= pstate and Savestate == 1 then
ApplyAndContinue()
InitGrids()
Savestate=0
pstate=Savestate
end
background(40, 40, 40)
fill(255,255,255,255)
for i=1,nX do
for j=1,nY do
local z = CpRect[i][j]
z:draw(theMesh)
end
end
for i=1,nX+1 do
for j=1,nY+1 do
local z = CpGrid[i][j]
z:draw()
end
end
end