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))