PDA

View Full Version : Is there a gl_ClipPlane alternative?



hashbrown
06-01-2017, 11:08 PM
I'm reading on how to simulate water, but unfortunately stuck trying to find an alternative for gl_ClipPlane. I'm using WebGL 1 and it uses GLSL 1.0. Is there anyway I can do the same thing (gl_ClipPlane) does within the vertex shader (or frag shader)?

I found the following solution on Stack Overflow: https://stackoverflow.com/a/22634303

...but having trouble trying to understand how I can discard a pixel. I'm not using Three.js, but thought this was the closest answer.

Thanks!

GClements
06-02-2017, 04:31 AM
I'm reading on how to simulate water, but unfortunately stuck trying to find an alternative for gl_ClipPlane. I'm using WebGL 1 and it uses GLSL 1.0. Is there anyway I can do the same thing (gl_ClipPlane) does within the vertex shader (or frag shader)?
In desktop OpenGL, user-defined clip planes are implemented as a fixed-function process. The vertex shader simply sets the interpolation parameter which this requires. You could do it with a geometry shader, but ES doesn't have those either.



I found the following solution on Stack Overflow: https://stackoverflow.com/a/22634303

...but having trouble trying to understand how I can discard a pixel.
With a "discard" statement in the fragment shader, as shown in that example.

Note that you can move the dot products into the vertex shader, setting user-defined outputs which are the equivalent of gl_ClipDistance. Then you just need to discard any fragment for which the clip distance is negative.

hashbrown
06-02-2017, 07:54 PM
In desktop OpenGL, user-defined clip planes are implemented as a fixed-function process. The vertex shader simply sets the interpolation parameter which this requires. You could do it with a geometry shader, but ES doesn't have those either.


With a "discard" statement in the fragment shader, as shown in that example.

Note that you can move the dot products into the vertex shader, setting user-defined outputs which are the equivalent of gl_ClipDistance. Then you just need to discard any fragment for which the clip distance is negative.

Gc thanks a lot, with this I have enough info to keep going. By the way I honestly didn't know that discard was actually a real statement, thought it was pseudo code. :doh: I got the hang of opengl implementation, but now I'm learning glsl.

hashbrown
06-03-2017, 05:27 PM
Got it working. I'll share the code, the Stack Overflow answer and GClements suggestion is what I used.



// vertex
....
varying float check; // bad name i know

void main (void) {
vec3 worldPos = vec3(model * vertexPos);

check = dot(vec4(worldPos, 1.0), clipPlane); // As GClements suggested, I calculate dot product in the vertex shader
}

// frag
....
varying float check;

void main (void) {

vec4 baseColor = texture2D(texture, texCoords);

if (check < 0.0) {
discard;
}

gl_FragColor = baseColor;
}





and I get what I was looking for:

https://s23.postimg.org/je62jtmh3/forum.png (https://postimg.org/image/je62jtmh3/)