Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

At what stage is clipping performed in the graphics pipeline?

I am writing a soft renderer in C++ for learning purposes and I have reached a bit of a block. I don't know if I am missing something but I am not sure when clipping is performed. Some sources talk about it happening in clip space after the perspective projection matrix is applied but before the perspective divide (so still in 3D space). Other sources talk about performing it in 2D space (after the perspective divide) with an algorithm such as the Cohen-Sutherland line clipping algorithm. If I am supposed to clip in 3D space then is the depth buffer only used for deciding what order to rasterize objects or must I clip in 2D but also prevent rasterization of points beyond -1 in the z buffer?

Basically, the information on this is not very clear and I haven't actually seen how one can actually clip in homogeneous clip space. It is said that you should do it but nobody shows how.

like image 523
zzef Avatar asked Mar 03 '23 15:03

zzef


2 Answers

Clipping is done in the Vertex Post-Processing stage in the Rendering Pipeline.

Primitives are clipped to the view volume, depending on the homogeneous clip-space position of its vertices (gl_Position), before the perspective divide. The clipping rule is:

-w <=  x, y, z  <= w.

For a detailed information see OpenGL 4.6 API Core Profile Specification - 13.7 Primitive Clipping

like image 68
Rabbid76 Avatar answered Apr 09 '23 23:04

Rabbid76


Some sources talk about it happening in clip space after the perspective projection matrix is applied but before the perspective divide (so still in 3D space). Other sources talk about performing it in 2D space (after the perspective divide) with an algorithm such as the Cohen-Sutherland line clipping

Traditional clipping algorithms like Cohen-Sutherland line clipping or Sutherland-Hodgman polygon clipping might be described in 2D, but they in principle work in 3D as well.

Basically, the information on this is not very clear and I haven't actually seen how one can actually clip in homogeneous clip space. It is said that you should do it but nobody shows how.

It follows naturally from the definition of the homogeneous space and the clipping condition -w <= x,y,z <= w. Consider clipping a straight line segment AB (and polygon clipping can basically be implemented as a series of line clipping on the polygon edges). The Cohen-Sutherland line clipping algorithm trivially extends to homogeneous space, you just apply -w and w as the bounds of the viewing volume in each dimension. For the actual intersection calculation, we need to search for a new homogeneous point C =(C_x, C_y, C_z, C_w)^Tso that C lies on the intersection plane and on AB, so we need to find a t for

C = t * A + (1-t) * B

Assume we want to clip against the near plane, which is just z = -w.

For C to lie on that plane, it follows that C_z = -C_w. Which leaves us with a simple linear system of equations:

C_x = t * A_x + (1-t) * B_x
C_y = t * A_y + (1-t) * B_y
C_z = t * A_z + (1-t) * B_z = -t * A_w - (1-t)*B_w
C_w = t * A_w + (1-t) * B_w 

From the equation for C_z if follows that:

t = (B_z + B_w) / ( (B_z + B_w) - (A_z + A_w))

Also note that this t can also be used to interpolate all the associated vertex attributes for C. The linear interpolation is perfectly sufficient even in perspective distorted cases, because we are before the perspective divide here, were the whole perspective transformation is perfectly affine wrt. the 4D space we work in.

like image 29
derhass Avatar answered Apr 09 '23 22:04

derhass