Okay, so let's say I have a point in 3d. And I have a camera state. That is, I know the camera position, the camera target and the camera up vector.

I am using the perspective projection to get the screen coordinates of that point, given viewport size.

Now, for two different viewport sizes: V1: (w1, h1) and V2: (w2, h2). If I know the screen coordinates of a point in V1, how would I compute the screen coordinates of the same point in V2?

So we are keeping the camera state constant, the field of view constant and only changing the size of the viewport.

That is, I want to determine the function F, such that F(x1, y1) = (x2, y2) which would depend on w1, h1, w2 and h2. I'm having a hard time working this out since it's been a long while since I touched any 3d graphics math.

I'm guessing it would be linear in x1, y1. Since, if we keep the same aspect ratio in both the viewports, (x2, y2) would simply be w2*(x1, y1)/w1.

But happens when the aspect ratio changes?

Also, as a separate question, by what factor would an object be scaled as we go from viewport V1 to V2? Here, again the answer seems simple when the aspect ratio is maintained but I can't figure out what happens if it isn't.

  • $\begingroup$ "But happens when the aspect ratio changes?" What aspect ratio are we talking about? The one that's often part of the perspective projection matrix, or the aspect ratio of the viewport? $\endgroup$ Mar 11, 2022 at 0:33
  • $\begingroup$ "by what factor would an object be scaled as we go from viewport V1 to V2?" You realize that this is not a single number, right? If something gets twice as tall but half as wide, which would it be called? Or would the factor be 1, if it covers the same area just in a different shape? $\endgroup$ Mar 11, 2022 at 0:35
  • $\begingroup$ @NicolBolas I'm talking about the aspect ratio of the viewport. $\endgroup$ Mar 11, 2022 at 6:04
  • $\begingroup$ @NicolBolas An object doesn't get squished or stretched in dimension as we change viewport sizes right? I'm guessing the relative ratio of width/height should remain the same for an object if we keep the camera state intact. So that's why I refer to a single factor. Correct me if I'm wrong? $\endgroup$ Mar 11, 2022 at 6:08
  • $\begingroup$ "An object doesn't get squished or stretched in dimension as we change viewport sizes right?" Unless you modify the perspective matrix's aspect ratio accordingly, it does. That is, if the only thing you change is glViewport, then objects should become distorted if you change the viewport. $\endgroup$ Mar 11, 2022 at 7:18

1 Answer 1


Turns out when the viewport sizes change, the zoom is simply either h2 / h1 or w2 / w1, depending on what field of view we are keeping constant.

In my case, I had the vertical field of view constant, thus I used h2 / h1. So we can have f(x1, y1) = (h2*x1/h1, h2*y2/h1).

This will give us the correct y coordinate but we might get an offset on the x coordinate since the aspect ratio changes. To fix this, we just compute the offset by using the fact that the smaller/larger size of the viewport in the x-axis would have the same centre as the original viewport.

So the offset is just (change in width) / 2 = (w2 - h2*w1/h1) / 2 Finally, f(x1, y1) = ((w2 - h2*w1/h1) / 2 + h2*x1/h1, h2*y2/h1) is the function I needed!

I plugged this into my program and got the desired results!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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