Friday, August 17, 2018

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?

Solved

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 NDC1,1 is the top-right corner.

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

  • NDCX = (2.0 * (ScreenX - ViewportX) / ViewportW) - 1.0;

  • NDCY = (2.0 * (ScreenY - ViewportY) / ViewportH) - 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 h-y


No comments:

Post a Comment