# How to translate screen coordinates of a 3d point as the viewport size changes assuming camera state remains constant?

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.

• "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? Mar 11 at 0:33
• "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? Mar 11 at 0:35
• @NicolBolas I'm talking about the aspect ratio of the viewport. Mar 11 at 6:04
• @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? Mar 11 at 6:08
• "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. Mar 11 at 7:18

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).
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!