# Z Value after Perspective Divide is always less than -1

This topic is 3109 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Also posted on StackOverflow: http://stackoverflow.com/questions/3255837/z-value-after-perspective-divide-is-always-less-than-1

Hi All,

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!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 9
• ### Forum Statistics

• Total Topics
634082
• Total Posts
3015400
×