Action::ResultE check(NodePtr& node) { AttachmentPtr a = node->findAttachment(Name::getClassType()); if(a != NullFC) { NamePtr n = NamePtr::dcast(a); if(n->getField().getValue() == *_name) { _found = node; return Action::Quit; } } return Action::Continue; }
// Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { // OSG init osgInit(argc,argv); // GLUT init int winid = setupGLUT(&argc, argv); // the connection between GLUT and OpenSG GLUTWindowPtr gwin = GLUTWindow::create(); gwin->setId(winid); gwin->init(); // load the scene NodePtr scene; if(argc < 2) { FWARNING(("No file given!\n")); scene = makeTorus(.5, 2, 16, 16); } else { /* All scene file loading is handled via the SceneFileHandler. */ scene = SceneFileHandler::the().read(argv[1]); } /* An Attachment is a special field container that can be attached to many of the internal classes like Nodes and NodeCores. There can be multiple Attachments attached to an object. Attachments can be attached to all FieldContainers that are derived from AttachmentContainer. This includes most higher-level classes in the system, like Nodes, NodeCores, Windows, Viewports etc. The only predefined kind of Attachment is the Name, which can keep the name of an object. Some of loaders (e.g. the WRL loader) create these kinds of Attachments for named nodes. */ /* An Attachment is a FieldContainer and as such needs to be created using ::create(). */ NamePtr name = Name::create(); /* The NameAttachment only has a single field, there's no need to use the mask here. */ beginEditCP(name); { name->getField().setValue("Scene"); } endEditCP (name); /* Attach the name to the scene node. */ beginEditCP(scene, Node::AttachmentsFieldMask); { scene->addAttachment(name); } endEditCP (scene, Node::AttachmentsFieldMask); /* Check if the scene has a Name attachment Attachments are categorized by the GroupID of their class. Every AttachmentContainer generally keeps only one attachment of a specific kind. */ AttachmentPtr a; a = scene->findAttachment(Name::getClassType()); if(a!=NullFC) { NamePtr n = NamePtr::dcast(a); SLOG << "Node name: " << n->getField().getValue() << endl; } else { SLOG << "Node has no name!" << endl; } // use the finder helper to find a named object finder f; NodePtr found; found = f.find(scene, "Scene"); SLOG << "Found object " << found << " named Scene." << endl; found = f.find(scene, "TF_DETAIL"); if(found == NullFC) { SLOG << "Found no object named TF_DETAIL (did you load the tie?)." << endl; } else { SLOG << "Found object " << found << " named TF_DETAIL." << endl; } // Use the simple attachment defined above MyAttachmentPtr mya = MyAttachment::create(); beginEditCP(mya); { mya->getField().setValue(42); } endEditCP (mya); // attach it to the scene beginEditCP(scene, Node::AttachmentsFieldMask); { scene->addAttachment(mya); } endEditCP (scene, Node::AttachmentsFieldMask); // and check if it's still there a = scene->findAttachment(MyAttachment::getClassType()); if(a!=NullFC) { MyAttachmentPtr m = MyAttachmentPtr::dcast(a); SLOG << "Node my value: " << m->getField().getValue() << endl; } else { SLOG << "Node has no myAttachment!" << endl; } // create the SimpleSceneManager helper mgr = new SimpleSceneManager; // tell the manager what to manage mgr->setWindow(gwin ); mgr->setRoot (scene); // show the whole scene mgr->showAll(); // GLUT main loop glutMainLoop(); return 0; }