Attribute interpolation for new vertices after clipping in homogenous clip space

Started by
5 comments, last by pseudomarvin 7 years, 4 months ago

I am implementing SW rasterization. I want to clip triangles against the z = -near (so that strange things don't happen after the perspective divide). I would like to do the clipping right after the vertex shader - that is right after the vertices are multiplied by the MVP matrix and before they are divided by the w coordinate (they are in homogenous clip space), so I would really clip against the z = -w plane. However, after clipping the triangles, new vertices have to be created replacing the clipped ones. Attribute values for these new vertices have to be interpolated based on the distance of the clipped vertex to the z = -w plane relative to the total length of the edge that was clipped.

I know that if I clipped in view space I could get away with simple linear interpolation of the vertex attributes. Is this still true in homogenous clip space (since we have not done perspective division yet)?

Advertisement

You can't use linear interpolation in screen space for attributes which aren't linear in screen space (e.g. UV, position, normal etc.).

BTW you need also some guard-bard (clipping against offseted frustum sides). Without guard-band you will have precision issues when interpolating attributes.

Yeah, thanks. I guess I use a guard band (clamp the raster space coordinates to 0, width -1 or height - 1) and use Floating Point arithmetic so actually an infinite guard band.

Guard-band's idea is that you reduce amount of clipping to minimum. You clip only if a triangle intersects near plane or intersects massively offseted frustum sides. In your case it look like you will be clipping more triangles, as you clip with non-offseted frustum sides.

This is just an explanation what's is guard band. It's not crucial for a fast rasterizer, as clipping is a relatively rare operation. The crucial part is to do some clipping with (offseted or not) frustum sides, so your interpolation math won't explode.

Yes, you can linearly interpolate in homogeneous clip space since the MVP is a linear transformation in 4d. Unfortunately I can't think of a better reference than my own software rasterizer but I'm sure googling would be useful as it was for myself: https://github.com/loreStefani/SoftRP/blob/master/SoftRP/SHClipper.cpp

@IoreStefani you are 100% correct, sorry pseudomarvin for the misinformation. I just checked my own clipper and I'm also doing linear interpolation in homogeneous space.

Thanks for the clarification :) .

This topic is closed to new replies.

Advertisement