Rendering a canvas to itself?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
lqdev
Prole
Posts: 4
Joined: Sun Oct 21, 2018 4:30 pm
Contact:

Rendering a canvas to itself?

Post 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?
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Rendering a canvas to itself?

Post 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
User avatar
lqdev
Prole
Posts: 4
Joined: Sun Oct 21, 2018 4:30 pm
Contact:

Re: Rendering a canvas to itself?

Post 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.
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: Rendering a canvas to itself?

Post 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?
User avatar
lqdev
Prole
Posts: 4
Joined: Sun Oct 21, 2018 4:30 pm
Contact:

Re: Rendering a canvas to itself?

Post 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!
Post Reply

Who is online

Users browsing this forum: darkfrei, Google [Bot], secretsue92 and 78 guests