PDA

View Full Version : Mouse XY to 3D XZ?



Andy Gilbert
04-08-2010, 03:54 AM
Hi, in my simple editor im having a go at, im trying to implement a 3D cursor i suppose.

So when i want to add an objec to the scene, i want a 3D cursor that is poition along X & Z but in relation to where the mouse is on screen and regardless to what camera angle is. The cameras all done, and therfore i can get Camera positions and Rotation values.

I just cant think how to do it?

Thanks
Andy

strattonbrazil
04-08-2010, 08:24 AM
regardless to what camera angle is

I think you mean in relation to where the camera is.

Okay, XZ plane that goes through the origin of the world at (0,0,0), right?

I'd use gluProject to build a ray and intersect that against your XZ plane. That will give you the 3D position on the plane.

gluUnProject(winX, winY, .3,
modelViewMatrix,
projectionMatrix,
viewport,
object1X, object1Y, object1Z);


gluUnProject(winX, winY, .6,
modelViewMatrix,
projectionMatrix,
viewport,
object2X, object2Y, object2Z);

Make a vector from the first point to the second point. This will be in world space. Normalize it and you have the direction for a ray. Your ray origin is your camera position.

Do a ray-plane intersection that like the one below. Move the ray t units in the direction of the ray and you have your plane intersection!

http://www.siggraph.org/education/materials/HyperGraph/raytrace/rayplane_intersection.htm