Sign in to follow this  
shinypixel

How to add a spotlight on a 2D torch?

Recommended Posts

shinypixel    226

Well, I'm trying to add a small yellow spotlight to my torch. I'm a little lost on how to do it.

 

Basically, what I'm trying to do is add a yellow "ball of light" at the center of the torch flames (left image).

 

This is the code that produced the image on the right, but it's not giving the right result. Any thoughts on the code? If it matters, my gluOrtho2D function is set as (0,w,h,0). Thanks.

    GLfloat lightPos[] = { 640.0f, 640.0f, 400.0f, 1.0f };   // 1280x800 screen resolution
    GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
    GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    GLfloat ambientLight[] = { 0.5f, 0.5f, 0.5f, 1.0f};
    GLfloat spotDir[] = { 0.0f, 0.0f, -1.0f };

    glEnable(GL_LIGHTING);
    
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
    glLightfv(GL_LIGHT0,GL_DIFFUSE,ambientLight);
    glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
    glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
    glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,60.0f);
    glEnable(GL_LIGHT0);
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
    glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
    glMateriali(GL_FRONT, GL_SHININESS,128);
Edited by shinypixel

Share this post


Link to post
Share on other sites
WiredCat    1451

spotlight is a directional light that has direction you are not setting, anyway i suggest you use shaders for this instead of unused gl_lighting, and i believe you are talking about 

this:

 

ppatten1.png

 

which is very simple shader

 

all you need is to pass light and vertex position (vertex pos from vertex shader)  to fragment/pixel shader then you just define the radius of the light.

 

For actual spotlight i believe theres a simplier way to do it than my solution which i tell that is: get the light projection matrix, binding a round circle as a light texture and projecting it onto a scene, while rendering scene from cam view. this is a projective texture mapping indeed but it can be used as a spotlight, however there should be simplier way like with the point light with just using cone equation

Share this post


Link to post
Share on other sites
tonemgub    2008

You can't use glLight in 2D to get the real spotlight effect as shown by WireCat. However, if you still want to try it out, you have to specify the position of the light in homogeneous object coordinates. You can also try setting the modelview matrix(es) to the identity matrix, then use (0, 0, 0, 1) as the light's position. What you used in your example are screen coordinates, so when they are re-interpreted as homogenous coordinates, they end up way outside the viewport, so no lighting is applied. Just mess around with the light's position (keep x and y at 0, and change z to any value in the range [-1,1].), and the ligh's cutoff value until something happens... (0, 0, 0) is the center of the homogenous coordinate space in OpenGL/WebGL, IIRC.

Edited by tonemgub

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this