1
\$\begingroup\$

Here is a question in which Questioner asks how to convert world coordinates to 1 to -1 coordinates like Opengl, I want to know how to do that in reverse, that given a coordinates between 1 and -1 (both x and y axis), how can i change it back to screen coordinates. i.e if my width and height is 1000, and given a coordinate (0,0), how can i change it to 500, 500, So that i can render it.

\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

For this problem specifically:

screenX = ((glX + 1) / 2.0) * maxScreenX;
screenY = ((glY + 1) / 2.0) * maxScreenY;

In general, the solution for going from coordinate system A to coordinate system B is:

B = ((A - minA) / (maxA - minA)) * (maxB - minB) + minB;

For instance substituting into your example with B as screenX & A as glX, we'd get:

screenX = ((glX - minGlX) / (maxGlX - minGlX)) * (maxScreenX - minScreenX) + minScreenX;
screenX = ((glX - -1) / (1 - -1)) * (1000 - 0) + 0;
screenX = ((glX + 1) / 2) * 1000;
\$\endgroup\$
2
  • \$\begingroup\$ Do you mean B(x, y) by B and A(x, y) by A ? \$\endgroup\$
    – user82860
    Jun 6, 2016 at 16:06
  • \$\begingroup\$ Sort of, but the min & max apply to each coordinate component separately. I've added an example showing how to substitute the coords in. \$\endgroup\$
    – Pikalek
    Jun 6, 2016 at 16:38

You must log in to answer this question.