Page 1 of 2

Preventing lighting from being drawn on the background

Posted: Thu Jul 24, 2014 8:06 pm
by Whatthefuck
So I have lighting in my game, however I came across an issue - the shadows are drawn on the background. I need a way of preventing the shadows from being drawn on the background. Here's the problem I came across:

Image

The orange'ish color is the background, and obviously, it should be void of any light being drawn over it.
How do I prevent that? Use a shader? And if so, what kind? Any examples?

Now, not all backgrounds should be void of the lighting. Here's an example of the background that does receive the lighting and should stay that way:

Image

Re: Preventing lighting from being drawn on the background

Posted: Thu Jul 24, 2014 10:23 pm
by Zer0
Probably using a canvas for the foreground (everything BUT the background) that is shaded and drawing it ontop of the unshaded background.
[wiki]Canvas[/wiki]
EDIT: I sure hope you're using spritebatches for those tiles. (Just mentioning because alot of draw calls is WAAAY more expensive than one draw call with a spritebatch)

Re: Preventing lighting from being drawn on the background

Posted: Fri Jul 25, 2014 6:06 am
by Whatthefuck
Zer0 wrote:Probably using a canvas for the foreground (everything BUT the background) that is shaded and drawing it ontop of the unshaded background.
[wiki]Canvas[/wiki]
That won't do anything.

Re: Preventing lighting from being drawn on the background

Posted: Fri Jul 25, 2014 6:40 am
by micha
What method are you using for drawing the lighting? Is it a shader or a mesh or something else?

Re: Preventing lighting from being drawn on the background

Posted: Fri Jul 25, 2014 6:42 am
by Whatthefuck
micha wrote:What method are you using for drawing the lighting? Is it a shader or a mesh or something else?
It's a spritebatch.

Re: Preventing lighting from being drawn on the background

Posted: Fri Jul 25, 2014 7:59 am
by kikito
Show us the code. Give us a .love file showing the issue. Otherwise it's too difficult to help you.

Re: Preventing lighting from being drawn on the background

Posted: Fri Jul 25, 2014 8:31 am
by Zer0
Whatthefuck wrote:
micha wrote:What method are you using for drawing the lighting? Is it a shader or a mesh or something else?
It's a spritebatch.
A spriteBatch like 1 sprite per pixel? *cough* inefficient *cough*
I was guessing for a shader and you did not really explain how you did the lightning.
1. Draw the background
2. Start drawing to canvas with lightning shader
3. Draw everything shaded
4. Draw the canvas

If you don't want to use a shader for some reason use a stencil.
1. Draw background without lightning
2. Use the foreground as a stencil and draw it.

Re: Preventing lighting from being drawn on the background

Posted: Fri Jul 25, 2014 8:33 am
by Whatthefuck
Zer0 wrote:
Whatthefuck wrote:
micha wrote:What method are you using for drawing the lighting? Is it a shader or a mesh or something else?
It's a spritebatch.
A spriteBatch like 1 sprite per pixel? *cough* inefficient *cough*
I was guessing for a shader and you did not really explain how you did the lightning.
1. Draw the background
2. Start drawing to canvas with lightning shader
3. Draw everything shaded
4. Draw the canvas

If you don't want to use a shader for some reason use a stencil.
1. Draw background without lightning
2. Use the foreground as a stencil and draw it.
1 sprite per 20x20 pixels, it's actually pretty efficient (not THE most efficient method but for a person who has no clue how to write shaders and in the way I use it, it produces good results). And no, I don't use any shaders.
The background is just the default one with the colors being changed via love.graphics.setBackgroundColor.
How would I use a stencil for that?
For reference, the finalized lighting is drawn into a separate canvas, which is then upscaled.

Re: Preventing lighting from being drawn on the background

Posted: Fri Jul 25, 2014 8:34 am
by Whatthefuck
kikito wrote:Show us the code. Give us a .love file showing the issue. Otherwise it's too difficult to help you.
There's so much code and stuff that I'd have to spend hours to pack everything into a separate .love file.

Edit: argh, no automerge. ;_;

Re: Preventing lighting from being drawn on the background

Posted: Fri Jul 25, 2014 8:55 am
by Zer0
Whatthefuck wrote:How would I use a stencil for that?

Code: Select all

    draw_background()
    love.graphics.setStencil(draw_foreground())
    draw_foreground()
    love.graphics.setStencil()
[wiki]love.graphics.setStencil[/wiki]
setStencil says you ONLY draw to where the stencil draws. (the stencil is just a function with stuff that is drawn)