# OpenGL Lighting [duplicate]

I have a simple day and night cycle by at day disabling OpenGL lighting and at night enabling openGL Lighting. When I enable everything appears darker. My question is How would I make it that at a specific spot there would be a light that will only light up its surrounding area for example:

Where the light is is where I want to position my light. My application is in 2D.

EDIT:

I read up more about 2D Lighting and I thought about it how would I do it so that I have a QUAD that is over all the screen and has a certain opacity and then i draw s circle on a QUAD that is yellow and it overwrites the quad so that the images that are drawn behind it aren't effected, like poking a hole in it..

• Just add a point or spotlight. – rasmus Sep 13 '12 at 23:20
• How? :P I cant find how to do so anywhere.. – Amit Assaraf Sep 14 '12 at 17:43

I would use a 2D lightmap and blend it using GL_DST_COLOR,GL_ZERO and playing on opacity.

If you want a more complex thing look for projected texture using a shader :

// The vertex shader

varying vec4 pos;

void main(void){
pos=vec4(gl_Vertex);
gl_Position=ftransform();
}

uniform sampler2D inputImage;
varying vec4 pos;

void main(void){
gl_FragColor=texture2DProj(inputImage,gl_TextureMatrix[0]*pos);
}


Another solution is by using 3D lightmapping (if 3D textures are available on your platform), wich is also easy to implement.

I hope it helps :)

• I read up more about 2D Lighting and I thought about it how would I do it so that I have a QUAD that is over all the screen and has a certain opacity and then i draw s circle on a QUAD that is yellow and it overwrites the quad so that the images that are drawn behind it aren't effected, like poking a hole in it.. – Amit Assaraf Sep 14 '12 at 17:20
• @gopgop: That is basically exactly what he just told you to do in his first suggestion. Since it seems you may have found your own answer, it is generally helpful to post that answer and accept it (so anyone else searching for this same problem can see your solution). – Sean Middleditch Oct 15 '12 at 1:15

Another option is to perform per-pixel lighting in your pixel shaders. If you calculate vector L to light (in screenspace) for each fragment (and supposing fragment normal N is (0,0,1) in screenspace ) then modulate fragment color by some f(N, L). If you prefer some fancy shading model, you're free to implement one.

This solutions is not dependent on some texture resolution and is always smooth, but requires either additional post-process pass, or using this piece of shader to color each object with spot on it...

• I read up more about 2D Lighting and I thought about it how would I do it so that I have a QUAD that is over all the screen and has a certain opacity and then i draw s circle on a QUAD that is yellow and it overwrites the quad so that the images that are drawn behind it aren't effected, like poking a hole in it.. – Amit Assaraf Sep 14 '12 at 17:22