Sign in to follow this  
Prozak

OpenGL Object Picking Intersection Ray

Recommended Posts

I'm trying to code an object picking function, the intersection of the projected eye ray, and the objects in the frustrum view's AABB. I'm having problems finding the math to construct the intersection ray, im using OpenGL (but that should matter little). Does anyone have good links on tutorials on this? I'm not interested in the other picking method OpenGL employs, that involves a re-rendering of the scene. Thank you.

Share this post


Link to post
Share on other sites
In my own editor, I did object selection using colors.

I basically render every selectable object to the screen with a unique color, then get the mouse coords and retrive that color with glReadPixels.


Then I loop through all my objects to see who's color matches the color I read.

Its by far the easiest method and you don't have to deal with things like intersecting multiple objects when casting a ray through the scene.

Share this post


Link to post
Share on other sites
I recently started a thread very similar to this, the only difference is that I am using directx, but hopefully that won't matter.

Here's what my function looks like. I accepts four parameters. The first two are the mouse positions, the third is a pointer to a vector that will be the origin, and the last parameter is a vector that will be the direction.

void ScreenTo3D(float mx,float my,D3DXVECTOR3 * p1,D3DXVECTOR3 * p2)
{

float dy,dx;
float HalfGameWidth = GameWidth / 2.0f;
float HalfGameHeight = GameHeight / 2.0f;
float tang = tanf((float)FOV * 0.5f);


dx = tang * (mx / HalfGameWidth - 1.0f) / (float)ASPECT;
dy = tang * (1.0f - my / HalfGameHeight);

//calculate the beginning and end points of the ray
p1->x = dx * NEAR;p1->y = dy * NEAR;p1->z = NEAR;
p2->x = dx * FAR;p2->y = dy * FAR;p2->z = FAR;

//inverse them by the view matrix
D3DXMATRIX invMat; //the inverse of the view matrix
D3DXMatrixInverse(&invMat,NULL,&matView); //inverse the view matrix

D3DXVec3TransformCoord(p2,p2,&invMat);
D3DXVec3TransformCoord(p1,p1,&invMat);

//inverse them by the world matrix
D3DXMATRIX invwMat; //the inverse of the view matrix
D3DXMatrixInverse(&invwMat,NULL,&matWorld); //inverse the world matrix

D3DXVec3TransformCoord(p2,p2,&invwMat);
D3DXVec3TransformCoord(p1,p1,&invwMat);

D3DXVec3Normalize(p2,&(*p2 - *p1));

}




And to give of an idea of what some of those variables are, heres how I set up my projection matrix.

D3DXMatrixPerspectiveFovLH( &matProj, FOV, ASPECT, NEAR, FAR);

I don't know if this will be of any help to you, because your doing opengl. But maybe you'll be able to translate.

Share this post


Link to post
Share on other sites
I wrote a small tutorial on this:

http://www.mt-wudan.com/wu_tut.php?t=mouseray

The tutorial deals with building the mouseray, there's tons of info on ray-triangle intersection tests available.

Let me know if this helps / doesn't help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this