Hello,
I know OpenGL first calculates the 3D coordinates and then transform them into screen coordinates in order to render. My question is, is there a way to find the 3D coordinate of one point, privided its screen(relative to the window) coordinate and the Z value are given?

For example, my mouse click on a point which has the 2D coordinate of (50,50) relative to the window, and I want to know the 3D coordinate of this point inside OpenGL's internal system, with the Z value being -100.
Is there a way to find it?

Originally posted by Coconut_Crab:
[b]Hello,
I know OpenGL first calculates the 3D coordinates and then transform them into screen coordinates in order to render. My question is, is there a way to find the 3D coordinate of one point, privided its screen(relative to the window) coordinate and the Z value are given?

For example, my mouse click on a point which has the 2D coordinate of (50,50) relative to the window, and I want to know the 3D coordinate of this point inside OpenGL's internal system, with the Z value being -100.

I read that article you provided. It assumed there was already a vertex, or object in the 3D space and what I do is to “select” it. But my problem is I want to find the 3D coordinate even if there was originally no object/vertex there.

What I want to do is not to select an object, but rather to find the 3D coordinate from the 2D coordinate, so I may construct my own object at the specific 3D coordinate from my mouse’s position.

I confess I don’t quite understand how gluPickMatrix() and gluPerspective() work to limit the drawing area, since I never see change in the viewing direction like with gluLookat(). Maybe if I understand the concept I will be able to achieve my goal with the knowledge on the webpage you provided. Can someone please enlighten me a little bit?

A 2D point maps to a 3D line (all points on that line project to the same 2D point).
If you want to unproject from a 2D point to a 3D point, you need to decide on a depth value.
Find that either by intersecting the line with an existing 3D object, or let the user pick the depth somehow (for a modeller you could use a movable “current depth” plane).

Which OpenGL functions I can I use to find the the vector(or math function) of that line? Can you please give a code example, or perhaps point to me to a place that has this specific code example?

I read the article to do the reverse of view matrix with DirectX which you provided. It's very helpful. But do you know the functions in OpenGL that can do the same thing, namely, what DirectX can do with these 2 functions:

gluUnProject() - takes an XYZ window coordinate and returns the back-transformed XYZ object coordinate equivalent.

is what you need.

also you might search ( google) for inverting matrixes ( invert matrix) and see how to get model view matrix with glGet and GL_MODELVIEW_MATRIX param ( though there are ways to keep track on current view matrix … see different open scene graphs code how )

If you need this for simple cases (clicking, etc) and don’t call it hundreds of times per frame, I’d suggest using Get() on the matrices and viewport, and passing that into gluUnProject(). It’s easy, simple, and works. Look at the man page for gluUnProject() and it’ll probably have a usage example.

I have a question. For the coordinates returned by gluUnProject(), what are their units? I mean, say that I get the returned values of X, Y, and Z, can I then use the same values of X, Y, Z to directly draw a vertex with glBegin?

If so, it would be very convenient; if not, what other calculations do I need to do to transform the unit returned by gluUnProject() to the unit which I draw?

Thank you for your help, and I really appreciate it.

After I get my XYZ values from gluUnProject(), are they the values relative to the current MODELVIEW matrix, meaning they can be used directly like glVertex3f(X,Y,Z);, or are they the coordinates of a MODELVIEW matrix aquired from glTranslate()?

Thank you.

Coco

[This message has been edited by Coconut_Crab (edited 12-22-2003).]