<p>Turns out when the viewport sizes change, the zoom is simply either <code>h2 / h1</code> or <code>w2 / w1</code>, depending on what field of view we are keeping constant.</p>
<p>In my case, I had the vertical field of view constant, thus I used <code>h2 / h1</code>.
So we can have <code>f(x1, y1) = (h2*x1/h1, h2*y2/h1)</code>.</p>
<p>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.</p>
<p>So the offset is just (change in width) / 2 = <code>(w2 - h2*w1/h1) / 2</code>
Finally, <code>f(x1, y1) = ((w2 - h2*w1/h1) / 2 + h2*x1/h1, h2*y2/h1)</code> is the function I needed!</p>
<p>I plugged this into my program and got the desired results!</p>

CC BY-SA 4.0
2022-03-11T06:31:20.410Z
2022-03-11T06:42:58.473Z
2022-03-11T06:42:58.473Z
Tanay Karnik
<p>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.</p>
<p>I am using the perspective projection to get the screen coordinates of that point, given viewport size.</p>
<p>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?</p>
<p>So we are keeping the camera state constant, the field of view constant and only changing the size of the viewport.</p>
<p>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.</p>
<p>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.</p>
<p>But happens when the aspect ratio changes?</p>
<p>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.</p>

CC BY-SA 4.0
2022-03-11T00:33:21.110Z
"*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?
Nicol Bolas
CC BY-SA 4.0
2022-03-11T00:35:28.297Z
"*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?
Nicol Bolas
CC BY-SA 4.0
2022-03-11T06:04:30.120Z
@NicolBolas I'm talking about the aspect ratio of the viewport.
Tanay Karnik
CC BY-SA 4.0
2022-03-11T06:08:02.443Z
@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?
Tanay Karnik
CC BY-SA 4.0
2022-03-11T07:18:38.930Z
"*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.
Nicol Bolas
<3d><transformations><perspective>
How to translate screen coordinates of a 3d point as the viewport size changes assuming camera state remains constant?
Tanay Karnik
# How to translate screen coordinates of a 3d point as the viewport size changes assuming camera state remains constant?

(Asked by: username_0 on 2022-03-10)

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.

**Tags:** 3d, transformations, perspective

> **Comments:**
> **username_1**: "*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?  
> **username_1**: "*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?  
> **username_0**: @username_1 I'm talking about the aspect ratio of the viewport.  
> **username_0**: @username_1 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?  
> **username_1**: "*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.  

---

## 1 answers

### Answer by username_0 on 2022-03-11. Score: 0 | Accepted answer

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!


---

