Coordinates the device coordinates of the mouse event to the world coordinates.
1. First, describe the device coordinates, the viewport coordinates, and the world coordinates.
Device coordinate: the unit is pixel, coordinate system, the upper left corner of the screen is the origin, X axis horizontal to the right, Y axis vertical downward increase.
The correspondence between the world coordinates and the viewport coordinates is as follows:
Glfloat left, right, bottom, top; // projected range in the world coordinate system
Float a, B, c, d; // scale and shift parameters of the world coordinates and the viewport coordinates
Setwindowandviewport (width, height); // you can call this operation to set the correspondence between the world coordinates and the viewport coordinates.
// Width and height, respectively representing the size of the customer Zone
Setwindowandviewport (glint width, glint height)
{
Left = Bottom =-10000;
Right = Top = 10000;
// Create the simplest Coordinate System
Glmatrixmode (gl_projection );
Glloadidentity ();
// Set the projection range of the world coordinate system
Gluortho2d (left, right, bottom, top );
// Set the size of the projection range in the viewport to the width and height of the Windows window.
Glviewport (0, 0, width, height );
// Calculate the scaling and Heping Parameters
A = (width-0)/(right-left );
B = (height-0)/(top-bottom );
C = 0-a * left;
D = 0-b * bottom;
Trace ("A = % F, B = % F, c = % F, D = % F/N", A, B, C, D );
}
After the preceding settings, the coordinate system of the view is as follows. The lower left corner of the window is the origin, the X axis is increased horizontally to the right, and the Y axis is increased vertically.
After practice, this process goes through the following two processes.
1. From the device coordinates to the viewport coordinates.
2. From the View coordinates to the world coordinates
Onlbuttondown (uint nflags, cpoint point)
{
// Todo: add your message handler code here and/or call default
Trace ("device point X = % d, y = % d/N", point. X, point. y );
Crect clientrect;
Getclientrect (clientrect );
Cpoint windowpoint; // Save the point in the world coordinate system
Windowpoint. x = point. X ;//Convert the device coordinates to the visual interface coordinates
Windowpoint. Y = clientrect. Height ()-point. Y;
Trace ("view point X = % d, y = % d/N", windowpoint. X, windowpoint. y );
Windowpoint = getwindowpoint (windowpoint );//Use a custom function from the View coordinates to the world coordinates
Trace ("world point X = % d, y = % d/N", windowpoint. X, windowpoint. y );
// Draw in the world coordinate system
Glbegin (gl_points );
Glvertex2i (windowpoint. X, windowpoint. y );
Glend ();
Glflush ();
Cview: onlbuttondown (nflags, point );
}
Cpoint getwindowpoint (cpoint viewportpoint)
{
Cpoint windowpoint;
Windowpoint. x = (viewportpoint. X-C)/;
Windowpoint. Y = (viewportpoint. Y-d)/B;
Return windowpoint;
}