This is not a feature request nor a bug. Just informational, perhaps future versions of AOG will want to take advantage of this. I spent a long time searching google until I stumbled on this, so I’m posting it here so I can find it again some day.
Here are the steps for taking a mouse click viewport coordinate (0,0 is bottom left in GL world, so opposite of the screen) and translating them into field easting and northing coordinates. The code posted here is C++ with Qt, but C# has the exact same functionality in its matrix library.
QMatrix4x4 modelview;
QMatrix4x4 projection;
//convert screen coords to GL viewport coords:
float y = viewport_height - mouse_y
float x = mouse_x
//this must be exactly the same as what OpenGL.Designer.cs does when setting up openGL for drawing:
projection.setToIdentity();
// Create a perspective transformation.
projection.perspective(glm::toDegrees(fovy), width / (double)height, 1.0f, camDistanceFactor * camera.camSetDistance);
modelview.setToIdentity();
//camera does translations and rotations, from CCamera
//back the camera up
modelview.translate(0,0,camSetDistance * 0.5);
//rotate the camera down to look at fix
modelview.rotate(camera.camPitch, 1.0, 0, 0);
//pan if set
modelview.translate(panX, panY, 0);
//following game style or N fixed cam
if(camera.camFollowing) {
modelview.rotate(camera.camYaw, 0,0,1);
modelview.translate(camera.-camPosX, camera.-camPosY, camera.-camPosZ);
} else {
modelview.translate(camera.-camPosX, camera.-camPosY, camera.-camPosZ);
}
/* only do this if you want to pan the field with the mouse
modelview.translate(vehicle.pivotAxlePos.easting, vehicle.pivotAxlePos.northing, 0);
modelview.translate(sin(vehicle.fixHeading) * tool.hitchLength,
cos(vehicle.fixHeading) * tool.hitchLength, 0);
//rotate to face vehicle heading
modelview.rotate(glm::toDegrees(-fixHeading), 0.0, 0.0, 1.0);
if (camera.camFollowing)
modelview.rotate(glm::toDegrees(-fixHeading), 0.0, 0.0, 1.0);
*/
//get point on the near plane
QVector3D worldpoint_near = QVector3D( { x, y, 0} ).unproject(modelview,projection,QRect(0,0,viewport_width, viewport_height));
//get point on the far plane
QVector3D worldpoint_far = QVector3D( { x, y, 1} ).unproject(modelview, projection,QRect(0,0,viewport_width, viewport_height));
//get direction vector from near to far
QVector3D direction = worldpoint_far - worldpoint_near;
//determine intercept with z=0 plane, and calculate easting and northing
double lambda = -(worldpoint_near.z()) / direction.z();
mouseEasting = worldpoint_near.x() + lambda * direction.x();
mouseNorthing = worldpoint_near.y() + lambda * direction.y();
I don’t pretend to understand how it works but it does work.