After the result is submitted, a drawable is highlighted:
If (picker-> containsintersections ())
{
OSG: vec3 ptworldintersectpointfirst = picker-> getfirstintersection (). getworldintersectpoint ();
Cout <"World coords vertex (" <ptworldintersectpointfirst. X () <","
<Ptworldintersectpointfirst. Y () <
"," <Ptworldintersectpointfirst. Z () <")" <STD: Endl;
// Highlight this point
Double dpointradius = 15.0f;
OSG: shapedrawable * pshd =
New OSG: shapedrawable (
New OSG: sphere (ptworldintersectpointfirst, dpointradius ));
Pshd-> setcolor (OSG: vec4 (0, 1, 0, 1 ));
Geode-> adddrawable (pshd );
//// Highlight this drawable (change its material-scattered light)
// OSG: ref_ptr <OSG: stateset> pssdra = picker-> getfirstintersection (). drawable-> getorcreatestateset ();
//// Change the color
// OSG: Material * MAT = dynamic_cast <OSG: Material *>
// (Pssdra-> getattribute (OSG: stateattribute: Material ));
// If (MAT = NULL)
//{
// MAT = new OSG: material;
// Mat-> setdatavariance (OSG: object: Dynamic );
// Pssdra-> setattribute (MAT );
//}
// Mat-> setdiffuse (OSG: Material: front_and_back, OSG: vec4 (1, 0, 0, 0 ));
// Directly change its color
OSG: ref_ptr <OSG: geometry> pgmt = picker-> getfirstintersection (). drawable-> asgeometry ();
If (pgmt! = NULL)
{
// Color (obtain the original color array first, and then call dirty () for update)
OSG: vec4array * pv4acolorsold =
Dynamic_cast <OSG: vec4array *> (pgmt-> getcolorarray ());
If (pv4acolorsold! = NULL)
{
Pv4acolorsold-> clear ();
Pv4acolorsold-> push_back (OSG: vec4 (1.0f, 0.0f, 0.0f, 1.0f); // red
Pv4acolorsold-> dirty (); // update the color array
Pgmt-> getorcreatestateset ()-> setmode (gl_lighting, OSG: stateattribute: Off |
OSG: stateattribute: override); // turn off the light
}
Else
{
Pv4acolorsold = new OSG: vec4array;
Pv4acolorsold-> push_back (OSG: vec4 (1.0f, 0.0f, 0.0f, 1.0f); // red
Pgmt-> setcolorarray (pv4acolorsold );
Pgmt-> setcolorbinding (OSG: geometry: bind_overall/* bind_per_primitive_set :*/);
Pgmt-> getorcreatestateset ()-> setmode (gl_lighting, OSG: stateattribute: Off |
OSG: stateattribute: override); // turn off the light
}
}
}
Note: you must first obtain the color array (getcolorarray () and then update (dirty ()). If you call geometry-> setcolorarray (...) To reset the color array.
Another method is to change the material, but enable the light, and the highlighted color may change with the light.
―――――――――――――――――――――――――
The complete code is as follows:
Bool chud_viewpoint: handle (const osgga: guieventadapter & EA, osgga: guiactionadapter & aa)
{
Switch (EA. geteventtype ())
{
Case (osgga: guieventadapter: Push ):
{
M_vposwindowmouse.set (EA. getx (), EA. Gety (); // The Window coordinate at the mouse click
Osgviewer: viewer * viewer = dynamic_cast <osgviewer: viewer *> (& aa );
If (viewer)
{
Updatetext (viewer, EA); // update text information
// Master camera
OSG: ref_ptr <OSG: Camera> cameramaster = viewer-> getcamera ();
OSG: matrix mvpw = cameramaster-> getviewmatrix () * cameramaster-> getprojectionmatrix ();
If (cameramaster-> getviewport () mvpw. postmult (cameramaster-> getviewport ()-> computewindowmatrix ());
OSG: matrix _ inversemvpw;
_ Inversemvpw. Invert (mvpw );
OSG: vec3d nearpoint = OSG: vec3d (EA. getx (), EA. Gety (), 0.0) * _ inversemvpw; // intersection of znear planes in Perspective Projection
OSG: vec3d Farpoint = OSG: vec3d (EA. getx (), EA. Gety (), 1.0) * _ inversemvpw; // zfar plane point in Perspective Projection
//// Submit
// OSG: ref_ptr <osgutil: linesegmentintersector> picker = new osgutil: linesegmentintersector (
// Osgutil: intersector: window, EA. getx (), EA. Gety ());
// Osgutil: intersectionvisitor IV (Picker. Get ());
/// G_kgmouse-> getparent (0)-> getchild (0)-> Accept (IV); // model Intersection
// Cameramaster-> Accept (IV); // model intersection (traversing down from the camera)
// Submit
OSG: ref_ptr <osgutil: linesegmentintersector> picker = new osgutil: linesegmentintersector (
Nearpoint, Farpoint); // line segment (real world coordinates)
Osgutil: intersectionvisitor IV (Picker. Get ());
// G_kgmouse-> getparent (0)-> getchild (0)-> Accept (IV); // evaluate the model intersection /**/
G_kgmouse-> getparent (0)-> getchild (0)->/* asgroup ()-> getchild (0)-> */accept (IV ); // model intersection /**/
// Mt node of the root node cow
// Cameramaster-> Accept (IV); // model intersection (traversing down from the camera)
If (picker-> containsintersections ())
{
OSG: vec3 ptworldintersectpointfirst = picker-> getfirstintersection (). getworldintersectpoint ();
Cout <"World coords vertex (" <ptworldintersectpointfirst. X () <","
<Ptworldintersectpointfirst. Y () <
"," <Ptworldintersectpointfirst. Z () <")" <STD: Endl;
// Highlight this point
Double dpointradius = 15.0f;
OSG: shapedrawable * pshd =
New OSG: shapedrawable (
New OSG: sphere (ptworldintersectpointfirst, dpointradius ));
Pshd-> setcolor (OSG: vec4 (0, 1, 0, 1 ));
Geode-> adddrawable (pshd );
//// Highlight this drawable (change its material-scattered light)
// OSG: ref_ptr <OSG: stateset> pssdra = picker-> getfirstintersection (). drawable-> getorcreatestateset ();
//// Change the color
// OSG: Material * MAT = dynamic_cast <OSG: Material *>
// (Pssdra-> getattribute (OSG: stateattribute: Material ));
// If (MAT = NULL)
//{
/// MAT = new OSG: material;
// Mat-> setdatavariance (OSG: object: Dynamic );
// Pssdra-> setattribute (MAT );
//}
// Mat-> setdiffuse (OSG: Material: front_and_back, OSG: vec4 (1, 0, 0, 0 ));
// Directly change its color
OSG: ref_ptr <OSG: geometry> pgmt = picker-> getfirstintersection (). drawable-> asgeometry ();
If (pgmt! = NULL)
{
// Color (obtain the original color array first, and then call dirty () for update)
OSG: vec4array * pv4acolorsold =
Dynamic_cast <OSG: vec4array *> (pgmt-> getcolorarray ());
If (pv4acolorsold! = NULL)
{
Pv4acolorsold-> clear ();
Pv4acolorsold-> push_back (OSG: vec4 (1.0f, 0.0f, 0.0f, 1.0f); // red
Pv4acolorsold-> dirty (); // update the color array
Pgmt-> getorcreatestateset ()-> setmode (gl_lighting, OSG: stateattribute: Off |
OSG: stateattribute: override); // turn off the light
}
Else
{
Pv4acolorsold = new OSG: vec4array;
Pv4acolorsold-> push_back (OSG: vec4 (1.0f, 0.0f, 0.0f, 1.0f); // red
Pgmt-> setcolorarray (pv4acolorsold );
Pgmt-> setcolorbinding (OSG: geometry: bind_overall/* bind_per_primitive_set :*/);
Pgmt-> getorcreatestateset ()-> setmode (gl_lighting, OSG: stateattribute: Off |
OSG: stateattribute: override); // turn off the light
}
}
}
}
Return
True;
}
Default:
Return
False;
}
}