# Casting Ray from Mouse to Plane....?

Hi peeps,

I’m using OpenGL Selection to select objects in my scene with a single or double click. This is slow but usable because of the time it takes to do a double click. However, when dragging I need to project the mouse cursor position back into the scene without doing any selection at all (because once an object is selected it is the current object and is dragged around in a given plane).

So, what I need to do is find the intersection of a ray, defined perhaps by the mouse cursor position and (probably) also the eye point, through a given plane defined of course as vector and distance. This will enable me to drag an object accurately along an abritrary axis I think.

Any ideas?

You can find the line from the mouse position by gluUnprojecting the mouse coordinates (wx, wy) with a supplied wz = 0.0f and wz = 1.0f.
This results in two points, which give you exactly one line.
Intersect the line with the plane in which movement takes place to determine the world (x, y, z) for the object.

HTH

Jean-Marc.

Ah - I thought it might be something simple like that.

Thanks a lot - thats made my day

Well, this is what I did and I seem to get garbage out! Have I put garbage in?

Thanks —>

// Get the ray from the cursor into the scene - no selection occurs - this is just a utility method.

void CSelectionBuffer :: GetRay ( int MouseX, int MouseY, CVertex3D& p1, CVertex3D& p2 )

{

GLdouble	Projection [16], Model [16];
GLint		Viewport [4];

//
// Now get the matrices and viewport.
//

glGetDoublev ( GL_PROJECTION_MATRIX, Projection );
glGetDoublev ( GL_MODELVIEW_MATRIX, Model );
glGetIntegerv( GL_VIEWPORT, Viewport );

//
// We will need to invert mouse y (because its top left whereas our window is bottom left origin).
//

double Invert_m_y = static_cast&lt;double&gt; ( Viewport [3] ) - static_cast&lt;double&gt; ( MouseY );

HITRECORD h1, h2;

//
// Inverse transform the given point - note its important that the viewing transforms are setup as they were rendered.
//

gluUnProject (	static_cast&lt;double&gt; ( MouseX ),			// Mouse x.
Invert_m_y,								// Inverted mouse y.
0.0,									// z depth
Model,									// Model view matrix.
Projection,								// Projection matrix.
Viewport,								// Viewport co-ordinates.
&h1.x,									// Final result x.
&h1.y,									// Final result y.
&h1.z );								// Final result z.

gluUnProject (	static_cast&lt;double&gt; ( MouseX ),			// Mouse x.
Invert_m_y,								// Inverted mouse y.
1.0,									// z depth 					Model,									// Model view matrix.
Projection,								// Projection matrix.
Viewport,								// Viewport co-ordinates.
&h2.x,									// Final result x.
&h2.y,									// Final result y.
&h2.z );								// Final result z.

//
// Copy results across ( from double to float ).
//

p1.v [0] = ( float ) h1.x;
p1.v [1] = ( float ) h1.y;
p1.v [2] = ( float ) h1.z;

p2.v [0] = ( float ) h2.x;
p2.v [1] = ( float ) h2.y;
p2.v [2] = ( float ) h2.z;

}

[This message has been edited by Robbo (edited 02-28-2002).]