Archived

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

jaba

mouse to 3d

Recommended Posts

jaba    122
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 this post


Link to post
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.

Setup your MODELVIEW matrix to be your camera''s orientation.

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 this post


Link to post
Share on other sites
shawty    122
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 this post


Link to post
Share on other sites
skow    248
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 this post


Link to post
Share on other sites
jaba    122
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 this post


Link to post
Share on other sites
jaba    122
Thank you Lukerd for your tutorial, you have solved my problem, but when the model is renderized with lines your method doesn''t works, because you must click exactly over the lines due to the z-buffer. Any idea?

Share this post


Link to post
Share on other sites
Lukerd    122
Hmm, i''m not sure, maybe you could try something like this...

RenderSolid();
GetOGLPos();
glClearBuffers();
RenderWire();
SwapBuffers();

It does mean making 2 passes tho, and I havent tried it.

Hyperdev

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

Share this post


Link to post
Share on other sites