Page 1 of 1

Rendering a canvas to itself?

Posted: Sun Oct 21, 2018 4:37 pm
by lqdev
I tried to create a shader chaining functionality to my game engine, but I have to render a canvas to itself somehow. I know that I have to use offscreen buffers somehow, but I'm not sure how. Here's what I'm ultimately trying to achieve:

Code: Select all

canvas = nil

function love.load()
  canvas = love.graphics.newCanvas()
end

function love.draw()
  love.graphics.setCanvas(canvas)
  -- [set the first shader]
  love.graphics.draw(canvas) -- here's where the method fails
  -- [set the second shader]
  love.graphics.draw(canvas)
  -- etc
end
How to do this properly?

Re: Rendering a canvas to itself?

Posted: Sun Oct 21, 2018 10:07 pm
by grump
You need to use two canvasses.

Code: Select all

function love.draw()
  love.graphics.setCanvas(canvas2)
  -- [set the first shader]
  love.graphics.draw(canvas)
  -- [set the second shader]
  love.graphics.setCanvas(canvas)
  love.graphics.draw(canvas2)
  -- etc
end

Re: Rendering a canvas to itself?

Posted: Mon Oct 22, 2018 1:47 pm
by lqdev
grump wrote: Sun Oct 21, 2018 10:07 pm You need to use two canvasses.

Code: Select all

function love.draw()
  love.graphics.setCanvas(canvas2)
  -- [set the first shader]
  love.graphics.draw(canvas)
  -- [set the second shader]
  love.graphics.setCanvas(canvas)
  love.graphics.draw(canvas2)
  -- etc
end
That's what I tried to do: have two canvases, draw onto the first canvas, swap them (first becomes second and vice versa) using

Code: Select all

canvas1, canvas2 = canvas2, canvas1
then draw onto canvas1, rinse and repeat, but this still throws the error.

Re: Rendering a canvas to itself?

Posted: Mon Oct 22, 2018 3:11 pm
by pgimeno
Works for me:

Code: Select all

local canvas1 = love.graphics.newCanvas()
local canvas2 = love.graphics.newCanvas()

function love.draw()
  love.graphics.setCanvas(canvas1)
  love.graphics.clear()
  love.graphics.rectangle("fill", 10, 20, 30, 40)

  canvas1, canvas2 = canvas2, canvas1
  love.graphics.setCanvas(canvas1)
  love.graphics.clear()
  love.graphics.draw(canvas2)

  canvas1, canvas2 = canvas2, canvas1
  love.graphics.setCanvas(canvas1)
  love.graphics.clear()
  love.graphics.draw(canvas2)

  canvas1, canvas2 = canvas2, canvas1
  love.graphics.setCanvas(canvas1)
  love.graphics.clear()
  love.graphics.draw(canvas2)

  love.graphics.setCanvas()
  love.graphics.draw(canvas1)
end
Can you show some code that demonstrates the problem?

Re: Rendering a canvas to itself?

Posted: Sat Oct 27, 2018 11:52 am
by lqdev
pgimeno wrote: Mon Oct 22, 2018 3:11 pm Works for me:

(...)
Actually, the problem lied in my order of operations, I changed it to be similar to yours and it works now. Thanks for your help!