void VRMLSwitchBinder::init(VRMLToOSGAction *) { if(_pNode == NULL) return; VRMLSwitch *pNode = dynamic_cast<VRMLSwitch *>(_pNode); if(pNode == NULL) return; NodePtr pSwNode = Node ::create(); SwitchPtr pSwitch = Switch::create(); beginEditCP(pSwitch, Switch::ChoiceFieldMask); { pSwitch->setChoice(pNode->getSFWhichChoice()->getValue()); } endEditCP (pSwitch, Switch::ChoiceFieldMask); beginEditCP(pSwNode, Node::CoreFieldMask); { pSwNode->setCore(pSwitch); } endEditCP (pSwNode, Node::CoreFieldMask); _pFieldContainer = pSwNode; }
void setModel(Int32 which) { SwitchPtr s = SwitchPtr::dcast(g_scene->getCore()); beginEditCP(s, Switch::ChoiceFieldMask); s->setChoice(which); endEditCP (s, Switch::ChoiceFieldMask); if(g_mgr != NULL) g_mgr->showAll(); }
// react to keys void keyboard(unsigned char k, int x, int y) { Int64 JumpAmount(50000000); switch(k) { case 27: { //DeInit TheVideo->stop(); TheVideo->close(); getDefaultVideoManager()->exit(); OSG::osgExit(); exit(0); } break; case 'p': case ' ': TheVideo->pauseToggle(); break; case 's': TheVideo->stop(); break; case 'r': TheVideo->stop(); TheVideo->seek(0); TheVideo->pause(); break; case 'f': TheVideo->jump(JumpAmount); break; case 'v': TheVideo->jump(-JumpAmount); break; case 'u': UpdateImage = !UpdateImage; break; case 'w': beginEditCP(GeometryNodeSwitch, Switch::ChoiceFieldMask); GeometryNodeSwitch->setChoice((GeometryNodeSwitch->getChoice() + 1) % SwitchNode->getNChildren()); endEditCP(GeometryNodeSwitch, Switch::ChoiceFieldMask); break; } }
// Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { // OSG init osgInit(argc,argv); getDefaultVideoManager()->init(argc, argv); TheVideo = getDefaultVideoManager()->createVideoWrapper(); TutorialVideoListener TheVideoListener; TheVideo->addVideoListener(&TheVideoListener); //TheVideo->open(Path("./Data/ExampleVideo.avi")); TheVideo->open(Path("./Data/ExampleVideo.avi")); TheVideo->pause(); // GLUT init int winid = setupGLUT(&argc, argv); // the connection between GLUT and OpenSG GLUTWindowPtr gwin= GLUTWindow::create(); gwin->setId(winid); gwin->init(); // create the scene TheVideo->updateImage(); Real32 AspectRatio(static_cast<Real32>(TheVideo->getImage()->getWidth())/static_cast<Real32>(TheVideo->getImage()->getHeight())); MaterialPtr VideoMaterial = createVideoMaterial(); //Plane Geometry GeometryPtr PlaneGeometry = makePlaneGeo(10.0*AspectRatio,10.0,10,10); beginEditCP(PlaneGeometry, Geometry::MaterialFieldMask); PlaneGeometry->setMaterial(VideoMaterial); endEditCP(PlaneGeometry, Geometry::MaterialFieldMask); NodePtr PlaneGeometryNode = Node::create(); beginEditCP(PlaneGeometryNode, Node::CoreFieldMask); PlaneGeometryNode->setCore(PlaneGeometry); endEditCP(PlaneGeometryNode, Node::CoreFieldMask); //Box Geometry GeometryPtr BoxGeometry = makeBoxGeo(10.0*AspectRatio,10.0,10.0,2,2,2); beginEditCP(BoxGeometry, Geometry::MaterialFieldMask); BoxGeometry->setMaterial(VideoMaterial); endEditCP(BoxGeometry, Geometry::MaterialFieldMask); NodePtr BoxGeometryNode = Node::create(); beginEditCP(BoxGeometryNode, Node::CoreFieldMask); BoxGeometryNode->setCore(BoxGeometry); endEditCP(BoxGeometryNode, Node::CoreFieldMask); //Sphere Geometry GeometryPtr SphereGeometry = makeSphereGeo(2,5.0); beginEditCP(SphereGeometry, Geometry::MaterialFieldMask); SphereGeometry->setMaterial(VideoMaterial); endEditCP(SphereGeometry, Geometry::MaterialFieldMask); NodePtr SphereGeometryNode = Node::create(); beginEditCP(SphereGeometryNode, Node::CoreFieldMask); SphereGeometryNode->setCore(SphereGeometry); endEditCP(SphereGeometryNode, Node::CoreFieldMask); //Torus Geometry GeometryPtr TorusGeometry = makeTorusGeo(2.0,5.0,32,32); beginEditCP(TorusGeometry, Geometry::MaterialFieldMask); TorusGeometry->setMaterial(VideoMaterial); endEditCP(TorusGeometry, Geometry::MaterialFieldMask); NodePtr TorusGeometryNode = Node::create(); beginEditCP(TorusGeometryNode, Node::CoreFieldMask); TorusGeometryNode->setCore(TorusGeometry); endEditCP(TorusGeometryNode, Node::CoreFieldMask); //Switch Node GeometryNodeSwitch = Switch::create(); beginEditCP(GeometryNodeSwitch, Switch::ChoiceFieldMask); GeometryNodeSwitch->setChoice(0); endEditCP(GeometryNodeSwitch, Switch::ChoiceFieldMask); SwitchNode = Node::create(); beginEditCP(SwitchNode, Node::CoreFieldMask | Node::ChildrenFieldMask); SwitchNode->setCore(GeometryNodeSwitch); SwitchNode->addChild(PlaneGeometryNode); SwitchNode->addChild(BoxGeometryNode); SwitchNode->addChild(SphereGeometryNode); SwitchNode->addChild(TorusGeometryNode); endEditCP(SwitchNode, Node::CoreFieldMask | Node::ChildrenFieldMask); NodePtr scene = Node::create(); trans = Transform::create(); beginEditCP(scene, Node::CoreFieldMask | Node::ChildrenFieldMask); { scene->setCore(trans); scene->addChild(SwitchNode); } endEditCP (scene, Node::CoreFieldMask | Node::ChildrenFieldMask); // To check OpenGL extensions, the Window needs to have run through // frameInit at least once. This automatically happens when rendering, // but we don't don't to wait for that here. gwin->activate(); gwin->frameInit(); // Now we can check for OpenGL extensions hasNPOT = gwin->hasExtension("GL_ARB_texture_rectangle"); // Print what we've got SLOG << "Got " << (isPOT?"":"non-") << "power-of-two images and " << (hasNPOT?"can":"cannot") << " use NPOT textures, changing " << (changeOnlyPart?"part":"all") << " of the screen" << endLog; // create the SimpleSceneManager helper mgr = new SimpleSceneManager; // tell the manager what to manage mgr->setWindow(gwin ); mgr->setRoot (scene); mgr->setStatistics(true); // show the whole scene mgr->showAll(); //Start playing video TheVideo->play(); // GLUT main loop glutMainLoop(); //DeInit TheVideo->stop(); TheVideo->close(); getDefaultVideoManager()->exit(); osgExit(); return 0; }
NodePtr createScenegraph(){ //At first we load all needed models from file NodePtr w_high = SceneFileHandler::the().read("data/woman_high.wrl"); NodePtr w_medium = SceneFileHandler::the().read("data/woman_medium.wrl"); NodePtr w_low = SceneFileHandler::the().read("data/woman_low.wrl"); //we check the result if ((w_high == NullFC)&&(w_medium == NullFC)&&(w_low == NullFC)){ std::cout << "It was not possible to load all needed models from file" << std::endl; return NullFC; } //now the LOD core DistanceLODPtr lod = DistanceLOD::create(); beginEditCP(lod, DistanceLOD::CenterFieldMask | DistanceLOD::RangeFieldMask); lod->getSFCenter()->setValue(Pnt3f(0,0,0)); lod->getMFRange()->push_back(200); lod->getMFRange()->push_back(500); endEditCP(lod, DistanceLOD::CenterFieldMask | DistanceLOD::RangeFieldMask); //the node containing the LOD core. The three models will be //added as its children NodePtr lodNode = Node::create(); beginEditCP(lodNode); lodNode->setCore(lod); lodNode->addChild(w_high); lodNode->addChild(w_medium); lodNode->addChild(w_low); endEditCP(lodNode); //create the node with switch core ******************** SwitchPtr sw = Switch::create(); beginEditCP(sw, Switch::ChoiceFieldMask); //Notice: the first choice is 0 sw->setChoice(0); endEditCP(sw, Switch::ChoiceFieldMask); NodePtr switchNode = Node::create(); beginEditCP(switchNode); switchNode->setCore(sw); switchNode->addChild(lodNode); endEditCP(switchNode); //end witch creation ********************************** NodePtr root = Node::create(); beginEditCP(root); root->setCore(Group::create()); root->addChild(switchNode); endEditCP(root); // we know want to extract the mesh geometry out of the graph // it is sufficent to pass the model only as root for searching NodePtr womanGeometry = checkName(w_high); GeometryPtr geo; if (womanGeometry != NullFC){ geo = GeometryPtr::dcast(womanGeometry->getCore()); if (geo == NullFC) std::cout << "Casting failed!" << std::endl; }else{ std::cout << "No correct geometry node found!" << std::endl; //create a dummy object geo = makeBoxGeo(0.5,0.5,0.5,1,1,1); } // generating a material ********************************* SimpleMaterialPtr mat = SimpleMaterial::create(); beginEditCP(mat); mat->setAmbient(Color3f(0.2,0.2,0.2)); mat->setDiffuse(Color3f(0.6,0.3,0.1)); mat->setSpecular(Color3f(1,1,1)); mat->setShininess(0.8); endEditCP(mat); beginEditCP(geo, Geometry::MaterialFieldMask); geo->setMaterial(mat); endEditCP(geo, Geometry::MaterialFieldMask); // end material generation ******************************* //new node with "old" geometry core referenced NodePtr woman = Node::create(); beginEditCP(woman); woman->setCore(geo); endEditCP(woman); //translate it a bit to see both women NodePtr womanTrans = Node::create(); TransformPtr t = Transform::create(); beginEditCP(t); Matrix m; m.setIdentity(); m.setTranslate(Vec3f(0,0,200)); t->setMatrix(m); endEditCP(t); beginEditCP(womanTrans); womanTrans->setCore(t); womanTrans->addChild(woman); endEditCP(womanTrans); //add it to the root beginEditCP(root); root->addChild(womanTrans); endEditCP(root); return root; }