Jump to content
  • Advertisement
Sign in to follow this  
terrysworkstations

Ray casting Picking

This topic is 369 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

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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!