title: What is wrong with my Quaternion rotation in OpenGL?
body: <p>Going off notes on the internet helps construct quaternion classes properly. My problem is putting them to practical use and using them for rotations in raw OpenGL.</p>
  
  <p>In my input events I have the following:</p>
  
  <pre><code>Quaternion&lt;float&gt; qPrev = qRot;
  qRot = qRot.UnitQuaternion(); // #1.
  Quaternion&lt;float&gt; axis(5., 0., 1., 0.);
  qRot = qPrev*axis;
  qRot *= qPrev.conjugate();
  //#2. qRot = qRot.UnitQuaternion();
  </code></pre>
  
  <p>If I use #1 make the rotation result unit it rotates fine for a few seconds, speeds up and then vanishes completely . </p>
  
  <p>If I use #2 for a unit result the box "wobbles" and never rotates. </p>
  
  <p>Alternatively, I've used this based off other implementations:</p>
  
  <pre><code>Quaternion&lt;float&gt; qPrev = qRot;
  Quaternion&lt;float&gt; axis(-5./100, 0., 1., 0.); // #3.
  axis = axis.UnitQuaternion();
  qRot = qPrev*axis;
  // #4. qRot *= qPrev.conjugate();
  </code></pre>
  
  <p>Where #3. makes the most sense, taking the unit of a non-unit quat and multiplying it initially with the identity quat; keeping everything in unit-quats.</p>
  
  <p>Where #4. tried multiplying the conjugate based off my understanding of the equation's rotation definition. </p>
  
  <p>All of these produce a small wobble and #1 is the closest I got; the box rotates, then quickly, then vanishes.</p>
  
  <hr>
  
  <p>My understanding is that I have an axis I which to rotate around where w = how much (angle in radians). I simply multiply by the orientation and multiply that result by the negative of the orientation; e.g. the conjugate. </p>
  
  <hr>
  
  <p>Rendering code (this could be the culprit):</p>
  
  <pre><code>// Apply some transformations
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0, 0, -100.f);
  glRotatef(qRot.x, 1., 0, 0);
  glRotatef(qRot.y, 0, 1., 0);
  glRotatef(qRot.z, 0, 0, 1.);
  
  // Draw the cube
  glDrawArrays(GL_TRIANGLES, 0, 36);
  
  // Draw some text on top of our OpenGL object
  window.pushGLStates();
  </code></pre>
  
  <hr>
  
  <p>I appreciate the help</p>
  
  <hr>
  
  <p><strong>Edit: Many thanks to @ybungalobill for helping out</strong>.</p>
  
  <p>What I wanted from my quaternion is a very specific application in terms of quaternion math. I wanted an axis of rotation while in my example I'm creating a quaternion with the direct values. This is not how the rotation matrix is derived. I needed more steps:</p>
  
  <pre><code>void FromAxisAngle(_Tp theta, _Tp x, _Tp y, _Tp z)
  {
      //Angle should be in radians
      this-&gt;w = cos(theta/2);
  
      //Axes
      this-&gt;x = x * sin(theta/2);
      this-&gt;y = y * sin(theta/2);
      this-&gt;z = z * sin(theta/2);
      //Normalize
      *this = this-&gt;UnitQuaternion();
  }
  </code></pre>
  
  <p>Again, getting the rotation matrix is a very specific application with very specific steps. Quaternions don't do this 'out of the box'.</p>
  
  <p>Some minor changes to the rotation logic as well:</p>
  
  <pre><code>   Quaternion&lt;float&gt; qPrev = qRot;
     Quaternion&lt;float&gt; axis;
     axis.FromAxisAngle(-5./100, 1., 0., 0.);
     qRot = qPrev*axis;
  </code></pre>
  
  <p>So now I've created a mathematically correct quaternion FROM my rotation axis. Then multiply. </p>
  
  <p>Finally, the last thing I had to do was create a matrix from my quaternion that OpenGL could use. So we have more math to do in order to get it in a notation that rotates things:</p>
  
  <pre><code>void GetMatrix(_Tp matrix[16])
  {
     Quaternion&lt;_Tp&gt; temp(this-&gt;UnitQuaternion());
    _Tp qw = temp.w;
    _Tp qx = temp.x;
    _Tp qy = temp.y;
    _Tp qz = temp.z;
  
    matrix[0] = 1.0f - 2.0f*qy*qy - 2.0f*qz*qz;
    matrix[1] = 2.0f*qx*qy - 2.0f*qz*qw;
    matrix[2] = 2.0f*qx*qz + 2.0f*qy*qw;
    matrix[3] = 0;
  
    matrix[4] = 2.0f*qx*qy + 2.0f*qz*qw;
    matrix[5] = 1.0f - 2.0f*qx*qx - 2.0f*qz*qz;
    matrix[6] = 2.0f*qy*qz - 2.0f*qx*qw;
    matrix[7] = 0;
  
    matrix[8] = 2.0f*qx*qz - 2.0f*qy*qw;
    matrix[9] = 2.0f*qy*qz + 2.0f*qx*qw;
    matrix[10] = 1.0f - 2.0f*qx*qx - 2.0f*qy*qy;
    matrix[11] = 0;
  
    matrix[12] = 0;
    matrix[13] = 0;
    matrix[14] = 0;
    matrix[15] = 1;
  }
  </code></pre>
  
  <p>Using <code>glMultMatrix</code> the cube rotates.</p>