简体   繁体   中英

From window to opengl coordinate system intuitive explanation

I am trying to understand the map from window coordinate axis (origin top-left) to OpenGL coordinate axis (origin left-bottom) when using the mouse function. In relevant book this map is described by the two following lines:

points[count].x = (float) x / (w/2) - 1.0;
points[count].y = (float) (h-y) / (h/2) - 1.0;

I suspect that these two lines depict a scale. Could you please give an intuitive-mathematical explanation of this map?

What book are you referring to? The origin in NDC-space is the center of the viewport ( 0,0 is the center; -1,-1 is the bottom-left; 1,1 is the top-right). Any other coordinate space is defined by your projection matrix.

I believe what the book is trying to teach you is that NDC -1,-1 is the bottom-left corner of your viewport and NDC 1,1 is the top-right corner.

A more complete mapping would include the X and Y location of your viewport:

  • NDC X = (2.0 * (Screen X - Viewport X ) / Viewport W ) - 1.0;

  • NDC Y = (2.0 * (Screen Y - Viewport Y ) / Viewport H ) - 1.0;

This mapping is illustrated below (the square on the right is the viewport):

http://upload.wikimedia.org/wikipedia/commons/2/23/Viewport_transformation.png

You of course have one additional step necessary since the Y-axis runs the opposite direction in your mouse coordinate system. That is why you see the Y-axis flipped in your mapping hy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM