Reputation: 96071
I recently realized that OpenGL performs perspective division not only for x
and y
, but for z
too.
In my understanding x /= w;
and y /= w;
would be enough. Of course, then we would need different projection matrixes.
So, why OpenGL does z /= w;
? To make z-buffer more precise on short distances but less precise on long ones?
Upvotes: 0
Views: 244
Reputation: 45322
Mathematically, dividing all components is the correct way. That way, interpolating z
in screen space linearily (perspective correct interpolation is not done for position data, as it is supposed to be interpolated in screen space).
The linear interpolation in sceen space of course means that looking at this in eye or object space, it appears nonlinear. It simply means for an object not parallel to the image plane, going one pixel to the left on the screen does mean going a variable amount along + or -z, depending on the distance - so the perspecitve actually does distort the z axis, too.
The side effect is that Z buffer precision is highest at the near plane, and that is actually a good thing for most scenes.
Using an "undivided" Z for depth test is called W buffer. But that means that the linear interpolation can't be used any more. However, with modern GPUs, that is not a too big issue.
Upvotes: 1
Reputation: 4763
The projection transformation has the role of moving the objects from World Space to Projection Space (Actually it will be the Camera Space before Projection space, but it's not in the scope ).
Visually every other space is a cube going from -1 to 1 , while The Projection space is a Pyramid section, with the near plane at Z0 and the FarPlane at Z1 (or Z-1 depending on right hand or left hand system) . So z gets morphed as well (unless you have an orthonormal projection). Z goes from 0 to 1 because it doesn't really make any sense for objects behind near plane to get into the rendering pipeline.
You mentioned yourself and in the comment as well about the Z Buffer precision. It's precision won't be changed, however, after Projection Transformation, the objects Z deltas will be less between objects near the far plane and more near the objects near the near plane (In less fancier words : the distance on the Z axis between objects close to the NearPlane will be increased, while the distance on the Z axis between objects near the FarPlane will be decreased).
This is why reducing the Near Plane and Far Plane distance sometimes fixes Z Fighting : The distance between far away objects will be reduced with less if the distance between the two planes is lesser.
Upvotes: 0