OSG Project Experience 1<MFC+OSG model point selection effect >

Source: Internet
Author: User
Tags gety

The main point is the guieventhandler of the overloaded OSG,

1 classCpickhandler: Publicosgga::guieventhandler{//Custom callback function name: Cpickhandler2Osgviewer::viewer * Mviewer;//define variables to be used in overloaded functions here3Osg::group *Mgroup;4Osg::node *Lastselect;5Osg::node *Replacednode;6     intReplacenum;7  Public:8Cpickhandler (Osgviewer::viewer * viewer, Osg::group *group): Mviewer (Viewer), Mgroup (Group), Lastselect (0), Replacednode (0){}//declaring a callback function in parentheses is the parameter that the callback function needs to pass in, and the content after the colon is initialized to a custom variable within the function, preceded by the variable name, and the initialization value in parentheses. 9     Virtual BOOLHandleConstOsgga::guieventadapter & EA, Osgga::guiactionadapter &AA) {Ten         Switch(Ea.geteventtype ()) { One              Case(osgga::guieventadapter::P ush): { A                 if(lastselect) { -Lastselect =0; -                 } thePick (Ea.getx (), ea.gety ());//The EA is where the mouse clicks, ea.getx (), Ea.gety (), is the screen coordinates for getting the mouse click.  -             } -                 return true; -         } +         return false; -     } +  A protected: at     voidPick (floatXfloatY) {}//in the pick () function, the screen coordinates of the mouse click are passed in, and you can write the actions you want to click to finish according to the screen coordinates.  -}

It is important to note that there are multiple coordinate systems in the OSG, we get only the screen coordinates through the mouse click, we often need to add something in the scene, we need to get a point of the world coordinates in order to better operation:

But in the case where we know which node it is, we can base it on:

Geode->getbound (). Center () * Osg::computelocaltoworld (Geode->getparentalnodepaths () [0])

To get the world coordinates of the node Geode.

The 3-D model of my project is an ive and osg file that was exported from the obj format, and the original obj model was made with SketchUp, and when it was created, it was given a separate name for each model, and in OSG the names could be obtained by->getname (). I can see these names by opening the obj file of the model with the text browser notepad++, the names of which are all beginning with g mash, for convenience, I change the name of each part of each model to the same, and Python solves the problem well.

So my pick function is this:

1 voidPick (floatXfloaty) {2 osgutil::linesegmentintersector::intersections intersections;3 4         if(mviewer->computeintersections (x, y, intersections)) {5Osgutil::linesegmentintersector::intersection intersection = *Intersections.begin ();6              for(Osgutil::linesegmentintersector::intersections::iterator Hitr = Intersections.begin (); hitr!= intersections.end (); ++Hitr) {7                 if(!hitr->nodepath.empty () &&! (Hitr->nodepath.back ()getName (). empty ())) {8                     Constosg::nodepath& NP = hitr->Nodepath;9                     Ten                      for(inti = np.size ()-1; I >=0; -I.) {//traverse the selected node Oneosg::node* nd = Dynamic_cast<osg::node *>(Np[i]); A                         if(nd) { -                             if(Nd->getname () = ="pipelineTeam1"){//determine whether a model is selected based on the name of the model -                                     /* the write the operation on the model here -                                     */ -                             } -                             return; +                         } -                     } +                 } A             } at}

However, this just writes out a callback function, and finally needs to add a sentence in the void Cosg::initcameraconfig (void) {} function in the OSG control class (Mine is Osg_mfc.cpp):

Mviewer->addeventhandler (new Cpickhandler (Mviewer, Mroot));  // To convert the pointer to a reference, because Mviewer is defined as osgviewer::viewer* mviewer; now you want to use &mviewer

So when we click on the screen, we automatically call the pick () function in the callback function to do it.

My project in the pick () callback, so that the location of the selected node to add a geometry, display text messages on the geometry, and the selected model with the selected effect, can be made to highlight and add a highlight (custom color) border, which will be described in the next article.

OSG Project Experience 1<MFC+OSG model point selection effect >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.