Re: [osg-users] Culling with custom clip planes

2015-10-26 Thread Robert Osfield
Hi Jannik,

Just catching up with this thread, but am afraid my brain isn't yet clear
enough this Monday morning to be able spot anything.  I'm afraid I'm still
a bit foggy brained, did a 38 mile, hilly and muddy ultramarathon at the
weekend :-)

For now I don't have anything else to add to your experiments.  If you are
still struggling after your latest experiments I'll just to my head around
the issue later.

Robert.

On 24 October 2015 at 22:50, Jannik Heller  wrote:

> Ok, setting on projectionCullingStack works fine for the whole subgraph,
> you just have to transform the plane to view space first:
>
>
> Code:
>
> virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
> {
> osgUtil::CullVisitor* cv = dynamic_cast(nv);
> if (cv)
> {
> osg::CullingSet& cullingStack =
> cv->getProjectionCullingStack().back();
>
> osg::Polytope::PlaneList origPlaneList =
> cullingStack.getFrustum().getPlaneList();
>
> osg::Plane transformed = mCullPlane;
> transformed.transform(cv->getCurrentCamera()->getViewMatrix());
>
> cullingStack.getFrustum().add(transformed);
>
> traverse(node, nv);
>
> // undo
> cullingStack.getFrustum().set(origPlaneList);
> }
> else
> traverse(node, nv);
> }
>
>
>
> It's a bit ugly, but good enough for me. Unless Robert has a better idea,
> I'll stick with this approach!
>
> Cheers,
> Jannik[/code]
>
> --
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=65434#65434
>
>
>
>
>
> ___
> osg-users mailing list
> osg-users@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] Culling with custom clip planes

2015-10-24 Thread Jannik Heller
Hi Robert,

I have just noticed that approach of adding culling planes doesn't work 100% 
accurately. Some meshes that have their bounding box fully outside of the 
culling plane don't get culled.

To test with please check the attached osgreflect.cpp. I added a culling plane 
so that everything above the mirror mesh should get culled. The aircraft and 
the sphere are clearly above, but don't get culled for some reason:

https://i.imgur.com/ip0iIc9.png

Once I move the sphere up by 2 more units then it gets culled.
transform->setMatrix(osg::Matrix::translate(osg::Vec3f(20, 20, z + 
sphereRadius + 4)));

For the aircraft, the inaccuracy seems to be larger. I have to move the culling 
plane down by 21 units before the aircraft gets culled:

rootNode->addCullCallback(new 
PlaneCullCallback(osg::Plane(osg::Vec3d(0,0,-1),osg::Vec3d(0,0,z-21;

Any ideas why this could be happening?

Thanks,
Jannik

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



/* OpenSceneGraph example, osgreflect.
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy
*  of this software and associated documentation files (the "Software"), to deal
*  in the Software without restriction, including without limitation the rights
*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the Software is
*  furnished to do so, subject to the following conditions:
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*  THE SOFTWARE.
*/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 

#include 
#include 

#include 

//
// A simple demo demonstrating planar reflections using multiple renderings
// of a subgraph, overriding of state attribures and use of the stencil buffer.
//
// The multipass system implemented here is a variation of Mark Kilgard's
// paper "Improving Shadows and Reflections via the Stencil Buffer" which
// can be found on the developer parts of the NVidia web site.
//
// The variations comes from the fact that the mirrors stencil values
// are done on the first pass, rather than the second as in Mark's paper.
// The second pass is now Mark's first pass - drawing the unreflected scene,
// but also unsets the stencil buffer.  This variation stops the unreflected
// world poking through the mirror to be seen in the final rendering and
// also obscures the world correctly when on the reverse side of the mirror.
// Although there is still some unresolved issue with the clip plane needing
// to be flipped when looking at the reverse side of the mirror.  Neither
// of these issues are mentioned in the Mark's paper, but trip us up when
// we apply them.



class PlaneCullCallback : public osg::NodeCallback
{
public:
PlaneCullCallback(const osg::Plane& cullPlane)
: osg::NodeCallback()
, mCullPlane(cullPlane)
{
}

PlaneCullCallback(const PlaneCullCallback& copy, const osg::CopyOp& copyop)
: osg::NodeCallback(copy, copyop)
, mCullPlane(copy.mCullPlane)
{
}

PlaneCullCallback()
{
}

META_Object(MWRender, PlaneCullCallback)

virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osgUtil::CullVisitor* cv = dynamic_cast(nv);
if (cv)
{
osg::Polytope::PlaneList origPlaneList = cv->getCurrentCullingSet().getFrustum().getPlaneList();

cv->getCurrentCullingSet().getFrustum().add(mCullPlane);

traverse(node, nv);

// undo
cv->getCurrentCullingSet().getFrustum().set(origPlaneList);
}
else
traverse(node, nv);
}

void setCullPlane (const osg::Plane& plane)
{
mCullPlane = plane;
}

private:
osg::Plane mCullPlane;
};

osg::StateSet* createMirrorTexturedState(const std::string& filename)
{
osg::StateSet* dstate = new osg::StateSet;
dstate->setMode(GL_CULL_FACE,osg::StateAttribute::OFF|osg::StateAttribute::PROTECTED);

// set up the texture.
osg::Image* image = osgDB::readImageFile(filename.c_str());
if (image)
{
osg::Texture2D* texture = new osg::Texture2D;
texture->setImage(image);
dstate->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON|osg::StateAttribute::PROTECTED);
}

return dstate;
}


osg::Drawable* 

Re: [osg-users] Culling with custom clip planes

2015-10-24 Thread Jannik Heller
Oops, wrong file. Use this one please.

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



/* OpenSceneGraph example, osgreflect.
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy
*  of this software and associated documentation files (the "Software"), to deal
*  in the Software without restriction, including without limitation the rights
*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the Software is
*  furnished to do so, subject to the following conditions:
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*  THE SOFTWARE.
*/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 

#include 
#include 

#include 

//
// A simple demo demonstrating planar reflections using multiple renderings
// of a subgraph, overriding of state attribures and use of the stencil buffer.
//
// The multipass system implemented here is a variation of Mark Kilgard's
// paper "Improving Shadows and Reflections via the Stencil Buffer" which
// can be found on the developer parts of the NVidia web site.
//
// The variations comes from the fact that the mirrors stencil values
// are done on the first pass, rather than the second as in Mark's paper.
// The second pass is now Mark's first pass - drawing the unreflected scene,
// but also unsets the stencil buffer.  This variation stops the unreflected
// world poking through the mirror to be seen in the final rendering and
// also obscures the world correctly when on the reverse side of the mirror.
// Although there is still some unresolved issue with the clip plane needing
// to be flipped when looking at the reverse side of the mirror.  Neither
// of these issues are mentioned in the Mark's paper, but trip us up when
// we apply them.



class PlaneCullCallback : public osg::NodeCallback
{
public:
PlaneCullCallback(const osg::Plane& cullPlane)
: osg::NodeCallback()
, mCullPlane(cullPlane)
{
}

PlaneCullCallback(const PlaneCullCallback& copy, const osg::CopyOp& copyop)
: osg::NodeCallback(copy, copyop)
, mCullPlane(copy.mCullPlane)
{
}

PlaneCullCallback()
{
}

META_Object(MWRender, PlaneCullCallback)

virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osgUtil::CullVisitor* cv = dynamic_cast(nv);
if (cv)
{
osg::Polytope::PlaneList origPlaneList = cv->getCurrentCullingSet().getFrustum().getPlaneList();

cv->getCurrentCullingSet().getFrustum().add(mCullPlane);

traverse(node, nv);

// undo
cv->getCurrentCullingSet().getFrustum().set(origPlaneList);
}
else
traverse(node, nv);
}

void setCullPlane (const osg::Plane& plane)
{
mCullPlane = plane;
}

private:
osg::Plane mCullPlane;
};

osg::StateSet* createMirrorTexturedState(const std::string& filename)
{
osg::StateSet* dstate = new osg::StateSet;
dstate->setMode(GL_CULL_FACE,osg::StateAttribute::OFF|osg::StateAttribute::PROTECTED);

// set up the texture.
osg::Image* image = osgDB::readImageFile(filename.c_str());
if (image)
{
osg::Texture2D* texture = new osg::Texture2D;
texture->setImage(image);
dstate->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON|osg::StateAttribute::PROTECTED);
}

return dstate;
}


osg::Drawable* createMirrorSurface(float xMin,float xMax,float yMin,float yMax,float z)
{

// set up the drawstate.

// set up the Geometry.
osg::Geometry* geom = new osg::Geometry;

osg::Vec3Array* coords = new osg::Vec3Array(4);
(*coords)[0].set(xMin,yMax,z);
(*coords)[1].set(xMin,yMin,z);
(*coords)[2].set(xMax,yMin,z);
(*coords)[3].set(xMax,yMax,z);
geom->setVertexArray(coords);

osg::Vec3Array* norms = new osg::Vec3Array(1);
(*norms)[0].set(0.0f,0.0f,1.0f);
geom->setNormalArray(norms, osg::Array::BIND_OVERALL);

osg::Vec2Array* tcoords = new osg::Vec2Array(4);
(*tcoords)[0].set(0.0f,1.0f);
(*tcoords)[1].set(0.0f,0.0f);
(*tcoords)[2].set(1.0f,0.0f);
(*tcoords)[3].set(1.0f,1.0f);
geom->setTexCoordArray(0,tcoords);

osg::Vec4Array* colours = new osg::Vec4Array(1);
(*colours)[0].set(1.0f,1.0f,1.0,1.0f);

Re: [osg-users] Culling with custom clip planes

2015-10-24 Thread Jannik Heller
Hi,

The interaction of CullingSet, CullStack and CullVisitor is really confusing to 
look at, but I think I have a lead!

It seems like the added frustum plane is reset right here: 
https://github.com/openscenegraph/osg/blob/master/src/osg/CullStack.cpp#L214 
when the cull visitor applies a transform.  So the approach of adding a plane 
in the CullCallback won't work on the whole subgraph.

Looking at the code if I change the frustum on the projectionCullingStack 
instead of modelViewCullingStack, it should persist for the whole subgraph... 
but then I have to specify my culling plane in projection space instead of 
model space, which is a bit annoying. I will keep looking for a solution.

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





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


Re: [osg-users] Culling with custom clip planes

2015-10-24 Thread Jannik Heller
Ok, setting on projectionCullingStack works fine for the whole subgraph, you 
just have to transform the plane to view space first:


Code:

virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osgUtil::CullVisitor* cv = dynamic_cast(nv);
if (cv)
{
osg::CullingSet& cullingStack = 
cv->getProjectionCullingStack().back();

osg::Polytope::PlaneList origPlaneList = 
cullingStack.getFrustum().getPlaneList();

osg::Plane transformed = mCullPlane;
transformed.transform(cv->getCurrentCamera()->getViewMatrix());

cullingStack.getFrustum().add(transformed);

traverse(node, nv);

// undo
cullingStack.getFrustum().set(origPlaneList);
}
else
traverse(node, nv);
}



It's a bit ugly, but good enough for me. Unless Robert has a better idea, I'll 
stick with this approach!

Cheers,
Jannik[/code]

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





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


Re: [osg-users] Culling with custom clip planes

2015-04-10 Thread Jannik Heller
I have implemented the CullCallback way and it seems to work great. Thanks for 
the tip!

Cheers
Jannik

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





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


Re: [osg-users] Culling with custom clip planes

2015-04-09 Thread Robert Osfield
Hi Jannik,

You could add culling planes to the frustum during the cull traversal (see
osg::CullingStack/osg::CullingSet or osgUtil::CullVisitor), or use the
osg::ConvexPlanarOccluder support.

Going the cull traversal route will be doing something I haven't done
before but in principle you should be able write a cull cullback that gets
the current Frustum and adds the planes you want to test against.  You'd
need to restore the original Frustum planes after the traversal of the
subgraph below where you've attached the cull back.

Robert.

On 8 April 2015 at 20:58, Jannik Heller scr...@baseoftrash.de wrote:

 Hi,

 In the osgreflect example a ClipNode is used to remove unwanted objects
 behind the reflection. Is there a way that in addition to clipping these
 objects, we could also cull them? Objects that are entirely behind the
 reflection obviously do not need to be rendered. From what I could tell in
 the source code OSG performs no such culling. Is there an easy way to
 achieve that?

 Thank you!

 Cheers,
 Jannik

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





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

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


[osg-users] Culling with custom clip planes

2015-04-08 Thread Jannik Heller
Hi,

In the osgreflect example a ClipNode is used to remove unwanted objects 
behind the reflection. Is there a way that in addition to clipping these 
objects, we could also cull them? Objects that are entirely behind the 
reflection obviously do not need to be rendered. From what I could tell in the 
source code OSG performs no such culling. Is there an easy way to achieve that? 

Thank you!

Cheers,
Jannik

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





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