How does one glow?

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
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: How does one glow?

Post by tentus »

TylertheDesigner wrote:
Taehl wrote:The "right" way to do this would be to use an OpenGL fragment shader. Unfortunately, Love doesn't have support for them (yet?). Don't mess with framebuffers - the next best thing is to use images which have the bloom baked into them. Here, have some (consider them public domain).
Thanks for the response Taehl! I expected that would be the required route. My hope was to do it in code, so I could create random shapes and apply glow to them, without having to draw them out as multiple "bloomed" lines. While I know the framebuffer route is not the best regarding speed, is it plausible?
It's plausible, but here's the problem you will run into if you just scale framebuffers:
fb.jpg
fb.jpg (22.42 KiB) Viewed 1054 times
You gets this weird 3D effect that gets more prominent around the outer edges of the screen.

Now, if you multi-draw each image that'll fix that problem, but it'll hit the framerate even harder.
Kurosuke needs beta testers
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: How does one glow?

Post by vrld »

Taehl wrote:Plausible? I guess anything's possible, if you want to completely disregard performance.(...) spritebatch - this would give you a huge performance boost.
Spritebatches are in fact not faster than drawing the images yourself. Framebuffers are the fastest method to draw static things.
tentus wrote:It's plausible, but here's the problem you will run into if you just scale framebuffers:
Interesting effect, but not what I meant. What I meant was something along this (some tweaking with the scale parameters required):

Code: Select all

-- .love attached, see below.
function love.load()
	-- create framebuffer info
	local function scaled_fb(scale)
		return {
			scale = scale,
			fb = love.graphics.newFramebuffer(800 * scale, 600 * scale),
		}
	end

	-- create framebuffers
	fbs = {}
	for i = 1,3 do
		fbs[i] = scaled_fb(.2 + i * .05)
	end

	love.graphics.setLine(5)
end

-- return function that renders scaled scene
function render(scale, alpha)
	return function()
		love.graphics.push()
		love.graphics.scale(scale,scale)
		love.graphics.setColor(200,240,140, alpha)
		love.graphics.circle('line', 300,300, 50, 32)
		love.graphics.setColor(120,100,250, alpha)
		love.graphics.rectangle('line', 500,200,100,200)
		love.graphics.setColor(100,240,140, alpha)
		love.graphics.circle('line', 100,400,100,6)
		love.graphics.pop()
	end
end

function love.draw()
	-- render scene to framebuffers
	for i, info in ipairs(fbs) do
		info.fb:renderTo(render(info.scale, 180))
	end

	if not love.keyboard.isDown('b') then
		-- blur pass
		love.graphics.setBlendMode('additive')
		love.graphics.setColor(255,255,255)
		for i, info in ipairs(fbs) do
			love.graphics.draw(info.fb, 0,0, 0, 1 / info.scale)
		end
	end

	-- draw scene
	render(1)()
	love.graphics.setBlendMode('alpha')
	love.graphics.setColor(255,255,255)
	love.graphics.print('FPS: ' .. love.timer.getFPS(), 10,10)
end
The result:
crappy-blur.jpg
crappy-blur.jpg (15.6 KiB) Viewed 1048 times
As opposed to without blur:
crappy-no-blur.jpg
crappy-no-blur.jpg (12.42 KiB) Viewed 1048 times
Edit: This is not PO2 safe, so it might not work for everyone... :roll:
Attachments
crappyblur.love
(679 Bytes) Downloaded 90 times
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: How does one glow?

Post by Taehl »

vrld wrote:Spritebatches are in fact not faster than drawing the images yourself.
I've found this to be completely untrue. I use spritebatches extensively for the terrain in Isome - drawing thousands of tiles individually was more than an order of magnitude slower.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: How does one glow?

Post by vrld »

Taehl wrote:
vrld wrote:Spritebatches are in fact not faster than drawing the images yourself.
I've found this to be completely untrue.
You're right. I should have benchmarked before making that statement. Let's do that now (I get a varying factor of 6 to 7):

Code: Select all

function love.load()
	img = love.graphics.newImage('bloomsquare.png')
	sb = love.graphics.newSpriteBatch(img, 500)
	fb = love.graphics.newFramebuffer()

	math.randomseed(1)
	for i = 1,500 do
		local x,y,r,s = math.random(0,800), math.random(0,600), math.random()*3.14, math.random() * .45 + .8
		sb:add(x,y,r,s)
	end
end

local rounds = 1000
local iter = 0
local start = 0
local time_sb, time_self = 0,0
function love.draw()
	if iter == 0 then
		start = os.clock()
	elseif iter == rounds then
		time_sb = os.clock() - start
		start = os.clock()
	elseif iter == 2 * rounds then
		time_self = os.clock() - start
		start = os.clock()
	end

	if iter < rounds then
		love.graphics.draw(sb, 0,0)
	elseif iter < 2 * rounds then
		math.randomseed(1)
		for i = 1,500 do
			local x,y,r,s = math.random(0,800), math.random(0,600), math.random()*3.14, math.random() * .45 + .8
			love.graphics.draw(img, x,y,r,s)
		end
	else
		love.graphics.print(string.format("Spritebatch: total=%fs, avg=%fs", time_sb, time_sb / rounds), 100,100)
		love.graphics.print(string.format("Without: total=%fs, avg=%fs", time_self, time_self / rounds), 100,120)
	end

	iter = iter + 1
end
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How does one glow?

Post by Robin »

Which makes sense, both because setting setting up things in Lua once is faster than doing it every frame, and because crossing the Lua/C++ border is relatively expensive. (A SpriteBatch only crosses it once per frame, without it 500 times per frame).
Help us help you: attach a .love.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: How does one glow?

Post by vrld »

Robin wrote:Which makes sense, both because setting setting up things in Lua once is faster than doing it every frame, and because crossing the Lua/C++ border is relatively expensive.
And also interesting: A while back over in #love, itsnotabigtruck benchmarked sprite-batches and found that they are not faster than individual drawing.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How does one glow?

Post by Robin »

Wait what, that's counter-intuitive.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests