/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2011 * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or * (at your option) any later version. The full license is in LICENSE file * included with this distribution, and on the openscenegraph.org website. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * OpenSceneGraph Public License for more details. */ #ifndef MYDRAGGER_H #define MYDRAGGER_H #include "BaseDragger.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "../Assist/SceneGraphDef.h" namespace osgDragger { osg::Geode* createPlane(); osg::Geode* createAxis(std::string tex, osgText::Font* font, osg::Vec4 color); osg::Geode* InsertAxis(float size); osg::Drawable* InsertCircle(float radius, unsigned int numSegments); template class TemplateMoveDragger : public BaseDragger { public: typedef Dragger1DType LineDragger; typedef Dragger2DType PlaneDragger; TemplateMoveDragger(void) { float pixelSize = 80.0f; osg::ref_ptr scaler = new osg::MatrixTransform; scaler->setMatrix(osg::Matrix::scale(pixelSize, pixelSize, pixelSize)); //根据像素来缩放 osg::ref_ptr at = new osg::AutoTransform; at->setAutoScaleToScreen(true); at->addChild(scaler); osg::ref_ptr as = new osgManipulator::AntiSquish; as->addChild(at); addChild(as); _xDragger = new LineDragger(osg::Vec3(0.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0)); scaler->addChild(_xDragger.get()); addDragger(_xDragger.get()); _yDragger = new LineDragger(osg::Vec3(0.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0)); scaler->addChild(_yDragger.get()); addDragger(_yDragger.get()); _zDragger = new LineDragger(osg::Vec3(0.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0)); scaler->addChild(_zDragger.get()); addDragger(_zDragger.get()); //_xoyDragger = new PlaneDragger(); //scaler->addChild(_xoyDragger.get()); //addDragger(_xoyDragger.get()); //_xozDragger = new PlaneDragger(); //scaler->addChild(_xozDragger.get()); //addDragger(_xozDragger.get()); //_yozDragger = new PlaneDragger(); //scaler->addChild(_yozDragger.get()); //addDragger(_yozDragger.get()); setParentDragger(getParentDragger()); } virtual void setupDefaultGeometry() { osg::Vec4 xColor = osg::Vec4(1.0, 0.0, 0.0, 1.0); osg::Vec4 yColor = osg::Vec4(0.0, 1.0, 0.0, 1.0); osg::Vec4 zColor = osg::Vec4(0.0, 0.0, 1.0, 1.0); osg::Vec4 pickColor = osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f); osg::ref_ptr font = osgText::readFontFile("font/times.ttf"); //simsun.ttc arial.ttf font\\times.ttf _xDragger->addChild( createAxis("x", font.get(), xColor)); _yDragger->addChild( createAxis("y", font.get(), yColor)); _zDragger->addChild( createAxis("z", font.get(), zColor)); _xDragger->setColor(xColor); _yDragger->setColor(yColor); _zDragger->setColor(zColor); // Rotate X-axis dragger appropriately. { osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(1.0f, 0.0f, 0.0f)); _xDragger->setMatrix(osg::Matrix(rotation)); } // Rotate Y-axis dragger appropriately. { osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(0.0f, 1.0f, 0.0f)); _yDragger->setMatrix(osg::Matrix(rotation)); } ////xoz->xoy //{ // osg::Quat rotation; // rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(0.0f, 1.0f, 0.0f)); // _xoyDragger->setMatrix(osg::Matrix(rotation)); //} ////xoz->yoz //{ // osg::Quat rotation; // rotation.makeRotate(osg::Vec3(1.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 1.0f, 0.0f)); // _yozDragger->setMatrix(osg::Matrix(rotation)); //} // //{ // osg::ref_ptr plane = createPlane(); // _xoyDragger->addChild( plane.get() ); // _yozDragger->addChild( plane.get() ); // _xozDragger->addChild( plane.get() ); // _xoyDragger->setColor(zColor); // _yozDragger->setColor(xColor); // _xozDragger->setColor(yColor); //} } protected: virtual ~TemplateMoveDragger(void) {} osg::ref_ptr< LineDragger > _xDragger; osg::ref_ptr< LineDragger > _yDragger; osg::ref_ptr< LineDragger > _zDragger; osg::ref_ptr< PlaneDragger > _yozDragger; osg::ref_ptr< PlaneDragger > _xoyDragger; osg::ref_ptr< PlaneDragger > _xozDragger; }; //指定方向移动拖动器 template class TemplateMoveDirDragger : public BaseDragger { public: typedef Dragger1DType LineDragger; typedef Dragger2DType PlaneDragger; TemplateMoveDirDragger(void) { float pixelSize = 80.0f; osg::ref_ptr scaler = new osg::MatrixTransform; scaler->setMatrix(osg::Matrix::scale(pixelSize, pixelSize, pixelSize)); //根据像素来缩放 osg::ref_ptr at = new osg::AutoTransform; at->setAutoScaleToScreen(true); at->addChild(scaler); osg::ref_ptr as = new osgManipulator::AntiSquish; as->addChild(at); addChild(as); m_Dragger = new LineDragger(osg::Vec3(0.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0)); scaler->addChild(m_Dragger.get()); addDragger(m_Dragger.get()); setParentDragger(getParentDragger()); } virtual void setupDefaultGeometry() { osg::Vec4 Color = osg::Vec4(1.0, 0.0, 0.0, 1.0); osg::Vec4 pickColor = osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f); osg::ref_ptr font = osgText::readFontFile("font/times.ttf"); //simsun.ttc arial.ttf font\\times.ttf m_Dragger->addChild( createAxis("", font.get(), Color)); m_Dragger->setColor(Color); } protected: virtual ~TemplateMoveDirDragger(void) {} osg::ref_ptr< LineDragger > m_Dragger; }; //指定方向移动+绕轴旋转拖动器 template class TemplateMoveDirAndRotateDragger : public BaseDragger { public: typedef DraggerType1 LineDragger; typedef DraggerType2 RotateDragger; TemplateMoveDirAndRotateDragger(void) { float pixelSize = 80.0f; osg::ref_ptr scaler = new osg::MatrixTransform; scaler->setMatrix(osg::Matrix::scale(pixelSize, pixelSize, pixelSize)); osg::ref_ptr at = new osg::AutoTransform; at->setAutoScaleToScreen(true); at->addChild(scaler); osg::ref_ptr as = new osgManipulator::AntiSquish; as->addChild(at); addChild(as); m_Dragger = new LineDragger(osg::Vec3(0.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0)); scaler->addChild(m_Dragger.get()); addDragger(m_Dragger.get()); m_RotateZ = new RotateDragger; scaler->addChild(m_RotateZ.get()); addDragger(m_RotateZ.get()); setParentDragger(getParentDragger()); } virtual void setupDefaultGeometry() { osg::Vec4 Color = osg::Vec4(1.0, 0.0, 0.0, 1.0); osg::Vec4 pickColor = osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f); osg::ref_ptr font = osgText::readFontFile("font/times.ttf"); //simsun.ttc arial.ttf font\\times.ttf m_Dragger->addChild( createAxis("", font.get(), Color)); m_Dragger->setColor(Color); osg::ref_ptr geode = new osg::Geode; geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF); osg::ref_ptr hints = new osg::TessellationHints; hints->setCreateTop(true); hints->setCreateBottom(true); hints->setCreateBackFace(true); osg::ref_ptr cylinder = new osg::Cylinder; cylinder->setHeight(0.1f); osg::ref_ptr cylinderDrawable = new osg::ShapeDrawable(cylinder,hints); geode->addDrawable(cylinderDrawable); osgManipulator::setDrawableToAlwaysCull(*cylinderDrawable); geode->addDrawable(InsertCircle(0.5f, 150)); osg::ref_ptr lineWidth = new osg::LineWidth; lineWidth->setWidth(2.0f); geode->getOrCreateStateSet()->setAttributeAndModes(lineWidth.get(), osg::StateAttribute::ON); m_RotateZ->addChild(geode); osg::Matrix mat; mat.setTrans(osg::Vec3d(0,0,1.0)); m_RotateZ->setMatrix(mat); } protected: virtual ~TemplateMoveDirAndRotateDragger(void) {} osg::ref_ptr m_Dragger; osg::ref_ptr m_RotateZ; }; //2D移动拖动器加旋转 template class TemplateMoveDragger2D : public BaseDragger { public: typedef Dragger1DType LineDragger; typedef Dragger2DType RotateDragger; TemplateMoveDragger2D(void) { osg::ref_ptr as = new osgManipulator::AntiSquish; this->addChild(as); osg::ref_ptr at = new osg::AutoTransform; at->setAutoScaleToScreen(true); as->addChild(at); osg::ref_ptr scaler = new osg::MatrixTransform; scaler->setMatrix(osg::Matrix::scale(80.0f, 80.0f, 80.0f)); at->addChild(scaler); m_TranslateX1 = new LineDragger(osg::Vec3(0.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0)); scaler->addChild(m_TranslateX1.get()); addDragger(m_TranslateX1.get()); m_TranslateX2 = new LineDragger(osg::Vec3(0.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0)); scaler->addChild(m_TranslateX2.get()); addDragger(m_TranslateX2.get()); m_TranslateY1 = new LineDragger(osg::Vec3(0.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0)); scaler->addChild(m_TranslateY1.get()); addDragger(m_TranslateY1.get()); m_TranslateY2 = new LineDragger(osg::Vec3(0.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0)); scaler->addChild(m_TranslateY2.get()); addDragger(m_TranslateY2.get()); m_RotateZ = new RotateDragger; scaler->addChild(m_RotateZ.get()); addDragger(m_RotateZ.get()); setParentDragger(getParentDragger()); } virtual void setupDefaultGeometry() { osg::Quat rotation; osg::Vec4 Color1 = osg::Vec4(0.0, 0.0, 1.0, 1.0); osg::Vec4 Color2 = osg::Vec4(0.0, 1.0, 0.0, 1.0); osg::Vec4 Color3 = osg::Vec4(0.0, 1.0, 1.0, 1.0); osg::ref_ptr font = osgText::readFontFile("font/times.ttf"); m_TranslateX1->addChild(createAxis("", font.get(), Color1)); //m_TranslateX1->setColor(Color1); rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(1.0f, 0.0f, 0.0f)); m_TranslateX1->setMatrix(osg::Matrix(rotation)); m_TranslateX2->addChild(createAxis("", font.get(), Color1)); //m_TranslateX2->setColor(Color1); rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(-1.0f, 0.0f, 0.0f)); m_TranslateX2->setMatrix(osg::Matrix(rotation)); m_TranslateY1->addChild(createAxis("", font.get(), Color2)); //m_TranslateY1->setColor(Color2); rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(0.0f, 1.0f, 0.0f)); m_TranslateY1->setMatrix(osg::Matrix(rotation)); m_TranslateY2->addChild(createAxis("", font.get(), Color2)); //m_TranslateY2->setColor(Color2); rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(0.0f, -1.0f, 0.0f)); m_TranslateY2->setMatrix(osg::Matrix(rotation)); osg::ref_ptr geode = new osg::Geode; geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF); osg::ref_ptr hints = new osg::TessellationHints; hints->setCreateTop(false); hints->setCreateBottom(false); hints->setCreateBackFace(false); osg::ref_ptr cylinder = new osg::Cylinder; cylinder->setHeight(0.515f); osg::ref_ptr cylinderDrawable = new osg::ShapeDrawable(cylinder,hints); geode->addDrawable(cylinderDrawable); osgManipulator::setDrawableToAlwaysCull(*cylinderDrawable); geode->addDrawable(InsertCircle(1.0f, 150)); osg::ref_ptr lineWidth = new osg::LineWidth; lineWidth->setWidth(1.4f); geode->getOrCreateStateSet()->setAttributeAndModes(lineWidth.get(), osg::StateAttribute::ON); m_RotateZ->addChild(geode); } protected: virtual ~TemplateMoveDragger2D(void) {} osg::ref_ptr m_TranslateX1; osg::ref_ptr m_TranslateX2; osg::ref_ptr m_TranslateY1; osg::ref_ptr m_TranslateY2; osg::ref_ptr m_RotateZ; }; template class TemplateRotateDragger : public BaseDragger { public: typedef Dragger1DType DraggerCylinder; typedef Dragger2DType DraggerSphere; TemplateRotateDragger(void) { float pixelSize = 80.0f; osg::ref_ptr scaler = new osg::MatrixTransform; scaler->setMatrix(osg::Matrix::scale(pixelSize, pixelSize, pixelSize)); osg::ref_ptr at = new osg::AutoTransform; at->setAutoScaleToScreen(true); at->addChild(scaler); osg::ref_ptr as = new osgManipulator::AntiSquish; as->addChild(at); addChild(as); _opDragger = new PointDragger; scaler->addChild(_opDragger); addDragger(_opDragger.get()); _xDragger = new DraggerCylinder; scaler->addChild(_xDragger.get()); addDragger(_xDragger.get()); _yDragger = new DraggerCylinder; scaler->addChild(_yDragger.get()); addDragger(_yDragger.get()); _zDragger = new DraggerCylinder; scaler->addChild(_zDragger.get()); addDragger(_zDragger.get()); //_xyzDragger = new DraggerSphere; //scaler->addChild(_xyzDragger.get()); //addDragger(_xyzDragger.get()); _clipPar = new osg::AutoTransform; _clipPar->setAutoRotateMode(osg::AutoTransform::ROTATE_TO_SCREEN); _clipPar->setNormal(osg::Vec3(0, -1, 0)); scaler->addChild(_clipPar.get()); osg::ref_ptr clipNode = new osg::ClipNode; osg::ref_ptr cp = new osg::ClipPlane(0,0,0,1,0.01f); clipNode->addClipPlane(cp); _clipPar->addChild(clipNode.get()); scaler->setStateSet(clipNode->getStateSet()); osg::ref_ptr scalerAxis = new osg::MatrixTransform; scalerAxis->setMatrix(osg::Matrix::scale(pixelSize, pixelSize, pixelSize)); scalerAxis->addChild(InsertAxis(0.4f)); at->addChild(scalerAxis); setParentDragger(getParentDragger()); } virtual void setupDefaultGeometry() { osg::ref_ptr geode = new osg::Geode; geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF); { osg::ref_ptr hints = new osg::TessellationHints; hints->setCreateTop(false); hints->setCreateBottom(false); hints->setCreateBackFace(false); osg::ref_ptr cylinder = new osg::Cylinder; cylinder->setHeight(0.15f); osg::ref_ptr cylinderDrawable = new osg::ShapeDrawable(cylinder,hints); geode->addDrawable(cylinderDrawable); osgManipulator::setDrawableToAlwaysCull(*cylinderDrawable); geode->addDrawable(InsertCircle(1.0f, 150)); } // Draw in line mode. osg::ref_ptr polymode = new osg::PolygonMode; polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE); osg::ref_ptr lineWidth = new osg::LineWidth; lineWidth->setWidth(1.4f); //geode->getOrCreateStateSet()->setAttributeAndModes(polymode.get(), osg::StateAttribute::ON); geode->getOrCreateStateSet()->setAttributeAndModes(lineWidth.get(), osg::StateAttribute::ON); _opDragger->addChild(HUtility::InsertSphere(osg::Vec3(0.0, 0.0, 0.0), 0.05, Green_Color)); _xDragger->addChild(geode); _yDragger->addChild(geode); _zDragger->addChild(geode); { osg::ref_ptr clipGeode = new osg::Geode; clipGeode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF); //clipGeode->getOrCreateStateSet()->setAttributeAndModes(polymode.get(), osg::StateAttribute::ON); clipGeode->getOrCreateStateSet()->setAttributeAndModes(lineWidth.get(), osg::StateAttribute::ON); clipGeode->addDrawable(InsertCircle(1.0f, 150)); clipGeode->addDrawable(InsertCircle(1.2f, 150)); osgManipulator::setMaterialColor(osg::Vec4(188/255.0,143/255.0,143/255.0,255/255.0),*clipGeode); _clipPar->addChild(clipGeode); } { osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(1.0f, 0.0f, 0.0f)); _xDragger->setMatrix(osg::Matrix(rotation)); } { osg::Quat rotation; rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), osg::Vec3(0.0f, 1.0f, 0.0f)); _yDragger->setMatrix(osg::Matrix(rotation)); } osg::Vec4 xColor = osg::Vec4(1.0, 0.0, 0.0, 1.0); osg::Vec4 yColor = osg::Vec4(0.0, 1.0, 0.0, 1.0); osg::Vec4 zColor = osg::Vec4(0.0, 0.0, 1.0, 1.0); _xDragger->setColor(xColor); _yDragger->setColor(yColor); _zDragger->setColor(zColor); { //osg::Drawable* sphereDrawable = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), 0.95f)); //osgManipulator::setDrawableToAlwaysCull(*sphereDrawable); //osg::Geode* sphereGeode = new osg::Geode; //sphereGeode->addDrawable(sphereDrawable); //_xyzDragger->addChild(sphereGeode); } } protected: virtual ~TemplateRotateDragger(){}; protected: osg::ref_ptr _opDragger; //旋转球心操作器 osg::ref_ptr _xDragger; osg::ref_ptr _yDragger; osg::ref_ptr _zDragger; osg::ref_ptr _xyzDragger; osg::ref_ptr _clipPar; }; template class TemplateCylinderDragger : public BaseDragger { public: typedef Dragger1DType DraggerCylinder; TemplateCylinderDragger(void) { float pixelSize = 80.0f; osg::ref_ptr scaler = new osg::MatrixTransform; scaler->setMatrix(osg::Matrix::scale(pixelSize, pixelSize, pixelSize)); osg::ref_ptr at = new osg::AutoTransform; at->setAutoScaleToScreen(true); at->addChild(scaler); osg::ref_ptr as = new osgManipulator::AntiSquish; as->addChild(at); addChild(as); _zDragger = new DraggerCylinder; scaler->addChild(_zDragger.get()); addDragger(_zDragger.get()); _clipPar = new osg::AutoTransform; _clipPar->setAutoRotateMode(osg::AutoTransform::ROTATE_TO_SCREEN); _clipPar->setNormal(osg::Vec3(0, -1, 0)); scaler->addChild(_clipPar.get()); osg::ref_ptr clipNode = new osg::ClipNode; osg::ref_ptr cp = new osg::ClipPlane(0,0,0,1,0.01f); clipNode->addClipPlane(cp); _clipPar->addChild(clipNode.get()); scaler->setStateSet(clipNode->getStateSet()); osg::ref_ptr scalerAxis = new osg::MatrixTransform; scalerAxis->setMatrix(osg::Matrix::scale(pixelSize, pixelSize, pixelSize)); scalerAxis->addChild(InsertAxis(0.4f)); at->addChild(scalerAxis); setParentDragger(getParentDragger()); } virtual void setupDefaultGeometry() { osg::ref_ptr geode = new osg::Geode; geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF); { osg::ref_ptr hints = new osg::TessellationHints; hints->setCreateTop(false); hints->setCreateBottom(false); hints->setCreateBackFace(false); osg::ref_ptr cylinder = new osg::Cylinder; cylinder->setHeight(0.15f); osg::ref_ptr cylinderDrawable = new osg::ShapeDrawable(cylinder,hints); geode->addDrawable(cylinderDrawable); osgManipulator::setDrawableToAlwaysCull(*cylinderDrawable); geode->addDrawable(InsertCircle(1.0f, 150)); } // Draw in line mode. osg::ref_ptr polymode = new osg::PolygonMode; polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE); osg::ref_ptr lineWidth = new osg::LineWidth; lineWidth->setWidth(1.4f); //geode->getOrCreateStateSet()->setAttributeAndModes(polymode.get(), osg::StateAttribute::ON); geode->getOrCreateStateSet()->setAttributeAndModes(lineWidth.get(), osg::StateAttribute::ON); _zDragger->addChild(geode); { osg::ref_ptr clipGeode = new osg::Geode; clipGeode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF); //clipGeode->getOrCreateStateSet()->setAttributeAndModes(polymode.get(), osg::StateAttribute::ON); clipGeode->getOrCreateStateSet()->setAttributeAndModes(lineWidth.get(), osg::StateAttribute::ON); clipGeode->addDrawable(InsertCircle(1.0f, 150)); clipGeode->addDrawable(InsertCircle(1.2f, 150)); osgManipulator::setMaterialColor(osg::Vec4(188/255.0,143/255.0,143/255.0,255/255.0),*clipGeode); _clipPar->addChild(clipGeode); } osg::Vec4 zColor = osg::Vec4(0.0, 0.0, 1.0, 1.0); _zDragger->setColor(zColor); } protected: virtual ~TemplateCylinderDragger(){}; protected: osg::ref_ptr _zDragger; osg::ref_ptr _clipPar; }; class ScaleDragger : public BaseDragger { public: ScaleDragger(void); virtual void setupDefaultGeometry(); protected: virtual ~ScaleDragger(); protected: osg::ref_ptr _xDragger; osg::ref_ptr _yDragger; osg::ref_ptr _zDragger; }; typedef TemplateMoveDragger MoveDragger; typedef TemplateMoveDirDragger MoveDirDragger; typedef TemplateRotateDragger RotateDragger; typedef TemplateCylinderDragger RotateDirDragger; typedef TemplateMoveDirAndRotateDragger MoveDirAndRotateDragger; typedef TemplateMoveDragger2D TranslateRotate2DDragger; } #endif