View Full Version : 2Dto3D
12-07-2000, 10:48 AM
thats a question i have never found an answare to, how can i find the position in space that was clicked on by the mouse?
something with gluUnproject right?, but how to use it?
i need it to be able to choose different tiles on a plane(im writing a tile based 3d engine like fallout, but not 2d).
[This message has been edited by okapota (edited 12-07-2000).]
9.100 How can I find the screen coordinates for a given object-space coordinate?
You can use the GLU library gluProject() utility routine if you only need to find it for a few vertices. For a large number of coordinates, it can be more efficient to use the Feedback mechanism.
To use gluProject(), you'll need to provide the ModelView matrix, projection matrix, viewport, and input object space coordinates. Screen space coordinates are returned for X, Y, and Z, with Z being normalized (0 <= Z <= 1).
This comes from OpenGL FAQ (http://www.opengl.org/developers/faqs)
12-08-2000, 02:49 AM
i have asked the reverse thing. not 3dto2d.
The answer is also there (http://www.opengl.org/developers/faqs), but under the following paragraph (9.110)
posted 11-23-2000 03:34 AM
void WhenIClick(int button, int state, int winx, int winy)
// Check here if it's the event you waited for, i.e. the right button and state.
GLdouble objx, objy, objz;
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winz);
gluUnproject((GLdouble)winx, (GLdouble)winy, winz, modelMatrix, projMatrix, viewport, &objx, &objy, &objz);
This is an example of a callback function that you can use with glut. Register it like this:
When you click, objx, objy and objz will hold the world coordinates of the point you clicked.
12-08-2000, 07:33 AM
Oh cool Moz, you dah man! http://www.opengl.org/discussion_boards/ubb/smile.gif
12-08-2000, 08:28 AM
thnx, i always wondered how to retrive this winz, but what is the x and y parameters?
and i heard the glGet() funcs are very slow, is it true?
Sorry Paul, I don't understand, I don't speak English.
12-10-2000, 04:21 AM
I tried what u said, well, winz is always 1.00, which is good, but the final coords are totally wrong. the objz is not even initialized, and the objx and objy always gets values around 3-4. why?
12-10-2000, 04:45 AM
ok, i got it working, but the actual coordinates are very very low, y = -77...., and z = -43...., i dont know why, but it looks like under the mouse, but i to know where i clicked on a 2d plane, y=0;
should i intersect the vector i get from gluUnproject with the plane, and see where i clicked on?
Sorry, but I don't understand your previous post. What actual coordinates are you talking about (window, 3D world)? What do you mean by it looks like under the mouse ? And what's the y = 0 stuff ?
As I said in my post, the function I gave is supposed to be used w/ glut as a callback (in order to be used as is). If you just want to get the window coord of the point you click, just read the winx and winy values that glut passes to your function.
12-10-2000, 09:24 AM
no no, i will explain myself again.
i have a grid, i want to be able to click on this grid, and where i clicked a wall will be created, or a sphere, it doesnt matter.
now, with the glu function, i get the 3d coordinates from the mouse click, but they are very far away, and if i keep clicking in the same place, it gets closer and closer.
but i want to know where on the grid, that is on th xy plane(y=0), the user clicked on.
is it more clear now? if not, tell me.
You know where your camera is.
You know some point into the screen from
where the camera is.
You want to know the x,z when the y is 0.
All you have to do is project a ray from
the camera point through the point that the
user clicked and solve for y = 0. Warning
that this will be behind the camera if the
user clicked in the sky, and unsolvable
(division by zero) if he clicked on the
I'm assuming you went to a US high school and
actually didn't learn this simple fact of
geometry while growing up :-)
vector3 camera(10., 20., 5.);
vector3 clicked(5., -500., -2.);
float fraction = camera.y/(camera.y-clicked.y);
assert(camera.y == fraction*(camera.y-clicked.y));
12-11-2000, 06:45 AM
well, first of all, i live in israel. and i just began high school, but never mind.
you are actually saying what i said, dont u?
i should intersect a ray from the camera pos throu the clicked spot, with the plane right?
so i have the camera spot, and i have the 2d coordinates of the clicked spot. i use gluUnProject() to find the 3d coords, find the ray between the two points, and intersect it with the plane, right?
is this what you mean? it sounds right, and that what i was going to do anyway, so tell me if it is the right way.
12-11-2000, 10:04 AM
ok, close the thread, i got it working.
Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.