osg::MatrixTransform 模型基本变换

VCNodeMatrix.h

#pragma once
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers> 
#include <osgViewer/CompositeViewer> 
#include <osgDB/ReadFile>
#include <osg/Geode>
#include <osg/Node>
#include <osgGA/TrackballManipulator>
#include <osg/GraphicsContext>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include <osg/Image>
#include <osg/Texture2D>
#include <osg/TexEnv>
#include <osg/TexGen>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osg/AnimationPath>

class  VCNodeMatrix :
    public osg::MatrixTransform
{
public:
    VCNodeMatrix();
    ~VCNodeMatrix();

    //添加
    void addChildVC(osg::Node *nodeParam);
    //设置模型转动方式
    void rotateObject(const osg::Vec3d &  pivot, const osg::Vec3d &  axis, float  angularVelocity);
    
    //模型旋转
    void toRotate(float anguarVelocity);
    void toRotate(const osg::Matrix &matrixParam);

    //模型缩放
    void scaleModel(float scaleSize);
    void scaleModel(const osg::Matrix &matrixParam);

    //模型移动
    void toPosition(osg::Vec3d &pos);

    //限制模型大小
    void adaptModelSize(osg::BoundingSphere &boundingS);
    void adaptModelSize(osg::Node *nodePAram);

private:
    osg::ref_ptr<osg::MatrixTransform> matParam;
    osg::BoundingSphere bSphere;
    osg::Node *oriNode;
    float level;//缩放参数
};

 

VCNodeMatrix.cpp

#include "VCNodeMatrix.h"

VCNodeMatrix::VCNodeMatrix()
{
    matParam = new osg::MatrixTransform;
    addChild(matParam.get());
    level = 1.0;
}

VCNodeMatrix::~VCNodeMatrix()
{
    //delete matParam;
}

void VCNodeMatrix::rotateObject(const osg::Vec3d &  pivot, const osg::Vec3d &  axis, float  angularVelocity)
{
    setUpdateCallback(new osg::AnimationPathCallback(pivot, axis, angularVelocity));
}

//模型旋转
void VCNodeMatrix::toRotate(float anguarVelocity)
{
    //setMatrix(matrixParam);

}

void VCNodeMatrix::toRotate(const osg::Matrix &matrixParam)
{
    matParam->setMatrix(matrixParam);
}

//模型缩放
void VCNodeMatrix::scaleModel(float scaleSize)
{
    matParam->setMatrix(osg::Matrix::scale(scaleSize,scaleSize,scaleSize));
}

void VCNodeMatrix::scaleModel(const osg::Matrix &matrixParam)
{
    matParam->setMatrix(matrixParam);
}

void VCNodeMatrix::addChildVC(osg::Node *nodeParam)
{
    oriNode = nodeParam;
    bSphere = nodeParam->getBound();
    //matParam->addChild(nodeParam);
    matParam->addChild(nodeParam);
}

//模型移动
void VCNodeMatrix::toPosition(osg::Vec3d &pos)
{
    osg::Vec3d vec3d;
    vec3d.set(bSphere.center().x()*level, bSphere.center().y()*level, bSphere.center().z()*level);
    matParam->setMatrix(osg::Matrix::translate(vec3d)*osg::Matrix::translate(pos));
}

//限制模型大小
void VCNodeMatrix::adaptModelSize(osg::BoundingSphere &boundingS)
{
    float level = boundingS.radius() / bSphere.radius();
    matParam->setMatrix(osg::Matrix::scale(level, level, level));

}

void VCNodeMatrix::adaptModelSize(osg::Node *nodeParam)
{
    osg::BoundingSphere bsNode = nodeParam->getBound();
    level = bsNode.radius() / bSphere.radius();
    matParam->setMatrix(osg::Matrix::scale(level, level, level));
}

 

osg::ref_ptr<VCNodeMatrix> OSG_Qt_Rotating_0624::cretateObj()
{
    osg::ref_ptr<VCNodeMatrix> vcnode = new VCNodeMatrix;
    osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("D:\\参考手册\\BIM\\osg\\build1.OSGB");
    //vcnode->addChild(node.get());
    vcnode->addChildVC(node.get());

    //vcnode->rotateObject(osg::Vec3d(10.0,0.0,0.0),osg::Z_AXIS,1.0);
    //vcnode->toRotate(osg::Matrix::rotate(osg::Quat(2.0,osg::Vec3d(1.0,0.0,0.0))));
    //vcnode->toRotate(osg::Matrix::rotate(1.0,osg::Z_AXIS));
    //vcnode->toRotate(osg::Matrix::translate(10.0,0.0,0.0));
    vcnode->toPosition(osg::Vec3d(20.0, 0.0, 0.0));

    //vcnode->adaptModelSize(vcnode.get());

    return vcnode;
}

 

 

 

转载于:https://www.cnblogs.com/herd/p/11078515.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值