3D项目处理点选操作步骤

时间:2022-09-17 17:38:37
 1、用notepad++模型的obj格式文件,查找到模型各个部分的名称,命名规则:g mesh......,把名字改为规则命名。
 2、选择处理
#ifdef _DEBUG
#pragma comment(lib,"osgGAd.lib")
#pragma comment(lib,"osgViewerd.lib")
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"OpenThreadsd.lib")
#pragma comment(lib,"osgd.lib")
#pragma comment(lib,"osgWidgetd.lib")
#pragma comment(lib,"osgTextd.lib")
#pragma comment(lib,"osgManipulatord.lib")
#pragma comment(lib,"osgUtild.lib")
#pragma comment(lib,"osgParticled.lib")
#else
#pragma comment(lib,"osgGA.lib")
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"OpenThreads.lib")
#pragma comment(lib,"osg.lib")
#pragma comment(lib,"osgWidget.lib")
#pragma comment(lib,"osgText.lib")
#pragma comment(lib,"osgManipulator.lib")
#pragma comment(lib,"osgUtil.lib")
#pragma comment(lib,"osgParticle.lib")
#endif
#include <osgViewer/Viewer>
#include <osgGA/GUIEventHandler>
#include <osg/Group>
#include <osg/MatrixTransform>
#include <osg/Material>
#include <osgDB/ReadFile>
#include <osgUtil/LineSegmentIntersector>
#include <osg/ShapeDrawable>
#include <osgText/Text>
#include <vector>
osg::ref_ptr<osg::Node> cow;
class CPickHandler : public osgGA::GUIEventHandler{
osgViewer::Viewer * mViewer;
osg::Group * mGroup;
osg::Node * lastSelect;
public:
CPickHandler(osgViewer::Viewer * viewer, osg::Group *group) :mViewer(viewer),mGroup(group),lastSelect(){}
virtual bool handle(const osgGA::GUIEventAdapter & ea, osgGA::GUIActionAdapter & aa){
switch(ea.getEventType()){
case(osgGA::GUIEventAdapter::PUSH):{
if(lastSelect){
//osg::StateSet * state = lastSelect->getOrCreateStateSet();
//state->setMode(GL_BLEND, osg::StateAttribute::ON);
/*osg::ref_ptr<osg::Material> material = new osg::Material();
material->setTransparency(osg::Material::FRONT_AND_BACK, 1.0);
material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0,0.0,0.0,1.0));
alarmSphere->getOrCreateStateSet()->setAttributeAndModes(material,osg::StateAttribute::ON);*/
//osg::Material * mtrl = dynamic_cast<osg::Material *>(state->getAttribute(osg::StateAttribute::MATERIAL));
//mtrl->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 1.0));
//state->setAttributeAndModes(mtrl, osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
//state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
lastSelect = ;
}
Pick(ea.getX(),ea.getY()); //获得单击的鼠标坐标
}
return true;
}
return false;
}
protected:
void Pick(float x, float y){ 在pick函数中写触发函数
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() - ; i >= ; --i){
osg::Node* nd = dynamic_cast<osg::Node *> (np[i]);
if(nd){
//std::cout<< "************"<<std::endl;
std::cout << nd->getName() <<std::endl;
std::cout << np.size() <<std::endl;
if(nd->getName()=="Mesh12 Group4 Group1 Model"||nd->getName() == "Mesh18 Group6 Group1 Model"||nd->getName() == "Mesh6 Group2 Group1 Model"||nd->getName() == "Mesh16 Group6 Group1 Model"||nd->getName() == "Mesh10 Group4 Group1 Model"||nd->getName() == "Mesh4 Group2 Group1 Model"){
//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;
//}
//osg::ref_ptr<osg::Material> material = new osg::Material();
//material->setTransparency(osg::Material::FRONT_AND_BACK, 0.5);
//material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0,0.0,0.0,1.0));
//alarmSphere->getOrCreateStateSet()->setAttributeAndModes(material,osg::StateAttribute::ON);
////nd->setNodeMask(false);
//mtrl->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 0.0, 0.0, 1.0));
//state->setAttributeAndModes(mtrl, osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
//state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
osg::Vec3 eye, center, up; //改变视角代码段
center = cow->computeBound()._center;
eye = center;
eye[] += cow->computeBound()._radius * ;
up.set( .f, .f, .f );
osg::Matrix mt;
mt.makeLookAt( eye, center, up );
mViewer->getCameraManipulator()->setByInverseMatrix( mt );
lastSelect = nd;
}
return ;
}
}
}
}
}
return ;
}
};
void main(){
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Group> root = new osg::Group();
osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
cow = osgDB::readNodeFile("D:\\information of buaa\\LAST CHANCE\\lab\\NewModels\\toUse\\pipelineTeam_3_boiler2\\pipelineTeam1.ive");
//osg::ref_ptr<osg::Node> cow = osgDB::readNodeFile("cow.osg");
cow->setName("officeBuilding");
mt->addChild(cow);
mt->setName("officeBuilding");
root->addChild(mt);
//root->getChild(0);
//std::cout<< root->getChild(2)->getName() << std::endl;
/*osg::ref_ptr<osg::MatrixTransform> mtRobot = new osg::MatrixTransform;
osg::ref_ptr<osg::Node> robot = osgDB::readNodeFile("robot.osg");
mtRobot->setMatrix(osg::Matrix::translate(20,0,0));
robot->setName("robot");
mtRobot->addChild(robot);
root->addChild(mtRobot);*/
//std::cout << root->getChild(0)->getName() << std::endl;
//osg::ref_ptr<osg::Plane> Plane (new osg::Plane());
/*osg::ShapeDrawable *shapeBall=new osg::ShapeDrawable(new osg::Box(mt->computeBound()._center, mt->computeBound()._radius/3, mt->computeBound()._radius/10, mt->computeBound()._radius/3));
osg::ref_ptr<osg::Material> material = new osg::Material();
material->setTransparency(osg::Material::FRONT_AND_BACK, 1.0);
material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0,0.0,0.0,1.0));
alarmSphere->getOrCreateStateSet()->setAttributeAndModes(material,osg::StateAttribute::ON);
alarmSphere->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);
alarmSphere->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
alarmSphere->addDrawable(shapeBall);
root->addChild(alarmSphere);
*/
viewer.setSceneData(root.get());
viewer.addEventHandler(new CPickHandler(&viewer, root));
viewer.realize();
viewer.run();
return ;
}