Advertisement

mouse to 3d

Started by September 02, 2003 05:52 AM
10 comments, last by jaba 21 years, 6 months ago
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
============================== Videoman Library Project
I am to interesting in this!
Advertisement
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

- Thomas Cowellwebsite | journal | engine video

Thank you very much thomas.
============================== Videoman Library Project
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
This is the Sound of ### # # ### # # ### # ## # # # # # # # # # #### #### ### # # # # ### # # # # # # # # # #### # # # # ##### # ##Still Code Crazy, Best and only way to be!!
code here:
http://onemancrew.sinfree.net
under downloads
Advertisement
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

My nothing-to-write-home-about OpenGL webpage. (please pardon the popups!)

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.



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.
============================== Videoman Library Project
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"
"To err is human, to really mess up requires a computer"

This topic is closed to new replies.

Advertisement