// draw callback that will tweak the far clipping plane just
    // before rendering a drawable.
    struct OverrideNearFarValuesCallback : public osg::Drawable::DrawCallback
    {
        OverrideNearFarValuesCallback(double radius)
            : _radius(radius) {}

virtual void drawImplementation(osg::RenderInfo& renderInfo,
            const osg::Drawable* drawable) const
        {
            osg::Camera* currentCamera = renderInfo.getCurrentCamera();
            if (currentCamera)
            {
                // Get the current camera position.
                osg::Vec3 eye, center, up;
                renderInfo.getCurrentCamera()->getViewMatrixAsLookAt( eye, center, up);

// Get the max distance we need the far plane to be at,
                // which is the distance between the eye and the origin
                // plus the distant from the origin to the object (star sphere
                // radius, sun distance etc), and then some.
                double distance = eye.length() + _radius*2;

// Save old values.
                osg::ref_ptr<osg::RefMatrixd> oldProjectionMatrix = new osg::RefMatrix;
                oldProjectionMatrix->set( renderInfo.getState()->getProjectionMatrix());

// Get the individual values
                double left, right, bottom, top, zNear, zFar;
                oldProjectionMatrix->getFrustum( left, right, bottom, top, zNear, zFar);

// Build a new projection matrix with a modified far plane
                osg::ref_ptr<osg::RefMatrixd> projectionMatrix = new osg::RefMatrix;
                projectionMatrix->makeFrustum( left, right, bottom, top, zNear, distance );

renderInfo.getState()->applyProjectionMatrix( projectionMatrix.get());

// Draw the drawable
                drawable->drawImplementation(renderInfo);

// Reset the far plane to the old value.
                renderInfo.getState()->applyProjectionMatrix( oldProjectionMatrix.get() );
            }
            else
            {
                drawable->drawImplementation(renderInfo);
            }
        }

double _radius;
    };

struct AddCallbackToDrawablesVisitor : public osg::NodeVisitor
    {
        AddCallbackToDrawablesVisitor(double radius)
            : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
            _radius(radius) {}

virtual void apply(osg::Geode& node)
        {
            for (unsigned int i = 0; i < node.getNumDrawables(); i++)
            {
                node.getDrawable(i)->setDrawCallback( new OverrideNearFarValuesCallback(_radius) );

// Do not use display lists otherwise the callback will only
                // be called once on initial compile.
                node.getDrawable(i)->setUseDisplayList(false);
            }
        }

double _radius;
    };

调用

***********************************************

AddCallbackToDrawablesVisitor visitor( mpr_innerRadius );
 geode->accept( visitor );

**********************************************

怎样在osg中动态的设置drawable的最近最远裁剪面的更多相关文章

  1. Android代码中动态设置图片的大小(自动缩放),位置

    项目中需要用到在代码中动态调整图片的位置和设置图片大小,能自动缩放图片,用ImageView控件,具体做法如下: 1.布局文件 <RelativeLayout xmlns:android=&qu ...

  2. 在后台代码中动态生成pivot项并设置EventTrigger和Action的绑定

    最近在做今日头条WP的过程中,遇到需要动态生成Pivot项的问题.第一个版本是把几个频道写死在xaml里了,事件绑定也写在xaml里,每个频道绑定一个ObservableCollection<A ...

  3. Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)

    什么是动态定时任务:是由客户制定生成的,服务端只知道该去执行什么任务,但任务的定时是不确定的(是由客户制定).      这样总不能修改配置文件每定制个定时任务就增加一个trigger吧,即便允许客户 ...

  4. 使用装饰器模式动态设置Drawable的ColorFilter

    使用装饰器模式动态设置Drawable的ColorFilter 欢迎各位关注我的新浪微博:微博 转载请标明出处(kifile的博客) 非常多时候我们都希望Android控件点击的时候,有按下效果,选中 ...

  5. mvc中动态给一个Model类的属性设置验证

    原文:mvc中动态给一个Model类的属性设置验证 在mvc中有自带的验证机制,比如如果某个字段的类型是数字或者日期,那么用户在输入汉字或者英文字符时,那么编译器会自动验证并提示用户格式不正确,不过这 ...

  6. Quartz在Spring中动态设置cronExpression

    什么是动态定时任务:是由客户制定生成的,服务端只知道该去执行什么任务,但任务的定时是不确定的(是由客户制定). 这样总不能修改配置文件每定制个定时任务就增加一个trigger吧,即便允许客户修改配置文 ...

  7. 微信小程序首页index.js获取不到app.js中动态设置的globalData的原因以及解决方法

    前段时间开发了一款微信小程序,运行了也几个月了,在index.js中的onLoad生命周期里获取app.js中onLaunch生命周期中在接口里动态设置的globalData一直没有问题,结果昨天就获 ...

  8. 分别在javascript和JSP中动态设置下拉列表默认值

    一.JavaScript中动态设置select标签中<option>选项的默认值: 比如,要完成下边这个下拉列表的动态显示,并且当进行前后翻页时,下拉列表中的值自动更新为当前页码: 图1 ...

  9. OSG中的示例程序简介

    OSG中的示例程序简介 转自:http://www.cnblogs.com/indif/archive/2011/05/13/2045136.html 1.example_osganimate一)演示 ...

随机推荐

  1. shell 删除日志

    一般线上服务的日志都是采用回滚的防止,写一定数量的日志 或是有管理工具定期去转移老旧日志 前几天删除一个测试环境的日志,只保留两天的日志,结果把正在写的日志都给删掉了,不得不重启了服务,经过这一次的错 ...

  2. python 去掉\n\t多余空格

    >>> import re >>> sss = "SELECT a.id,\n       a.customer_id as user_id,\n     ...

  3. Strom-7 Storm Trident 详细介绍

    一.概要 1.1 Storm(简介)      Storm是一个实时的可靠地分布式流计算框架.      具体就不多说了,举个例子,它的一个典型的大数据实时计算应用场景:从Kafka消息队列读取消息( ...

  4. CCMoveTo 等函数理解

    CCMoveTo: 使用CCMoveTo action来让对象从右侧屏幕外移动到屏幕左侧.注意可以通过指定duration参数控制这一过程需要多久,这里我们随机给他2-4秒的时间. CCCallFun ...

  5. C# 引用类型的"祸害"

    前端时间刚刚弄完一个项目,终于有时间来总结与回顾了. 项目需求:给用户发送邮件,邮件分为系统邮件和个人邮件,需要按时间.未读降序排列. 一开始以为,这是一个很简单的需求,给邮件建了一个对象: clas ...

  6. 【hadoop代码笔记】Mapreduce shuffle过程之Map输出过程

    一.概要描述 shuffle是MapReduce的一个核心过程,因此没有在前面的MapReduce作业提交的过程中描述,而是单独拿出来比较详细的描述. 根据官方的流程图示如下: 本篇文章中只是想尝试从 ...

  7. SqlServer修改数据库文件及日志文件存放位置

    --查看当前的存放位置 select database_id,name,physical_name AS CurrentLocation,state_desc,size from sys.master ...

  8. libyuv颜色空间转换开源库

    libyuv据说在缩放和颜色空间转换,比ffmpeg效率高很多倍.不知道和我们的PP库比起来怎么样.同样有neon指令集优化.支持移动设备.

  9. 自动化运维工具ansible-如何设置客户端多python版本问题

    问题:在使用ansible进行管理客户主机时,发现客户主机安装了多个版本的python,并且默认版本为3.0 shell>>cat list 192.168.2.9 shell>&g ...

  10. Argument 'xxx' is not a function, got undefined,初学Angular的第一个坑

    终于考完试了,在没更新的这一段时间里,一直都在忙于应付考试.不过在期间也是接触到不少好玩的东西,比如Html5的Canvas,我用lufylegend的Html5引擎做了个<看你有所色>的 ...