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;
}