OSGProgramDesign tutorial Chapter 5 interactive pick instances
Code # Include < Iostream >
# Include < Osgdb / Readfile >
# Include < Osgviewer / Viewer >
# Include < OSG / Group >
# Include < OSG / Node >
# Include < Osgfx / Scribe >
# Include < Osgga / Guieventhandler >
# Include < Osgutil / Linesegmentintersector >
Class Cpickhandler: Public Osgga: guieventhandler
{
Public :
Cpickhandler (osgviewer: Viewer * Viewer): mviewer (viewer ){};
Virtual Bool Handle ( Const Osgga: guieventadapter & EA, osgga: guiactionadapter && Aa)
{
Switch (Ea. geteventtype ())
{
// Push, button down
Case Osgga: guieventadapter: Push:
// 1, left button push
If (Ea. getbutton () = 1 )
{
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)
{
Osgfx: scribe * SC = Dynamic_cast < Osgfx: scribe *> (NP [I]);
If (SC ! = Null)
{
If (SC -> Getnodemask () ! = 0 )
SC -> Setnodemask ( 0 );
}
}
}
}< BR >}< br> osgviewer: viewer * mviewer;
};
// argc: the number of argv
/// argv: an array of arguments,
// ARGs [0] always is the path and name of the program itself.
int main ( int argc, char ** argv)
{
OSG: ref_ptr<Osgviewer: Viewer>Viewer= NewOsgviewer: Viewer ();
OSG: ref_ptr < OSG: Group > Root = New OSG: group ();
Root -> Addchild (osgdb: readnodefile ( " Cessna. OSG " ));
OSG: ref_ptr < OSG: Node > Cow = Osgdb: readnodefile ( " Cow. OSG " );
OSG: ref_ptr < Osgfx: scribe > SC = New Osgfx: scribe ();
SC -> Addchild (cow. Get ());
Root -> Addchild (cow. Get ());
Root -> Addchild (SC. Get ());
Viewer -> Setscenedata (root. Get ());
Viewer->Addeventhandler (NewCpickhandler (viewer.Get()));
Viewer->Realize ();
Viewer->Run ();
}