title: How to use spot light in opengl
body: <p>I am trying to learn how to use a spot light in OpenGL.  I wish to shine a spot (torch) light from a point marked (lighpos), and shown as grey dot, onto the side of a GLUT teapot centered at (possph) in 3D space.  I have attached an example where I have tried to do this but I cannot get the light to focus on the teapot.  I am expecting a almost touch light focus based on the parameters I have tried to use.</p>
  
  <p>Could someone point out what I have missed / mistake I have made.  </p>
  
  <p>Thanks</p>
  
  <p>Stuart</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;GL/glut.h&gt;
  #include &lt;stdio.h&gt;
  
  GLfloat lighpos[] = { -300., 200., 250., 1.0 }; // Location of light
  GLfloat possph[] = { -50., 350., 150. }; // Position of teapot
  GLfloat ligdir[] = { 250., 150., -100. }; // Direction from light to teapot
  
  void init(void) {
      glClearColor(0.0, 0.0, 0.0, 0.0);
      glShadeModel(GL_SMOOTH);
      glEnable(GL_COLOR_MATERIAL);
      glEnable(GL_DEPTH_TEST);
  
      glLightfv(GL_LIGHT0, GL_POSITION, lighpos);
      glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, ligdir);
      glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 10.0);
  
      GLfloat ambientLight0[] = { 0.25, 0.25, 0.25, 1.0 };
      glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight0);
      glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight0);
  
      GLfloat diffuseLight0[] = { 1.0, 1.0, 1.0, 1.0 };
      glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight0);
  
      GLfloat specularLight0[] = { 1.0, 1.0, 1.0, 1.0 };
      glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight0);
  
      glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 128.0f);
      glEnable(GL_LIGHTING);
      glEnable(GL_LIGHT0);
  }
  
  void drawAxis() {
      glBegin(GL_LINES);
      glColor3f(1.0, 0.0, 0.0);  glVertex3f(-500., 0.0, 0.0);  glVertex3f(500., 0.0, 0.0);
      glColor3f(0.0, 1.0, 0.0);  glVertex3f(0.0, -500., 0.0);  glVertex3f(0.0, 500., 0.0);
      glColor3f(0.0, 0.0, 1.0);  glVertex3f(0.0, 0.0, -500.);  glVertex3f(0.0, 0.0, 500.);
      glEnd();
  }
  
  void display(void) {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
      drawAxis();
      glPushMatrix();
      glPointSize(10);
      glColor3f(1.0, 1.0, 1.0);
      glBegin(GL_POINTS);
      glVertex3d(lighpos[0], lighpos[1], lighpos[2]);
      glEnd();
      glPopMatrix();
  
      glColor3f(1., 0., 0.); 
      glPushMatrix();
      glTranslated(possph[0], possph[1], possph[2]);
      glutSolidTeapot(100.);
      glPopMatrix();
      glutSwapBuffers();
  }
  
  void reshape(int w, int h) {
      glViewport(0, 0, (GLsizei)w, (GLsizei)h);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glFrustum(-1.0, 1.0, -1.0, 1.0, 4, 3000.0);
  
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      gluLookAt(1500.0, 1500.0, 1500.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  }
  
  
  int main(int argc, char** argv) {
      glutInit(&amp;argc, argv);
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);   
      glutInitWindowPosition(0.0, 0.0);   
      glutInitWindowSize(800, 800);     
      glutCreateWindow("spotlight"); 
      init();
      glutDisplayFunc(display);
      glutReshapeFunc(reshape);               
      glutMainLoop();
      return 0;
  }
  </code></pre>