第5课时《osg矩阵变换(下)》
来源:互联网 发布:计算机c语言教学视频 编辑:程序博客网 时间:2024/05/16 18:10
一、模型转动
//矩阵变换
osg::ref_ptr<osg::Node> MatrixOpation()
{
osg::ref_ptr<osg::Group> group= new osg::Group;
osg::ref_ptr<osg::MatrixTransform> maxT= new osg::MatrixTransform;
osg::ref_ptr<osg::MatrixTransform> maxTsub1= new osg::MatrixTransform;
osg::ref_ptr<osg::Node> node= osgDB::readNodeFile("robot.osg");
//模型沿着Z轴转动
maxTsub1->addChild(node.get());
maxTsub1->setMatrix(osg::Matrix::translate(5.0,0.0,0.0));
maxT->addChild(maxTsub1);
//回调函数,使用路径回调
//在模型渲染的过程中,也就是最后一步:viewer.run(),通过回调函数进行相关事件的调用
maxT->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3(5.0,0.0,0.0),osg::Z_AXIS,1.0));
group->addChild(maxT);
group->addChild(node.get());
return group;
}二、模型控制类实现模型的平移、旋转、缩放等操作
1.NodeMatrixTransform类
#include <osg/MatrixTransform>
#include <osg/Node>
#include <osg/BoundingSphere>
class NodeMatrxiTransform : public osg::MatrixTransform
{
public:
NodeMatrxiTransform(void);
~NodeMatrxiTransform(void);
public:
/**设置当前模型转动方式*/
void rotating(const osg::Vec3d &pivot, const osg::Vec3d &axis, float angularVelocity);
/**旋转模型*/
void toRotate(const osg::Matrix &mat);
/**旋转模型*/
void toRotate(float angle, const osg::Vec3f &axis);
/**缩放模型*/
void toScale(const osg::Matrix &mat);
/**缩放模型*/
void toScale(double &lel);
/**addsChild方法*/
void addsChild(osg::Node *node);
/**将模型移到目的点*/
void toPosition(osg::Vec3d &pos);
/**得到模型当前的位置*/
osg::Vec3d getPosition();
/**限制模型大小*/
void adapt(osg::BoundingSphere &sps);
/**限制模型大小*/
void adapt(osg::Node *node);
private:
osg::ref_ptr<osg::MatrixTransform> pat;
osg::BoundingSphere ps;
osg::Node * pnode;
float level;
osg::Vec3d position;
};2. NodeMatrixTransform.cpp文件
#include "nodematrxitransform.h"
#define NODE_MATRIX __declspec(dllexport)
NodeMatrxiTransform::NodeMatrxiTransform(void)
{
pat = new osg::MatrixTransform;
addChild(pat.get());
level = 1.0;
position = osg::Vec3d(0, 0, 0);
}
NodeMatrxiTransform::~NodeMatrxiTransform(void)
{
}
/**设置当前模型转动方式*/
void NodeMatrxiTransform::rotating(const osg::Vec3d &pivot, const osg::Vec3d &axis, float angularVelocity)
{
setUpdateCallback(new osg::AnimationPathCallback(pivot, axis, angularVelocity));
}
/**旋转模型*/
void NodeMatrxiTransform::toRotate(const osg::Matrix &mat)
{
pat->setMatrix(mat);
}
/**旋转模型*/
void NodeMatrxiTransform::toRotate(float angle, const osg::Vec3f &axis)
{
pat->setMatrix(osg::Matrix::rotate(angle, axis));
}
/**缩放模型*/
void NodeMatrxiTransform::toScale(const osg::Matrix &mat)
{
pat->setMatrix(mat);
}
/**缩放模型*/
void NodeMatrxiTransform::toScale(double &lel)
{
pat->setMatrix(osg::Matrix::scale(lel, lel, lel));
}
/**addsChild方法*/
void NodeMatrxiTransform::addsChild(osg::Node *node)
{
pat->addChild(node);
pnode = node;
ps = node->getBound();
osg::notify(osg::NOTICE)<<ps.center().x() << " " <<ps.center().y() << " " <<ps.center().z() << std::endl;
}
/**将模型移到目的点*/
void NodeMatrxiTransform::toPosition(osg::Vec3d &pos)
{
osg::Vec3d cps;
cps.set(-ps.center().x()*level, -ps.center().y()*level, -ps.center().z()*level);
pat->setMatrix(osg::Matrix::translate(cps)*osg::Matrix::translate(pos));
position = pos;
}
/**限制模型大小*/
void NodeMatrxiTransform::adapt(osg::BoundingSphere &sps)
{
float level = sps.radius()/ps.radius();
pat->setMatrix(osg::Matrix::scale(level, level, level));
}
/**限制模型大小*/
void NodeMatrxiTransform::adapt(osg::Node * node)
{
osg::BoundingSphere sps = node->getBound();
level = sps.radius()/ps.radius();
pat->setMatrix(osg::Matrix::scale(level, level, level));
}
/**得到当前的位置*/
osg::Vec3d NodeMatrxiTransform::getPosition()
{
return position;
}3. 导出dll
TEMPLATE = lib
DEFINES += NODE_MATRIXTRANSFORM------------------------------------------------------------------
//导出dll库
#if defined(NODE_MATRIXTRANSFORM)
# define NODE_MATRIXTRANSFORM_EXPORT Q_DECL_EXPORT
#else
# define NODE_MATRIXTRANSFORM_EXPORT Q_DECL_IMPORT
#endif
4. 引用dll 使用其方法
//pro文件
win32:DEPENDPATH +=C:/OpenSceneGraph/include \
$$PWD \
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/ -lNodeMatrixelse:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/ -lNodeMatrixdelse:unix:!macx: LIBS += -L$$PWD/ -lNodeMatrix
//cpp文件
#include "./nodematrixtransform.h"
osg::ref_ptr<NodeMatrxiTransform> nodeT= new NodeMatrxiTransform;
nodeT->addsChild(osgDB::readNodeFile("Cow.osg"));
nodeT->toPosition(osg::Vec3d(10.0, 0.0, 0.0));
nodeT->adapt(node.get());
0 0
- 第5课时《osg矩阵变换(下)》
- 第4课时《osg矩阵变换(上)》
- 第7课时《osg交互》
- 第3课时《osg模型控制》
- OSG学习:矩阵变换节点示例
- osg学习第一篇:矩阵
- 第5课时,自测
- 矩阵变换、osg的回调、动画路径和Geometry
- 第4课时,实践5
- 提高第5课时,自测
- 进阶第5课时,自测
- osg之矩阵(一)
- osg之矩阵(二)
- OSG学习:裁剪变换(1)
- OSG学习:裁剪变换(2)
- osg矩阵
- HDU4998Rotate(矩阵变换)
- 第5课时.python的数据类型
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round]A Raising Bacteria
- 利用MyEclipse对数据表产生CRUD方法却失效的解决方案
- Hadoop学习笔记(6)-简述分布式文件系统HDFS原理
- nyoj 545 第五届河南省程序设计大赛F
- grub分析
- 第5课时《osg矩阵变换(下)》
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round]B Finding Team Member
- myeclipse安装和基本配置
- iOS 网络篇4—GET请求和POST请求
- 《剑指offer》——二维数组中的查找
- SODBASE CEP学习进阶篇(二)续:日志采集-Flume Syslog采集
- ReactiveCocoa操作方法(过滤,秩序,时间,重复)
- 算法思想系列--分治策略(Divide-Conquer-Combine)(待补充)
- LeetCode-78.Subsets