Sign in to follow this  
Followers 0
masterbubu

Ray Picking - Need help.

1 post in this topic

Hi,

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.

tnx



[CODE]
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,
(winy-(float)viewport[1])/(float)viewport[3]*2.0-1.0,
2.0*winz-1.0,
1.0 );
Vector4f out = inverseVP * in;
if(out.w == 0.0f)
return 0;
out[3]=1.0/out[3];
objectCoordinate->x=out.x*out.w;
objectCoordinate->y=out.y*out.w;
objectCoordinate->z=out.z*out.w;
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...
}
[/CODE]
0

Share this post


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

[url="http://www.screencast.com/t/bhlS5fBXN50"]http://www.screencas...m/t/bhlS5fBXN50[/url]
the ray settings are:
m_Direction = _far - *pCamera->GetCameraPos();
m_Direction.normalize();
m_Origen = *pCamera->GetCameraPos();


[url="http://www.screencast.com/t/KC0WpYICfQ1T"]http://www.screencas.../t/KC0WpYICfQ1T[/url]
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_Direction.normalize();
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 ?
0

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  
Followers 0