查看: 1061|回复: 3

新人求助OSG选择点或线的高亮问题

[复制链接]

该用户从未签到

发表于 2015-6-3 19:13:48 | 显示全部楼层 |阅读模式
#include<osgViewer\Viewer>
#include<osgDB\ReadFile>
#include <osg/Group>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Texture2D>
#include <osg/PositionAttitudeTransform>
#include <osg/TexEnv>
#include <osg/Image>
#include <osg/StateSet>
#include <osgGA/GUIEventAdapter>
#include <osgGA/GUIEventAdapter>
#include <osgUtil/LineSegmentIntersector>
#include <osgViewer/ViewerEventHandlers>
#include <osg/Material>
#include <osg/LineWidth>
#include <iostream>
#include<osgManipulator/TrackballDragger>
#include<osgManipulator/TranslateAxisDragger>
#include<osgManipulator/ScaleAxisDragger>
#include<osg/MatrixTransform>
#include <osg/TexGen>
#include <osg/TexEnv>
#include<osg/Light>
#include<osg/LightSource>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Material>
#include <osg/Vec3>
#include <osg/MatrixTransform>
#include <osg/Texture2D>
#include <osg/PolygonStipple>
#include <osg/TriangleFunctor>
#include <osg/io_utils>
#include<osg/Point>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
#include <osg/Math>
#include<osgFX/Scribe>
#include <iostream>
#include <string.h>
#pragma comment(lib,"osgUtild.lib")
#pragma comment(lib,"OpenThreads.lib")
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"osgManipulator.lib")
#pragma comment(lib,"osg.lib")
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osgGA.lib")
#pragma comment(lib,"osgFX.lib")
class CPickHandler:public osgGA::GUIEventHandler
{
public:
    CPickHandler(osgViewer::Viewer*viewer):mViewer(viewer),lastSelect(0){}
    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
    {
        switch(ea.getEventType())
        {
        case(osgGA::GUIEventAdapter::MOVE):
        {
            if(ea.getButton()==1)
            {
                Pick(ea.getX(),ea.getY());
            }
            return true;
        }
        return false;
        }
    }
protected:void Pick(float x,float y)
    {
        osgUtil:ineSegmentIntersector::Intersections intersections;
        if(mViewer->computeIntersections(x,y,intersections))
        {
            for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();
                hitr!=intersections.end();++hitr)
            {
                if(!hitr->nodePath.empty()&&!(hitr->nodePath.back()->getName().empty()))
                {
                    const osg::NodePath& np=hitr->nodePath;
                    for(int i=np.size()-1;i>=0;--i)
                    {
                        osg::Geometry *sc=dynamic_cast<osg::Geometry *>(np[i]);
                        if(sc!=NULL)
                        {
                            if(sc->getName()=="linesGeom"||sc->getName()=="pointsGeom"||sc->getName()=="pointsGeom3"||
                                    sc->getName()=="pointsGeom4"||sc->getName()=="pointsGeom5")
                            {
                            osg::Vec4Array* colors6 = new osg::Vec4Array;  //变颜色
                            colors6->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
                            sc->setColorArray(colors6);
                            sc->setColorBinding(osg::Geometry::BIND_OVERALL);
                            osg::Vec3Array* normals6 = new osg::Vec3Array;
                            normals6->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
                            sc->setNormalArray(normals6);
                            }
                        }

                    }

                }

            }
        }
    }
    osgViewer::Viewer*mViewer;
    osg::Node*lastSelect;
};

int main()
{
    osg::ref_ptr<osg::Group> root = new osg::Group;
    osgViewer::Viewer myViewer;
    osg::Geode* geode = new osg::Geode;
     osg::Geode* geode2 = new osg::Geode;//两点
     osg::ref_ptr<osg::Node> node=new osg::Node;
    osg::Geometry* linesGeom = new osg::Geometry();//骨架直线
    osg::Vec3Array* vertices = new osg::Vec3Array(10);
    (*vertices)[0].set(20,20,40);
    (*vertices)[1].set(20,20,0);
    (*vertices)[2].set(20,20,20);
    (*vertices)[3].set(5,20,20);
    (*vertices)[4].set(20,20,20);
    (*vertices)[5].set(20,5,20);
    (*vertices)[6].set(27.5,27.5,0);
    (*vertices)[7].set(12.5,27.5,0);
    (*vertices)[8].set(27.5,27.5,0);
    (*vertices)[9].set(27.5,12.5,0);
    linesGeom->setVertexArray(vertices);

    osg::Vec4Array* colors = new osg::Vec4Array;
    colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
    linesGeom->setColorArray(colors);
    linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
    linesGeom->setName("linesGeom");
    osg::Vec3Array* normals = new osg::Vec3Array;
    normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));

    linesGeom->setNormalArray(normals);
    linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
    linesGeom->addPrimitiveSet(new osg:rawArrays(osg:rimitiveSet::LINES,0,vertices->size()));

    //曲线1
    osg::Geometry* pointsGeom = new osg::Geometry();
    osg::Vec3Array* vertices2 = new osg::Vec3Array;
    #define PI 3.14159f
    #define RADIUS 15.0f
    GLfloat x = 0, y = 0, angle = 0.0;
    for ( angle = -PI; angle <= -0.5f * PI; angle += 0.001f )
    {
        x = RADIUS * sin( angle )+27.5;
        y = RADIUS * cos( angle )+27.5;
        vertices2->push_back(osg::Vec3(x,y,0));
    }
    pointsGeom->setVertexArray(vertices2);

    osg::Vec4Array* colors2 = new osg::Vec4Array;
    colors2->push_back(osg::Vec4(0.0f,1.0f,1.0f,1.0f));
    pointsGeom->setColorArray(colors2);
    pointsGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
    osg::Vec3Array* normals2 = new osg::Vec3Array;
    normals2->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
    pointsGeom->setNormalArray(normals2);

    pointsGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
    pointsGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices2->size()));
    pointsGeom->setName("pointsGeom");
    //曲线2
    osg::Geometry* pointsGeom3 = new osg::Geometry();
    osg::Vec3Array* vertices3 = new osg::Vec3Array;
    GLfloat y3 = 0, z3 = 0, angle3 = 0.0;
    for ( angle3 = -0.5f * PI; angle3 <= 0; angle3 += 0.001f )
    {
        y3 = RADIUS * sin( angle3 )+20;
        z3 = RADIUS * cos( angle3 )+20;
        vertices3->push_back(osg::Vec3(20,y3,z3));
    }
    pointsGeom3->setVertexArray(vertices3);

    osg::Vec4Array* colors3 = new osg::Vec4Array;
    colors3->push_back(osg::Vec4(0.0f,1.0f,0.0f,1.0f));
    pointsGeom3->setColorArray(colors3);
    pointsGeom3->setColorBinding(osg::Geometry::BIND_OVERALL);
    osg::Vec3Array* normals3 = new osg::Vec3Array;
    normals3->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
    pointsGeom3->setNormalArray(normals3);

    pointsGeom3->setNormalBinding(osg::Geometry::BIND_OVERALL);
    pointsGeom3->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices3->size()));
    pointsGeom3->setName("pointsGeom3");
    //曲线3
    osg::Geometry* pointsGeom4 = new osg::Geometry();
    osg::Vec3Array* vertices4 = new osg::Vec3Array;
    GLfloat x4 = 0, z4 = 0, angle4 = 0.0;
    for ( angle4 = -0.5f * PI; angle4 <= 0; angle4 += 0.001f )
    {
        x4 = RADIUS * sin( angle4 )+20;
        z4 = RADIUS * cos( angle4 )+20;
        vertices4->push_back(osg::Vec3(x4,20,z4));
    }
    pointsGeom4->setVertexArray(vertices4);

    osg::Vec4Array* colors4 = new osg::Vec4Array;
    colors4->push_back(osg::Vec4(1.0f,0.0f,1.0f,1.0f));
    pointsGeom4->setColorArray(colors4);
    pointsGeom4->setColorBinding(osg::Geometry::BIND_OVERALL);
    osg::Vec3Array* normals4 = new osg::Vec3Array;
    normals4->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
    pointsGeom4->setNormalArray(normals4);

    pointsGeom4->setNormalBinding(osg::Geometry::BIND_OVERALL);
    pointsGeom4->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices4->size()));
    pointsGeom4->setName("pointsGeom4");
    //两点
    osg::Geometry* pointsGeom5 = new osg::Geometry();
    osg::Vec3Array* vertices5 = new osg::Vec3Array;
    vertices5->push_back(osg::Vec3(20,20,40));
    vertices5->push_back(osg::Vec3(20,20,0));
    pointsGeom5->setVertexArray(vertices5);
    osg::Vec4Array* colors5 = new osg::Vec4Array;
    colors5->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
    pointsGeom5->setColorArray(colors5);
    pointsGeom5->setColorBinding(osg::Geometry::BIND_OVERALL);
    osg::Vec3Array* normals5 = new osg::Vec3Array;
    normals5->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
    pointsGeom5->setNormalArray(normals5);

    pointsGeom5->setNormalBinding(osg::Geometry::BIND_OVERALL);
    pointsGeom5->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices5->size()));
    pointsGeom5->setName("pointsGeom5");

    osg::ref_ptr <osg::Point> ptSize = new osg::Point;
    ptSize->setSize( 9.0) ;
    geode2->getOrCreateStateSet()->setAttributeAndModes( ptSize.get (),osg::StateAttribute::ON);

    osg::ref_ptr<osg::MatrixTransform> selection=new osg::MatrixTransform;

    //tad
    osgManipulator::TranslateAxisDragger * tad=new osgManipulator::TranslateAxisDragger();
    float scale=20.0;
    tad->setupDefaultGeometry();
    tad->setMatrix(osg::Matrix::scale(scale,scale,scale)*osg::Matrix::translate(osg::Vec3(-20,-20,-20)));
    tad->addTransformUpdating(selection);
    tad->setHandleEvents(true);
    tad->setActivationModKeyMask(osgGA::GUIEventAdapter::MODKEY_CTRL);


    geode->addDrawable(pointsGeom);
    geode->addDrawable(pointsGeom3);
    geode->addDrawable(pointsGeom4);
    geode2->addDrawable(pointsGeom5);
    geode->addDrawable(linesGeom);
    root->addChild(geode);
    root->addChild(geode2);
    root->addChild(tad);
    myViewer.setSceneData(root.get());
   myViewer.addEventHandler(new CPickHandler(&myViewer));
    myViewer.realize();
    myViewer.run();
}


代码在上面,想要实现鼠标移动过右侧的模型的点或者线让点或者线变颜色。但是进去能显示正常模型,但是就动不了了。哪里出了问题那?求大神指导,最好能帮助修改一下。

该用户从未签到

 楼主| 发表于 2015-6-3 19:16:44 | 显示全部楼层
  1. #include<osgViewer\Viewer>
  2. #include<osgDB\ReadFile>
  3. #include <osg/Group>
  4. #include <osg/Geode>
  5. #include <osg/ShapeDrawable>
  6. #include <osg/Texture2D>
  7. #include <osg/PositionAttitudeTransform>
  8. #include <osg/TexEnv>
  9. #include <osg/Image>
  10. #include <osg/StateSet>
  11. #include <osgGA/GUIEventAdapter>
  12. #include <osgGA/GUIEventAdapter>
  13. #include <osgUtil/LineSegmentIntersector>
  14. #include <osgViewer/ViewerEventHandlers>
  15. #include <osg/Material>
  16. #include <osg/LineWidth>
  17. #include <iostream>
  18. #include<osgManipulator/TrackballDragger>
  19. #include<osgManipulator/TranslateAxisDragger>
  20. #include<osgManipulator/ScaleAxisDragger>
  21. #include<osg/MatrixTransform>
  22. #include <osg/TexGen>
  23. #include <osg/TexEnv>
  24. #include<osg/Light>
  25. #include<osg/LightSource>
  26. #include <osg/Geode>
  27. #include <osg/Geometry>
  28. #include <osg/Material>
  29. #include <osg/Vec3>
  30. #include <osg/MatrixTransform>
  31. #include <osg/Texture2D>
  32. #include <osg/PolygonStipple>
  33. #include <osg/TriangleFunctor>
  34. #include <osg/io_utils>
  35. #include<osg/Point>
  36. #include <osgDB/ReadFile>
  37. #include <osgDB/WriteFile>
  38. #include <osgGA/TrackballManipulator>
  39. #include <osgViewer/Viewer>
  40. #include <osg/Math>
  41. #include<osgFX/Scribe>
  42. #include <iostream>
  43. #include <string.h>
  44. #pragma comment(lib,"osgUtild.lib")
  45. #pragma comment(lib,"OpenThreads.lib")
  46. #pragma comment(lib,"osgDB.lib")
  47. #pragma comment(lib,"osgManipulator.lib")
  48. #pragma comment(lib,"osg.lib")
  49. #pragma comment(lib,"osgViewer.lib")
  50. #pragma comment(lib,"osgGA.lib")
  51. #pragma comment(lib,"osgFX.lib")
  52. class CPickHandler:public osgGA::GUIEventHandler
  53. {
  54. public:
  55.     CPickHandler(osgViewer::Viewer*viewer):mViewer(viewer),lastSelect(0){}
  56.     virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
  57.     {
  58.         switch(ea.getEventType())
  59.         {
  60.         case(osgGA::GUIEventAdapter::MOVE):
  61.         {
  62.             if(ea.getButton()==1)
  63.             {
  64.                 Pick(ea.getX(),ea.getY());
  65.             }
  66.             return true;

  67.         }
  68.         return false;
  69.         }
  70.     }
  71. protected:void Pick(float x,float y)
  72.     {
  73.         osgUtil::LineSegmentIntersector::Intersections intersections;
  74.         if(mViewer->computeIntersections(x,y,intersections))
  75.         {
  76.             for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();
  77.                 hitr!=intersections.end();++hitr)
  78.             {
  79.                 if(!hitr->nodePath.empty()&&!(hitr->nodePath.back()->getName().empty()))
  80.                 {
  81.                     const osg::NodePath& np=hitr->nodePath;
  82.                     for(int i=np.size()-1;i>=0;--i)
  83.                     {
  84.                         osg::Geometry *sc=dynamic_cast<osg::Geometry *>(np[i]);
  85.                         if(sc!=NULL)
  86.                         {
  87.                             if(sc->getName()=="linesGeom"||sc->getName()=="pointsGeom"||sc->getName()=="pointsGeom3"||
  88.                                     sc->getName()=="pointsGeom4"||sc->getName()=="pointsGeom5")
  89.                             {
  90.                             osg::Vec4Array* colors6 = new osg::Vec4Array;  //变颜色
  91.                             colors6->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
  92.                             sc->setColorArray(colors6);
  93.                             sc->setColorBinding(osg::Geometry::BIND_OVERALL);
  94.                             osg::Vec3Array* normals6 = new osg::Vec3Array;
  95.                             normals6->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
  96.                             sc->setNormalArray(normals6);
  97.                             }
  98.                         }

  99.                     }
  100.                 }

  101.             }
  102.         }
  103.     }
  104.     osgViewer::Viewer*mViewer;
  105.     osg::Node*lastSelect;
  106. };

  107. int main()
  108. {
  109.     osg::ref_ptr<osg::Group> root = new osg::Group;
  110.     osgViewer::Viewer myViewer;
  111.     osg::Geode* geode = new osg::Geode;
  112.      osg::Geode* geode2 = new osg::Geode;//两点
  113.      osg::ref_ptr<osg::Node> node=new osg::Node;
  114.     osg::Geometry* linesGeom = new osg::Geometry();//骨架直线
  115.     osg::Vec3Array* vertices = new osg::Vec3Array(10);
  116.     (*vertices)[0].set(20,20,40);
  117.     (*vertices)[1].set(20,20,0);
  118.     (*vertices)[2].set(20,20,20);
  119.     (*vertices)[3].set(5,20,20);
  120.     (*vertices)[4].set(20,20,20);
  121.     (*vertices)[5].set(20,5,20);
  122.     (*vertices)[6].set(27.5,27.5,0);
  123.     (*vertices)[7].set(12.5,27.5,0);
  124.     (*vertices)[8].set(27.5,27.5,0);
  125.     (*vertices)[9].set(27.5,12.5,0);
  126.     linesGeom->setVertexArray(vertices);

  127.     osg::Vec4Array* colors = new osg::Vec4Array;
  128.     colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
  129.     linesGeom->setColorArray(colors);
  130.     linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
  131.     linesGeom->setName("linesGeom");
  132.     osg::Vec3Array* normals = new osg::Vec3Array;
  133.     normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));

  134.     linesGeom->setNormalArray(normals);
  135.     linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
  136.     linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,vertices->size()));

  137.     //曲线1
  138.     osg::Geometry* pointsGeom = new osg::Geometry();
  139.     osg::Vec3Array* vertices2 = new osg::Vec3Array;
  140.     #define PI 3.14159f
  141.     #define RADIUS 15.0f
  142.     GLfloat x = 0, y = 0, angle = 0.0;
  143.     for ( angle = -PI; angle <= -0.5f * PI; angle += 0.001f )
  144.     {
  145.         x = RADIUS * sin( angle )+27.5;
  146.         y = RADIUS * cos( angle )+27.5;
  147.         vertices2->push_back(osg::Vec3(x,y,0));
  148.     }
  149.     pointsGeom->setVertexArray(vertices2);

  150.     osg::Vec4Array* colors2 = new osg::Vec4Array;
  151.     colors2->push_back(osg::Vec4(0.0f,1.0f,1.0f,1.0f));
  152.     pointsGeom->setColorArray(colors2);
  153.     pointsGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
  154.     osg::Vec3Array* normals2 = new osg::Vec3Array;
  155.     normals2->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
  156.     pointsGeom->setNormalArray(normals2);

  157.     pointsGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
  158.     pointsGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices2->size()));
  159.     pointsGeom->setName("pointsGeom");
  160.     //曲线2
  161.     osg::Geometry* pointsGeom3 = new osg::Geometry();
  162.     osg::Vec3Array* vertices3 = new osg::Vec3Array;
  163.     GLfloat y3 = 0, z3 = 0, angle3 = 0.0;
  164.     for ( angle3 = -0.5f * PI; angle3 <= 0; angle3 += 0.001f )
  165.     {
  166.         y3 = RADIUS * sin( angle3 )+20;
  167.         z3 = RADIUS * cos( angle3 )+20;
  168.         vertices3->push_back(osg::Vec3(20,y3,z3));
  169.     }
  170.     pointsGeom3->setVertexArray(vertices3);

  171.     osg::Vec4Array* colors3 = new osg::Vec4Array;
  172.     colors3->push_back(osg::Vec4(0.0f,1.0f,0.0f,1.0f));
  173.     pointsGeom3->setColorArray(colors3);
  174.     pointsGeom3->setColorBinding(osg::Geometry::BIND_OVERALL);
  175.     osg::Vec3Array* normals3 = new osg::Vec3Array;
  176.     normals3->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
  177.     pointsGeom3->setNormalArray(normals3);

  178.     pointsGeom3->setNormalBinding(osg::Geometry::BIND_OVERALL);
  179.     pointsGeom3->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices3->size()));
  180.     pointsGeom3->setName("pointsGeom3");
  181.     //曲线3
  182.     osg::Geometry* pointsGeom4 = new osg::Geometry();
  183.     osg::Vec3Array* vertices4 = new osg::Vec3Array;
  184.     GLfloat x4 = 0, z4 = 0, angle4 = 0.0;
  185.     for ( angle4 = -0.5f * PI; angle4 <= 0; angle4 += 0.001f )
  186.     {
  187.         x4 = RADIUS * sin( angle4 )+20;
  188.         z4 = RADIUS * cos( angle4 )+20;
  189.         vertices4->push_back(osg::Vec3(x4,20,z4));
  190.     }
  191.     pointsGeom4->setVertexArray(vertices4);

  192.     osg::Vec4Array* colors4 = new osg::Vec4Array;
  193.     colors4->push_back(osg::Vec4(1.0f,0.0f,1.0f,1.0f));
  194.     pointsGeom4->setColorArray(colors4);
  195.     pointsGeom4->setColorBinding(osg::Geometry::BIND_OVERALL);
  196.     osg::Vec3Array* normals4 = new osg::Vec3Array;
  197.     normals4->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
  198.     pointsGeom4->setNormalArray(normals4);

  199.     pointsGeom4->setNormalBinding(osg::Geometry::BIND_OVERALL);
  200.     pointsGeom4->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices4->size()));
  201.     pointsGeom4->setName("pointsGeom4");
  202.     //两点
  203.     osg::Geometry* pointsGeom5 = new osg::Geometry();
  204.     osg::Vec3Array* vertices5 = new osg::Vec3Array;
  205.     vertices5->push_back(osg::Vec3(20,20,40));
  206.     vertices5->push_back(osg::Vec3(20,20,0));
  207.     pointsGeom5->setVertexArray(vertices5);
  208.     osg::Vec4Array* colors5 = new osg::Vec4Array;
  209.     colors5->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
  210.     pointsGeom5->setColorArray(colors5);
  211.     pointsGeom5->setColorBinding(osg::Geometry::BIND_OVERALL);
  212.     osg::Vec3Array* normals5 = new osg::Vec3Array;
  213.     normals5->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
  214.     pointsGeom5->setNormalArray(normals5);

  215.     pointsGeom5->setNormalBinding(osg::Geometry::BIND_OVERALL);
  216.     pointsGeom5->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices5->size()));
  217.     pointsGeom5->setName("pointsGeom5");

  218.     osg::ref_ptr <osg::Point> ptSize = new osg::Point;
  219.     ptSize->setSize( 9.0) ;
  220.     geode2->getOrCreateStateSet()->setAttributeAndModes( ptSize.get (),osg::StateAttribute::ON);

  221.     osg::ref_ptr<osg::MatrixTransform> selection=new osg::MatrixTransform;

  222.     //tad
  223.     osgManipulator::TranslateAxisDragger * tad=new osgManipulator::TranslateAxisDragger();
  224.     float scale=20.0;
  225.     tad->setupDefaultGeometry();
  226.     tad->setMatrix(osg::Matrix::scale(scale,scale,scale)*osg::Matrix::translate(osg::Vec3(-20,-20,-20)));
  227.     tad->addTransformUpdating(selection);
  228.     tad->setHandleEvents(true);
  229.     tad->setActivationModKeyMask(osgGA::GUIEventAdapter::MODKEY_CTRL);


  230.     geode->addDrawable(pointsGeom);
  231.     geode->addDrawable(pointsGeom3);
  232.     geode->addDrawable(pointsGeom4);
  233.     geode2->addDrawable(pointsGeom5);
  234.     geode->addDrawable(linesGeom);
  235.     root->addChild(geode);
  236.     root->addChild(geode2);
  237.     root->addChild(tad);
  238.     myViewer.setSceneData(root.get());
  239.    myViewer.addEventHandler(new CPickHandler(&myViewer));
  240.     myViewer.realize();
  241.     myViewer.run();
  242. }
复制代码

该用户从未签到

 楼主| 发表于 2015-6-3 19:17:40 | 显示全部楼层
没用代码模式粘贴。。。在2楼。

该用户从未签到

 楼主| 发表于 2015-6-3 19:56:38 | 显示全部楼层
括号问题,能动了,但是还是实现不了功能,最新代码。
  1. #include<osgViewer\Viewer>
  2. #include<osgDB\ReadFile>
  3. #include <osg/Group>
  4. #include <osg/Geode>
  5. #include <osg/ShapeDrawable>
  6. #include <osg/Texture2D>
  7. #include <osg/PositionAttitudeTransform>
  8. #include <osg/TexEnv>
  9. #include <osg/Image>
  10. #include <osg/StateSet>
  11. #include <osgGA/GUIEventAdapter>
  12. #include <osgGA/GUIEventAdapter>
  13. #include <osgUtil/LineSegmentIntersector>
  14. #include <osgViewer/ViewerEventHandlers>
  15. #include <osg/Material>
  16. #include <osg/LineWidth>
  17. #include <iostream>
  18. #include<osgManipulator/TrackballDragger>
  19. #include<osgManipulator/TranslateAxisDragger>
  20. #include<osgManipulator/ScaleAxisDragger>
  21. #include<osg/MatrixTransform>
  22. #include <osg/TexGen>
  23. #include <osg/TexEnv>
  24. #include<osg/Light>
  25. #include<osg/LightSource>
  26. #include <osg/Geode>
  27. #include <osg/Geometry>
  28. #include <osg/Material>
  29. #include <osg/Vec3>
  30. #include <osg/MatrixTransform>
  31. #include <osg/Texture2D>
  32. #include <osg/PolygonStipple>
  33. #include <osg/TriangleFunctor>
  34. #include <osg/io_utils>
  35. #include<osg/Point>
  36. #include <osgDB/ReadFile>
  37. #include <osgDB/WriteFile>
  38. #include <osgGA/TrackballManipulator>
  39. #include <osgViewer/Viewer>
  40. #include <osg/Math>
  41. #include<osgFX/Scribe>
  42. #include <iostream>
  43. #include <string.h>
  44. #pragma comment(lib,"osgUtild.lib")
  45. #pragma comment(lib,"OpenThreads.lib")
  46. #pragma comment(lib,"osgDB.lib")
  47. #pragma comment(lib,"osgManipulator.lib")
  48. #pragma comment(lib,"osg.lib")
  49. #pragma comment(lib,"osgViewer.lib")
  50. #pragma comment(lib,"osgGA.lib")
  51. #pragma comment(lib,"osgFX.lib")
  52. class CPickHandler:public osgGA::GUIEventHandler
  53. {
  54. public:
  55.     CPickHandler(osgViewer::Viewer*viewer):mViewer(viewer),lastSelect(0){}
  56.     virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
  57.     {
  58.         switch(ea.getEventType())
  59.         {
  60.         case(osgGA::GUIEventAdapter::MOVE):
  61.             if(ea.getButton()==1)
  62.             {
  63.                 Pick(ea.getX(),ea.getY());
  64.             }
  65.             return true;
  66.         }
  67. //            if(lastSelect)
  68. //            {
  69. //                osg::StateSet* state=lastSelect->getOrCreateStateSet();//?
  70. //                state->setMode(GL_BLEND,osg::StateAttribute::ON);
  71. //                osg::Material*mtrl=dynamic_cast<osg::Material*>(state->getAttribute(osg::StateAttribute::MATERIAL));
  72. //                mtrl->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,1.0,1.0,1.0));
  73. //                mtrl->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,1.0,1.0,1.0));
  74. //                mtrl->setTransparency(osg::Material::FRONT_AND_BACK,0.0);
  75. //                state->setAttributeAndModes(mtrl,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
  76. //                state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
  77. //                lastSelect=0;
  78. //            }
  79. //            Pick(ea.getX(),ea.getY());
  80. //        }
  81. //            return true;
  82.         return false;

  83.     }
  84. protected:void Pick(float x,float y)
  85.     {
  86.         osgUtil::LineSegmentIntersector::Intersections intersections;
  87.         if(mViewer->computeIntersections(x,y,intersections))
  88.         {
  89.             for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();
  90.                 hitr!=intersections.end();++hitr)
  91.             {
  92.                 if(!hitr->nodePath.empty()&&!(hitr->nodePath.back()->getName().empty()))
  93.                 {
  94.                     const osg::NodePath& np=hitr->nodePath;
  95.                     for(int i=np.size()-1;i>=0;--i)
  96.                     {
  97.                         osg::Geometry *sc=dynamic_cast<osg::Geometry *>(np[i]);
  98.                         if(sc!=NULL)
  99.                         {
  100.                             if(sc->getName()=="linesGeom"||sc->getName()=="pointsGeom"||sc->getName()=="pointsGeom3"||
  101.                                     sc->getName()=="pointsGeom4"||sc->getName()=="pointsGeom5")
  102.                             {

  103.                             osg::Vec4Array* colors6 = new osg::Vec4Array;  //变颜色
  104.                             colors6->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
  105.                             sc->setColorArray(colors6);
  106.                             sc->setColorBinding(osg::Geometry::BIND_OVERALL);
  107.                             osg::Vec3Array* normals6 = new osg::Vec3Array;
  108.                             normals6->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
  109.                             sc->setNormalArray(normals6);
  110.                             }
  111.                         }

  112.                     }
  113. //                    {
  114. //                        osg::Node*nd=dynamic_cast<osg::Node *>(np[i]);
  115. //                        if(nd)
  116. //                        {
  117. //                            if(nd->getName()=="root")
  118. //                            {
  119. //                                osg::StateSet*state=nd->getOrCreateStateSet();
  120. //                                state->setMode(GL_BLEND,osg::StateAttribute::ON);
  121. //                                osg::Material*mtrl=dynamic_cast<osg::Material*>(state->getAttribute(osg::StateAttribute::MATERIAL));
  122. //                                if(!mtrl)
  123. //                                {
  124. //                                    mtrl=new osg::Material;
  125. //                                }
  126. //                                mtrl->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,0.0,0.0,0.8));
  127. //                                mtrl->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,0.0,0.0,0.8));
  128. //                                mtrl->setTransparency(osg::Material::FRONT_AND_BACK,0.2);
  129. //                                state->setAttributeAndModes(mtrl,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
  130. //                                state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
  131. //                                lastSelect=nd;

  132. //                            }
  133. //                            return;
  134. //                        }
  135. //                    }
  136.                 }

  137.             }
  138.         }
  139.     }
  140.     osgViewer::Viewer*mViewer;
  141.     osg::Node*lastSelect;
  142. };

  143. int main()
  144. {
  145.     osg::ref_ptr<osg::Group> root = new osg::Group;
  146.     osgViewer::Viewer myViewer;
  147.     osg::Geode* geode = new osg::Geode;
  148.      osg::Geode* geode2 = new osg::Geode;//两点
  149.      osg::ref_ptr<osg::Node> node=new osg::Node;
  150.     osg::Geometry* linesGeom = new osg::Geometry();//骨架直线
  151.     osg::Vec3Array* vertices = new osg::Vec3Array(10);
  152.     (*vertices)[0].set(20,20,40);
  153.     (*vertices)[1].set(20,20,0);
  154.     (*vertices)[2].set(20,20,20);
  155.     (*vertices)[3].set(5,20,20);
  156.     (*vertices)[4].set(20,20,20);
  157.     (*vertices)[5].set(20,5,20);
  158.     (*vertices)[6].set(27.5,27.5,0);
  159.     (*vertices)[7].set(12.5,27.5,0);
  160.     (*vertices)[8].set(27.5,27.5,0);
  161.     (*vertices)[9].set(27.5,12.5,0);
  162.     linesGeom->setVertexArray(vertices);

  163.     osg::Vec4Array* colors = new osg::Vec4Array;
  164.     colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
  165.     linesGeom->setColorArray(colors);
  166.     linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
  167.     linesGeom->setName("linesGeom");
  168.     osg::Vec3Array* normals = new osg::Vec3Array;
  169.     normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));

  170.     linesGeom->setNormalArray(normals);
  171.     linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
  172.     linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,vertices->size()));

  173.     //曲线1
  174.     osg::Geometry* pointsGeom = new osg::Geometry();
  175.     osg::Vec3Array* vertices2 = new osg::Vec3Array;
  176.     #define PI 3.14159f
  177.     #define RADIUS 15.0f
  178.     GLfloat x = 0, y = 0, angle = 0.0;
  179.     for ( angle = -PI; angle <= -0.5f * PI; angle += 0.001f )
  180.     {
  181.         x = RADIUS * sin( angle )+27.5;
  182.         y = RADIUS * cos( angle )+27.5;
  183.         vertices2->push_back(osg::Vec3(x,y,0));
  184.     }
  185.     pointsGeom->setVertexArray(vertices2);

  186.     osg::Vec4Array* colors2 = new osg::Vec4Array;
  187.     colors2->push_back(osg::Vec4(0.0f,1.0f,1.0f,1.0f));
  188.     pointsGeom->setColorArray(colors2);
  189.     pointsGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
  190.     osg::Vec3Array* normals2 = new osg::Vec3Array;
  191.     normals2->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
  192.     pointsGeom->setNormalArray(normals2);

  193.     pointsGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
  194.     pointsGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices2->size()));
  195.     pointsGeom->setName("pointsGeom");
  196.     //曲线2
  197.     osg::Geometry* pointsGeom3 = new osg::Geometry();
  198.     osg::Vec3Array* vertices3 = new osg::Vec3Array;
  199.     GLfloat y3 = 0, z3 = 0, angle3 = 0.0;
  200.     for ( angle3 = -0.5f * PI; angle3 <= 0; angle3 += 0.001f )
  201.     {
  202.         y3 = RADIUS * sin( angle3 )+20;
  203.         z3 = RADIUS * cos( angle3 )+20;
  204.         vertices3->push_back(osg::Vec3(20,y3,z3));
  205.     }
  206.     pointsGeom3->setVertexArray(vertices3);

  207.     osg::Vec4Array* colors3 = new osg::Vec4Array;
  208.     colors3->push_back(osg::Vec4(0.0f,1.0f,0.0f,1.0f));
  209.     pointsGeom3->setColorArray(colors3);
  210.     pointsGeom3->setColorBinding(osg::Geometry::BIND_OVERALL);
  211.     osg::Vec3Array* normals3 = new osg::Vec3Array;
  212.     normals3->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
  213.     pointsGeom3->setNormalArray(normals3);

  214.     pointsGeom3->setNormalBinding(osg::Geometry::BIND_OVERALL);
  215.     pointsGeom3->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices3->size()));
  216.     pointsGeom3->setName("pointsGeom3");
  217.     //曲线3
  218.     osg::Geometry* pointsGeom4 = new osg::Geometry();
  219.     osg::Vec3Array* vertices4 = new osg::Vec3Array;
  220.     GLfloat x4 = 0, z4 = 0, angle4 = 0.0;
  221.     for ( angle4 = -0.5f * PI; angle4 <= 0; angle4 += 0.001f )
  222.     {
  223.         x4 = RADIUS * sin( angle4 )+20;
  224.         z4 = RADIUS * cos( angle4 )+20;
  225.         vertices4->push_back(osg::Vec3(x4,20,z4));
  226.     }
  227.     pointsGeom4->setVertexArray(vertices4);

  228.     osg::Vec4Array* colors4 = new osg::Vec4Array;
  229.     colors4->push_back(osg::Vec4(1.0f,0.0f,1.0f,1.0f));
  230.     pointsGeom4->setColorArray(colors4);
  231.     pointsGeom4->setColorBinding(osg::Geometry::BIND_OVERALL);
  232.     osg::Vec3Array* normals4 = new osg::Vec3Array;
  233.     normals4->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
  234.     pointsGeom4->setNormalArray(normals4);

  235.     pointsGeom4->setNormalBinding(osg::Geometry::BIND_OVERALL);
  236.     pointsGeom4->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices4->size()));
  237.     pointsGeom4->setName("pointsGeom4");
  238.     //两点
  239.     osg::Geometry* pointsGeom5 = new osg::Geometry();
  240.     osg::Vec3Array* vertices5 = new osg::Vec3Array;
  241.     vertices5->push_back(osg::Vec3(20,20,40));
  242.     vertices5->push_back(osg::Vec3(20,20,0));
  243.     pointsGeom5->setVertexArray(vertices5);
  244.     osg::Vec4Array* colors5 = new osg::Vec4Array;
  245.     colors5->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
  246.     pointsGeom5->setColorArray(colors5);
  247.     pointsGeom5->setColorBinding(osg::Geometry::BIND_OVERALL);
  248.     osg::Vec3Array* normals5 = new osg::Vec3Array;
  249.     normals5->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
  250.     pointsGeom5->setNormalArray(normals5);

  251.     pointsGeom5->setNormalBinding(osg::Geometry::BIND_OVERALL);
  252.     pointsGeom5->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices5->size()));
  253.     pointsGeom5->setName("pointsGeom5");

  254.     osg::ref_ptr <osg::Point> ptSize = new osg::Point;
  255.     ptSize->setSize( 9.0) ;
  256.     geode2->getOrCreateStateSet()->setAttributeAndModes( ptSize.get (),osg::StateAttribute::ON);

  257.     osg::ref_ptr<osg::MatrixTransform> selection=new osg::MatrixTransform;

  258.     //tad
  259.     osgManipulator::TranslateAxisDragger * tad=new osgManipulator::TranslateAxisDragger();
  260.     float scale=20.0;
  261.     tad->setupDefaultGeometry();
  262.     tad->setMatrix(osg::Matrix::scale(scale,scale,scale)*osg::Matrix::translate(osg::Vec3(-20,-20,-20)));
  263.     tad->addTransformUpdating(selection);
  264.     tad->setHandleEvents(true);
  265.     tad->setActivationModKeyMask(osgGA::GUIEventAdapter::MODKEY_CTRL);


  266.     geode->addDrawable(pointsGeom);
  267.     geode->addDrawable(pointsGeom3);
  268.     geode->addDrawable(pointsGeom4);
  269.     geode2->addDrawable(pointsGeom5);
  270.     geode->addDrawable(linesGeom);
  271.     root->addChild(geode);
  272.     root->addChild(geode2);
  273.     root->addChild(tad);
  274.     myViewer.setSceneData(root.get());
  275.    myViewer.addEventHandler(new CPickHandler(&myViewer));
  276.     myViewer.realize();
  277.     myViewer.run();
  278. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表