Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Object Picking Intersection Ray

This topic is 5464 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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


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


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:


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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!