Convert screen coordinates to OpenGL coordinates

Source: Internet
Author: User

Note: gluperspective (gldouble FOV, gldouble aspect, gldouble near, gldouble far) is used );
Convert the screen coordinate to OpenGL coordinate while defining the field of view.

First, understand the gluperspective function.
The FOV parameter indicates the number of degrees at the Y axis.
Aspect shows the ratio of screen width to height
Near and far indicate the distance between the near-cut plane and the far-cut plane

Now let's consider a problem if we perform translation transformation.
Gltranslatef (0, 0,-1 );
Starting from (0, 0), draw a point in length of 0.01 units per grid along the Y axis. How many points can be opened on the screen?

We know that it is closely related to the FOV parameter.
If the FOV parameter is set to 90
The angle between the positive y axis and the negative Y axis is 45.
In gltranslatef (,-1), we can observe that the length of the positive direction of the Y axis is 1/TAN (45 degrees) = 1.
So we can plot 1/0. 01 points.

This is equivalent to drawing a graph with glulookat (, 0 ,-,).
In this case, one unit length can be observed in the positive direction of the Y axis.

Now we can calculate the ratio required for the conversion coordinate.

Since the square length of the Y axis is 1, the half of the screen width indicates the length of 1 OpenGL unit.
The screen length of the current window can be obtained in the wm_size message. The lparam height is high, and the low position is wide.

Int Height = hiword (lparam );
Int width = loword (lparam );

In this way, the unit pixel unit_pixel = (height/2)/TAN (FOV/2) radians) is calculated)

Now assume that the half of the screen width is to be converted to the screen coordinate (), and the half of the screen width is represented by half_h.
The X and Y coordinates in the converted OpenGL coordinate system are represented by (OX, Oy ).
Ox = (10-half_w)/unit_pixel;
Oy = (half_h-20)/unit_pixedl;

Note that our unit_pixel is measured under the gltanslatef (,-1) condition, so
The actual OpenGL coordinates are (OX, oy,-1 );
Or perform translation transformation first.
Gltranslatef (0, 0,-1)
Then the OpenGL coordinates are (OX, oy, 0)

I used to always use (OX, oy,-1)

Update uint_pixel when the screen size changes.

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.