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.

  • \$\begingroup\$ 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 :)) \$\endgroup\$ – James May 24 '11 at 16:53
  • \$\begingroup\$ If that's it, look for 'picking'. \$\endgroup\$ – The Communist Duck May 24 '11 at 16:55
  • \$\begingroup\$ alright, ill look aroundm thanks for the suggestion, but im also wondering how to change from 3D to 2D for health bars and such. \$\endgroup\$ – Molmasepic May 24 '11 at 16:56
  • \$\begingroup\$ hey i just found this. is this a good example?(i was skimming xD) \$\endgroup\$ – Molmasepic May 24 '11 at 17:00
  • \$\begingroup\$ This isn't your inquiry here, but you may also find it useful to see this question about translating axes between 2D and 3D. \$\endgroup\$ – Grace Note May 24 '11 at 18:02

You can convert between 3D and 2D coordinates in a variety of ways, some of which have more meaning than others. For example you could just drop the Z coordinate, yielding an (x,y) pair, but that probably doesn't do what you want semantically.

But you're specifically asking how to convert between model, world or view space 3D and screen space 2D as used by your graphics API, it sounds like. This primarily involves application of the appropriate transformation matrix, as well as performing the perspective division by W and any viewport transformations that may be warranted. gluProject does exactly this operation, all you have to do is provide it with input coordinates and matrices.

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).

To perform the operation in reverse, you can use gluUnproject. Note that since you'll have a 2D coordinate you'll have manually specify an arbitrary depth value. The 3D → 2D conversion perforce loses data, so you must invent new data when going in the other direction.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.