0
$\begingroup$

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): Frustum in homogenous coordinates

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

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

Thanks!

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – lightxbulb
    Jul 12, 2021 at 7:38
  • $\begingroup$ 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 $\endgroup$
    – Cattani
    Jul 13, 2021 at 1:01
  • $\begingroup$ 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$. $\endgroup$
    – lightxbulb
    Jul 13, 2021 at 10:49

1 Answer 1

1
$\begingroup$

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

$\endgroup$

Your Answer

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

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