Skip to main content
edited body
Source Link
Nicol Bolas
  • 10.1k
  • 20
  • 28

In this case, the geometry of similar triangles ABC and ADE is used to determine the height of D via the solution of DE. It is obvious that if the near plane is at 0 (AE=0), then a division by 0 occurs -- hence, why the near plane cannot be located at position 0.

This is not why the nearZ plane cannot be zero. The goal of perspective math is not to project the E onto the near plane. If you look at the actual perspective matrices, you'll find that the nearZ only applies to the computed clip-space Z coordinate, not the X and Y.

Remember: projection is essentially transforming a position such that you lose one or more dimensions. You're projecting from 3D space to 2D space. So projection is removing the Z component, projecting a scene into a 2D region of space. Orthographic projection just discards the Z; perspective projection does something more complex, rescaling the X and Y based on the Z.

Of course, we're not really doing that; Z still exists. Otherwise, the depth buffer wouldn't work. But the basic perspective projection math does not define how the Z is computed.

Therefore, we can compute the Z however we want. The traditional depth equation (for OpenGL's standard NDC space) is where near/farZ come from:

$$Z_{ndc} = \frac{\frac{Z_{camera}(F + N)}{N - F} + \frac{2NF}{N - F}}{-Z_{camera}}$$

N and F are near/farZ, respectively. If N is zero, then the equation degenerates:

$$Z_{ndc} = \frac{\frac{Z_{camera}(F)}{-F} + 0}{-Z_{camera}}$$ $$Z_{ndc} = \frac{-Z_{camera}}{-Z_{camera}}$$ $$Z_{ndc} = 1$$

Oops.

So your question really is why we use depth computations (which again, don't really have anything to do with perspective projection) that require near/farZ values like this.

This equation, and those like it, has some very nice properties. It plays well with the clip-space W component needed for perspective projection (the division by $-Z_{camera}$). But it also has the effect of being a non-linear Z projection. It puts more precision at the front of the scene than in the distance. And that's generally where you need depth precision the most.

There are alternative ways to compute the clip-space Z. You can use a linear method, which doesn't forbid near Z from being 0. You can also use a method that allows for an infinitely distant far Z (but still relies on near Z); this requires a floating-point Z buffer, though. And so forth.

You haven't really shown your code, so I can't say if it truly "works". The clip-space W in particular is important, as it is used for perspective-correct interpolation of vertex dataperspective-correct interpolation of vertex data. If you set it to one, then you won't get proper perspective-correct interpolation.

In this case, the geometry of similar triangles ABC and ADE is used to determine the height of D via the solution of DE. It is obvious that if the near plane is at 0 (AE=0), then a division by 0 occurs -- hence, why the near plane cannot be located at position 0.

This is not why the nearZ plane cannot be zero. The goal of perspective math is not to project the E onto the near plane. If you look at the actual perspective matrices, you'll find that the nearZ only applies to the computed clip-space Z coordinate, not the X and Y.

Remember: projection is essentially transforming a position such that you lose one or more dimensions. You're projecting from 3D space to 2D space. So projection is removing the Z component, projecting a scene into a 2D region of space. Orthographic projection just discards the Z; perspective projection does something more complex, rescaling the X and Y based on the Z.

Of course, we're not really doing that; Z still exists. Otherwise, the depth buffer wouldn't work. But the basic perspective projection math does not define how the Z is computed.

Therefore, we can compute the Z however we want. The traditional depth equation (for OpenGL's standard NDC space) is where near/farZ come from:

$$Z_{ndc} = \frac{\frac{Z_{camera}(F + N)}{N - F} + \frac{2NF}{N - F}}{-Z_{camera}}$$

N and F are near/farZ, respectively. If N is zero, then the equation degenerates:

$$Z_{ndc} = \frac{\frac{Z_{camera}(F)}{-F} + 0}{-Z_{camera}}$$ $$Z_{ndc} = \frac{-Z_{camera}}{-Z_{camera}}$$ $$Z_{ndc} = 1$$

Oops.

So your question really is why we use depth computations (which again, don't really have anything to do with perspective projection) that require near/farZ values like this.

This equation, and those like it, has some very nice properties. It plays well with the clip-space W component needed for perspective projection (the division by $-Z_{camera}$). But it also has the effect of being a non-linear Z projection. It puts more precision at the front of the scene than in the distance. And that's generally where you need depth precision the most.

There are alternative ways to compute the clip-space Z. You can use a linear method, which doesn't forbid near Z from being 0. You can also use a method that allows for an infinitely distant far Z (but still relies on near Z); this requires a floating-point Z buffer, though. And so forth.

You haven't really shown your code, so I can't say if it truly "works". The clip-space W in particular is important, as it is used for perspective-correct interpolation of vertex data. If you set it to one, then you won't get proper perspective-correct interpolation.

In this case, the geometry of similar triangles ABC and ADE is used to determine the height of D via the solution of DE. It is obvious that if the near plane is at 0 (AE=0), then a division by 0 occurs -- hence, why the near plane cannot be located at position 0.

This is not why the nearZ plane cannot be zero. The goal of perspective math is not to project the E onto the near plane. If you look at the actual perspective matrices, you'll find that the nearZ only applies to the computed clip-space Z coordinate, not the X and Y.

Remember: projection is essentially transforming a position such that you lose one or more dimensions. You're projecting from 3D space to 2D space. So projection is removing the Z component, projecting a scene into a 2D region of space. Orthographic projection just discards the Z; perspective projection does something more complex, rescaling the X and Y based on the Z.

Of course, we're not really doing that; Z still exists. Otherwise, the depth buffer wouldn't work. But the basic perspective projection math does not define how the Z is computed.

Therefore, we can compute the Z however we want. The traditional depth equation (for OpenGL's standard NDC space) is where near/farZ come from:

$$Z_{ndc} = \frac{\frac{Z_{camera}(F + N)}{N - F} + \frac{2NF}{N - F}}{-Z_{camera}}$$

N and F are near/farZ, respectively. If N is zero, then the equation degenerates:

$$Z_{ndc} = \frac{\frac{Z_{camera}(F)}{-F} + 0}{-Z_{camera}}$$ $$Z_{ndc} = \frac{-Z_{camera}}{-Z_{camera}}$$ $$Z_{ndc} = 1$$

Oops.

So your question really is why we use depth computations (which again, don't really have anything to do with perspective projection) that require near/farZ values like this.

This equation, and those like it, has some very nice properties. It plays well with the clip-space W component needed for perspective projection (the division by $-Z_{camera}$). But it also has the effect of being a non-linear Z projection. It puts more precision at the front of the scene than in the distance. And that's generally where you need depth precision the most.

There are alternative ways to compute the clip-space Z. You can use a linear method, which doesn't forbid near Z from being 0. You can also use a method that allows for an infinitely distant far Z (but still relies on near Z); this requires a floating-point Z buffer, though. And so forth.

You haven't really shown your code, so I can't say if it truly "works". The clip-space W in particular is important, as it is used for perspective-correct interpolation of vertex data. If you set it to one, then you won't get proper perspective-correct interpolation.

Source Link
Nicol Bolas
  • 10.1k
  • 20
  • 28

In this case, the geometry of similar triangles ABC and ADE is used to determine the height of D via the solution of DE. It is obvious that if the near plane is at 0 (AE=0), then a division by 0 occurs -- hence, why the near plane cannot be located at position 0.

This is not why the nearZ plane cannot be zero. The goal of perspective math is not to project the E onto the near plane. If you look at the actual perspective matrices, you'll find that the nearZ only applies to the computed clip-space Z coordinate, not the X and Y.

Remember: projection is essentially transforming a position such that you lose one or more dimensions. You're projecting from 3D space to 2D space. So projection is removing the Z component, projecting a scene into a 2D region of space. Orthographic projection just discards the Z; perspective projection does something more complex, rescaling the X and Y based on the Z.

Of course, we're not really doing that; Z still exists. Otherwise, the depth buffer wouldn't work. But the basic perspective projection math does not define how the Z is computed.

Therefore, we can compute the Z however we want. The traditional depth equation (for OpenGL's standard NDC space) is where near/farZ come from:

$$Z_{ndc} = \frac{\frac{Z_{camera}(F + N)}{N - F} + \frac{2NF}{N - F}}{-Z_{camera}}$$

N and F are near/farZ, respectively. If N is zero, then the equation degenerates:

$$Z_{ndc} = \frac{\frac{Z_{camera}(F)}{-F} + 0}{-Z_{camera}}$$ $$Z_{ndc} = \frac{-Z_{camera}}{-Z_{camera}}$$ $$Z_{ndc} = 1$$

Oops.

So your question really is why we use depth computations (which again, don't really have anything to do with perspective projection) that require near/farZ values like this.

This equation, and those like it, has some very nice properties. It plays well with the clip-space W component needed for perspective projection (the division by $-Z_{camera}$). But it also has the effect of being a non-linear Z projection. It puts more precision at the front of the scene than in the distance. And that's generally where you need depth precision the most.

There are alternative ways to compute the clip-space Z. You can use a linear method, which doesn't forbid near Z from being 0. You can also use a method that allows for an infinitely distant far Z (but still relies on near Z); this requires a floating-point Z buffer, though. And so forth.

You haven't really shown your code, so I can't say if it truly "works". The clip-space W in particular is important, as it is used for perspective-correct interpolation of vertex data. If you set it to one, then you won't get proper perspective-correct interpolation.