Mouse position in World Coorinates


I am currently creating an MFC, SDI application which has the view and document architecture. An important part of the program is to create objects on a top down viewport. I have got everything working so far. The only problem I have is converting the mouse coordinates to world coordinates in OpenGL.

Any help would be grealy appreciated!

Yours Sincerely,
Lea Hayes

This is not as simple as just tossing a few routines at the problem and doing it. First off you said that you are in top-down view, this is easier, but are you only choosing coordinates at a certain distance, or are you trying to get information at every elevation (for instance selecting an aircraft flying 2 miles above the ground vs selecting a location on the ground). Are you in perspective or orthographic mode? Orthographic mode is easy, there is a one for one relationship and all you have to deal with is elevation, and if you only choose items at a certain distance (like on the land) it makes the calculation easier.

Also what world coordinate frame? longitude, latitude and altitude (once again altitude is hard for a 2D device), or utm, or a local XYZ? All the calculations are similar, but the specifics change.

Here is one way:
Take Mouse xyz, project in perspective Z distance to ground to get world coordinate (need to know eye/camera location, rotation information, etc.), this best for choosing ground locations (or a part of one object).
Another way:
Take Mouse xyz, use line-tri intersection techniques to find what intersects it, time consuming and a bit overpowering unless your areas are broken down into regions that can be quickly tossed via bounding boxes, gives you nearest tri/object, not really world coordinates, but the world coordinate of that tri can be resolved.

or if you trying to select objects, which I have done, this is how I did it:
project all objects to screen position and check nearest to mouse coordinates, rather than vice-versa, offers an easier nearest-to resolution rather than just exact picking of objects.

Whate exactly are you trying to accomplish? a map-point selection via mouse? flat earth? round earth? full earth (choosing on a globe is tricky as the spherical edge has a greater density of world coordinates than the center)?

Just me,


In the top view I only need to mouse coordinates in world coordinates for the two top down coordinates, not the height coordinate as this is just for a totally flat viewport. All I want to be able to do is get this coordinate so that I can record the mouse position and then use it to create polygon objects. I know how to create the polygon objects it is just getting the mouse coordinates in world space.

Thanks for taking your time to help me!

Yours Sincerely,
Lea Hayes

The details would rely on just exactly how you have the projection matrix set.

If this is completely flat-view, orthographic, the math is linear convert the mouse x,y location from windows to your orthographic extents -x,+x to -y,+y etc.

For instance if your mouse is at 0,0 in a window of 100x100 and your orthographic is ±500, you take mousex/1002500-500 == -500
mousy/1002500-500 == -500

etc. once again, this is for ortho only projection, because it has no depth (visible). Perspective is a whole other ball-game. If you are in perspective mode you may have to ask here or in advanced how to project screen coordinates to the end of your perspective camera or to a seperate location of your perspective camera. That is tricky without using the view matrix math (which is NOT my area). because at the near plane of 1.0 distance and 45 degree field of view, the world coordinates are MUCH different than at the far plane of 10000 units distance.