@Ignatz, @Yojimbo2000 - drawing to texture (with visible buffer image & pseudo-unit-tests):
--# Main
-- MakePlane
function setup()
textMode(CORNER)
setUpTests()
makePlane = MakePlane()
texture = makePlane.plane.mesh.texture
colorCoordinates = {}
overlayImage = image(WIDTH, HEIGHT)
setUpBufferDisplay()
end
function setUpBufferDisplay()
bufferImage = image(WIDTH, HEIGHT)
local bufferFract = 1 / 6
cameoSize = vec2(WIDTH * bufferFract, HEIGHT * bufferFract)
camoPos = vec2((cameoSize.x/2)+20, (cameoSize.y/2)+20)
setContext(bufferImage) --fill image with yellow for visibility
strokeWidth(0)
fill(220, 210, 122, 255)
rect(0,0,WIDTH,HEIGHT)
setContext()
end
function setUpTests()
local parameterTable = {} --series of buttons to confirm visuals
local assignableAction = function() end
local nextParameter = function ()
parameter.clear()
if #parameterTable == 0 then
return
end
local testDefinition = parameterTable[1]
local passAction = function()
print(testDefinition..": PASS")
assignableAction()
end
local failAction = function()
print(testDefinition..": FAIL")
assignableAction()
end
parameter.action("PASS if "..testDefinition, passAction)
parameter.action("FAIL", failAction)
table.remove(parameterTable, 1)
end
assignableAction = nextParameter
parameterTable = {"plane is showing", "touching draws red dots", "dragging leaves trail", "touch activates shader", "buffer image in corner", "clipping drawn in buffer", "mesh clips in buffer", "buffer perspective matches", "buffer coordinates read", "touch draws to texture", "draws in right place", "all touches draw"}
nextParameter()
end
function draw()
backingMode(STANDARD)
background(69, 119, 104, 255)
drawPlane()
draw2D()
end
function drawPlane()
pushMatrix()
makePlane:draw()
popMatrix()
end
function draw2D()
ortho()
viewMatrix(matrix())
sprite(overlayImage, WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
sprite(bufferImage, camoPos.x, camoPos.y, cameoSize.x, cameoSize.y)
if #colorCoordinates > 0 then
drawTouchesToTexture()
end
end
function drawTouchesToTexture()
local textureX, textureY, baseX, baseY, marker, intCoord, fX, fY
textureX, textureY = 0, 0
for i in ipairs(colorCoordinates) do
coord = colorCoordinates[i]
intCoord = vec2(math.floor(coord.x), math.floor(coord.y))
baseX, baseY, marker = bufferImage:get(intCoord.x, intCoord.y)
if marker == 1 then
fX, fY = baseX / 255, baseY / 255
textureX = texture.width * fX
textureY = texture.height * fY
setContext(texture)
fill(0, 43, 255, 255)
ellipse(textureX, textureY, 25)
setContext()
end
end
text("x: "..textureX..", y: "..math.floor(textureY, 10, 10))
colorCoordinates = {}
end
function touched(touch)
setContext(overlayImage)
fill(255, 4, 0, 255)
ellipse(touch.x, touch.y, 10)
setContext()
if touch.state == MOVING then
toggleShader(true)
drawClipToBufferPosition(touch)
table.insert(colorCoordinates, vec2(touch.x, touch.y))
else
toggleShader(false)
end
end
function drawClipToBufferPosition(touch)
setContext(bufferImage)
pushMatrix()
clip(touch.x - 10, touch.y - 10, 20, 20)
makePlane:draw()
popMatrix()
setContext()
end
function toggleShader(onOrOff)
if onOrOff == true then
local planeShader = shader(PlaneShader.v, PlaneShader.f)
makePlane.plane.mesh.shader = planeShader
else
makePlane.plane.mesh.shader = nil
end
end
--# MakePlane
MakePlane = class()
function MakePlane:init()
self:createSimpleMesh()
end
function MakePlane:draw()
self:setupPerspective()
self.plane.mesh:draw()
end
function MakePlane:createSimpleMesh()
--settings for our rectangle (named "plane" below)
self.plane={}
self.plane.centre=vec3(0,0,0)
self.plane.rotate=vec3(45,0,20)
self.plane.size=vec2(500,500,0)
strokeWidth(10)
stroke(255, 0, 0, 255)
--set up mesh, needed for shader
self.plane.mesh=mesh()
self.plane.mesh.texture = readImage("SpaceCute:Background")
local s=self.plane.size
local x1,y1,x2,y2,z=-s.x/2,-s.y/2,s.x/2,s.y/2,s.z
self.plane.mesh.vertices={vec3(x1,y1,z),vec3(x2,y1,z),vec3(x2,y2,z),vec3(x2,y2,z),vec3(x1,y2,z),vec3(x1,y1,z)}
self.plane.mesh.texCoords={vec2(0,0),vec2(1,0),vec2(1,1),vec2(1,1),vec2(0,1),vec2(0,0)}
self.plane.mesh:setColors(color(255,255,0))
self.img=image(WIDTH, HEIGHT)
self.shaderImg=image(WIDTH, HEIGHT)
end
function MakePlane:setupPerspective()
perspective()
camera(0,0,900,0,0,-1)
translate(self.plane.centre.x, self.plane.centre.y, self.plane.centre.z)
rotate(self.plane.rotate.x,1,0,0)
rotate(self.plane.rotate.y,0,1,0)
rotate(self.plane.rotate.z,0,0,1)
end
PlaneShader = {
v = [[
uniform mat4 modelViewProjection;
attribute vec4 position;
attribute vec2 texCoord;
varying highp vec2 vTexCoord;
void main()
{
vTexCoord = texCoord;
gl_Position = modelViewProjection * position;
}
]],
f = [[
precision highp float;
varying highp vec2 vTexCoord;
void main()
{
lowp float marker = 1.0 /255.0;
gl_FragColor = vec4(vTexCoord.x,vTexCoord.y,marker,1.0);
}
]]}
Progress!