Billboarding using LWJGL

ok following method is a nice easy way to get Billboarding into your app, very useful for things like particle effects, player names (such as in football games) also other text in 3d mode, 2d spirites in 3d, etc.

its very simple to use when drawing your 3d objects just do a

GL11.GL_PushMatrix();
run the billboarding matrix changes
draw your stuff which needs to be bill boarded
then GL11.GL_PopMatrix(); to return back to your normal 3d matrix


....

GL11.glPushMatrix();

FloatBuffer buf = BufferUtils.createFloatBuffer(16 * 4);
// Get your current model view matrix from OpenGL. 
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, buf);
buf.rewind();

buf.put(0, 1.0f);
buf.put(1, 0.0f);
buf.put(2, 0.0f);

buf.put(4, 0.0f);
buf.put(5, 1.0f);
buf.put(6, 0.0f);
			
buf.put(8, 0.0f);
buf.put(9, 0.0f);
buf.put(10, 1.0f);
			
GL11.glLoadMatrix(buf);

// now draw your stuff here that needs billboarding

GL11.glPopMatrix(); // restores the normal matrix

....

if you spot any errors, inefficiencies please don’t hesitate to point them out :slight_smile:

p.s thx to UlfJack for some help on the billboarding and matrices

This is ok for simple cases where the sprite in front of you (near center of the screeen), but as the object moves to the sides, above or below, it will start to fail. To do proper bill boarding you need the sprite to face the camera location, not just it’s plane. For alot of cases, this is ok though.

It can be made simpler and faster If you are going to be translating the object and don’t need the current matrix translation applied (basically if you are using world coordintes), you can just do a :


GL11.glPushMatrix();
GL11.glLoadIdentity();

GL11.glTranslatef(...);
.. drawn sprite here

GL11.glPopMatrix();

That will execute faster and have the same effect because there is no call to get the modelview matrix which is a slow operation.

Actually it depends what effect you’re after, as they both have their drawbacks. Billboards facing the camera tend to cut into each other (as they’re all at slightly different angles) which can make sorting a pain and add odd artifacts when the camera translates. But it does have the advantage of leaving billboards unchanged if the viewport rotates, as plane aligned will give you annoying popping in this case.