<p>To reproduce the distortion in the image you would have to apply the model-view-projection matrix and then divide by <span class="math-container">$w$</span>. If you use a projection matrix that flips <span class="math-container">$Z$</span> then you would get an inverted version. For more details on the transformations that occur for a perspective projection to happen see:</p>
<p><a href="http://www.songho.ca/opengl/gl_projectionmatrix.html" rel="nofollow noreferrer">http://www.songho.ca/opengl/gl_projectionmatrix.html</a></p>
<p><a href="http://www.songho.ca/opengl/gl_transform.html" rel="nofollow noreferrer">http://www.songho.ca/opengl/gl_transform.html</a></p>
<p><a href="https://learnopengl.com/Getting-started/Transformations" rel="nofollow noreferrer">https://learnopengl.com/Getting-started/Transformations</a></p>

CC BY-SA 4.0
2021-07-19T09:58:34.580Z
2021-07-19T11:01:39.350Z
2021-07-19T11:01:39.350Z
lightxbulb
<p>For educational purposes I'm trying to simulate what a scene would look like with a perspective matrix deformation (before everything is brought to NDC) but I'm unsure how to do this.</p>
<p>I'd like to do this only with matrices transformations if that is possible. A cube would then deform depending on where the perspective camera is set up.</p>
<p>I've set up two cameras, the perspective camera and the observer camera.</p>
<p>This, in theory, could be achieved by applying the perspective camera perspetive-view matrix on the observer camera's cube, right? I've tried this with no luck. Wouldn't this supposed make all vertices to fit the perspective camera homogenous coordinates?</p>
<p>The effect I'm trying to achieve is this one (<a href="https://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/" rel="nofollow noreferrer">source</a>):
<img src="https://www.opengl-tutorial.org/assets/images/tuto-3-matrix/homogeneous.png" alt="Frustum in homogenous coordinates" /></p>
<p>Thank you!</p>
<p>Edit:</p>
<pre><code>// model translate
model = perspective_camera-&gt;projection * perspective_camera-&gt;view * model;
shaders[&quot;scene&quot;].setBool(&quot;second_camera&quot;, true);
// draw model
</code></pre>
<p>and the shader:</p>
<pre><code> if(second_camera) {
  vec4 model_div_w = model * model_coefficients;
  model_div_w = model_div_w/model_div_w.w;
  gl_Position = projection * view * model_div_w;
}
</code></pre>
<p>This is what I got from this code:
<a href="https://i.sstatic.net/Md3aS.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Md3aS.png" alt="Two camera setup" /></a></p>
<p>Panning the perspective camera <strong>slightly to the left</strong> gets me this, shouldn't the blocks pan to the <strong>other side</strong>? They are actually accompanying the camera. (Moving the camera further has the same effect but more even pronounced)
<a href="https://i.sstatic.net/ojg1M.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ojg1M.png" alt="enter image description here" /></a></p>
<p>Thanks!</p>

CC BY-SA 4.0
2021-07-12T07:38:53.570Z
You would apply the model-view-perspective matrix on the vertices of your model and then divide by $w$. This would yield the distorted models that you see in the image.
lightxbulb
CC BY-SA 4.0
2021-07-13T01:01:31.330Z
Hi @lightxbulb! Thanks! I updated my question, I'm still facing some issues, do you have any more tips? How does the code looks to you? Thanks a lot
Cattani
CC BY-SA 4.0
2021-07-13T10:49:53.757Z
You're likely using opengl's asinine convention of a projection matrix switching the handedness. The straightforward solution to this is using a projection matrix that doesn't map from negative $z$.
lightxbulb
<opengl><transformations><matrix><perspective>
Simulate projection matrix deformation in two camera setup
Cattani
# Simulate projection matrix deformation in two camera setup

(Asked by: username_0 on 2021-07-12)

For educational purposes I'm trying to simulate what a scene would look like with a perspective matrix deformation (before everything is brought to NDC) but I'm unsure how to do this.

I'd like to do this only with matrices transformations if that is possible. A cube would then deform depending on where the perspective camera is set up.

I've set up two cameras, the perspective camera and the observer camera.

This, in theory, could be achieved by applying the perspective camera perspetive-view matrix on the observer camera's cube, right? I've tried this with no luck. Wouldn't this supposed make all vertices to fit the perspective camera homogenous coordinates?

The effect I'm trying to achieve is this one ([source](https://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/)):
![Frustum in homogenous coordinates](https://www.opengl-tutorial.org/assets/images/tuto-3-matrix/homogeneous.png)

Thank you!

Edit:

```
// model translate
model = perspective_camera->projection * perspective_camera->view * model;
shaders["scene"].setBool("second_camera", true);
// draw model

```

and the shader:

```
 if(second_camera) {
  vec4 model_div_w = model * model_coefficients;
  model_div_w = model_div_w/model_div_w.w;
  gl_Position = projection * view * model_div_w;
}

```

This is what I got from this code:
[![Two camera setup](https://i.sstatic.net/Md3aS.png)](https://i.sstatic.net/Md3aS.png)

Panning the perspective camera **slightly to the left** gets me this, shouldn't the blocks pan to the **other side**? They are actually accompanying the camera. (Moving the camera further has the same effect but more even pronounced)
[![enter image description here](https://i.sstatic.net/ojg1M.png)](https://i.sstatic.net/ojg1M.png)

Thanks!

**Tags:** opengl, transformations, matrix, perspective

> **Comments:**
> **username_1**: You would apply the model-view-perspective matrix on the vertices of your model and then divide by $w$. This would yield the distorted models that you see in the image.  
> **username_0**: Hi @username_1! Thanks! I updated my question, I'm still facing some issues, do you have any more tips? How does the code looks to you? Thanks a lot  
> **username_1**: You're likely using opengl's asinine convention of a projection matrix switching the handedness. The straightforward solution to this is using a projection matrix that doesn't map from negative $z$.  

---

## 1 answers

### Answer by username_1 on 2021-07-19. Score: 1 | Accepted answer

To reproduce the distortion in the image you would have to apply the model-view-projection matrix and then divide by $w$. If you use a projection matrix that flips $Z$ then you would get an inverted version. For more details on the transformations that occur for a perspective projection to happen see:

<http://www.songho.ca/opengl/gl_projectionmatrix.html>

<http://www.songho.ca/opengl/gl_transform.html>

<https://learnopengl.com/Getting-started/Transformations>


---

