Question

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.

Was it helpful?

Solution 2

I would suggest you do the same you would do with C code: write a function or a macro. Put the function/macro in a separate file and "include" it into your base shader (glShaderSource() can take an arbitrary number of strings; each string can be the source code of a file). With time, you'll end up with a shader library of sorts, with all of your reusable code.

OTHER TIPS

OpenGL core didn't remove user clip planes.

You enable the first one with glEnable(GL_CLIP_DISTANCE_0) and you decide how to clip with setting gl_ClipDistance[0] in the vertex shader.

As to "changing all shaders", here is a potential solution: call a "potentialSetClipDistance" in all VS and simply implement that method in 2 different shader objects, linking against the right one depending on usage. That's the kind of things the driver does in compat GL behind your back.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top