Simulate projection matrix deformation in two camera setup

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):

Thank you!

Edit:

// model translate
model = perspective_camera->projection * perspective_camera->view * model;
// draw model


 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:

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)

Thanks!

• 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. Commented Jul 12, 2021 at 7:38
• 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 Commented Jul 13, 2021 at 1:01
• 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$. Commented Jul 13, 2021 at 10:49

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: