Using a shader to clip objects

Started by
0 comments, last by Hodgman 13 years, 5 months ago
Hi,

I created a shader in order to clip an object with a user defined clipping plane.

First I use the "standard OpenGL clipping", but this way restricted me too much because I just want to clip specific objects and not the whole halfspace.
So I decided to try it with GLSL.

Here my Vertex Shader:

varying float xpos;
varying float ypos;
varying float zpos;
varying vec3 normalVec;

varying float entfernung;

void main(void)
{
xpos = gl_Vertex.x;
ypos = gl_Vertex.y;
zpos = gl_Vertex.z;

normalVec = normalize (gl_NormalMatrix * gl_Normal);

gl_FrontColor = gl_Color;
gl_Position = ftransform();
}

And here my Fragment Shader: (clipping plane: Z = 10)

varying float xpos;
varying float ypos;
varying float zpos;

varying vec3 normalVec;

int enabled = 1;
bool invert = false;

float intensity;

float entfernung;

void main(void)
{
if(enabled == 1)
{

entfernung = zpos-10;

vec4 farbe = vec4(gl_Color);

if (!invert)
{
if (entfernung >= 0.0)
discard;
}
else
{
if (entfernung <= 0.0)
discard;
}
intensity = dot (vec3 (gl_LightSource[0].position), normalVec);
gl_FragColor = farbe * intensity;
}
else
{
gl_FragColor = gl_Color;
}
}

The result is that the object is clipped by the plane z=10. That's ok.
But taking a look at the "clipping side", I can look through the modell.
Means I have no back side.
(Is there no possibility to add an image?)

Can anybody help me? Or have anybody another idea how I can clip an modell?
I read something about a way to clip is to use a projected 2DTexture with a 2x1 texture, where 1 pixel alpha = 255 and the other pixel alpha = 0.
But I don't really understand it resp. don't know how to make it in this way.
Searching the www is until now not very helpful.

Thanks for help!!!



Advertisement
Quote:Original post by zoi111
But taking a look at the "clipping side", I can look through the modell.
Means I have no back side.
(Is there no possibility to add an image?)
Do you mean you want to fill in the hole created by the clipping plane?
The easiest way would be to diable backface culling on your object, so you can see the 'inside' polygons.
Quote:I read something about a way to clip is to use a projected 2DTexture with a 2x1 texture, where 1 pixel alpha = 255 and the other pixel alpha = 0.
This is just an old way to implement clipping planes (for cards that dont support shaders or hardware clip planes) -- it will still create a hole in the model.

This topic is closed to new replies.

Advertisement