title: How to rotate object around local axis in OpenGL?
body: <p>I am working on an ongoing project where I want to align the links of a chain so that it follows the contours of a Bezier curve. I am currently following the steps below.</p>
  
  <ol>
  <li>Drawing the curve.</li>
  <li>Use a display list to create one link of the chain.</li>
  <li>Use a FOR loop to repeatedly call a function that calculates the angle between two points on the curve, returns the angle and the axis around which the link should be rotated.</li>
  <li>Rotate by the angle "a" and translate to new position, place the link at the new position.</li>
  </ol>
  
  <p>Edit: I should also say that the centres of the two half torus must lie on the Bezier curve.
  Also I am aware that the method I use to draw the torus I tedious, I will use TRIANGLE_FAN or QUAD_STRIP later on to draw the torus in a more efficient way.</p>
  
  <p>While at first glance this logic looks like it would render the chain properly, the end result is not what I had imagined it to be. Here is a picture of what the chain looks like.</p>
  
  <p><img src="https://i.stack.imgur.com/LsqHe.jpg" alt="gold_chain"></p>
  
  <p>I read that you have to translate the object to the origin before rotation? Would I just call glTranslate(0,0,0) and then follow step 4 from above?</p>
  
  <p>I have included the relevant code from what I have done so far, I would appreciate any suggestions to get me code work properly.</p>
  
  <pre><code>/* this function calculates the angle between two vectors oldPoint and new point contain the x,y,z coordinates of the two points,axisOfRot is used to return the x,y,z coordinates of the rotation axis*/
  double getAxisAngle(pointType oldPoint[],
  pointType newPoint[],pointType axisOfRot[]){
  
  float tmpPoint[3];   
  
  float normA = 0.0,normB = 0.0,AB = 0.0,angle=0.0;
  int i;
  
  axisOfRot-&gt;x= oldPoint-&gt;y * newPoint-&gt;z - oldPoint-&gt;z * newPoint-&gt;y;
  axisOfRot-&gt;y= oldPoint-&gt;z * newPoint-&gt;x - oldPoint-&gt;x * newPoint-&gt;z;
  axisOfRot-&gt;z= oldPoint-&gt;x * newPoint-&gt;y - oldPoint-&gt;y * newPoint-&gt;x; 
  
  
  normA=sqrt(oldPoint-&gt;x * oldPoint-&gt;x + oldPoint-&gt;y * oldPoint-&gt;y + oldPoint-&gt;z * 
  oldPoint-&gt;z);
  normB=sqrt(newPoint-&gt;x * newPoint-&gt;x + newPoint-&gt;y * newPoint-&gt;y + newPoint-&gt;z *    
  newPoint-&gt;z);
  
  tmpPoint[0] =  oldPoint-&gt;x * newPoint-&gt;x;
  tmpPoint[1] =  oldPoint-&gt;y * newPoint-&gt;y;
  tmpPoint[2] =  oldPoint-&gt;z * newPoint-&gt;z;   
  
  for(i=0;i&lt;=2;i++)
   AB+=tmpPoint[i];  
  
  AB /= (normA * normB);  
  
  
      return angle = (180/PI)*acos(AB);  
   }
  
   /* this function calculates and returns the next point on the curve give the 4 initial points for the curve, t is the tension of the curve */
      void bezierInterpolation(float t,pointType cPoints[],
      pointType newPoint[]){
  
  newPoint-&gt;x = pow(1 - t, 3) * cPoints[0].x +3 * pow(1 - t , 2) * t * cPoints[1].x + 3 
  * pow(1 - t, 1) * pow(t, 2) * cPoints[2].x + pow(t, 3) * cPoints[3].x;
  
  newPoint-&gt;y = pow(1 - t, 3) * cPoints[0].y +3 * pow(1 - t , 2) * t * cPoints[1].y + 3 
  * pow(1 - t, 1) * pow(t, 2) * cPoints[2].y + pow(t, 3) * cPoints[3].y;
  
  newPoint-&gt;z = pow(1 - t, 3) * cPoints[0].z +3 * pow(1 - t , 2) * t * cPoints[1].z + 3 
  * pow(1 - t, 1) * pow(t, 2) * cPoints[2].z + pow(t, 3) * cPoints[3].z;
  
  }
  
  /* the two lists below are used to create a single link in a chain, I realize that creating a half torus using cylinders is a bad idea, I will use GL_STRIP or TRIANGLE_FAN once I get the alignment right 
  */
  torusList=glGenLists(1);   
  
  glNewList(torusList,GL_COMPILE);
  for (i=0; i&lt;=180; i++)
  {
    degInRad = i*DEG2RAD;
    glPushMatrix();
    glTranslatef(cos(degInRad)*radius,sin(degInRad)*radius,0);
    glRotated(90,1,0,0);
    gluCylinder(quadric,Diameter/2,Diameter/2,Height/5,10,10);
    glPopMatrix();      
  }
  
   glEndList();
  
      /*! create a list for the link , 2 half torus and 2 columns */
  
   linkList = glGenLists(1);
  
   glNewList(linkList, GL_COMPILE); 
   glPushMatrix();
   glCallList(torusList); 
   glRotatef(90,1,0,0);
   glTranslatef(radius,0,0);
   gluCylinder(quadric, Diameter/2, Diameter/2, Height,10,10); 
   glTranslatef(-(radius*2),0,0);
   gluCylinder(quadric, Diameter/2, Diameter/2, Height,10,10); 
   glTranslatef(radius,0, Height);
   glRotatef(90,1,0,0);
   glCallList(torusList); 
   glPopMatrix();
   glEndList();
  </code></pre>
  
  <p>Finally here is the code for creating the three links in the chain</p>
  
  <pre><code>t=0.031; 
  bezierInterpolation(t,cPoints,newPoint);  
  a=getAxisAngle(oldPoint,newPoint,axisOfRot);
   glTranslatef(newPoint-&gt;x,newPoint-&gt;y,newPoint-&gt;z); 
     glRotatef(a,axisOfRot-&gt;x,axisOfRot-&gt;y,axisOfRot-&gt;z);     
      glCallList(DLid);
     glRotatef(-a,axisOfRot-&gt;x,axisOfRot-&gt;y,axisOfRot-&gt;z); 
   glTranslatef(-newPoint-&gt;x,-newPoint-&gt;y,-newPoint-&gt;z);  
  
  oldPoint[0]=newPoint[0];
  bezierInterpolation(t+=GAP,cPoints,newPoint);  
  a=getAxisAngle(oldPoint,newPoint,axisOfRot);
   glTranslatef(newPoint-&gt;x,newPoint-&gt;y,newPoint-&gt;z); 
    glRotatef(90,0,1,0);
     glRotatef(a,axisOfRot-&gt;x,axisOfRot-&gt;y,axisOfRot-&gt;z);     
      glCallList(DLid);
     glRotatef(-a,axisOfRot-&gt;x,axisOfRot-&gt;y,axisOfRot-&gt;z); 
    glRotatef(90,0,1,0);  
   glTranslatef(-newPoint-&gt;x,-newPoint-&gt;y,-newPoint-&gt;z);  
  
   oldPoint[0]=newPoint[0];
   bezierInterpolation(t+=GAP,cPoints,newPoint);    
    a=getAxisAngle(oldPoint,newPoint,axisOfRot); 
     glTranslatef(newPoint-&gt;x,newPoint-&gt;y,newPoint-&gt;z);       
      glRotatef(-a,axisOfRot-&gt;x,axisOfRot-&gt;y,axisOfRot-&gt;z); 
       glCallList(DLid);
      glRotatef(a,axisOfRot-&gt;x,axisOfRot-&gt;y,axisOfRot-&gt;z); 
     glTranslatef(-newPoint-&gt;x,-newPoint-&gt;y,newPoint-&gt;z); 
  </code></pre>