• Advertisement
Sign in to follow this  

Near/far clipping in a software rasterizer

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys, I'm currently writing a little software-based triangle rasterizer and everything is going alright, besides having a problem clipping polygons that are in front of the near plane. What's the correct method of doing this? Clipping Z in the homogenous clipspace is not working, because vertices will sometimes go to infinity-values if they lie on z=0 in camera space. Do I need to clip against the w-value? That would be much more complex, because w can't be interpolated linearly in screenspace.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Clipping Z in the homogenous clipspace is not working, because vertices will sometimes go to infinity-values if they lie on z=0 in camera space.
After the perspective division, just clip z values outside the (-1,1) range. You can specifically look for the case where pre-division w=0 and discard those as well.

Share this post


Link to post
Share on other sites
What do you mean by "discard" them. I can't just not draw the entire triangle if one of the vertices lies on e.g. (0,0,0) in camera-space.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Try checking depth(Z) per pixel, not per vertex, then eliminating pixels that have a depth of <

Share this post


Link to post
Share on other sites
Crap, forgot to login; anyways, it'd be good to loop up 'Z-Buffering'--it solves the problem you're talking about.

Share this post


Link to post
Share on other sites
No it does NOT (and I'm already using it). It does not, because the vertex coordinates go to infinity even before drawing the triangle itself if the vertex lies on the camera-plane.

Share this post


Link to post
Share on other sites
Quote:
Original post by noVum
What do you mean by "discard" them. I can't just not draw the entire triangle if one of the vertices lies on e.g. (0,0,0) in camera-space.


From my understanding you generate new vertices at the edge. Don't quote me on that though :S

Share this post


Link to post
Share on other sites
Quote:
Original post by noVum
What do you mean by "discard" them. I can't just not draw the entire triangle if one of the vertices lies on e.g. (0,0,0) in camera-space.

If one of your vertices is at 0,0,0 at camera space, the triangle is not visible. Consider a triangle with one of the vertices in the middle of your eye (0,0,0 in camera space). Definitionally, the triangle is entirely perpendicular to your view direction and is thus not visile.

Share this post


Link to post
Share on other sites
Then take (2,3,0) or something else. It doesn't matter as long as it lies on the camera plane. Besides that I'm not convinced that a triangle that has (0,0,0) as one of it vertices is always not visible. Makes no sense to me.

Share this post


Link to post
Share on other sites
Quote:
Then take (2,3,0) or something else. It doesn't matter as long as it lies on the camera plane.
Not a problem, because clipping is done before the perspective division. So the values haven't gone to infinity yet. The 0,0,0 thing is really just an extra early-out.
Quote:
Besides that I'm not convinced that a triangle that has (0,0,0) as one of it vertices is always not visible. Makes no sense to me.
Hm... not sure how to convince you of this. Try to think of a counterexample.

Share this post


Link to post
Share on other sites
Quote:
Not a problem, because clipping is done before the perspective division.

That's what I was asking for... So I need to clip w<znear and w>zfar?

Quote:
Hm... not sure how to convince you of this. Try to think of a counterexample.

Not important now. That was perhaps just a bad example... There are certainly triangles that are visible and have one vertex on the camera-plane.

Share this post


Link to post
Share on other sites
Quote:
That's what I was asking for... So I need to clip w<znear and w>zfar?
Welllll.... yes. That's a little misleading, though, since once your W-values are no longer 1 the other clipping planes get weirder too. The fact that you can clip like that for the near and far planes is pretty much a clever artifact. This page has a moderately good description of the plane equations for clipping before the perspective division.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Clip before perspective division at z = w and z = -w. This should be done for all x and y also. Do it like this:

Multiply with your perspective matrix:
[x y z w]' = M * [x0 y0 z0 1]'

Clip the following planes:
x = w, x = -w, y = w, y = -w, z = w, z = -w

THEN do division:
x /= w, y /= w, z /= w

If you do it like this, all your problems should go away

Share this post


Link to post
Share on other sites
Because all the values are divided by W. So if you checked to see if each was in the range [-1,1] after the homogeneous divided, then you check to make sure each one is in-between [-W,W] before the homogeneous divide (in other words, the range is divided by W as well). The side planes are just as important and the near and far planes :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Clip before perspective division at z = w and z = -w. This should be done for all x and y also. Do it like this:

Multiply with your perspective matrix:
[x y z w]' = M * [x0 y0 z0 1]'

Clip the following planes:
x = w, x = -w, y = w, y = -w, z = w, z = -w

THEN do division:
x /= w, y /= w, z /= w

If you do it like this, all your problems should go away
That's pretty good advice, but it might not solve every last little problem.
I myself have a hobby software 3D renderer. I wonder if the OP has the same problem as me. Allow me to explain...

Even when the polygon is clipped perfectly as above (which it is in my case[smile]), I still use the original unclipped verticies for calculating the dx and dy of the polygon in screen-space. You can't just blindly use the clipped verticies because the 3 you pick after clipping might be all in a line, or very close to it, which wont work for calculating the dx & dy. The best way I know of is to use the original coordinates, which works perfectly fine except in the case where one of the verticies is on the camera plane. (Even if some are behind the camera it is no problem)

I know some people calculate the screenspace dx for each span. However that can be inaccurate in some situations, and it is supposed to be constant across the entire triangle anyway, and why calclulate it more than once when once will do.

Anyway, my solution is to offset all of the points of the triangle in the z-direction ever so slightly if one of them is on the camera plane (Well actually its a while loop just in case it actually puts a different one of the points on the camera plane in the process). It works, and produces no noticeable artifacts. It would be good to eliminate this litle loop, but I don't think that is possible.

Sorry if this is not the same problem the OP mentioned. I hope people will not confuse it with the OPs problem if it is different.

noVum: Are you interested in sharing code, or at least finding out what each others rasteriser is like?

Share this post


Link to post
Share on other sites
Quote:
Original post by noVum
Quote:
This should be done for all x and y also.

Why?
Because if you fully clip in 3D you can completely avoid 2D-clipping!
Also, if a triangle is way way off to the side, but just in front of the camera plane, you might get integer overflows that cause bits of that triangle to end up in view or something like that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement