The value of Z after a break in perspective is always less than -1

So, I am writing my own 3D transformation pipeline to better understand how it all works. I can get all the images on the screen correctly, and now I'm going to go back and see the cropping.

In my opinion, I should trim the vertex if the value of x or y after breaking the perspective goes beyond [-1, 1], and in my case, if the value of z goes beyond [0, 1].

When I implement this, my z value is always -1.xxxxxxxxxxx, where xxxxxxx is a very small number.

It's a little longer, and I apologize, but I wanted to make sure I gave all the information I could.

First agreements:

I use the left system, where the matrix looks like this:

[m00, m01, m02, m03] [m10, m11, m12, m13] [m20, m21, m22, m23] [m30, m31, m32, m33] 

And my vectors are columns looking like this:

 [x] [y] [z] [w] 

My camera is configured with

Vertical FOV in PI / 4 radians.

Aspect Ratio 1. (Square view port)

Close clip value 1.

The far clip value is 1000.

Starting world position x 0.

The starting position of the world y.

The initial position of the world is -500.

The camera is looking down along the Z axis (0, 0, 1)

Given the top, the pipeline works as follows:

Step 1: Multiply the vertex by the camera matrix.

Step 2: Multiply the vertex by the projection matrix.

Projection Matrix:

 [2.41421, 0, 0, 0] [0 2.41421, 0, 0] [0, 0, 1.001001, 1] [0, 0, -1.001001, 0] 

Step 3: Multiply the components x, y and z by 1 / w.

Step 4: [This is where the problem is] Fix the top if there is an outer border.

Step 5: Convert to screen coordinates.

The approximate peak that I have,

 (-100, -100, 0, 1) 

After multiplying by the camera matrix, I get:

 (-100, -100, 500, 1) 

Which makes sense, since with respect to the camera this vertex is 100 units to the left and down and 500 units ahead. It is also between close clip 1 and far clip 1000. W is still 1.

After multiplying by the projection matrix we get:

 (-241.42135, -241.42135, 601.600600, -600.600600) 

This I'm not sure if that makes sense. X and y seem correct, but I'm not sure about z and w, since the next step of diversity exploration is odd.

After breaking the perspective, I get:

 (0.401966, 0.401966, -1.001665, 1) 

Again, x and y make sense, they are both within [-1, 1]. But the value of z clearly goes beyond the limits, although I believe that it should still be inside the frust. W returns to 1, which again makes sense.

Again, I apologize for the romance, but I hope someone can help me figure out what I'm doing wrong.

Thanks!

+6
directx 3d opengl projection
source share
2 answers

Well, it looks like I understood what the problem is.

My projection matrix:

 [2.41421, 0, 0, 0] [0 2.41421, 0, 0] [0, 0, 1.001001, 1] [0, 0, -1.001001, 0] 

But it really should be transposed and be:

 [2.41421, 0, 0, 0] [0 2.41421, 0, 0] [0, 0, 1.001001, -1.001001] [0, 0, 1, 0] 

Using this matrix, the x and y values ​​remain the same as expected, and now my z values ​​are limited to [0, 1] and exceed this range only if they are outside the nearest clip plane.

The only problem right now is that I'm pretty confused about whether I use my right or left hand.

All I know is that now it works ...

+3
source share

I may be out of my league here, but I thought that the goal of the projection matrix and the separation perspective is to open the 2D position of this point on the screen. In this case, the left-over z value will no longer have any meaning, since the math is designed to determine these two values ​​x and y.

Update: I think I get it. Your math is correct. The camera and frustum that you describe have a near-clipping plane at Z = 1, so your example points to (-100, 100, 0) actually outside the clipping plane, so a z-buffer value just below -1 makes sense.

Try a sample point with a z-coordinate inside your truncation, say with a z-coordinate 2.

+2
source share

All Articles