# User clipping in vertex shader

This topic is 3735 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a vertex shader replacing functionality of the fixed pipeline. But the user clipping seems to be a problem. I have 5 self-defined clipping planes. So I assume clipping is only done, when enabled. Frustumclipping is always done automatically. I set my clipping planes relative to the a modelview matrix (camera*scenetransformation). This matrix is pushed to the vertex shader. I set gl_ClipVertex = matWorldView * gl_Vertex; But the result is a crappy rasterization, with lots of errors (depth test fails, etc.). Not setting the gl_ClipVertex results in the desired behaviour, but I can only use the default Projection matrix. If I manipulate the Projection matrix or the result gl_Positions homogenous coordinate the result is not as expected. The framerate is dropping enormously either. I read something in the vertex program specification that clipping is only done if the fixed pipeline transformation is used (ftransform), but I think thats why gl_ClipVertex was introduced from the vertex shader extension. Any hints ?

##### Share on other sites
I've never used user defined clipping planes, so I cannot help you there, however, standard clip space clipping happens after the vertex shader, regardless of calling ftransform().

EDIT : sorry, big oops ^ said fragment shader above, should be vertex shader!

In fact if you roll your own projection, you don't have to call ftransform() at all. If you do your own projection, what you need to remember is that (on OpenGL at least) your output x,y,z values get divided by w (after the vertex shader is finished) to create normalized device coordinates of which any geometry outside a -1 to 1 cube is clipped off. The fact that the normalized device z is clipped from -1 to 1 (instead of 0 to 1) can be confusing the first time.

If you are having z buffer artifacts I would look at the near plane in your projection equation as it greatly effects the precision of the z buffer.

[Edited by - TimothyFarrar on October 30, 2007 1:38:22 PM]

##### Share on other sites
I'm not sure if clipping in the vertex program would work since in the worst case a triangle would be clipped at all 3 edges and thus you get a hexagon. This would mean there are 3 new vertices that are introduced which isn't possible in a vertex program. I'd say that clipping can occur in the geometry shader at the earliest. If you don't have a geomtry shader capable GPU you need to perform clipping in the fragment program.

##### Share on other sites
I'm not entirely sure how gl_ClipVertex works, but Lord_Evil, what your are suggesting is wrong. Clipping does not require geometry shader capable hw (GF8) and it does not necessarily have to be done in the fragment shader.
Clipping for the view frustum planes is most definitly not done in the fragment shader. Clipping for user defined clipping planes can be done in the fragment shaders (Nvidia does it by computing the distance to the clipping plane and interpolating that as a varying across the triangle and then discarding pixels with negative distance; I think ATI did it the other way, before the fragment shader was executed).

[Edited by - Trenki on October 30, 2007 1:32:23 PM]

##### Share on other sites
Maybe this is of some value (from the ARB_vertex_shader spec):
8) How does user clipping work?    DISCUSSION: The OpenGL Shading Language provides a gl_ClipVertex    built-in variable in the vertex shading language. This variable provides    a place for vertex shaders to specify a coordinate to be used by the    user clipping plane stage. The user must ensure that the clip vertex and    user clipping planes are defined in the same coordinate space. Note that    this is different than ARB_vertex_program, where user clipping is    ignored unless the position invariant option is enabled (where all    vertex transformation options are performed by the fixed functionality    pipeline). Here are some tips on how to use user clipping in a vertex    shader:      1) When using a traditional transform in a vertex shader, compute the	 eye coordinates and store the result in gl_ClipVertex.      2) If clip planes are enabled with a vertex shader, gl_ClipVertex must	 be written to, otherwise results will be undefined.      3) When doing object-space clipping, keep in mind that the clip planes	 are automatically transformed to eye coordinates (see section 2.11	 of the GL 1.4 spec). Use an identity modelView matrix to avoid this	 transformation.

##### Share on other sites
Thanks for the good answers. I think if someone has the same problem this thread will give the right hints. And I have another one =)

I found from an ATI SDK document that the projection matrix has to be non-singular if using gl_ClipVertex.

So I will do some math first to check that my projection is correct.