• Advertisement
Sign in to follow this  

Ray casting Picking

Recommended Posts

Ive converted some code from opengl and need someone to look over the code to see if there are any mistakes. Im in Directx 11 with a LH coordinate system.

 

GetPickRay( float sx, float sy, float fov, float width, float height )
{
    float d = 1.0f/IvTan(fov*kPI/360.0f);
    float aspect = width/height;
    Vector3 viewPoint( 2.0f*aspect*sx/width - aspect, -2.0f*sy/height + 1.0f, d );

    viewPoint = mViewToWorldMatrix.TransformPoint( viewPoint );

    return viewPoint - mEyePoint;
}

 

 

OnMouseDown(WPARAM btnState, int x, int y)
{
	mLastMousePos.x = x;
	mLastMousePos.y = y;

	Vector3 ray = GetPickRay( (float) x, (float) y, 45.0f,(float) mClientWidth,(float)mClientHeight );

        // compute intersection with z=0 plane
        float t = -mEyePoint.z/ray.z;
        mClick = mEyePoint + t*ray;
		moveIT = XMMatrixTranslation(mClick.x,mClick.y,mClick.z);
	SetCapture(mhMainWnd);
}

 

Vector3 mUp(0.0f,1.0f,0.0f);
 Vector3 view = Vector3(0.0f,0.0f,0.0f) - mEyePoint;
    Vector3 right;
    Vector3 viewUp;
    view.Normalize();
    right =  view.Cross( mUp );
    right.Normalize();
    viewUp =  right.Cross( view );
    viewUp.Normalize();
  Matrix33 rotate;
//almost similar to a view matrix accept no negating eye point and dotting with uvw
rotate.SetRows( right, viewUp, view );
 mViewToWorldMatrix.Rotation(rotate);
    // set translation (eye position)
    mViewToWorldMatrix(0,3) = mEyePoint.x;
    mViewToWorldMatrix(1,3) = mEyePoint.y;
    mViewToWorldMatrix(2,3) = mEyePoint.z;

 

 

Edited by terrysworkstations
correction to title

Share this post


Link to post
Share on other sites
Advertisement

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  

  • Advertisement