Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Question about lighting in 2D game.  (Read 5948 times)

0 Members and 1 Guest are viewing this topic.

Kyubey

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Question about lighting in 2D game.
« on: May 18, 2014, 07:45:54 pm »
Hello there.
I've been talking with my friend about making a 2D game in SFML 2.1 and we wanted to make (kinda) realistic lighting. Let me use as example this trailer for Crawl . We really liked the view perspective this game shows, but the thing that really bugged us were lights that... didn't light up the room and didn't reflect on the walls. So here's our question - is it possible to make a decent lighting in SFML for 2D game without resorting to "3D" graphics and/or some sketchy code? (The whole idea is pretty much in infancy, so we're sorry for not being more specific -  we were just wondering if there is an easy way to do it)

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Question about lighting in 2D game.
« Reply #1 on: May 18, 2014, 07:55:33 pm »
There's at least one 2D lighting engine in the Projects subforum, so have a look through there.  Might also be something on the Wiki.

The one I'm aware of is Let There Be Light.  Not sure if it's still maintained, but at the very least its source code should give you some hints.
« Last Edit: May 18, 2014, 07:57:50 pm by Ixrec »

MoreAxes

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Question about lighting in 2D game.
« Reply #2 on: May 18, 2014, 08:42:20 pm »
Friend of OP here.

Unfortunately that's not at all what we want. Take a look at this screenshot from Legend of Dungeon. Notice how every light source lights up both the floor below and the wall behind it? What we're asking is whether this can be achieved without resorting to 3D programming, since all in all we intend the game to be 2D as far as player movement and mechanics are concerned. Also, with SFML support for 3D graphics being minimal at best, we would have to call another library or OpenGL directly, which overcomplicates things.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Question about lighting in 2D game.
« Reply #3 on: May 18, 2014, 08:58:25 pm »
I don't see anything in SFML that would prevent you from implementing that.
However, remember that SFML is not an engine but a fairly low-level library; there is AFAIK nothing in SFML that will help you implement this either.
But, if you can come up with the proper algorithms for calculating the lighting and applying it to textures/sprites/whatever, then I really don't see why SFML would be stopping you in applying it.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Question about lighting in 2D game.
« Reply #4 on: May 18, 2014, 09:01:26 pm »
I'm no expert on lighting systems but I believe that depends on how fixed your camera angle and floor/wall angles are going to be.  If the conceptual 3D shape of your rooms is always the same (ie, the floor is always at that angle, the camera never changes, etc) then it's probably possible to achieve this with "2D lighting", but I'm not aware of any existing code here that does that particular kind of lighting.

I *think* the right method for this is to use fragment shaders when drawing walls and floors which take the positions/colors/intensities of various lights as inputs.  Unfortunately sf::Shader doesn't support parameter arrays yet so I'm not sure how easy this would be.

P.S. I looked around a bit and found that one guy handled this by drawing lights to an sf::RenderTexture, then using a shader to blend that "lightmap" with each unlit texture.
« Last Edit: May 18, 2014, 09:11:56 pm by Ixrec »

MoreAxes

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Question about lighting in 2D game.
« Reply #5 on: May 18, 2014, 09:23:26 pm »
The camera angle will most likely stay fixed. I was actually researching how this could be done with fragment shaders. I think I'll end up using a sufficiently long array of GLSL uniforms, describing the parameters of each light, and then actually rendering them one by one in the shader.

The sf::RenderTexture approach seems nice, but I don't see how it could work if the surface to be lit is, say, normal mapped or otherwise not flat.

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Question about lighting in 2D game.
« Reply #6 on: May 18, 2014, 10:51:52 pm »
Well in this case you have no choice but to work with a 3-coordinate system. You need to know the distance to the floor, the wall and the horizontal placing to place the lighting. So it's impossible to work with less than 3 coordinates.
But as stated, you don't need any 3D engine of the sorts to do the lighting: for the lighting on the floor you can find it's middle point by using the distance to from the wall and the horizontal placing, the strength of the light casted on the floor (radius) by the distance to the floor. Then skew the circle of light proportional to the angle of the floor. In the screenshot it's clear they simply shortened the height. This should give a decent result as far as I can tell.

MoreAxes

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Question about lighting in 2D game.
« Reply #7 on: May 18, 2014, 11:00:29 pm »
Yes, I think this might be the approach I'll go with, but rather than blending the "circle of light" with lit surfaces I might try to do something more advanced, since as I said in the previous post, there is no way that I know of to make blending look legitimate with a possibly normal-mapped surface. If this fails, we're probably on a one-way trip to raw OpenGL land. Which is fine, just less convenient.

 

anything