How to use Framebuffer:renderTo() properly

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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to use Framebuffer:renderTo() properly

Post by Robin »

Jasoco wrote:How would I use this to:

Copy the contents of the main visible screen to a separate buffer
Manipulate the pixels of the separate newly copied buffer, say make all the pixels greyscale
Then use that separate buffer as an image to paste onto the main screen

?
That second part indeed sounds like something a shader would do.
Jasoco wrote:Well, mostly the first and third steps. Can I copy from one frame to another? Then can I take another and use it as image data?

That's mainly what I want. Or just how do I draw to another buffer and use that buffer on my main screen? I don't understand the code provided. Anyone have a working example .love file?
If I understand you correctly, you want to do something like:

Code: Select all

love.graphics.setRenderTarget(fb2)
love.graphics.draw(fb1, 0, 0)
love.graphics.setRenderTarget()
does that sound right to you?
Help us help you: attach a .love.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How to use Framebuffer:renderTo() properly

Post by Jasoco »

Are fb1 and fb2 names for each buffer? Because it looks like what I want.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to use Framebuffer:renderTo() properly

Post by Robin »

Jasoco wrote:Are fb1 and fb2 names for each buffer? Because it looks like what I want.
Jup.

Alternative syntax (that does exactly the same):

Code: Select all

fb2:renderTo(function() love.graphics.draw(fb1, 0, 0) end)
Help us help you: attach a .love.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How to use Framebuffer:renderTo() properly

Post by Jasoco »

So they're just called fb1 and fb2? How many can we have, or how can we find that out if it depends on our video card?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to use Framebuffer:renderTo() properly

Post by Robin »

Jasoco wrote:So they're just called fb1 and fb2? How many can we have, or how can we find that out if it depends on our video card?
I get to about 526 or something, it probably depends on the video card.

And no, those names are arbitrary. I assume somewhere earlier in the code it says:

Code: Select all

fb1 = love.graphics.newFramebuffer()
fb2 = love.graphics.newFramebuffer()
Help us help you: attach a .love.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: How to use Framebuffer:renderTo() properly

Post by TechnoCat »

Now that 0.7.0 is released and framebuffers are window dimension independent.
Here is a framebuffer example. They are very cool.

Code: Select all

function love.load()
  enabled = false
  image = love.graphics.newImage("image.png")
  
  translate = {x=0, y=0}
  scene = {}
  scene.width = 2048
  scene.height = 2048
  
  --Iniitialize framebuffer
  fb = love.graphics.newFramebuffer(scene.width, scene.height)
  
  --Create random objects placed everywhere
  imageSet = {}
  for i = 1, 10000 do
    local entry = {}
    entry.x = math.random(scene.width-128)
    entry.y = math.random(scene.height-128)
    imageSet[i] = entry
  end
  
  --Draw them to the framebuffers
  -- This is my preferable method
  love.graphics.setRenderTarget(fb)
  for _,v in ipairs(imageSet) do
    love.graphics.draw(image, v.x, v.y)
  end
  love.graphics.setRenderTarget()
  --]]
  --[[ This is the other method
  fb:renderTo(
      function()
        for _,v in ipairs(imageSet) do
          love.graphics.draw(image, v.x, v.y)
        end
      end
  )
  --]]
end


function love.update(dt)
  if love.keyboard.isDown("left") then
    translate.x = translate.x + 1000*dt
  elseif love.keyboard.isDown("right") then
    translate.x = translate.x - 1000*dt
  end
  
  if love.keyboard.isDown("up") then
    translate.y = translate.y + 1000*dt
  elseif love.keyboard.isDown("down") then
    translate.y = translate.y - 1000*dt
  end 
end

function love.draw()
  love.graphics.push()
  love.graphics.translate(translate.x, translate.y)
  if enabled then
    love.graphics.draw(fb, 0, 0)
  else
    for _,v in ipairs(imageSet) do
      love.graphics.draw(image, v.x, v.y)
    end
  end
  love.graphics.pop()
  
  fps = love.timer.getFPS()
  if enabled then
    love.graphics.setCaption(fps.." Framebuffer rendering enabled",0,0)
  else
    love.graphics.setCaption(fps.." Framebuffer rendering disabled",0,0)
  end
end

function love.keypressed(k)
  if k==" " then
    enabled = not enabled
  end
end
Attachments
Framebuffer.love
0.7.0 Framebuffer
(4.21 KiB) Downloaded 1247 times
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: How to use Framebuffer:renderTo() properly

Post by tentus »

TechnoCat wrote:Now that 0.7.0 is released and framebuffers are window dimension independent.
Here is a framebuffer example. They are very cool.

Code: Select all

function love.load()
  enabled = false
  image = love.graphics.newImage("image.png")
  
  translate = {x=0, y=0}
  scene = {}
  scene.width = 2048
  scene.height = 2048
  
  --Iniitialize framebuffer
  fb = love.graphics.newFramebuffer(scene.width, scene.height)
  
  --Create random objects placed everywhere
  imageSet = {}
  for i = 1, 10000 do
    local entry = {}
    entry.x = math.random(scene.width-128)
    entry.y = math.random(scene.height-128)
    imageSet[i] = entry
  end
  
  --Draw them to the framebuffers
  -- This is my preferable method
  love.graphics.setRenderTarget(fb)
  for _,v in ipairs(imageSet) do
    love.graphics.draw(image, v.x, v.y)
  end
  love.graphics.setRenderTarget()
  --]]
  --[[ This is the other method
  fb:renderTo(
      function()
        for _,v in ipairs(imageSet) do
          love.graphics.draw(image, v.x, v.y)
        end
      end
  )
  --]]
end


function love.update(dt)
  if love.keyboard.isDown("left") then
    translate.x = translate.x + 1000*dt
  elseif love.keyboard.isDown("right") then
    translate.x = translate.x - 1000*dt
  end
  
  if love.keyboard.isDown("up") then
    translate.y = translate.y + 1000*dt
  elseif love.keyboard.isDown("down") then
    translate.y = translate.y - 1000*dt
  end 
end

function love.draw()
  love.graphics.push()
  love.graphics.translate(translate.x, translate.y)
  if enabled then
    love.graphics.draw(fb, 0, 0)
  else
    for _,v in ipairs(imageSet) do
      love.graphics.draw(image, v.x, v.y)
    end
  end
  love.graphics.pop()
  
  fps = love.timer.getFPS()
  if enabled then
    love.graphics.setCaption(fps.." Framebuffer rendering enabled",0,0)
  else
    love.graphics.setCaption(fps.." Framebuffer rendering disabled",0,0)
  end
end

function love.keypressed(k)
  if k==" " then
    enabled = not enabled
  end
end
I get an OpenGL error on my work machine (line 11, which creates the fb). I'm assuming that means that this POS machine cannot do framebuffers at all?
Kurosuke needs beta testers
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to use Framebuffer:renderTo() properly

Post by Robin »

Maybe it's just too large? Try removing the arguments to newFramebuffer, see if that works.
Help us help you: attach a .love.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: How to use Framebuffer:renderTo() properly

Post by TechnoCat »

Robin wrote:Maybe it's just too large? Try removing the arguments to newFramebuffer, see if that works.
Well, chances are if you can't do a big buffer, removing the arguments and making an 800x600 size framebuffer will lead to even more problems, try 512x512.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: How to use Framebuffer:renderTo() properly

Post by tentus »

I did before posting (sorry I didn't mention that). Same result. I could whip up a screenshot if that would help?
Kurosuke needs beta testers
Post Reply

Who is online

Users browsing this forum: No registered users and 102 guests