Determining a location on a 3D model from mouse coordinates


I've looked through this forum for how to convert mouse coordinates in a 3D window to an actual location on a 3D model.

Using our old viewer which uses Immediate mode OpenGL, it's quite easy. See Below:

How is this done in Open Cascade? I've tried the V3d_View Convert method and it works, but only from the top view.

m_view->SetProj(V3d_TypeOfOrientation_Zup_Top); // Top View

Standard_Real x_ori, y_ori, z_ori;
m_view->Proj(x_ori, y_ori, z_ori); // returns 0.0, 0.0, 1.0 in the top view

Once you rotate the model in any other direction, the resulting location is wrong.

I've also tried the code snippets from Stephane Routelous and others and the result is the same. Only works from the top view.

Is there any straight forward way to accomplish this in OCCT?


/* ----------------------------------------------------------------------------------------------------------------- */
GetLocationFromScreen( const wxPoint screenLocation, location_p_t pCoordLocation )
GLint viewport[4];
GLdouble modelViewMatrix[16];
GLdouble projectionMatrix[16];
GLfloat winZ;
GLdouble posX, posY, posZ;

glGetIntegerv( GL_VIEWPORT, viewport );
glGetDoublev( GL_MODELVIEW_MATRIX, modelViewMatrix );
glGetDoublev( GL_PROJECTION_MATRIX, projectionMatrix );

GLint winX = screenLocation.x;
GLint winY = viewport[3] - screenLocation.y;
glReadPixels( winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, winZ, modelViewMatrix, projectionMatrix, viewport, &posX, &posY, &posZ );

pCoordLocation->x_coordinate = conversion__units_to_coordinate( object_$millimeters, posX );
pCoordLocation->y_coordinate = conversion__units_to_coordinate( object_$millimeters, posY );
/* ----------------------------------------------------------------------------------------------------------------- */

Kirill Gavrilov's picture

Have you tried solution from another topic using SelectMgr_ViewerSelector::PickedPoint() / vstate -entities?

Robert Bachman's picture

Thanks, I used the method PickPoint from the class AIS_ViewController and it is exactly what I needed.