In my current project I render with different materials that all have their own shader, and I have portals like in the Portal game. For the portals I had to do some extra clipping. In OpenGl there is a function called glClipPlane that would work perfectly for this job. But unfortunately I have a core context, and I implemented this clipping in the fragment shader with "discard". But it required me to copy and paste the code into every material that I had written so far. So what is the correct way that does not require me to copy paste my code everywhere?
As pointed out, there is a better way to solve the clipping problem, but this problem is only an example, so please do not try to find an alternative solution for clipping.
Here is example pseudo code:
//shader material1
void main() {
if( dot( plane, gl_Position ) < 0 )
discard;
color = light * texture(...);
}
other material
//shader material2
void main() {
if( dot( plane, gl_Position ) < 0 )
discard;
color = other fancy stuff;
}
the point is that I have the same code in both shaders, and this bugs me.
gl_ClipDistance [...]
. That will tell the primitive assembly stage the distance from an arbitrary set of clip planes. Pre-GL3 you would output a single clip position and during primitive assembly each plane would be tested, but in modern GL you do the distance calculation in the vertex shader and pass the result to primitive assembly. – Andon M. Coleman