Jump to content
  • Advertisement
Sign in to follow this  
KenHouser

OpenGL Gl_Clipplanes

This topic is 2893 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey ole gurus of opengl. I have a question or three on gl_clipPlanes.
let me tell you what I am asuming (yea yea I know) . I am wrtting a shader to draw the intersection of triangles and the user defined clipping planes
not rocket science I know but the clipping planes have me confused.
ok here is the vertex shader the draw line function just emits two vertexs for the line and does an end primitive. none of the line show up.
if I just draw the line of the triagles then every then works.
thanks in advance for any help

[color="#a31515"][color="#a31515"]void main()
[color="#a31515"][color="#a31515"]{
[color="#a31515"][color="#a31515"] vec3 vNormal;
[color="#a31515"][color="#a31515"] vNormal = gl_NormalMatrix * gl_Normal;
[color="#a31515"][color="#a31515"] vNormal = normalize(vNormal);
[color="#a31515"][color="#a31515"] gl_Position = ftransform();
[color="#a31515"][color="#a31515"] #ifdef __GLSL_CG_DATA_TYPES
[color="#a31515"][color="#a31515"] gl_ClipVertex = gl_ModelViewMatrix*gl_Vertex;
[color="#a31515"][color="#a31515"] #endif
[color="#a31515"][color="#a31515"] }
[color="#000000"] Geometry shader
void main()
{
vec4 a = gl_PositionIn[0];
vec4 b = gl_PositionIn[1];
vec4 c = gl_PositionIn[2];
// bounce all points of the triangle off each clipping plane to see if they are clipped or not
for ( int i=0; i<gl_MaxClipPlanes; i++ )
{
int iPosCount=0;
int iNegCount=0;
int iZeroCount=0;
int iPosIndex[3];
int iNegIndex[3];
int iZeroIndex[3]; gl_FrontColor = vec4(1.0,0.0,0.0,1.0);
float d[3];
//vec3 eyeSpaceNormal = gl_ClipPlane.xyz;
// eyeSpaceNormal = eyeSpaceNormal* gl_NormalMatrix;
// vec4 eyeSpaceClipPlane = vec4 (eyeSpaceNormal,(gl_ClipPlane.w * gl_NormalScale));

vec4 eyeSpaceClipPlane = gl_ClipPlane;
// get the dot product of the plane and the points of the triangle
d[0] = dot(vec4(gl_PositionIn[0].xyz,1.0), eyeSpaceClipPlane);
d[1] = dot(vec4(gl_PositionIn[1].xyz,1.0), eyeSpaceClipPlane);
d[2] = dot(vec4(gl_PositionIn[2].xyz,1.0), eyeSpaceClipPlane);
for ( int j=0; j<3; j++ )
{
if ( d[j] > 0.0)
{
iPosIndex[iPosCount] = j;
iPosCount++;
}
else if (d[j] < 0.0)
{
iNegIndex[iNegCount] = j;
iNegCount++;
}
else
{
iZeroIndex[iZeroCount] = j;
iZeroCount++;
}
}
// if the points on the left of the plane or the points on the right of the plane and the points on the plane add up to three then the triangle in not clipped
if ( !(iPosCount + iZeroCount == 3 || iNegCount + iZeroCount == 3 ))
{
// if only one point is on the plane the other two are on oppisite sides of the plane
if (iZeroCount == 1)
{
int pI = iPosIndex[0];
int nI = iNegIndex[0];
int zI = iZeroIndex[0];
float t = d[pI] / (d[pI] - d[nI]);
DrawLine(((gl_PositionIn[nI] - gl_PositionIn[pI]) * t) + gl_PositionIn[pI], gl_PositionIn[zI], gl_ClipVertexIn[nI] ,gl_ClipVertexIn[zI]);
}
else // two points are on the right or the left on the plane and the other is on the oppistie side
{
int flag=0;
int pI;
int nI;
int oI;
if (iPosCount == 1)
{
pI = iPosIndex[0];
nI = iNegIndex[0];
oI = iNegIndex[1];
}
else
{
pI = iPosIndex[0];
nI = iNegIndex[0];
oI = iPosIndex[1];
flag=1; // two positive one negitive
}
float t = d[pI] / (d[pI] - d[nI]);
vec4 point1 = ((gl_PositionIn[nI] - gl_PositionIn[pI]) * t) + gl_PositionIn[pI];
t = d[pI] / (d[pI] - d[oI]);
vec4 point2 = ((gl_PositionIn[oI] - gl_PositionIn[pI]) * t) + gl_PositionIn[pI];
DrawLine(point1, point2, gl_ClipVertexIn[nI] ,gl_ClipVertexIn[oI]);
}
}
}
[color="#000000"]Fragmet Shader
[color="#ff0000"]void main()
{
gl_FragData[0] = gl_Color;
};


.


Share this post


Link to post
Share on other sites
Advertisement
I've never needed to actually use them yet, so take this comment with care.

From what I believe remembering from when I read about it, there is really no such thing as as a clip plane any more, but rather you define your whatever-definition-of-clip-you-want any way you want.
For clipping to happen, you output a clip distance. Clipping will occur where this distance is zero, and parts where it is negative are invisible. The clip distance is a varying and is interpolated like all other varyings. So if you want to clip against some plane, you just use whatever representation you like, only the distance matters.

In principle, you should be perfectly able to have a "clip sphere" rather than a clip plane or any other geometric shape that allows you to calculate a distance to "something" (of course triangles will still only be clipped using straight lines, so some level of tesselation would be needed for it to look somewhat decent).

Share this post


Link to post
Share on other sites
until the glClipPlane is depracated then I still should be able to use them
the question is what format is gl_clipPlane and what coordinate system are they in, in the GLSL
thanks for any help

Share this post


Link to post
Share on other sites
It's given as the coefficients of the implicit equation of a plane, directed towards the inside of the clipping volume. If you don't know what "implicit equation of a plane" means, start your reading there.

Share this post


Link to post
Share on other sites
yes that is what I thought they were but I am surprized when I try to draw the intersection of the triangle and the plane the lines do not show up
so I was thinking that the gl_clipPlane needed to be transformed. so i tried all of the different transforms back and forth and still nothing.
so this is when I wanted to ask this group.

right out of the book
gl_Position = ftransform();
[color="#000000"]transforms the vertex to clip space
i should be able to use the gl_clipPlanes as is
but it does not seem to work

Share this post


Link to post
Share on other sites
What do you mean with "as is"?

Both vertices and clip planes must be in the same space. So, if your vertices are in clip-space, coefficients of the clip planes must also be defined in clip-space.

The philosophy is simple.   :)

Share this post


Link to post
Share on other sites
ok I guess this is what I have been wanting to know how do I get the clipplanes into clip space?
And what space are they in? I have tried against the projection matrix the modelview matrix the modelviewand projection matrix and even the normallized matrix

Thanks again fro yall's responce

Ken

Share this post


Link to post
Share on other sites
Clip-planes does not have to be defined in a particular space. For some use-cases it is easier to be define in eye-space, for some in clip-space, but if you want to cut-off object, it is the easiest way to define clip planes in object-space. Just use vertex coordinates before transformation to calculate clip-distance. In fact, the distance is not important, just the sign! Use scalar (dot) product to calculate clip-distances in the vertex shader.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!