Jump to content
  • Advertisement
Sign in to follow this  


This topic is 4899 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

I use this code to make a spotlight point in the middle of a rectangle, the problem is that the light doesn't work. Can anybody help me? Tnx
GLfloat diffuseLight[] = {0.8, 0.8, 0.8, 1.0};
GLfloat specularLight[] = {1.0, 1.0, 1.0, 1.0};
GLfloat ambientLight[] = {0.2, 0.2, 0.2, 1.0};
GLfloat lightPos[] = {0.0, 0.0, 5.0, 1.0};
GLfloat specularReflection[] = {1.0, 1.0, 1.0, 1.0};
GLfloat spotDir[] = {0.0, 0.0, -30.0};

glTranslatef(-20, -20, -30);
glColor4f(1, 1, 1, 1);
     glTexCoord2i(0, 1);     glVertex3i(40, 0, 0);   // Top right
     glTexCoord2i(1, 1);     glVertex3i(0, 0, 0);   // Top left
     glTexCoord2i(0, 0);     glVertex3i(40, 40, 0);   // Bottom right
     glTexCoord2i(1, 0);     glVertex3f(0, 40, 0);   // Bottom left

      glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
 glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);

       glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
        glMaterialfv(GL_FRONT, GL_SPECULAR, specularReflection);
        glMateriali(GL_FRONT, GL_SHININESS, 128);

       glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDir);
       glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0f);
      glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 2.0f);

     glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.0f);
        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);

Share this post

Link to post
Share on other sites
That code is in order? As in, you draw the rectangle first and then enable lighting? If yes, you need to have lighting enabled before you draw something you want lighting on.

You'll also most likely need more than 4 vertices to get a decent looking spotlight effect (unless you're doing per-pixel lighting, which you don't appear to be doing) because lighting is done per-vertex, and then the color values calculated at each vertex are interpolated between them for each pixel. So if no light is reaching any of the vertices (maybe the spotlight is too close to the rectangle) then it will interpolate black over the whole rectangle and it will appear to not be working.

Share this post

Link to post
Share on other sites
Same site, in the menu on the left there is a download. There you can downlaod the source of the tutorials, with a simple opengl framework too. Here if your lazy.


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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!