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\$
4
\$\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\$
  • \$\begingroup\$ Do you mean B(x, y) by B and A(x, y) by A ? \$\endgroup\$ – A---B Jun 6 '16 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 '16 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.