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