Visualization Library v1.0.3

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [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.

pagGuideClipping.jpg

[From App_ClipPlanes.cpp]

class App_ClipPlanes: public BaseDemo
{
public:
  void initEvent()
  {
    vl::Log::notify(appletInfo());

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

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

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

    // 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(), 0 );
    clip1_sh->enable(vl::EN_LIGHTING);
    clip1_sh->enable(vl::EN_DEPTH_TEST);
    clip1_sh->gocMaterial()->setBackDiffuse(vl::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)->bindTransform( mClipTr.get() );
    // setup clipping pass 2
    clip2_sh->setRenderState( light.get(), 0 );
    clip2_sh->enable(vl::EN_LIGHTING);
    clip2_sh->enable(vl::EN_DEPTH_TEST);
    clip2_sh->gocMaterial()->setBackDiffuse(vl::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)->bindTransform( 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()->gocLightModel()->setTwoSide(true);
    plane_fx->shader()->gocColor()->setValue(vl::fvec4(1,0,0,0.3f)); // transparent red
    // add plane actor
    vl::ref<vl::Geometry> plane = vl::makeGrid( vl::vec3(0,0,0), 4,4, 2,2 );
    sceneManager()->tree()->addActor( plane.get(), plane_fx.get(), mClipTr.get() );
  }

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

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

// Have fun!


Visualization Library v1.0.3 Reference Documentation
Copyright Michele Bosi. All rights reserved.
Updated on Tue Feb 7 2017 00:55:04.
Permission is granted to use this page to write and publish articles regarding Visualization Library.