View Full Version : 2Dto3D

okapota

12-07-2000, 11: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).

anyone?

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

okapota

12-08-2000, 03:49 AM

http://www.opengl.org/discussion_boards/ubb/smile.gif

i have asked the reverse thing. not 3dto2d.

2dto3d.

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.

if(it_s_good)

{

GLdouble winz;

GLdouble objx, objy, objz;

GLint viewport[4];

GLdouble modelMatrix[16];

GLdouble projMatrix[16];

glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winz);

glGetIntegerv(GL_VIEWPORT, viewport);

glGetFloatv(GL_MODELVIEW_MATRIX, modelMatrix);

glGetFloatv(GL_PROJECTION_MATRIX, projMatrix);

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:

glutMouseFunc(WhenIClick);

When you click, objx, objy and objz will hold the world coordinates of the point you clicked.

Moz

pleopard

12-08-2000, 08:33 AM

Oh cool Moz, you dah man! http://www.opengl.org/discussion_boards/ubb/smile.gif

okapota

12-08-2000, 09: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.

okapota

12-10-2000, 05: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?

okapota

12-10-2000, 05: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.

okapota

12-10-2000, 10: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

horizon.

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

return vector3(

camera.x-fraction*(camera.x-clicked.x),

0.,

camera.z-fraction*(camera.z-clicked.z));

okapota

12-11-2000, 07: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.

thnx.

okapota

12-11-2000, 11:04 AM

http://www.opengl.org/discussion_boards/ubb/smile.gif

ok, close the thread, i got it working.

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.