Coordinates the device coordinates of the mouse event to the world coordinates.

Source: Internet
Author: User

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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.