PDA

View Full Version : How to activate clip planes via shader?



Ehsan Kamrani
08-24-2010, 11:52 AM
I use a shader to draw 3d objects. now I need to enable clip planes:



double plane[4] = {0.0, -1.0, 0.0, waterHeight};
glEnable(GL_CLIP_PLANE0);
glClipPlane(GL_CLIP_PLANE0, plane);
//RenderWorld();
glDisable(GL_CLIP_PLANE0);


But I don't know how to enable and use clip planes in my shader

Dark Photon
08-24-2010, 04:32 PM
But I don't know how to enable and use clip planes in my shader
Search for clipplane in the GLSL Spec (http://www.opengl.org/registry/doc/GLSLangSpec.4.00.9.clean.pdf) and you'll come right to the two references.

In short, google for gl_ClipPlane GLSL for code snippets and tips.

Haven't used it so can't give you specifics, but the code fragments google flipped up made it look like it just contains the plane equation coefficients.

Aleksandar
08-25-2010, 03:21 AM
1. Define clip planes in your vertex shader. Something like this:

uniform vec4 ClipPlane[MaxClipPlanes];

2. Calculate clip distances and write the values in appropriate built-in output variables. Something like this:

for ( int i=0; i<MaxClipPlanes; i++ )
{
gl_ClipDistance[i] = dot( ClipPlane[i], vec4(MCvertex,1.0));
}

3. Set values of the clip planes in your application, and enable them. For example:

m_pProgram->SetUniform("ClipPlane[0]", vect, 4, 1);
glEnable(GL_CLIP_DISTANCE0);

Take care that both clip planes and vertices passed to dot product are in the same space (local, world, or eye).

Ehsan Kamrani
12-28-2010, 02:55 AM
Hi
Thanks for the replies
What's the "MCvertex" parameter?

I have used this code in my vertex shader:


gl_ClipDistance[0] = dot( ClipPlane, gl_Vertex);

( I just need 1 clipping plane )
And here's my clipping plane:


float plane[4] = { 0.0f, -1.0f, 0.0f, waterHeight );
glUniform4fv(glGetUniformLocation( g_render.m_shaderProgram , "ClipPlane"), 4, plane );


However I don't get the appropriate results( for example the depth test fails, etc ).

ugluk
12-28-2010, 04:17 AM
Sorry for asking, but what happens if the clip test fails? You can't clip a vertex, you can only clip a primitive. If the vertex if discarded, one would surely get strange results.

Ehsan Kamrani
12-28-2010, 04:57 AM
I just want to know what's wrong with my code and how can I fix it. my question is clear.
Edit: It seems that with my code, nothing happens and clip planes don't work.

Ehsan Kamrani
12-28-2010, 07:36 AM
I found the answer!
simply adding this code to vertex shader sloves the problem:


gl_Position = ftransform();

// fix of the clipping bug for both Nvidia and ATi
#ifdef __GLSL_CG_DATA_TYPES
gl_ClipVertex = gl_ModelViewMatrix * gl_Vertex;
#endif



We then enable and specifly our clipping planes via the OpenGL API and this code snippet does clipping for us!