Sign in to follow this  
MACnus

SpotLight

Recommended Posts

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


glPushMatrix();
glTranslatef(-20, -20, -30);
glColor4f(1, 1, 1, 1);
glBegin( GL_TRIANGLE_STRIP );
     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
glEnd();
glPopMatrix();


glEnable(GL_LIGHTING);
      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_CONSTANT_ATTENUATION, 2.0f);
     glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.0f);
       glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0);
       
        glEnable(GL_LIGHT0);
    
        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
   glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
       glEnable(GL_NORMALIZE);

Share this post


Link to post
Share on other sites
Kalidor    1087
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

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