title: How to rotate a point around an arbitrary axis?
body: <p>I want to rotate a point in OpenGL around an arbitrary axis. I want to utilize that to rotate a sphere.</p>
  
  <p>This is what I got so far:</p>
  
  <pre><code>float degreeBetweenTwoVec(glm::vec3 &amp;a, glm::vec3 b)
  {
      float prod = b.x * a.x + b.y * a.y + b.z * a.z;
      float mag_axis = sqrt((b.x * b.x) + (b.y * b.y) + (b.z * b.z));
      float mag_vec = sqrt((a.x * a.x) + (a.y * a.y) + (a.z * a.z));
      float degree = prod / (mag_axis * mag_vec);
  
      return acos(degree) * 180.0 / PI;;
  }
  </code></pre>
  
  <pre><code>void rotAroundZ(glm::vec3 &amp;point, float degree) 
  {
      glm::vec3 n_point;
  
      n_point.x = (point.x * cos(degree * PI / 180.0)) - (point.y * sin(degree * PI / 180.0));
      n_point.y = (point.x * sin(degree * PI / 180.0)) + (point.y * cos(degree * PI / 180.0));
      n_point.z = point.z;
  
      point.x = n_point.x;
      point.y = n_point.y;
      point.z = n_point.z;
  }
  </code></pre>
  
  <pre><code>void rotAroundY(glm::vec3&amp; point, float degree)
  {
      glm::vec3 n_point;
  
      n_point.x = (point.x * cos(degree * PI / 180.0)) + (point.z * sin(degree * PI / 180.0));
      n_point.y = point.y;
      n_point.z = ((point.x * -1.0f) * sin(degree * PI / 180.0)) + (point.z * cos(degree * PI / 180.0));;
  
      point.x = n_point.x;
      point.y = n_point.y;
      point.z = n_point.z;
  }
  </code></pre>
  
  <pre><code>void rotAroundA(glm::vec3&amp; point, glm::vec3 &amp;axis, float zdegree)
  {
      float xdegree = degreeBetweenTwoVec(axis, glm::vec3{ 1.0f, 0.0f, 0.0f });
      float ydegree = degreeBetweenTwoVec(axis, glm::vec3{ 0.0f, 1.0f, 0.0f });
  
      rotAroundZ(point,  xdegree);
      rotAroundY(point,  ydegree);
      rotAroundZ(point,  zdegree);
      rotAroundY(point, -ydegree);
      rotAroundZ(point, -xdegree);
  }
  </code></pre>
  
  <pre><code>void rotAObject(Object&amp; obj, glm::vec3 &amp;axis, float degree)
  {
      axis = glm::normalize(axis);
      translate(axis, glm::vec3{ axis.x, axis.y, axis.z });
      for (int i = 0; i &lt; obj.vertices.size(); i++)
      {
          rotAroundA(obj.vertices[i], axis, degree);
      }
      rotAroundA(obj.mp, axis, degree);
      translate(axis, glm::vec3{ axis.x * -1.0f, axis.y * -1.0f, axis.z * -1.0f });
  }
  </code></pre>
  
  <p>This works just fine if the given axis happens to be on one of the global axis. However, if it isn't and the given axis is basiclly rotating around something else. There is some kind of axis it is rotating around but as soon as change the given axis, for example rotating it around the z axis it rotates around a completlly different axis than before. It looks like for every position the given axis can take there is some other axis the object is actually rotating around.</p>
  
  <p>Any help is appreciated!</p>