Clipping is down in homogeneous clip space, therefore after multiplying the vertices by the model-view matrix. But as we do clipping in clip space, the position of the new vertices that we interpolate is in clip space, so how can I translate back those new vertices coordinates into view space?
I could perform the inverse transformation, but it seems costly. So how exactly do clipping work in the graphics pipeline? I mean, how can we get the position of the new generated vertices in view space?
For example, with OpenGL, I would do the following in the vertex shader:
layout (location = 0) in vec3 pos; // vertex position in model space
out vec3 posViewSpace;
// M V P are the model view projection matrices
void main() {
posViewSpace = V * M * pos; // I want the pos in view space for shading
gl_Position = vec4(P * V * M * pos, 1.0); // Give OpenGL the position in clip space
}
But how can this work, because posViewSpace
that will be given to the pixel shader has nothing to do with the new vertices generated after clipping? Or maybe it doesn't matter, because posViewSpace
will be interpolated from the original vertices, but the pixel shader won't notice? Will the interpolation be correct and usable in the pixel shader, even if it's performed from the original vertices and not the new generated vertices?