• Advertisement
Sign in to follow this  

Ray Picking - Need help.

This topic is 1869 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 for long time to implement picking ray.
I read lots of tutorials online, but still did not managed to make to work properly.

I'm created the near/far points using unproject method ( that works exactly the same as gluunproject does).
then I created the ray and check for intersection with the bounding box.

For visual reference I draw the ray. it looks like the ray does not comes out from the mouse cursor toward the scene.

Please help me in here.


int UnProject(float winx, float winy, float winz, const Matrix4f *modelview, const Matrix4f *projection, int viewport[4], Vector3f *objectCoordinate)
const m44<f32>& inverseVP = (*projection * *modelview).inverse();
Vector4f in( (winx-(float)viewport[0])/(float)viewport[2]*2.0-1.0,
1.0 );
Vector4f out = inverseVP * in;
if(out.w == 0.0f)
return 0;
return 1;

void sRay::Create( const int& MouseX,
const int& MouseY,
NS_CAMERA::sCamera* pCamera )
int viewport[4] = {0,0,WINDOW_WIDTH, WINDOW_HIGHT };
Vector3f _near;
Vector3f _far;
UnProject(MouseX, viewport[3]-MouseY,0,&pCamera->GetViewMatrix(),&pCamera->GetProjectionMatrix(),viewport,&_near); // get coords
UnProject(MouseX, viewport[3]-MouseY,1,&pCamera->GetViewMatrix(),&pCamera->GetProjectionMatrix(),viewport,&_far);
m_Direction = (_far - _near).normalize();
m_Origen = *pCamera->GetCameraPos();

void sRay::Render( NS_CAMERA::sCamera* pCamera )
Vector3f p0 = m_Origen;
Vector3f p1 = pCamera->fZFar *m_Direction;
// Drawing the line using OGL...

Share this post

Link to post
Share on other sites
Maybe visual reference will help to understand the problem:

the ray settings are:
m_Direction = _far - *pCamera->GetCameraPos();
m_Origen = *pCamera->GetCameraPos();

in here I tried different settings, where the most noticeable thing is that the blue dot is always on the center of the screen.

m_Direction = _far - _near;
m_Origen = *pCamera->GetCameraPos();

The blue dot. is a sphere at the far plane ( p = m_Origen + m_Direction * pCamera->GetFar() ).

I get intersection only when the blue dot is on the bounding box of the teapot.
I want to create the effect, where the ray is injected from the mouse toward the scene.

What am I missing ?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement