Confusion with perspective matrix. #328
Replies: 3 comments
-
I can't remember where I came up with that perspective projection matrices, some of them were contributed, but I suspect that they are using the D3D view coordinate system where y is up, x is right and z is forward, where as Vulkan expects y to be down. I think possibly the only differences is the y column needs to be negated, you could try Also, |
Beta Was this translation helpful? Give feedback.
-
Yes, these matrices assume that the axes are directed as you pointed out. Negating the y-column is not enough. Also,
|
Beta Was this translation helpful? Give feedback.
-
I do have an issue to try and address this sort of thing and provide methods that work for any (or any common) coordinate system - #79. I don't have any solutions as yet though. |
Beta Was this translation helpful? Give feedback.
-
Hi,
Hi, in my project I noticed that a matrix, created with
Mat4::perspective_rh
(together withMat4::look_at_rh
) produces strange results, namely, it transforms a 3d model from a right-handed to a left-handed coordinate system.This is the model I created in Blender:
From the picture, you can see, that the arrows create a right-handed system (x - right, y - bottom, z - front (into the screen)).
But this is what happens when I draw the same model with a view and perspective matrices from
glam::Mat4
:Maybe it's not clearly seen from the static image, but here x points right, y points down, z points back (from the screen), i.e. the handedness was changed.
The same model rendered in other third-party viewers:
Everything is as expected.
I tried to change the perspective matrix to the one from here:
As you can see, it's right-handed again.
Here are some tests I made with the matrix:
I'm working with Vulkan which uses a right-hand coordinate system with x coordinate pointing to the right, y pointing down, and z pointing forward (into the screen). So defining my three points x, y, and z I expect that after the projection and the perspective divide, they will stay on the same axes and keep the direction. But the first thing to note is that after the perspective divide, the homogeneous z coordinate is out of range (it's more than 1.0) despite the fact that initially, the z coordinate of all the three vectors is in the matrix range [0.1 - 100.0] and has the value of 10.0. The second thing is if we look at the NDC positions of these three vectors, we see that x is on the left half, y is on the top half, and z has a lower z-coordinate. If we pretend these would be vectors instead of points, the x is pointing to the left, y is pointing up and z is pointing back (from the screen), i.e. these 3 vectors form a left-hand triplet.
looking at the
Mat4::perspective_rh
source code I'm assuming that it "works" with a negative z direction as if it was positive. Indeed, if I change my test to use:(notice the negative z-coordinates) I get the correct output:
Now x points to the right, y - down, z - forward (into the screen). Everything is as it should be.
But now I want to know what I should do with my models to display them correctly. Should I always scale them over the z coordinate before applying the projection?
Beta Was this translation helpful? Give feedback.
All reactions