Question

I'm trying to do a simple perspective projection in the process of rasterizing a 3D point. Here are all the matrices and other info. All Matrices are row major. The coordinate system is Right Handed.

The Camera is at [0,0,-1] and the point is at [0,0,0] (w=1 for matrix operations)

Model View Matrix(Inverse of Cam Matrix i.e, tx = 0;ty = 0; tz = 1):

[1 0 0 tx]
[0 1 0 ty]
[0 0 1 tz]
[0 0 0 1 ]

Perspective Matrix:

[f/aspect,0,0,0]
0,f,0,0
0,0,-(near+far)/(near-far),2*far*near/(near-far)
0,0,1,0]

aspect is equal to 1 since the viewport is square. Far = 100 and Near = 0.1 f = 1/tan(fovDegress*M_PI/360);

The resultant Matrix is:

1.94445, 0,        0,        0
0,       1.944445, 0,        0
0,       0,        1.020202, -2.020202
0,       0,        1,        0

Now I apply the Model View Matrix and then the Projection Matrix to the point vector and then I get a new point Pv = {x,y,z,w} Then I get the normalized co-ordinates x' = x/w ; y' = y/w; and z' = z/w; x' and y' always lie in between [-1,1] as long as the point is in the frustum. But the same isn't the case for z'. As the point approaches near the camera, the z' values increases exponentially. When the point is at [0,0,0] z' value equals -1.

Now, I need to clip some lines, so I need z' value to be in between [1,-1]. I am wondering what's wrong with my procedure. Thank you.

Was it helpful?

Solution

What you experiencing is the nonlinearity of perspectivic depth mapping. With a frustum projection matrix this follows a 1/x law with distance from the viewpoint.

EDIT:

Just double checked your matrices: You got your frustum matrix wrong. The correct frustum matrix is

f/aspect, 0,                      0,                     0
0,        f,                      0,                     0
0,        0, -(far+near)/(far-near), 2*far*near/(far-near)
0,        0,                      1,                     0

Still you'll run into a division by zero if you approach the origin. Just put the vector (0,0,0,1) through that thing, which results in (x=0,y=0,z=2*far*near/(far-near),w=0) in clip space. And then the homogenous division {x,y,z}/(w=0) ← blows up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top