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

Author Topic: SFML Light System - Let There Be Light  (Read 258275 times)

0 Members and 4 Guests are viewing this topic.

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
SFML Light System - Let There Be Light
« on: December 31, 2011, 04:17:53 am »
Let There Be Light is a 2D dynamic soft shadows system that uses SFML.

Here are some features:
- Physically accurate soft shadows
- Quad tree scene graph
- Light texture caching
- Emissive lights
- Light beams
- Directional lights
- Adjustable light color, attenuation, spread, source size, cone angle, bloom, and cone edge softness
- Ambient light setting
- Extendable light shape rendering
- Light bloom rendering

Feel free to use this system in a game. Please put me in the credits somewhere :).

Download (includes demo): https://sourceforge.net/projects/letthebelight/

The light system in action in the game "Gore Factor":

« Last Edit: May 02, 2012, 12:38:50 am by lolz123 »
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

Teemperor

  • Newbie
  • *
  • Posts: 6
    • View Profile
SFML Light System - Let There Be Light
« Reply #1 on: December 31, 2011, 11:04:33 am »
I get this on Ubuntu 11.10 with g++ 4.6.1 and enabled C++0x-standard? (without C++0x i get warnings from the headers to active C++0x and many errors)

http://pastebin.com/wrsgCHQj

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
SFML Light System - Let There Be Light
« Reply #2 on: December 31, 2011, 04:00:07 pm »
Unfortunately, I am not very familiar with g++, so here is the limited help I can offer:
C++0x is required, but you seem to be missing a bunch of things in the older version of C++ too, such as auto_ptr. You also are not getting the math libaries (specifically the sqrt function), try including them the way you normally do in g++, and try switching sqrt to sqrtf. g++ seems to complain about the friend keyword a lot, which is strange. Try prefixing all the names of the classes following a friend declaration with "class". Also, your STL containers are complaining about the way I am accessing elements from the iterators, but this shouldn't be different between VS2010 and g++. Perhaps it is some compiler setting.
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

Haikarainen

  • Guest
SFML Light System - Let There Be Light
« Reply #3 on: December 31, 2011, 11:07:13 pm »
Tested the binary, doesnt work on my system. Shadows is all buggy.

Gore factor works perfectly.

AMD HD6870 Black Edition
AMD Athlon II 645 Quadcore
8GB DDR3 XMS3 RAM
Windows 7 Ultimate x64

Video of the problem; http://www.youtube.com/watch?v=eb_kEywmVF0&feature=youtu.be

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
SFML Light System - Let There Be Light
« Reply #4 on: January 01, 2012, 12:56:08 pm »
Thanks a lot for sharing the code!

I hope you don't mind if I give you some tips, maybe the bugs are related:
  • Your classes should have copy semantics or be noncopyable. In the former case, consider the Rule Of The Big Three and overload copy constructor and assignment operator.
  • I would use std::unique_ptr to store pointers, then there's no need for manual memory management. You seem to use C++11 already (std::unordered_set).
  • Why do you reimplement vector and color classes instead of using the SFML ones? You can easily provide functionality extensions like dot() as global functions, that's the better choice anyway.
  • I'd personally use <cmath> instead of <math.h>, since this is the C++ header. Then, sqrt() becomes std::sqrt(), and there are also the overloads for float/long double and the new math functions like exp2(), erf() etc.
  • You needn't define empty constructors and destructors. I know the IDE does it automatically when creating a class...
  • Instead of if (x) return true; return false; you can write return x; (in the operator== overloads)
Some points are tiny details, and I also see that you have just extracted the code from your project, but maybe some advice still helps you :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

bmn

  • Newbie
  • *
  • Posts: 18
    • View Profile
SFML Light System - Let There Be Light
« Reply #5 on: January 01, 2012, 01:37:27 pm »
I have the same problem as Haikarainen.

AMD HD6950, using latest drivers.

Another problem with ATI/AMD video cards?

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
SFML Light System - Let There Be Light
« Reply #6 on: January 01, 2012, 02:42:37 pm »
Thanks for the tips! The code is pretty messy, since when I was writing it, I was just struggling to get something to work at all. The quad tree has pretty clean code IMO, but the light rendering certainly doesn't. I think it is time that I clean it up some more. It depends too much on my base windowing code, I will try to make it more modular. I like my own vector functions however :). I could make a bunch of functions that do things like dot and cross products on them, but they cannot be members then, so I wouldn't get intellisense in VS2010 and it would involve more typing. And I am very lazy. I never use any functions requiring SFML vectors anyways. Also, this allows my to use my quad tree in non-sfml projects.

The color classes work differently from the SFML ones. The SFML ones use integers, mine use floats, which I find easier to work with in many cases.

I think I know what causes the strange rendering. I can replicate it on my machine by not saving the OpenGL states right before drawing to a render texture. I do not understand why this is necessary though. Saving states is slow anyways, so I am trying to find a way around this. I will let you know when I have fixed it (or at least have a new version to test).
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

Chaia*

  • Newbie
  • *
  • Posts: 21
    • View Profile
SFML Light System - Let There Be Light
« Reply #7 on: January 01, 2012, 03:17:44 pm »
Hey, I got another problem on my system.

If the light is too close to the shape, the shadow won't be correct.
I took some screenshots for comparison.
The red point is the light center.

http://imageshack.us/photo/my-images/849/lettherebelight20120101.jpg/

My System: NV gtx 470, Phenom II 955, Win 7 64 bit

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
SFML Light System - Let There Be Light
« Reply #8 on: January 01, 2012, 03:22:38 pm »
That happens when the light overlaps the hull. Don't forget, the lights are not points, but circles. I can make it detect this case and then simply not draw if you like.
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
SFML Light System - Let There Be Light
« Reply #9 on: January 01, 2012, 05:32:20 pm »
I uploaded a new version. Does this one work for you guys?
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

Anata

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
SFML Light System - Let There Be Light
« Reply #10 on: January 01, 2012, 06:23:31 pm »


some problems when the light is near the polygon

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
SFML Light System - Let There Be Light
« Reply #11 on: January 01, 2012, 06:24:44 pm »
Quote from: "lolz123"
I could make a bunch of functions that do things like dot and cross products on them, but they cannot be members then, so I wouldn't get intellisense in VS2010 and it would involve more typing
You could move the classes to a namespace (actually you should). Or use intelligent tools like Visual Assist X ;)

The advantage of global functions is that you can call them symmetrically with respect to the arguments. Also binary operators like +, -, == etc. should always be global functions to allow implicit conversions of the first argument (and not only of the second).

Quote from: "lolz123"
Also, this allows my to use my quad tree in non-sfml projects.
Good point.

Quote from: "Anata"
The video is private. And please use official Youtube links...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
SFML Light System - Let There Be Light
« Reply #12 on: January 01, 2012, 07:40:57 pm »
@ Anata: That isn't really a bug, it just doesn't render properly when a light is intersecting a hull. The lights are not points here, or else they would not cast a soft shadow. They have a radius, and when the light circle intersects a hull, the algorithm cannot find the right rays anymore, since the penumbra rays will intersect the hull shape. I now have an algorithm that detects if a light is intersecting a shape, and doesn't draw it when this happens, since it is hard to keep lights out of walls in some games (especially when they are attached to the player). It is ever so slightly slower now though :). I will let you know when the new version is complete.
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
SFML Light System - Let There Be Light
« Reply #13 on: January 02, 2012, 12:06:06 am »
I uploaded a new version, in which I made the system far more modular. You should be able to slap it on to any existing SFML application without changing much. It also now allows you to set a flag to determine whether or not it will render shadows that are intersecting hulls. It defaults to not drawing them.

If any of you would like some documentation, let me know.
Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

Haikarainen

  • Guest
SFML Light System - Let There Be Light
« Reply #14 on: January 02, 2012, 04:23:48 am »
Quote from: "lolz123"
I uploaded a new version, in which I made the system far more modular. You should be able to slap it on to any existing SFML application without changing much. It also now allows you to set a flag to determine whether or not it will render shadows that are intersecting hulls. It defaults to not drawing them.

If any of you would like some documentation, let me know.


Demo works for me now!