# Converting 3D coordinates to 2D and back?

I'm wondering if there is a simple way to convert 3D coordinates to 2D coordinates. Also, if it's possible, to convert in the reverse direction.

I'm using OpenGL(GLUT) in my C++ project.

I am also using SFML for the 2D information (sprites text etc.)

I found out that I can use gluProject(), but I have no idea how to use this.

I'm asking for a simple example of using gluProject() or another example to convert 3D coordinates (such as from the player) to 2D coordinates.

If I can't get the simple process I'm confident that I can figure out the rest.

• I think you are just looking for '3d object selection' in which case there is a lot of information both on this SE as well as on google (when you know what to search for :)) – James May 24 '11 at 16:53
• If that's it, look for 'picking'. – The Communist Duck May 24 '11 at 16:55
• alright, ill look aroundm thanks for the suggestion, but im also wondering how to change from 3D to 2D for health bars and such. – Molmasepic May 24 '11 at 16:56
• hey i just found this. is this a good example?(i was skimming xD) – Molmasepic May 24 '11 at 17:00
• This isn't your inquiry here, but you may also find it useful to see this question about translating axes between 2D and 3D. – Grace Note May 24 '11 at 18:02

gluProject assumes the input coordinates objX, objY, and objZ to be in model space, but they can be anywhere as long as the matrices you provide it via the model and proj parameters have associated meaningful values. The format of the model and proj matrices is an array of 16 floating point values, the same kind of data you'd get from glGetFloatv and such. The view parameter is an array of 4 integers representing the position and size of your viewport (from glGetIntegerv for example).