Jump to content
  • Advertisement
Sign in to follow this  

How to add a spotlight on a 2D torch?

This topic is 1247 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
    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

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 





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

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!