Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Z Value after Perspective Divide is always less than -1

So I'm writing my own custom 3D transformation pipeline in order to gain a better understanding of how it all works. I can get everything rendering to the screen properly and I'm now about to go back and look at clipping.

From my understanding, I should be clipping a vertex point if the x or y value after the perspective divide is outside the bounds of [-1, 1] and in my case if the z value is outside the bounds of [0, 1].

When i implement that however, my z value is always -1.xxxxxxxxxxx where xxxxxxx is a very small number.

This is a bit long, and I apologize, but I wanted to make sure I gave all the information I could.

First conventions:

I'm using a left-handed system where a 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 set up with:

A vertical FOV in radians of PI/4.

An aspect ration of 1. (Square view port)

A near clip value of 1.

A far clip value of 1000.

An initial world x position of 0.

An initial world y position of 0.

An initial world z position of -500.

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

Given a vertex, the pipeline works like this:

Step 1: Multiply the vertex by the camera matrix.

Step 2: Multiply the vertex by the projection matrix.

Projection matrix is:

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

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

Step 4: [This is where the problem is] Clip the vertex if outside bounds.

Step 5: Convert to screen coordinates.

An example vertex that I have is

(-100, -100, 0, 1)

After multiplying by the camera matrix i get:

(-100, -100, 500, 1)

Which makes sense because relative to the camera, that vertex is 100 units to the left and down and 500 units ahead. It is also between the near clip of 1 and the far clip of 1000. W is still 1.

After multiplying by the projection matrix i get:

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

This I'm not sure if it makes sense. The x and y seem to be correct, but i'm iffy about the z and w since the next step of perspective divide is odd.

After the perspective divide I get:

(0.401966, 0.401966, -1.001665, 1)

Again the x and y make sense, they are both within the bounds of [-1, 1]. But the z value is clearly outside the bounds even though i believe it should still be within the frustrum. W is back to 1 which again makes sense.

Again apologies for the novel, but I'm hoping someone can help me figure out what I'm doing incorrectly.

Thanks!

like image 980
Jon Avatar asked Jul 15 '10 13:07

Jon


People also ask

What is perspective divide?

Once all the vertices are transformed to clip space a final operation called perspective division is performed where we divide the x , y and z components of the position vectors by the vector's homogeneous w component; perspective division is what transforms the 4D clip space coordinates to 3D normalized device ...

How do you calculate perspective projection?

w′=x∗0+y∗0+z∗−1+1∗0=−z. This has for effect to set w' to -z. And if -z is different than 1, then the coefficient of the transformed points will need to be normalized. This how or when more precisely the perspective divide is performed when a point is multiplied by a projection matrix.

How do projection matrices work?

What are projection matrices? They are nothing more than 4x4 matrices, which are designed so that when you multiply a 3D point in camera space by one of these matrices, you end up with a new point which is the projected version of the original 3D point onto the canvas.

What is perspective transform matrix?

The homography or perspective transformation is defined by a matrix that defines a mapping of pixel coordinates: (6.4) Here are pixel coordinates in the output image, and are uniform pixel coordinates in the input image. The normal input pixel coordinates are given by. (6.5)


1 Answers

Ok, it looks like I figured out what the problem it was.

My projection matrix was:

[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]

When using this matrix, my x and y values stay the same as expected and now my z values are constrained to be within [0, 1] and only exceed that range if they are outside the near of far clip plane.

The only issue now is that I'm quite confused as to whether I'm using a right or left handed system.

All i know is that now it works...

like image 75
Jon Avatar answered Nov 15 '22 10:11

Jon