Gl_Clipplanes

Started by
5 comments, last by Aks9 13 years, 3 months ago
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

void main()<br /> {<br /> vec3 vNormal;<br /> vNormal = gl_NormalMatrix * gl_Normal;<br /> vNormal = normalize(vNormal);<br /> gl_Position = ftransform();<br /> #ifdef __GLSL_CG_DATA_TYPES<br /> gl_ClipVertex = gl_ModelViewMatrix*gl_Vertex;<br /> #endif<br /> }<br /> Geometry shader<br /> void main() <br /> { <br /> vec4 a = gl_PositionIn[0];<br /> vec4 b = gl_PositionIn[1];<br /> vec4 c = gl_PositionIn[2];<br /> // bounce all points of the triangle off each clipping plane to see if they are clipped or not<br /> for ( int i=0; i&lt;gl_MaxClipPlanes; i++ )<br /> {<br /> int iPosCount=0;<br /> int iNegCount=0;<br /> int iZeroCount=0;<br /> int iPosIndex[3];<br /> int iNegIndex[3];<br /> int iZeroIndex[3]; gl_FrontColor = vec4(1.0,0.0,0.0,1.0);<br /> float d[3];<br /> //vec3 eyeSpaceNormal = gl_ClipPlane<span style="font-weight:bold;">.xyz;<br /> // eyeSpaceNormal = eyeSpaceNormal* gl_NormalMatrix;<br /> // vec4 eyeSpaceClipPlane = vec4 (eyeSpaceNormal,(gl_ClipPlane<span style="font-weight:bold;">.w * gl_NormalScale));<br /> <br /> vec4 eyeSpaceClipPlane = gl_ClipPlane<span style="font-weight:bold;">;<br /> // get the dot product of the plane and the points of the triangle<br /> d[0] = dot(vec4(gl_PositionIn[0].xyz,1.0), eyeSpaceClipPlane);<br /> d[1] = dot(vec4(gl_PositionIn[1].xyz,1.0), eyeSpaceClipPlane);<br /> d[2] = dot(vec4(gl_PositionIn[2].xyz,1.0), eyeSpaceClipPlane);<br /> for ( int j=0; j&lt;3; j++ )<br /> {<br /> if ( d[j] &gt; 0.0)<br /> {<br /> iPosIndex[iPosCount] = j;<br /> iPosCount++;<br /> }<br /> else if (d[j] &lt; 0.0)<br /> {<br /> iNegIndex[iNegCount] = j;<br /> iNegCount++;<br /> }<br /> else <br /> {<br /> iZeroIndex[iZeroCount] = j;<br /> iZeroCount++;<br /> }<br /> }<br /> // 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 <br /> if ( &#33;(iPosCount + iZeroCount == 3 || iNegCount + iZeroCount == 3 ))<br /> {<br /> // if only one point is on the plane the other two are on oppisite sides of the plane<br /> if (iZeroCount == 1)<br /> {<br /> int pI = iPosIndex[0];<br /> int nI = iNegIndex[0];<br /> int zI = iZeroIndex[0];<br /> float t = d[pI] / (d[pI] - d[nI]);<br /> DrawLine(((gl_PositionIn[nI] - gl_PositionIn[pI]) * t) + gl_PositionIn[pI], gl_PositionIn[zI], gl_ClipVertexIn[nI] ,gl_ClipVertexIn[zI]);<br /> }<br /> else // two points are on the right or the left on the plane and the other is on the oppistie side <br /> {<br /> int flag=0;<br /> int pI;<br /> int nI;<br /> int oI;<br /> if (iPosCount == 1)<br /> {<br /> pI = iPosIndex[0];<br /> nI = iNegIndex[0];<br /> oI = iNegIndex[1];<br /> }<br /> else<br /> {<br /> pI = iPosIndex[0];<br /> nI = iNegIndex[0];<br /> oI = iPosIndex[1];<br /> flag=1; // two positive one negitive<br /> }<br /> float t = d[pI] / (d[pI] - d[nI]);<br /> vec4 point1 = ((gl_PositionIn[nI] - gl_PositionIn[pI]) * t) + gl_PositionIn[pI];<br /> t = d[pI] / (d[pI] - d[oI]);<br /> vec4 point2 = ((gl_PositionIn[oI] - gl_PositionIn[pI]) * t) + gl_PositionIn[pI];<br /> DrawLine(point1, point2, gl_ClipVertexIn[nI] ,gl_ClipVertexIn[oI]);<br /> }<br /> }<br /> }<br /> Fragmet Shader<br /> void main()<br /> {<br /> gl_FragData[0] = gl_Color;<br /> }; <br /> <br /> <br /> .<br /> <br /> <br />
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).
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
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.
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();
transforms the vertex to clip space<br /> i should be able to use the gl_clipPlanes as is<br /> but it does not seem to work
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.   :)
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
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.

This topic is closed to new replies.

Advertisement