# Visualization Library A lightweight C++ OpenGL middleware for 2D/3D graphics | [Home] [Tutorials] [All Classes] [Grouped Classes]

# Clipping Planes Tutorial

This tutorial shows how to use clipping, planes, how to animate them and how to use them with multipassing rendering.

[From `App_ClipPlanes.hpp`]

    class App_ClipPlanes: public BaseDemo
    {
    public:
      void initEvent()
      {
        BaseDemo::initEvent();

        // load model
        vl::ref<vl::Geometry> model = vl::loadResource("/models/3ds/monkey.3ds")->get<vl::Geometry>(0);

        // install transform
        mClipTr = new vl::Transform;
        vl::VisualizationLibrary::rendering()->as<vl::Rendering>()->transform()->addChild(mClipTr.get());

        // to be used later
        vl::ref<vl::Light> light = new vl::Light(0);

        // demonstrates multipassing clipping

        vl::ref<vl::Effect> clip_fx  = new vl::Effect;
        vl::ref<vl::Shader> clip1_sh = new vl::Shader;
        vl::ref<vl::Shader> clip2_sh = new vl::Shader;
        // setup clipping pass 1
        clip1_sh->setRenderState( light.get() );
        clip1_sh->enable(vl::EN_LIGHTING);
        clip1_sh->enable(vl::EN_DEPTH_TEST);
        clip1_sh->gocMaterial()->setBackDiffuse(vlut::yellow);
        clip1_sh->gocLightModel()->setTwoSide(true);
        // clipping plane 1 setup
        clip1_sh->gocClipPlane(0)->setPlane( vl::Plane(0.2f, vl::vec3(0,+1,0)) );
        clip1_sh->gocClipPlane(0)->followTransform( mClipTr.get() );
        // setup clipping pass 2
        clip2_sh->setRenderState( light.get() );
        clip2_sh->enable(vl::EN_LIGHTING);
        clip2_sh->enable(vl::EN_DEPTH_TEST);
        clip2_sh->gocMaterial()->setBackDiffuse(vlut::green);
        clip2_sh->gocLightModel()->setTwoSide(true);
        // clipping plane 2 setup
        clip2_sh->gocClipPlane(0)->setPlane( vl::Plane(0.2f, vl::vec3(0,-1,0)) );
        clip2_sh->gocClipPlane(0)->followTransform( mClipTr.get() );
        // install the two passes for LOD 0
        clip_fx->setLOD(0, clip1_sh.get(), clip2_sh.get());
        // add model to the scene
        sceneManager()->tree()->addActor( model.get(), clip_fx.get(), NULL );

        // renders a plane for visual feedback

        // setup effect
        vl::ref<vl::Effect> plane_fx = new vl::Effect;
        plane_fx->setRenderRank(1); // draw after the clipped model
        plane_fx->shader()->enable(vl::EN_DEPTH_TEST);
        plane_fx->shader()->enable(vl::EN_BLEND);
        plane_fx->shader()->gocMaterial()->setColorMaterialEnabled(true);
        plane_fx->shader()->gocLightModel()->setTwoSide(true);
        // add plane actor
        vl::ref<vl::Geometry> plane = vlut::makeGrid( vl::vec3(0,0,0), 4,4, 2,2 );
        plane->setColorArray(vl::fvec4(1,0,0,0.3f)); // transparent red
        sceneManager()->tree()->addActor( plane.get(), plane_fx.get(), mClipTr.get() );
      }

      virtual void run()
      {
        // animate the clipping planes and the rendered plane
        vl::Real t = (vl::Real)vl::Time::currentTime();
        mClipTr->setLocalMatrix( vl::mat4::rotation(t*90.0f, sin(t*(vl::fPi-3.0f)),1,cos(t)) );
      }

      virtual void shutdown() {}

    protected:
      vl::ref<vl::Transform> mClipTr;
    };

    // Have fun!

Visualization Library v2009.08 Reference Documentation
Copyright 2005-2009 Michele Bosi. All rights reserved.
Updated on Sun Nov 29 11:11:40 2009.
Permission is granted to use this page to write and publish articles regarding Visualization Library.