View Full Version : Getting 2D to 3D

03-10-2000, 04:24 AM
How can i translate a 2 dimension screen coordinate in a 3 dimension coordinate because i want to create a 3D object by a mouse clik on the screen.(sorry for my english because i'm french).

03-14-2000, 03:10 AM
Originally posted by MountainD:
How can i translate a 2 dimension screen coordinate in a 3 dimension coordinate because i want to create a 3D object by a mouse clik on the screen.(sorry for my english because i'm french).

One thing that is lacking when trying to use 2D coordinates to specify 3D coordinates is that you need a way to specify the z-depth of your point (distance from the camera).

OpenGL preserves all three coordinates (x, y z) in it's transformations. So you can use the gluProject and gluUnProject functions to determine your 3D point. gluProject will convert the 3D point into another 3D point, which is the projection of the first 3D point into the window. gluUnProject takes a 3D window coordinate and sticks it back into the world.

If you know the coordinates of a 3D point you can project 2D window points into the plane that is parallel to the camera by:
- Project your 3D point into the window using gluProject.

- Copy the z-value from the projection. Use it for the z-value of the 3D window point that you want to draw at (Currently, you have the x and y values from the mouse)

- Use gluUnProject to send this window point back into world coordinates. This point and the original 3D point will both be in the same plane parallel to and in front of the camera.


01-29-2004, 11:53 AM
I have a similar problem. In my "world" I have a flat surface at y=0 on the plane xz. This surface is made of quads.
The camera is positioned at position (5,5,5) looking at the origin (0,0,0). At this moment I don't need the user to be able to change the position of the camera, although this would be nice later.
What I need is the ability to get the x,y,z coordinates of the point intersected by the ray shoot by a mouse click on the viewport into the 3D world and the plane at y=0. Is there an easy way to do so?

01-29-2004, 12:25 PM
You need to take a look at gluUnProject.
It's designed specifically to convert a 2d coordinate (a mouse click) into world space.

You don't necessarily need to Project then Unproject, you can pick an arbitrary z value for the mouse point (ie. the near or far clipping planes) to save yourself the extra calculations. It will still be pretty accurate.

If you do need to use an arbitrary depth point, take a look at gluUnProject4() it handles depth values outside 0.0 - 1.0

[This message has been edited by McWare (edited 01-29-2004).]

01-29-2004, 03:10 PM
Hmmm... I don't think I understand how gluUnProject works... I tried and I am getting weird numbers that are very far from what I desire. Is there anybody whe have ever tried to create objects in 3D with the mouse? just creating points would be enough for me... a simple example that gets me started on this one. I would appreciate any help here...


01-30-2004, 05:42 AM
Something like this:

OnClick(int x, int y) {

GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realY //OpenGL coordinate, not window coordinate.
GLdouble wx, wy, wz //world space.

//test for which button, etc.....

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
//viewport[3] is height of window in pixels
realY = viewport[3]-(GLint)y;

//print out mouse point if you wish.

gluUnProject((GLdouble)x, (GLdouble)realY, 0.0, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);

//Print world space at Z=0.0f

gluUnProject(GLdouble)x, (GLdouble)realY, 1.0, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);

//Print world space at Z=1.0f;


You may have to adjust your realY depending on what platform you're on. Windows vs. Unix have different measures of coordinate systems. Windows is different from OpenGL in that windows measures positive Y as down, and Opengl measures positive Y as up (I THINK, I COULD BE WRONG ON THAT)