Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This question already has an answer here:

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:

http://media.giantbomb.com/uploads/0/276/1414275-light_large.png

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..

share|improve this question

marked as duplicate by Sean Middleditch, Trevor Powell, bummzack, Laurent Couvidou, Josh Petrie Feb 13 '13 at 16:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

2 Answers 2

up vote 0 down vote accepted

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();
}

// The fragment shader

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 :)

share|improve this answer
    
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.. –  gopgop 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
add comment

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...

share|improve this answer
    
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.. –  gopgop Sep 14 '12 at 17:22
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.