zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    ffmpeg 简单使用总结
    使用 Solr 构建企业级搜索服务器
    Linux 常用命令整理
    基于.net standard 的动态编译实现
    基于.net core 微服务的另类实现
    Java中线程总结
    关于EF中直接执行sql语句的参数化问题
    一个关于单据审核的流程演变
    java中匿名内部类总结
    Eclipse 中打开选中文件/文件夹所在目录
  • 原文地址:https://www.cnblogs.com/herd/p/11078515.html
Copyright © 2011-2022 走看看