osg :: MatrixTransform basic transformation model

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

    // add 
    void addChildVC (OSG :: * the Node nodeParam);
     // Set Model rotatably 
    void rotateObject ( const OSG :: Pivot Vec3d &, const OSG :: Vec3d & Axis, a float   angularVelocity);
    
    // model rotation 
    void toRotate ( float anguarVelocity);
     void toRotate ( const OSG :: the Matrix & matrixParam);

    // model scaling 
    void scaleModel ( float scaleSize);
     void scaleModel ( const OSG :: the Matrix & matrixParam);

    // model moving 
    void toPosition (OSG :: Vec3d & POS);

    // limit the size of the model 
    void adaptModelSize (OSG :: BoundingSphere & boundingS);
     void adaptModelSize (OSG :: the Node * nodePAram);

private:
    osg::ref_ptr<osg::MatrixTransform> matParam;
    osg::BoundingSphere bSphere;
    the Node :: OSG * oriNode;
     float Level; // scaling parameter 
};

 

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

// model rotation 
void VCNodeMatrix :: toRotate ( float anguarVelocity)
{
    //setMatrix(matrixParam);

}

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

// model scaling 
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);
}

// model moving 
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));
}

// limit the size of the model 
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;
    :: ref_ptr OSG <OSG the Node ::> :: readNodeFile osgDB Node = ( " D: \\ Reference OSG \\ \\ build1.OSGB the BIM \\ " );
     // vcnode-> the addChild (node.get () ); 
    vcnode-> addChildVC (the 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;
}

 

 

 

Guess you like

Origin www.cnblogs.com/herd/p/11078515.html