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 » Fri Nov 19, 2010 11:10 pm

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: 3642
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How to use Framebuffer:renderTo() properly

Post by Jasoco » Fri Nov 19, 2010 11:22 pm

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 » Fri Nov 19, 2010 11:31 pm

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: 3642
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How to use Framebuffer:renderTo() properly

Post by Jasoco » Sat Nov 20, 2010 12:22 am

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 » Sat Nov 20, 2010 1:28 am

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 » Wed Dec 08, 2010 5:15 pm

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 386 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 » Tue Dec 28, 2010 10:03 pm

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 » Wed Dec 29, 2010 9:56 pm

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 » Wed Dec 29, 2010 11:00 pm

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 » Thu Dec 30, 2010 5:39 am

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: Bing [Bot] and 8 guests