# Details of what GPU actually do when clipping in 4D homogeneous space?

I am learning programmable rendering pipeline by implementing a tiny software renderer. I try to implement it in a 'hardware' style. However, I am not familiar with the GPU pipeline and got some problems of homogeneous clipping.

The homogenous clipping space contains a w variable which various at each vertex. Is each vertex object's homogenous coordinate (between projection matrix and homogenous division by w) at its own clipping space? If so, how to clip the lines and triangles nearer than the Frustum or even stretching behind the camera (i.e. w <= frustum_znear)?

Update: this thread says that the clipping in homogeneous space is directly an intersection problem in the 4D homogeneous space. Which means the intersection point is p_vec4 = t * point1_vec4 + (1 - t) * point2_vec4. Say I have P0(-70, -70, 118, 120) and P1(-32, -99, -13, -11) in 4D homogeneous space, and the intersection point with plane w = -z (which in NDC is z = -1) is (-35, -96, -1, 0.9) t = 0.99, how to get the corrresponding vertex object in NDC space?

And once I get the correct intersection point, should I do the interpolation between vertex objects produced by vertex shader to get new vertex object?

• This strikes me as too many questions for a single post & should probably be split up. The general rule is a single question (or at least key question) per post. Commented May 28, 2016 at 22:32
• @Pikalek I remove the other two questions and try to make this thread reasonable. Thanks for your advice. Commented May 29, 2016 at 3:24

Clipping is done in 3D space before 'w' division, not in 4D space.

The GPU finds either just the near and far planes, or all 6 3D planes of the view frustum and clip the Geo to this.

If w division was done before the sign of coordinates would flip for vertices behind the eye/camera.

If only near-far planes are used to 3D clip before w division then may rely solely on 2D clipping at the rasterizing stage for the x & y planes.

W is just a projection divider directly related to Z, not actually a 4th dimension. The 4x4 matrix is a "hack" to include a translation and projection division in a convenient format. It works only because positions are implied to be (x, y, z, 1) and normals are implied to be (x, y, z, 0).

But it's not actually 4 independent dimensions.

Any other value for "W" that is not 1 or 0 make little sense for geometry, it's a convenient on/off switch for turning off translation.

If you're asking about converting homogeneous clip space coords to normalized device coordinates (NDC) coords, the process is: <x y z w> → <x/w y/w z/w>

This GDSE Q/A on Why is clip space always referred to as “homogeneous clip space”? may also be helpful to you.

• Thanks for your answer. However, my problem is about the interpolation. Say two points e1, e2 under 3D eye coordinate are projected to the 4D homogeneous clipping space h1, h2. Then we do interpolation in 4D homogeneous space, the segment h1-h2 is clipped at 4D point h(t)=t*h1+(1-t)*h2. Without loss of generality, suppose we have h1-h(t) part (which is viewable) feeding to the rasterization stage. So we need to generate the corresponding vertex properties (same as the output format of vertex shader). My question is how to generate these new vertices' properties? Commented May 29, 2016 at 15:36
• My understanding is that interpolation isn't done in HCS, but I might be mistaken. If you don't get more / better answers here you might try reasking / migrating to SO. Commented May 29, 2016 at 18:06
• Ok. Could I directly migrate this thread to SO, or I need to reopen a new question there. Commented May 30, 2016 at 3:50
• My mistake for suggesting you re-ask; cross posting is generally discouraged. Commented May 30, 2016 at 4:55
• I think the admins can migrate tho. Commented Dec 5, 2017 at 6:11