#### Archived

This topic is now archived and is closed to further replies.

# mouse to 3d

This topic is 5375 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, my question is How to transform mouse coordinates to 3d object coordinates?. I need to select a vertex of a mesh with the mouse. I am trying with gluUnProject, but this function returns a point of the infinite line that passes through the point, that you has clicked, and the eye of the camera. This is correct? I dot this: gluUnproject(mouse.x, mouse.y, 0.5, modelMat, projMat, viewport, &obX, &obY, &obZ); gluUnproject(mouse.x, mouse.y, 1.0, modelMat, projMat, viewport, &ob2X, &ob2Y, &ob2Z); whit this I suposse that I have two points of the infinite line. And now what? Plase help me and sorry for my english

##### Share on other sites
I am to interesting in this!

##### Share on other sites
What you''re probably after is a ray that shoots into the world from under your mouse cursor which you can use to perform intersection tests for picking objects, moving your character, etc.

Call gluUnproject as you do the first time (although i usually pass 0 for Z). This will give you the mouse position on the near clipping plane of the viewing frustum. This point will be the ray''s origin.

To get the ray''s direction simply subtract the camera position from the ray origin and normalize the result.

vRayDir = (vRayOrigin-vCameraPos).Normalize ();

HOORAY, you now have a picking ray in world space (note this will work with a perspective projection, not ortho).

Now, say you wanted to place an object 10 units away from the camera underneith the mouse (ie. the object will follow the mouse). This is a good test to see if the unprojecting works.
To get the object''s position simply do:
vObjectPos = vRayOrigin + vRayDir * 10;

To pick objects you will have to perform intersection tests against the picking ray (ray/sphere, ray/box, ray/triangle are all pretty well documented algorithms).

Hope this helps .

- Thomas

##### Share on other sites
Thank you very much thomas.

##### Share on other sites
Just a quicke...

Here''s the easy way of doing it, i call it an amazing waste of resources.

I create my scene, then i render both to my active viewport and to an offscreen pretransformed buffer (Created by picking off the coordinates once directx has done it''s transform to screen space) i then just match it to the nearest XY of the mouse using a simple algorithm.

One major problem though... the bigger your scene the more memory you need, works fine for small simple projects, but the ray method described above is better for large scenes.

Shawty/DS

##### Share on other sites
code here:
http://onemancrew.sinfree.net

##### Share on other sites
I''m not ashamed to toot my own horn - below in the thread "movement in 3D space" is an extensive discussion of gluUnProject and how it relates to this topic, and a demo I just posted.

Love means nothing to a tennis player

##### Share on other sites
Its basicly up to you how to use this projected line. What I am doing in my space RTS is i have the projected line intersect with the XZ plane of the ojbect selected.

##### Share on other sites
Hello, I have find a tutorial about gluUnProject
http://hyperdev.moddev.net/

To find the Z coord of the mouse I only must do
glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

Whit this is very simple to find the exacts coords of the selected vertex.

Thank you very much.

##### Share on other sites
OMG, so there are people visiting my site and my counter isn''t broken, lol!

Lukerd.

Hyperdev

"To err is human, to really mess up requires a computer"

• 9
• 9
• 13
• 41
• 15