How can I check if vertex is visible in the most simple way?
If my vertex shader looks like:
void main(void) {
vec4 glPosition = vec4(VTPosition.x * VTAspectRatio, VTPosition.y, VTPosition.z, 1.0);
gl_Position = VTProjection * VTModelview * glPosition;
}
Can I check visibility on CPU the same way ?
Vector4 vertex = {0.5, 0.5, -1.0, 1.0};
vertex = projectionMatrix * modelViewMatrix * vertex;
if vertex x and y value is in range -1.0 .. 1.0 (viewport coordinates) it is visible
The output position of the vertex shader (gl_Position
) will undergo perspective division to obtain NDC (Normalized Device Coordinates). In the NDC space, clipping is against the [-1.0, 1.0] range for all coordinates (1).
So to test if a given vertex will be clipped, you have to determine if gl_Position.xyz / gl_Position.w
is in the range [-1.0, 1.0] for all coordinates. GLSL code to test this condition could look like this:
if (any(lessThan(gl_Position.xyz, vec3(-gl_Position.w))) ||
any(greaterThan(gl_Position.xyz, vec3(gl_Position.w))))
{
// vertex will be clipped
}
(1) Strictly speaking, clipping can be performed at various points in the rendering pipeline, as long as geometry outside the viewing volume ends up being clipped. But it's easiest to express in NDC.