Convert screen coordinates to 3D coordinates

Source: Internet
Author: User

In 3D, it is difficult to accurately determine the Z value of a point. Here I use the Shooting Method to project a point in 2D to a plane in 3D, such as the xoy plane. In this way, X, the value of Y is accurate. At this time, Z = 0. For example, in 3D, the use of CAD is zero!

// Convert screen coordinates to 3D coordinates
Cpoint3d screento3d (INT iscreenx, int iscreeny ,/
Const gldouble modelmatrix [16], const gldouble projmatrix [16], const glint viewport [4],/
Plane flag)
{
Cpoint3d ptout (0, 0 );

Cpoint3d pbegin, pend;
// Obtain the intersection point on the near crop surface
Gluunproject (gldouble) iscreenx, (gldouble) (viewport [3]-iscreeny), 0.0,
Modelmatrix, projmatrix, viewport,
& Pbegin. X, & pbegin. Y, & pbegin. z );
// Obtain the intersection point on the remote cropping plane
Gluunproject (gldouble) iscreenx, (gldouble) (viewport [3]-iscreeny), 1.0,
Modelmatrix, projmatrix, viewport,
& Pend. X, & pend. Y, & pend. z );
 
Cray Ray (pbegin, pend );
Cplane plane;
Cpoint3darray ptarr;
Ptarr. Add (cpoint3d (0, 0, 0 ));

Switch (FLAG)
{
Case xoy:
Ptarr. Add (cpoint3d (1, 0, 0 ));
Ptarr. Add (cpoint3d (0, 1, 0 ));
Plane. Set (ptarr );
If (! Plane. intersectwith (Ray, ptout ))
{
Return cpoint3d (0, 0 );
}
Break;
Case XOZ:
Ptarr. Add (cpoint3d (1, 0, 0 ));
Ptarr. Add (cpoint3d (0, 0, 1 ));
Plane. Set (ptarr );
If (! Plane. intersectwith (Ray, ptout ))
{
Return cpoint3d (0, 0 );
}
Break;
Case yoz:
Ptarr. Add (cpoint3d (0, 1, 0 ));
Ptarr. Add (cpoint3d (0, 0, 1 ));
Plane. Set (ptarr );
If (! Plane. intersectwith (Ray, ptout ))
{
Return cpoint3d (0, 0 );
}
Break;
Case xyz_near:
Return pbegin;
Break;
Default:
Return cpoint3d (0, 0 );
}
Ptarr. removeall ();
Return ptout;
}

Application:

Cpoint3d ccityplanview: getpoint3d (cpoint point, plane)
{
Gldouble modelview [16];
Gldouble projection [16];
Glint viewport [4];
Wglmakecurrent (m_hdc, m_hrc );
Glgetdoublev( gl_modelview_matrix, modelview );
Glgetdoublev( gl_projection_matrix, projection );
Glgetintegerv (gl_viewport, viewport );

Cpoint3d Pt = screento3d (point. X, point. Y, modelview, projection, viewport, plane );

Wglmakecurrent (m_hdc, null );

Return pt;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.