zoukankan      html  css  js  c++  java
  • OSG学习:裁剪变换(1)

    在OSG中,默认了6个裁剪平面以去除没有必要显示的物体。也可以自己定义其他的裁剪平面来确定裁剪。
    osg::ClipPlane类继承自osg::StateAttribute类,封装了OpenGL中的glClipPlane()函数。
    在类的成员函数中,设置裁剪平面的有下面几个函数:

    void setClipPlane(const Plane &plane)
    void setClipPlane(double a, double b, double c, double d)
    void setClipPlane(const Vec4d &plane)

    参数都指向一个裁剪平面,裁剪平面可以用Ax+By+Cz+D=0方程表示。因此,A、B、C、D 4个数一次表示裁剪平面方程的4个参数。

    上面的方法只是设置一个裁剪平面的参数,如果想要调用该裁剪平面,还需要在属性中开启该裁剪平面,如下面的代码:

    root->getOrCreateStateSet()->setAttributeAndModes(cc,osg::StateAttribute::On);

    如果在应用程序中药指定多个裁剪平面,此时需要注意指定平面的索引,否则前面先指定的裁剪平面会被覆盖。可以使用下面的函数:

    void setClipPlaneNum(unsigned int num)
    unsigned int getClipPlaneNum() const

    得到一个裁剪平面的索引或者指定一个平面的索引。

    示例代码

    /**********************************************************
    *Write by FlySky
    *zzuxp@163.com  http://www.OsgChina.org   
    **********************************************************/
    
    #include <osgViewer/Viewer>
    
    #include <osg/Node>
    #include <osg/Geode>
    #include <osg/Group>
    #include <osg/ClipPlane>
    #include <osg/StateAttribute>
    
    #include <osgDB/ReadFile>
    #include <osgDB/WriteFile>
    
    #include <osgUtil/Optimizer>
    
    int main()
    {
        osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer() ;
    
        //创建一个裁剪平面
        osg::ref_ptr<osg::ClipPlane> cp1 = new osg::ClipPlane () ;
        //设置裁剪平面
        cp1->setClipPlane(0,1,1,1);
        //指定平面索引
        cp1->setClipPlaneNum(0);
    
        //创建一个裁剪平面
        osg::ref_ptr<osg::ClipPlane> cp2 = new osg::ClipPlane () ;
        //设置裁剪平面
        cp2->setClipPlane(1,0,0,1);
        //指定平面索引
        cp2->setClipPlaneNum(1);
    
        osg::ref_ptr<osg::Group> root  = new osg::Group();
    
        //读取模型
        osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("cow.osg");
    
        root ->addChild (node.get()) ;
    
        //启用裁剪平面
        root ->getOrCreateStateSet()->setAttributeAndModes (cp1.get(), osg::StateAttribute ::ON ) ;
        root ->getOrCreateStateSet()->setAttributeAndModes (cp2.get(), osg::StateAttribute ::ON ) ;
    
        //优化场景数据
        osgUtil::Optimizer optimzier ;
        optimzier.optimize(root.get());
    
        viewer->setSceneData(root.get());
    
        viewer->realize();
    
        viewer->run();
    
        return 0;
    }
    

    结果图:
    这里写图片描述

  • 相关阅读:
    hdu1593(find a way to escape)
    每日学习小记 11/02
    将博客搬至CSDN
    浏览器渲染机制
    适配器模式 The Adapter Pattern
    工厂方法模式 The Factory Method Pattern
    观察者模式 The Observer Pattern
    模板方法模式 The Template Method Pattern
    命令模式 The Command Pattern
    迭代器模式 The Iterator Pattern
  • 原文地址:https://www.cnblogs.com/huahai/p/7270920.html
Copyright © 2011-2022 走看看