Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
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
    
show 1 more comment

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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