VISH  0.2
ClippingPlane.cpp

A very simplistic interface to enable clipping of planes.

//#include <ocean/plankton/VModules.hpp>
#include <ocean/plankton/VCreator.hpp>
#include <ocean/plankton/VInputCreator.hpp>
#include <ocean/shrimp/VEnum.hpp>
#include <ocean/Anemonia/TransformOperator.hpp>
#include <math.h>

#include <GL/glew.h>

namespace
{

using namespace Wizt;

class   ClippingPlane : public VObject
{
public:
        VOutput<TransformOperator> myFunctor;

        TypedSlot<double>       Center, Thickness;

        TypedSlot<Enum>         Mode;

        struct Functor : TransformOperator::Functor 
        {
                double  startplane, endplane;
                bool    eye_coords;

                Functor(double Start, double End, bool ec)
                : startplane(Start), endplane(End)
                , eye_coords(ec)
                {}

                override bool begin(VRenderContext&) const;
                override void end(VRenderContext&) const;
        };


        ClippingPlane(const string&name, int p, const RefPtr<VCreationPreferences>&VP)
        : VObject(name, p, VP)
        , myFunctor( self(), "clipping", TransformOperator() )
        , Center   ( this, "center" , 0.0)
        , Thickness( this, "thickness", 1.0, 0)
        , Mode(this, "space", Enum("world", "eye"), 0)
        {
                Center.setProperty("min", -100.0);
                Center.setProperty("max", +100.0);
                Thickness.setProperty("min", -0.0);
                Thickness.setProperty("max", +100.0);
        }

        override bool update(VRequest&R, double precision);
};


bool ClippingPlane::Functor::begin(VRenderContext&Context) const
{
        glEnable( GL_CLIP_PLANE0 );
        glEnable( GL_CLIP_PLANE1 );

VRenderContext::tvector3 viewdir = Context.CameraSettings.viewdir(); 

GLdouble equation0[4] = { 0,0,1, startplane }; 
        if (eye_coords)
        {
                equation0[0] = viewdir[0]; 
                equation0[1] = viewdir[1]; 
                equation0[2] = viewdir[2];
        }

        glClipPlane(GL_CLIP_PLANE0, equation0);

GLdouble equation1[4] = { 0,0,-1, endplane };
        if (eye_coords)
        {
                equation1[0] = -viewdir[0]; 
                equation1[1] = -viewdir[1]; 
                equation1[2] = -viewdir[2];
        }

        glClipPlane(GL_CLIP_PLANE1, equation1);

        return true;
}

void ClippingPlane::Functor::end(VRenderContext&) const
{
        glDisable( GL_CLIP_PLANE0 );
        glDisable( GL_CLIP_PLANE1 );
}


bool    ClippingPlane::update(VRequest&Context, double precision)
{
Enum    M; 
        Mode << Context >> M; 

TransformOperator TO;
        myFunctor << Context >> TO; 

double  center = 0.0, thickness = 1.0; 
        Center    << Context >> center; 
        Thickness << Context >> thickness; 

RefPtr<Functor> rF = *TO; 
        if (!rF) rF = new Functor(0.0, 1.0, false); 

        rF->startplane = -center + thickness; 
        rF->endplane   = center + thickness; 
        rF->eye_coords = M("world"); 

        TO.MyFunctor = rF; 

        myFunctor << Context << TO; 
//      puts("CHANGE Transformation");

        return true;
}

static Ref<VInputCreator<TransformOperator, VCreator<ClippingPlane> > >
        myCreator("Create/ClippingPlane", 0, new VCreationPreferences("default") );


}