Advertisement

Ray casting Picking

Started by July 16, 2017 03:03 PM
0 comments, last by terrysworkstations 7 years, 4 months ago

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;

 

 

This topic is closed to new replies.

Advertisement