Why we need clipping?

Let’s start with a question. What is clipping? Clipping is a process in graphics where the GPU rules out every vertex out of camera view. There are many reasons for this process’ existence, first thing comes to mind is efficiency where eleminating vertices out of view would save us a lot of time further down the gpu pipe-line. Second thing is i’ve talked about camera transformations and projection matrices on my previous post, where every frame camera, gets first translated to origin, rotated so that viewing direction coincides with z-axis and the up vector coincides with y-axis(which is actually achieved by a single matrix as i’ve talked about few posts ago), then every vertex gets hit by projection matrix so that they can carried on to image-space and then to screen space. All this sounds nice and dandy but what about the vertices we don’t see? Remember our projection matrix:

$$\begin{bmatrix} \cot\frac{\alpha}{2} & 0 & 0 & 0 \\ 0 & \cot\frac{\alpha}{2} & 0 & 0 \\ 0 & 0 & \frac{f+n}{f-n} & \frac{2fn}{f-n} \\ 0 & 0 & 1 & 0 \end{bmatrix}$$

Take a look at the bottom row, 3rd column. Before multiplying with projection matrix, all our points look like this

\begin{bmatrix}x \\ y \\ z \\ 1\end{bmatrix}

but after multiplication because of the last row,3rd column part that i pointed out before, our matrix will look like this

\begin{bmatrix}x \\ y \\ z \\ w\end{bmatrix}

where the value is defined my that portion, which is actually the of our matrix. The idea here is that, greater the z value, further the object therefore smaller. So when we convert our point from 3d space to image-space, we divide all our , and coodinates by and voila! the greater the the smaller our object is(which is actually a really cool hack found by a cool dude named Mobius, and is the reason why we are using homogenous coordinates, to facilitate projection basically). So what is waiting for a vertex behind the camera then? Well lets take it step by step. When the camera gets translated to origin with all the rotation and what not, the vertex gets translated with it therefore ending up with a negative value, which will cause a negative after projection, so when we are porting to image space, dividing every coordinate with a negative will leave us with negative coordinates, which will end up as an upside down image. So clipping is a serious business.

How it is done?

Image our viewing frustum, you can imagine it as a funky box with confined by six planes and what we need to find out is if a vertex is inside this box or outside? Actually the process here is pretty simple and straightforward. Every plane here has a normal vector on it and a point. We shall call this and respectively. Say we have a point and we want to know where it lies, well what we do is to substract from which will give us a vector, if actually is on the plane, this means that our vector is perpendicular to so their dot product should be 0.

$$\vec{N} \cdot (Q-P) = 0$$

If is inside the box, this means they are not perpendicular anymore, lets call the angle between them ,

$$\vec{N} \cdot (Q-P) = \lVert\vec{N}\rVert \lVert Q-P\rVert \cos\theta$$

It is not hard to see that the product will be a positive number, where if the vertex is outside the box, will be more then 90 degrees which will produce a negative number. To summarize:

Doing this with a single vertex is pretty simple but lets be honest a vertex is never alone right?

Lets think about two points on is in the inside and one is outside. These points are bound with a line between them and guess what? We need to split this line i a way that we exclude the point on the outside but keep the line intact in our frustum, basically we need a point that is on the plane and also on the line. Lets start by taking two points and . We’ll call our point on the plane . Now we can define any point on vector by which includes our point also. So:

$$I=Q_1 + t(Q_2-Q_1)$$

But what is then? Brace yourselves. I will define two variables and where

$$d_1 = \vec{N} \cdot (Q_1 - P)$$

And:

$$d_2 = \vec{N} \cdot (Q_2 - P)$$

Now, by subtracting from both sides on our equation and then dotting both sides with i get an equation like this:

$$\vec{N} \cdot (I-P) = \vec{N} \cdot (Q_1-P) + t(\vec{N} \cdot (Q_2-P)-\vec{N} \cdot (Q_1-P))$$

Which is actually:

$$0 = d_1 + t(d_2-d_1)$$

Aaaand voila! here is our t:

$$t =\frac{d_1}{d_1- d_2}$$

Well i guess i covered the basics of clipping for now. There are many more to it i just scratched the surface but maybe i will continue on this topic later. Cheers for your time!