|
楼主 |
发表于 2015-6-3 19:56:38
|
显示全部楼层
括号问题,能动了,但是还是实现不了功能,最新代码。
- #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;
- }
- // if(lastSelect)
- // {
- // osg::StateSet* state=lastSelect->getOrCreateStateSet();//?
- // state->setMode(GL_BLEND,osg::StateAttribute::ON);
- // osg::Material*mtrl=dynamic_cast<osg::Material*>(state->getAttribute(osg::StateAttribute::MATERIAL));
- // mtrl->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,1.0,1.0,1.0));
- // mtrl->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,1.0,1.0,1.0));
- // mtrl->setTransparency(osg::Material::FRONT_AND_BACK,0.0);
- // state->setAttributeAndModes(mtrl,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
- // state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
- // lastSelect=0;
- // }
- // Pick(ea.getX(),ea.getY());
- // }
- // return true;
- return false;
- }
- protected:void Pick(float x,float y)
- {
- osgUtil::LineSegmentIntersector::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);
- }
- }
- }
- // {
- // osg::Node*nd=dynamic_cast<osg::Node *>(np[i]);
- // if(nd)
- // {
- // if(nd->getName()=="root")
- // {
- // osg::StateSet*state=nd->getOrCreateStateSet();
- // state->setMode(GL_BLEND,osg::StateAttribute::ON);
- // osg::Material*mtrl=dynamic_cast<osg::Material*>(state->getAttribute(osg::StateAttribute::MATERIAL));
- // if(!mtrl)
- // {
- // mtrl=new osg::Material;
- // }
- // mtrl->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,0.0,0.0,0.8));
- // mtrl->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,0.0,0.0,0.8));
- // mtrl->setTransparency(osg::Material::FRONT_AND_BACK,0.2);
- // state->setAttributeAndModes(mtrl,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
- // state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
- // lastSelect=nd;
- // }
- // return;
- // }
- // }
- }
- }
- }
- }
- 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::DrawArrays(osg::PrimitiveSet::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();
- }
复制代码 |
|