2D label on objects in 3D (LWJGL)

In the end I want to be able to create labels like the player names in the below image,

however, I want them to remain the same size no matter how far away or close up the camera is. My first thought is that the labels would have to be rendered orthographically, but then how can I tell where to render them (in order to be above their player)? I figured maybe some kind of backwards ray-tracing, but I’m not sure how this could be done, let alone if this is even the answer.

My question is how can I render labels orthographically over objects rendered with perspective?

What library are you using? In jMe you just add nodes to the guiNode. In general, you just render the gui separately (separate camera).

LWJGL, sorry, I’ll put that in the title.

I’m pulling this out of thin air, but I think you’d need to run the label’s position through the same projection as the character in order to get the label’s position in screen space. Then you apply your own scale to it. If your using prog. pipeline, then it should be easy enough to implement in a shader. No idea how you’d accomplish it in fixed func.

If you want to mix 2d and 3d, you are going to have to switch between 2D and 3D (gluPerspective and glOrtho). The methods are VERY similar to basic 2d loading and 3d loading:

private void make3D() {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(fov, width / height, zNear, zFar);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }

Just the basic setup with a call to loadIdentity at the end, same for 2D:

private void make2D() {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, width, height, 0, -1, 1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }

Of course, you do have to enable/disable everything you need on every call to these methods.
Here is an example of implementation:

private void render() {
make3D();
//draw 3d stuff
make2D();
//draw2D stuff
}

to zFollette: thanks for the help, at first I though it was that simple, but then I realized If I’m rendering the labels on a 2D plane (the screen) how am I to tell where to render the label so that it’s over top of the player in 3D?

to nerb: That was my guess too (pretty much reverse ray tracing), and now that I know I just have to find out how I could use the perspective matrix to cast the 3D coordinate to the a 2D coordinate on the screen.

You’re 3/4 of the way there then. Just multiply your coordinate by the projection matrix. How are you with matrix math? A quick google should reveal all, if you are uncertain.

EDIT: This works if by ‘perspective matrix’ you mean the MVP matrix??? But that’s essentially what you’ll need to do. Send the point through all the same transformations as the rest of the model, and then bingo!

Ah! I forgot vertices can be used in matrix math :stuck_out_tongue:

Thanks for clearing things up, now it’s just up to me to work out the grimy details. Thanks to everyone who replied so quickly! I’ll post how I did it when I do :smiley:

Matrices are used in OpenGL a ton so I would get used to them!