face the camera and say cheese..

I need certain billboard-type objects in my game to always face toward the camera. I’ve tried some rotation calls involving arctangents but I can’t get it quite right. If the object is at coordinates x, y, z, originally facing in vector (0, 0, -1) and the camera is at a, b, c, could someone less confused at this math that I am please post the few lines of glRotate calls that will rotate the object so that it’s exactly facing the camera (assuming it’s already been translated to the origin). Thanks!

This is the code I uses. It’s LWJGL, not jogl. But I hope you will understand it anyway.


/**
* @param eye the position of the camera
* @param pos the position of the billboard.
*/
public void drawBillboard(Vector3f eye, Vector3f pos) {
  GL.glPushMatrix();
  Vector3f zAxis = new Vector3f(pos);
  zAxis.sub(eye);
  zAxis.normalize();
  Vector3f yAxis = new Vector3f(0, 1, 0);
  // Build the X axis vector based on the two existing vectors
  Vector3f xAxis = new Vector3f();
  xAxis.cross(yAxis, zAxis);
  xAxis.normalize();
  // Correct the Y reference vector
  yAxis.cross(xAxis, zAxis);
  yAxis.normalize();
  yAxis.scale(-1);
  float m[] = new float[16];
  m[0] = xAxis.x; m[1] = xAxis.y; m[2] = xAxis.z; m[12] = pos.x;
  m[4] = yAxis.x; m[5] = yAxis.y; m[6] = yAxis.z; m[13] = pos.y;
  m[8] = zAxis.x; m[9] = zAxis.y; m[10] = zAxis.z; m[14] = pos.z;
  m[15] = 1;

  FloatBuffer matBuff = ByteBuffer.allocateDirect(16*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  matBuff.rewind();
  matBuff.put(m);
  matBuff.flip();

  GL.glMultMatrixf(matBuff);

  //...draw a quad between (-2, -2, 0) and (2, 2, 0)

  GL.glPopMatrix();
}

hmmm… tried your code but apparently something went wrong somewhere, because the objects flew all over the place instead of rotating in place. I used your exact code, only adapting it for JOGL, so I’m not sure why it didn’t work… ???

I forgot to mention of to use the function. The top of the matrix stack must contain the “camera matrix”. So before drawBillboard I call:
“GLU.gluLookAt(eye.x, eye.y, eye.z, target.x, target.y, target.z, 0, 1, 0);”

Where eye is the position of the camera and the same as I pass to drawBillboard.

Beside this I’m clueless to why it doesn’t work. Sorry!! :frowning:

Interesting… I haven’t even heard of the function gluLookAt. Then again, I haven’t used glu so far anywhere in my game, whose transformations consist entirely of glTranslatef and glRotatef calls… forgive me if I’m somewhat new at this.