Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / How do I create simple lightning?
- - By Makki Date 2014-07-02 17:24
I know this might not be specifically directed at gosu, but I've been searching around the webs for a long time now, and I still haven't found a solution to create basic light- and darkening.
The method im particularly interested in is described at 1:28 in this video: https://www.youtube.com/watch?v=0FZIKX1Y_8I , where the lights are rendered onto a lighting mask, which is then rendered to the game by multiplying their colors. Keep in mind that im only asking for lights, without any shadows, to keep it simple.
I've tried doing this in ruby, but the problem occurs when I have to render both the lighting mask and the lights together as one, and it seems that there's no direct way to do it, using gosu?
I was hoping to find another library out there to satisfy my needs, and I've come across TexPlay, Ashton and even RMagick, but the only I found working (Ashton), used a much different technique (or something) that didn't work out for me...
Parent - - By jlnr (dev) Date 2014-07-02 17:28
There have been several discussions about light & shadow in this forum, you should be able to find them using the forum search. I don't think I have ever seen finished code, though. :(
Parent - By Makki Date 2014-07-02 19:41 Edited 2014-07-02 19:50
oh thanks for pointing that out, I just did a search and found some interesting posts regarding this topic.
I also experimented a little more with another gem I installed called Devil: https://github.com/banister/devil , which seems very similar to Ashton and Texplay.
By using this I managed to almost create what I needed, except its poorly optimized and it crashes occasionally... but I was hoping to find a solution to that sooner or later.

I've uploaded a .zip file with the example here: https://www.dropbox.com/s/tql5h17cd6case9/light_source_test.zip

If you have any idea on what might cause the bug/crash, i'd really like to know!

Edit: Oh nevermind this doesn't work at all...
Parent - - By lol_o2 Date 2014-07-04 17:55
This effect is possible with only Gosu, though using different method and with poor effect (but quite efficient): http://www.libgosu.org/cgi-bin/mwf/topic_show.pl?tid=600
I achieved the effect few times, first using TexPlay (very slow), but I got best result with Ashton, using the same mask multiply technique. Does the library work for you or you have errors?
Parent - - By Makki Date 2014-07-07 14:40
Yeah I saw that link you sent me, but I was looking for lighting that renders each pixel individually.
I also tried Ashton once more, and I found that it actually worked quite well, but the lights were lacking some pretty important options e.g. intensity and blur, and I wanted to have a transparent shadow that wasn't all black, which wasn't possible for some reason...

I decided to just give up an move on, but thanks anyway :)
Parent - - By lol_o2 Date 2014-07-07 18:42
Well, the effects you want are possible with Ashton, using WindowBuffer (FrameBuffer) feature.
I'm making a 'framework', which has quite a few useful functions, simple shadow system is among them. Link to topic: -> Topic <- You can check the provided examples, the one called 'Lighting'. You can get it separately from GitHub page. Tell me if something doesn't work.
Transparent shadow you can achieve by drawing an image onto whole mask like normal light. Blurring - using a special blurred light image. And I don't know what you exactly mean by intensity (I understand it as lights with more or less blurring), I could do this with multiple different images, but it may be possible with shaders.
The example I suggested you above is integrated with my system, if you want, I can explain you how to do this and even give some code.
Parent - - By Makki Date 2014-07-11 18:55
I tried looking at the library files in Ashton, but It seemed like the blur in the lights were rendered using another file which was written in C++. Now I can barely understand what happened in the library files, so I quickly gave up on this. But I've just now found that the Light in ashton, had a fifth argument 'luminosity' which does basically what I need with the blur effect (facepalm). So in that case it seems reasonable for me to use Ashton again. Also here are some images illustrating what I was trying to tell in my earlier post: http://imgur.com/a/WZJF3#0

I guess the 'intensity' I'm talking about can be altered by changing the color of the light, right?
Parent - - By lol_o2 Date 2014-07-11 19:59
The light you used is really done a different way, supposed to make advanced lighting with solid surfaces. I've never used it.
And I don't know what you mean by your 'intensity' but the one mentioned in your images seems to affect only color. You could achieve the same effect by making gray light instead of white, but I don't know if Ashton lighting thing supports colored lights.
Parent - - By Makki Date 2014-07-13 12:43
Yeah thats what I said about intensity earlier.
But Ashton seems really weird to me, there are a lot of things that "doesn't work the way they should" in my opinion.
I also looked at your framework Sapphire, but as I'm still inexperienced in programming, I dont really understand much of it. The thing I was looking at specifically was the lighting system that you used, but how does that work? Do you also use Ashton for your lighting or do you use something else? and when you create a new light for example: @light=Light.new(@x, @y, 250, 0xff0080ff, 8) , where can I find the class Light ?
Parent - - By lol_o2 Date 2014-07-14 18:43
The Sapphire is designated to provide useful methods to make programming as simple as possible. The code you shouldn't change isn't even commented and it's not made to be understood easily.
I use Ashton and since you are inexperienced and I'm quite helpful, I can explain you how does this work.

First you need:
-some game state/Gosu window with update and draw methods (obviously)
-an image for light (white circle on black background)(you can get it from 'data/core' directory in Sapphire example)
-and eventually a completely black or white image with size of your window.

In state's/window's initialize method, add these:
@lights=[] #an array to store lights
@darkness=Ashton::WindowBuffer.new #a canvas that you can draw on, used for the effect


You have to fill light's array with something in state's/window's update method. In my way, it's a temporary array, that is filled and cleared each frame. It should contain data for lights: position, radius, color, blur, whatever. You can put it into array, like [x,y,radius,color]. Light's position should be calculated to skip lights outside screen etc.
The Light class in my example is to fill the array with it's values

And in draw method:
@darkness.render do #start drawin on your canvas
##you need to clear the canvas each frame. For this one, you can either draw the full-black image or use a draw_quad method
##you need now to use each light to draw the prepared light image. Remember to draw it with :additive mode

You have to iterate through all lights, like this:
light_image.draw_rot(light_x, light_y, 0, 0, 0.5, 0.5, radius, radius, light_color, :additive) #add the values yourself
end


Then:
@lights.clear #clear the array to recalculate it later.

The above should be put in the beginning of draw method to avoid bugs in Ashton.

And after you done all your image drawing, draw the shadow over everything:
@darkness.draw(0, 0, darkness_z_order, :mode=>:multiply)

Some notes/advices:
-light image from Sapphire example contains multiple blur versions of light. You can achieve this effect also with shader, but you need to make one (well, I will, later)
-the full-white image I mentioned can be used as a light for half-dark shadows. Draw it :additive and gray
-there are two ways to organise code: one class per file and multiply classes per file (the one I use). If you use the second way, CTRL + F is your good friend

Hope you understood.
Parent - By Makki Date 2014-07-18 12:57
Thanks for explaining that for me!
I used your information to create some basic lighting and it works the way it should :)
- - By lol_o2 Date 2014-07-19 21:48
To ultimately close the subject, I present a 100%-working, portable, easy-to-use, well-explained code to download here: Download
It contains the code for lighting and an example. There's also a dedicated shader for better blurring.
All you need to do is to copy simple_lighting directory to your project and load the provided code. Everything is explained in readme.
Of course, it requires ashton.
Parent - By RunnerPack Date 2014-07-20 23:24
Wow, nice work! I haven't looked at the code, but if it's as easy to integrate as it says in the readme, I definitely have some uses for this! Thanks for sharing.
Up Topic Gosu / Gosu Exchange / How do I create simple lightning?

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill