[osg-users] User clip planes and shaders

158 views
Skip to first unread message

Dave Jones

unread,
Dec 7, 2011, 1:36:49 PM12/7/11
to osg-...@lists.openscenegraph.org
Hi,

I have some code that uses osg::ClipPlane to clip a hierarchy of meshes. All I do is create some plane equations relative to the root node's transform and set the planes as attributes that affect the entire tree.

This works pretty well. When I look at the output in gDEBugger, I can see that the clip equations are invoked after glLoadMatrix is called on the root transform. glEnable is called on the clip planes before any of the geometry in the hierarchy is drawn.

Currently, I need to port this functionality to a vertex shader for a project. Clipping in a shader is pretty straightforward, but I'm getting a little tripped up on what transformation space the clip planes should be in and how to transfer that to the vertex shader.

According to the glClipPlane docs, the plane equations are transformed to view space. In my shader, I'm passing the plane equations in as uniforms. For this to work on all vertices, I need to ensure that the clip plane equations have been transformed by the inverse transpose of the model view matrix. The only way that I can think of doing that is creating a uniform update callback which always updates the plane equations based on the camera's current model view matrix.

This seems a little clunky to me, but I wasn't sure how else to do it. Is there a simpler way that I'm overlooking? This fixed function pipeline model was convenient since it did a lot of this work for me, but it seems like I have to do on my own if I go the programmable pipeline route. Is this a correct assessment?

Thanks! And sorry if this is a bit long-winded.

-Dave

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=44278#44278

_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Paul Martz

unread,
Dec 8, 2011, 4:31:14 PM12/8/11
to osg-...@lists.openscenegraph.org
On 12/7/2011 11:36 AM, Dave Jones wrote:
> According to the glClipPlane docs, the plane equations are transformed to view space.

This is only applicable if you want to perform clipping in the same space as the
OpenGL FFP. Once you start using shaders, this is optional, and you can perform
clipping in some other space if it's more convenient. It's up to you.

> In my shader, I'm passing the plane equations in as uniforms. For this to work on all vertices, I need to ensure that the clip plane equations have been transformed by the inverse transpose of the model view matrix. The only way that I can think of doing that is creating a uniform update callback which always updates the plane equations based on the camera's current model view matrix.

If you're using a GLSL 1.20 shader, your shader would just multiply the clip
plane by the built-in uniform gl_ModelViewMatrixInverseTranspose.
-Paul

Sergey Polischuk

unread,
Dec 12, 2011, 6:44:16 AM12/12/11
to osg-...@lists.openscenegraph.org
Hi

If you need to get same result as opengl FFP, you should add following line in your vertex shader: gl_ClipVertex = gl_ModelViewMatrix * gl_Vertex;
IIRC you need #version 120 to use this. Then all clip planes enabled via glEnable would work with shaders

07.12.2011, 22:36, "Dave Jones" <dave_...@hotmail.com>:

Reply all
Reply to author
Forward
0 new messages