Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » allegro 5 - 2d lightning - opengl

This thread is locked; no one can reply to it. rss feed Print
allegro 5 - 2d lightning - opengl
davidemo89
Member #14,215
April 2012

Hello everyone.
I'm trying to add a lightning system in my little 2d game. I don't have problem with math. I have the radius where it should be lighten but I don't know how to make the part darker and lighten where I want. I was thinking about a mask but... for now no luck :-(

yeah search in Google blah blah, I whave tried for three long weeks before opening a topic here. I'm really desperate.

Any suggestion with a little example or a link worth an article I can read?

thank you everyone for the help and sorry for my bad English

jmasterx
Member #11,410
October 2009

Have you considered doing it with shaders and glsl?
http://www.youtube.com/watch?v=fsbECSpwtig

davidemo89
Member #14,215
April 2012

ok it's impressive. Now I have to look to some article on how to implement this.
if you have a link can you share it with me?
searching now thank you a lot.

I have found this
www.opengl.org/documentation/glsl/

I think it's ok to start

I'm having big big big difficult making something. Any help or code example? IThe only thing I did was a easy tint

Edit: NEWS! I've added successfully a blur effect

jmasterx
Member #11,410
October 2009

Although it's for 3D, there is some useful info here http://www.lighthouse3d.com/tutorials/glsl-tutorial/?pipeline

More good info here:
http://www.opengl.org/wiki/GLSL

The idea of a fragment shader is that each time you go to draw something with it on,
(a bitmap for example) it will run your program for each pixel of the fragment. It is then up to you to decide if the color should change.

This was the code for the light in that YouTube video I showed you:

#SelectExpand
1uniform vec2 lightpos; 2uniform vec3 lightColor; 3uniform float screenHeight; 4uniform vec3 lightAttenuation; 5uniform float radius; 6 7uniform sampler2D texture; 8 9void main() 10{ 11 vec2 pixel=gl_FragCoord.xy; 12 pixel.y=screenHeight-pixel.y; 13 vec2 aux=lightpos-pixel; 14 float distance=length(aux); 15 float attenuation=1.0/(lightAttenuation.x+lightAttenuation.y*distance+lightAttenuation.z*distance*distance); 16 vec4 color=vec4(attenuation,attenuation,attenuation,1.0)*vec4(lightColor,1.0); 17 gl_FragColor = color;//*texture2D(texture,gl_TexCoord[0].st); 18}

The uniform variables can be modified by your program using an API.
http://www.opengl.org/wiki/GLSL_Uniform#Uniform_management

Next, he makes a vec2 (a Point if you will) that corresponds to the pixel Y value on the screen.

A formula is then used to figure out the color of that pixel and the pixel is set.

At that point it is just math and you just need to understand lighting math.

As a simple example, if I wanted to create a spotlight that emitted from pixel A with a radius of 20 px, then I would simply get the distance between pixel A and B and would multiply the color of B by a brightness factor determined by how close I am.

So if I'm 30 px away, I multiply it by 0. If I'm 1 pix away, I might multiply it by 3 making the pixel 3 times as bright.

If you want omniscient light, you can just make it so the minimum multiplier is something like 0.5 .

davidemo89
Member #14,215
April 2012

I love you.
If I have other question can I ask you? Thank you a lot =D

jmasterx
Member #11,410
October 2009

Sure :) Glad it helps.

Go to: