Darker image from framebuffer

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
Motig
Prole
Posts: 28
Joined: Fri Oct 15, 2010 3:43 am

Darker image from framebuffer

Post by Motig »

I'm trying to use a framebuffer in my game so I can draw something that would normally take a lot of draw operations in only one. The problem is that whenever I draw the imagedata created by the framebuffer it always comes out really dark. I've tried toying around with the setColors but I can't seem to figure out what's causing it.

The code below shows the difference between the two. By default it just draws everything every frame but if you comment out the first line of .draw and let the if chain run it will use the framebuffer and come out really dark.

Code: Select all

function love.load()
	font = love.graphics.newFont(20)
	love.graphics.setFont(font);
end

function love.draw()
	drawBackground()
	--[[
	if background ~= nil then
		love.graphics.draw(background, 0, 0)
	else
		renderGameBackground()
	end
	--]]
end

function drawBackground()
	local a = 0;
	for i = 1, 10 do
		love.graphics.setColor(255, 255, 255, 150)
		if i < 10 then
			love.graphics.print(a*2, 5, (i*50)+15)
			love.graphics.print(a*2, 795 - font:getWidth(a*2), (i*50)+15)
		else
			love.graphics.print(a*2, 5, (i*50)+27)
			love.graphics.print(a*2, 795 - font:getWidth(a*2), (i*50)+27)			
		end
		if i < 6 then
			a = a + 1
		else
			a = a - 1
		end
		if i == 10 then love.graphics.setColor(255, 0, 0, 255); i = 10.5 end
		love.graphics.line(0, 50+(i*50), 800, 50+(i*50))
	end
	love.graphics.setColor(255, 255, 255, 255)
end

function renderGameBackground()
	local backGroundBuffer = love.graphics.newFramebuffer()
	backGroundBuffer:renderTo(function()
		drawBackground()
	end)
	background = love.graphics.newImage(backGroundBuffer:getImageData())	
end

User avatar
Eshaktaar
Prole
Posts: 3
Joined: Wed Jan 13, 2010 12:45 pm
Contact:

Re: Darker image from framebuffer

Post by Eshaktaar »

It seems that transparency isn't displayed properly with framebuffers in 0.7.0.
I've noticed similar issues when using images with alpha channels that are not just black and white. You can see the effect in TechnoCat's example:
http://love2d.org/forums/viewtopic.php?p=23730#p23730
Press space to toggle the framebuffer and you can see a slight difference at the circles' edges.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Darker image from framebuffer

Post by Boolsheet »

The alpha is handled correctly in LÖVE.
The framebuffer appears darker, because the default blendMode "alpha" blends it twice this way. This is expected behaviour.

Sadly, we don't have the proper blendMode available at the moment to to this the correct way. There's an issue in the tracker that proposes adding the "premultiplied" blendMode, which does exactly what we need.

Once it is included we can draw to the framebuffer with the default blendMode and then switch to premultiplied if we draw the framebuffer itself.


A few comments to your code:
You only have to create one framebuffer if it is like a static background. Creating one every update is very costly.
It is important to note that love.graphics.setRenderTarget (or the :renderTo variant) clears the frambuffer (Not that it is relevant to your code, just trying to avoid confusion). This behaviour might change with 0.8.0 LÖVE.
It might be clearer to use love.graphics.setRenderTarget, but that's just me.

Code: Select all

function love.load()
	font = love.graphics.newFont(20)
	love.graphics.setFont(font)

	backGroundBuffer = love.graphics.newFramebuffer()

	love.graphics.setRenderTarget(backGroundBuffer)
	drawBackground()
	love.graphics.setRenderTarget()
end

function love.draw()
	if love.keyboard.isDown(" ") then
		love.graphics.draw(backGroundBuffer, 0, 0)
	else
		drawBackground()
	end
end

function drawBackground()
	local a = 0;
	for i = 1, 10 do
		love.graphics.setColor(255, 255, 255, 150)
		if i < 10 then
			love.graphics.print(a*2, 5, (i*50)+15)
			love.graphics.print(a*2, 795 - font:getWidth(a*2), (i*50)+15)
		else
			love.graphics.print(a*2, 5, (i*50)+27)
			love.graphics.print(a*2, 795 - font:getWidth(a*2), (i*50)+27)         
		end
		
		if i < 6 then
			a = a + 1
		else
			a = a - 1
		end
		
		if i == 10 then
			love.graphics.setColor(255, 0, 0, 255)
			i = 10.5
		end
		love.graphics.line(0, 50+(i*50), 800, 50+(i*50))
	end
	love.graphics.setColor(255, 255, 255, 255)
end
(The Framebuffer is of course still drawing darker than we want. This is just to show what I said in the comments.)
Shallow indentations.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Darker image from framebuffer

Post by bartbes »

Yeah, I remember something about that..

@vrld: Do you remember if we added alpha support? Or is that even impossible? (As it's pre-rendered.)
User avatar
Motig
Prole
Posts: 28
Joined: Fri Oct 15, 2010 3:43 am

Re: Darker image from framebuffer

Post by Motig »

Thanks for the answers! Good to know it wasn''t me going crazy!

Boolsheet: I know you shouldn't use the framebuffer every update if the image is static. I'm pretty sure the code I posted doesn't do that either since the variable background won't be nil after it has been filled with the framebuffer thus it just draws it. But it's probably better to do it in the love.load() anyway in this example... Cheers!
Last edited by Motig on Sat Feb 12, 2011 6:00 pm, edited 2 times in total.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Darker image from framebuffer

Post by vrld »

bartbes wrote:@vrld: Do you remember if we added alpha support? Or is that even impossible? (As it's pre-rendered.)
There isn't (yet) premultiplied alpha. But a since the drawing of a framebuffer is the same as drawing an image, this should be possible.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 5 guests