OpenSceneGraph中创建一个大小不变的小球

OpenSceneGraph中创建一个大小不变的小球

OpenSceneGraph中创建一个大小不变的小球

核心思想是利用osg::AutoTransform类。具体用法可以看osg自带的例子autotrans

功能验证代码,就不要纠结内存泄露不泄露的问题了,直接裸指针搞起。

核心功能在Build_No_Scaling()函数中。

不过有一个问题就是,为什么小球在缩放过程中会失去光照效果呢?尤其是场景变小(物体放大)的过程中。有知道的留言说一下吧。

直接上代码把


// OpenSceneGraph library.
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgGA/StateSetManipulator>
#include <osgViewer/ViewerEventHandlers>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/ShapeDrawable>
#include <osg/LineWidth>
#include <osg/MatrixTransform>
#include <osgFX/Scribe>
#include <osgUtil/SmoothingVisitor>
//下面两个头文件设置点和线宽
#include <osg/Point>
#include <osg/AutoTransform>
#include <osg/Light>

#pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgViewerd.lib")
#pragma comment(lib, "osgFXd.lib")

const osgFX::Scribe * one_scribe = NULL;



// 返回一个不缩放的小球. 但是为什么缩小的时候,才有光照呢?
osg::Node* Build_No_Scaling(float x, float y, float z)
{
   
   

    // 7. add a sphere 
    osg::Sphere* cylinder = new osg::Sphere(osg::Vec3f(x, y, z), 6.0f);
    osg::ShapeDrawable * drawable = new osg::ShapeDrawable(cylinder);
    drawable->setColor(osg::Vec4f(1.0, 0.5, 0.5, 0.0));
    osg::Geode*  geode = new osg::Geode;
    geode ->addDrawable(drawable);

    //  这里可以让圆球不缩放. 
    osg::AutoTransform* at = new osg::AutoTransform;
    at->addChild(geode);
    at->setMinimumScale(0.0);
    at->setMaximumScale(FLT_MAX);

    at->setAutoRotateMode(osg::AutoTransform::ROTATE_TO_SCREEN);
    //at->setAutoRotateMode(osg::AutoTransform::NO_ROTATION);
    at->setAutoScaleToScreen(true);
    at->setScale(osg::Vec3f(1, 1, 1))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值