# Near/far clipping in a software rasterizer

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

## 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 on other sites
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 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 on other sites
Try checking depth(Z) per pixel, not per vertex, then eliminating pixels that have a depth of <

##### 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 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 on other sites
Quote:
 Original post by noVumWhat 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 on other sites
Quote:
 Original post by noVumWhat 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 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 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.

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633313
• Total Posts
3011315
• ### Who's Online (See full list)

There are no registered users currently online

×