Is there a gl_ClipPlane alternative?

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: three.js - glClipPlane - Is there an equivalent in webGL? - Stack Overflow

…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!

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.

[QUOTE=hashbrown;1287316]
I found the following solution on Stack Overflow: three.js - glClipPlane - Is there an equivalent in webGL? - Stack Overflow

…but having trouble trying to understand how I can discard a pixel.[/QUOTE]
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.

[QUOTE=GClements;1287320]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.[/QUOTE]

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.

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:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.