OSG 模型添加(含自己画的模型,已有模型),对添加的模型做平移 缩放等。

添加已有模型 

在osg中同时加入多个模型(A,B,C……),可通过Group->addChild(A)添加。然后再把group添加到viewer中就可以了


添加自定义模型

添加用户自定义模型,我使用函数osg::ref_prt<osg::Node> createSceneGraph() 自己画了一个模型,然后通过group->addChild(createSceneGraph().get());添加到场景里


平移,缩放,旋转


osg::ref_ptr<osg::Node> cow = osgDB::readNodeFile("cow.osg");


//上平移 5
osg::ref_ptr<osg::MatrixTransform> tran = new osg::MatrixTransform;
tran->setMatrix(osg::Matrix::translate(0,0,5));
tran->addChild(cow.get());

//缩放 0.5倍 右移8,上移5
osg::ref_ptr<osg::MatrixTransform> scale = new osg::MatrixTransform;
scale->setMatrix(osg::Matrix::scale(0.5,0.5,0.5)*osg::Matrix::translate(8,0,5));
scale->addChild(cow.get());

//旋转 平放30°
osg::ref_ptr<osg::MatrixTransform> rotate = new osg::MatrixTransform;
rotate->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(30.0),1,1,1)*osg::Matrix::scale(0.5,0.5,.05)*osg::Matrix::translate(6,0,-5));
rotate->addChild(cow.get());


完整代码:

#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/MatrixTransform>
//#include <osg/Group>


//自定义模型

osg::ref_ptr<osg::Node> createSceneGraph()
{
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
//画顶点
osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
geom->setVertexArray( v.get() );
v->push_back( osg::Vec3( -6.f, 0.f, -1.f ) );
v->push_back( osg::Vec3( -3.f, 0.f, -1.f ) );
v->push_back( osg::Vec3( -3.f, 0.f, 1.f ) );
v->push_back( osg::Vec3( -6.f, 0.f, 1.f ) );

//给颜色
osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
geom->setColorArray(c.get());
geom->setColorBinding( osg::Geometry::BIND_PER_VERTEX );


c->push_back( osg::Vec4( 1.f, 0.f, 0.f, 1.f ) );
c->push_back( osg::Vec4( 0.f, 1.f, 0.f, 1.f ) );
c->push_back( osg::Vec4( 0.f, 0.f, 1.f, 1.f ) );
c->push_back( osg::Vec4( 1.f, 1.f, 1.f, 1.f ) );

// 为唯一的法线创建一个数组
osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
geom->setNormalArray( n.get() );
geom->setNormalBinding( osg::Geometry::BIND_OVERALL );
n->push_back( osg::Vec3( 0.f, -1.f, 0.f ) );

// 由保存的数据绘制四个顶点的多边形
geom->addPrimitiveSet(new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ) );


// 向Geode 类添加几何体(Drawable)并返回Geode
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable( geom.get() );
return geode.get();


}
int main()
{
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Group> group = new osg::Group;



osg::ref_ptr<osg::Node> cow = osgDB::readNodeFile("cow.osg");


//上平移
osg::ref_ptr<osg::MatrixTransform> tran = new osg::MatrixTransform;
tran->setMatrix(osg::Matrix::translate(0,0,5));
tran->addChild(cow.get());

//缩放
osg::ref_ptr<osg::MatrixTransform> scale = new osg::MatrixTransform;
scale->setMatrix(osg::Matrix::scale(0.5,0.5,0.5)*osg::Matrix::translate(8,0,5));
scale->addChild(cow.get());

//旋转
osg::ref_ptr<osg::MatrixTransform> rotate = new osg::MatrixTransform;
rotate->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(30.0),1,1,1)*osg::Matrix::scale(0.5,0.5,.05)*osg::Matrix::translate(6,0,-5));
rotate->addChild(cow.get());



group->addChild(osgDB::readNodeFile("robot.osg"));
group->addChild(createSceneGraph().get());
group->addChild(tran.get());
group->addChild(scale.get());
group->addChild(rotate.get());
viewer.setSceneData(group);
viewer.realize();
return viewer.run();
}


效果如图



  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
osg(OpenSceneGraph)是一个开源的3D图形引擎,支持多种平台和操作系统,包括Windows、Linux、macOS等。如果您想在osg添加骨架和动模型,可以按照以下步骤进行操作: 1. 导入模型:使用osgDB库中的readNodeFile函数,将模型文件导入到场景图中。例如,可以使用以下代码导入.obj格式的模型文件: ``` osg::Node* model = osgDB::readNodeFile("model.obj"); ``` 2. 添加骨架:osg中的骨架是通过osgAnimation库实现的。可以使用Skeleton类创建骨架,并将其添加模型节点中。例如,可以使用以下代码创建骨架: ``` osg::ref_ptr<osgAnimation::Skeleton> skeleton = new osgAnimation::Skeleton; ``` 然后,将骨架添加模型节点中: ``` osgAnimation::UpdateSkeletonVisitor* visitor = new osgAnimation::UpdateSkeletonVisitor; model->accept(*visitor); model->setUpdateCallback(new osgAnimation::UpdateSkeletonCallback(skeleton.get())); ``` 3. 添加osgAnimation库支持多种动格式,包括.osg、.ive、.osgAnimation等。可以使用AnimationManager类加载动文件,并将其应用到骨架上。例如,可以使用以下代码加载动文件: ``` osg::ref_ptr<osgAnimation::AnimationManager> manager = new osgAnimation::AnimationManagerBase; osgDB::Registry::instance()->loadAnimationObject("animation.osganimation", manager.get()); ``` 然后,将动应用到骨架上: ``` osg::ref_ptr<osgAnimation::Animation> animation = manager->getAnimation(0); animation->setPlayMode(osgAnimation::Animation::LOOP); osgAnimation::AnimationUpdateCallback* ac = new osgAnimation::AnimationUpdateCallback; ac->setAnimation(animation.get()); skeleton->setUpdateCallback(ac); ``` 以上是添加骨架和动模型的大致操作流程。具体实现要根据您的需求进行调整和修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值