title: OpenGL Rotation from Velocity Vector
body: <p>This should be easy, but I've been all over trying to find a simple explanation that I can grasp.  I have an object that I'd like to represent in OpenGL as a cone.  The object has x, y, z coordinates and a velocity vector vx, vy, and vz.  The cone should point in the direction of the velocity vector.</p>
  
  <p>So, I think my PyOpenGL code should look something like this:</p>
  
  <pre><code>glPushMatrix()
  glTranslate(x, y, z)
  glPushMatrix()
  
  # do some sort of rotation here #
  
  glutSolidCone(base, height, slices, stacks)
  glPopMatrix()
  glPopMatrix()
  </code></pre>
  
  <p>So, is that correct (so far)?  What do I put in place of the "# do some sort of rotation here #" ?</p>
  
  <p>In my world, the Z-axis points up (0, 0, 1) and, without any rotations, so does my cone.</p>
  
  <hr>
  
  <p>Okay, Reto Koradi's answer seems to be the approach that I should take, but I'm not sure of some of the implementation details and my code is not working.</p>
  
  <p>If I understand correctly, the rotation matrix should be a 4x4.  Reto shows me how to get a 3x3, so I'm assuming that the 3x3 should be the upper-left corner of a 4x4 identity matrix.  Here's my code:</p>
  
  <pre><code>import numpy as np
  
  def normalize(v):
      norm = np.linalg.norm(v)
      if norm &gt; 1.0e-8:  # arbitrarily small
          return v/norm
      else:
          return v
  
  def transform(v):
      bz = normalize(v)
      if (abs(v[2]) &lt; abs(v[0])) and (abs(v[2]) &lt; abs(v[1])):
          by = normalize(np.array([v[1], -v[0], 0]))
      else:
          by = normalize(np.array([v[2], 0, -v[0]]))
          #~ by = normalize(np.array([0, v[2], -v[1]]))
  
      bx = np.cross(by, bz)
      R =  np.array([[bx[0], by[0], bz[0], 0],
                     [bx[1], by[1], bz[1], 0],
                     [bx[2], by[2], bz[2], 0],
                     [0,     0,     0,     1]], dtype=np.float32)
  
      return R
  </code></pre>
  
  <p>and here is the way it gets inserted into the rendering code:</p>
  
  <pre><code>glPushMatrix()
  glTranslate(x, y, z)
  glPushMatrix()
  
  v = np.array([vx, vy, vz])
  glMultMatrixf(transform(v))
  
  glutSolidCone(base, height, slices, stacks)
  glPopMatrix()
  glPopMatrix()
  </code></pre>
  
  <p>Unfortunately, this isn't working.  My test case cones just do not point correctly and I can't identify the failure mode.  Without the "glutMultMatrixf(transform(v)" line, the cones align along the z-axis, as expected.</p>
  
  <hr>
  
  <p>It's working.  Reto Koradi correctly identified that the rotation matrix needed to be transposed in order to match the column order of OpenGL.  The code should look like this (before optimization):</p>
  
  <pre><code>def transform(v):
      bz = normalize(v)
      if (abs(v[2]) &lt; abs(v[0])) and (abs(v[2]) &lt; abs(v[1])):
          by = normalize(np.array([v[1], -v[0], 0]))
      else:
          by = normalize(np.array([v[2], 0, -v[0]]))
          #~ by = normalize(np.array([0, v[2], -v[1]]))
  
      bx = np.cross(by, bz)
      R =  np.array([[bx[0], by[0], bz[0], 0],
                     [bx[1], by[1], bz[1], 0],
                     [bx[2], by[2], bz[2], 0],
                     [0,     0,     0,     1]], dtype=np.float32)
  
      return R.T
  </code></pre>