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.
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
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;