5

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.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
rwb
  • 621
  • 1
  • 12
  • 21

1 Answers1

3

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.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • But if they are normalized homogenous shouldn't the depth be in the range of [-1,1]? If thats how it is supposed to be then how does OpenGL perform clipping at this stage of the pipeline? Please bear with me, i am just trying to understand. Thank you. – rwb May 04 '11 at 15:48
  • @rwb: The depth range [-1,1] is mapped between near and far clipping plane. However the viewpoint itself is always closer than the near clipping plane (and a near clip distance of 0 is illegal in perspective mode, as this would be division through 0). – datenwolf May 04 '11 at 15:52
  • 1
    Ah! So it isn't possible to clip lines near the camera plane? It has to be done only at the Near clipping plane? If so can the near clipping plane be less than one but not zero? In my case the near is 0.1, but when the cam is at (0,0,-1) and the point is at (0,0,-0.9) the z value is still astronomically high. Shouldn't it be in the range at that coordinate since its on the near plane? – rwb May 04 '11 at 22:56