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; } }
ArnoldMeshLight::ArnoldMeshLight( const std::string &name ) : GafferScene::FilteredSceneProcessor( name, IECore::PathMatcher::NoMatch ) { // ArnoldAttributesNode. This hides the objects from the majority // of ray types, since we don't want to add the poor sampling of the // object on top of the nice sampling of the light. The only visibility // option we don't turn off is camera visibility - instead we promote // so the user can decide whether or not the mesh should be visible in // the render. ArnoldAttributesPtr attributes = new ArnoldAttributes( "__attributes" ); attributes->inPlug()->setInput( inPlug() ); attributes->filterPlug()->setInput( filterPlug() ); for( CompoundDataPlug::MemberPlugIterator it( attributes->attributesPlug() ); !it.done(); ++it ) { if( boost::ends_with( (*it)->getName().string(), "Visibility" ) && (*it)->getName() != "cameraVisibility" ) { (*it)->enabledPlug()->setValue( true ); (*it)->valuePlug<BoolPlug>()->setValue( false ); } } addChild( attributes ); Plug *internalCameraVisibilityPlug = attributes->attributesPlug()->getChild<Plug>( "cameraVisibility" ); PlugPtr cameraVisibilityPlug = internalCameraVisibilityPlug->createCounterpart( "cameraVisibility", Plug::In ); addChild( cameraVisibilityPlug ); internalCameraVisibilityPlug->setInput( cameraVisibilityPlug ); // Shader node. This loads the Arnold mesh_light shader. ArnoldShaderPtr shader = new ArnoldShader( "__shader" ); shader->loadShader( "mesh_light" ); addChild( shader ); PlugPtr parametersPlug = shader->parametersPlug()->createCounterpart( "parameters", Plug::In ); addChild( parametersPlug ); for( PlugIterator srcIt( parametersPlug.get() ), dstIt( shader->parametersPlug() ); !srcIt.done(); ++srcIt, ++dstIt ) { (*dstIt)->setInput( *srcIt ); // We don't need the parameters to be dynamic, because we create the // plugs in our constructor when calling `loadShader()`. (*srcIt)->setFlags( Plug::Dynamic, false ); } // ShaderAssignment node. This assigns the mesh_light shader // to the objects chosen by the filter. ShaderAssignmentPtr shaderAssignment = new ShaderAssignment( "__shaderAssignment" ); shaderAssignment->inPlug()->setInput( attributes->outPlug() ); shaderAssignment->filterPlug()->setInput( filterPlug() ); shaderAssignment->shaderPlug()->setInput( shader->outPlug() ); addChild( shaderAssignment ); // Set node. This adds the objects into the __lights set, // so they will be output correctly to the renderer. SetPtr set = new Set( "__set" ); set->inPlug()->setInput( shaderAssignment->outPlug() ); set->filterPlug()->setInput( filterPlug() ); set->namePlug()->setValue( "__lights" ); set->modePlug()->setValue( Set::Add ); addChild( set ); // Default lights Set node. BoolPlugPtr defaultLightPlug = new BoolPlug( "defaultLight", Plug::In, true ); addChild( defaultLightPlug ); SetPtr defaultLightsSet = new Set( "__defaultLightsSet" ); defaultLightsSet->inPlug()->setInput( set->outPlug() ); defaultLightsSet->filterPlug()->setInput( filterPlug() ); defaultLightsSet->enabledPlug()->setInput( defaultLightPlug.get() ); defaultLightsSet->namePlug()->setValue( "defaultLights" ); defaultLightsSet->modePlug()->setValue( Set::Add ); addChild( defaultLightsSet ); // Switch for enabling/disabling SwitchPtr enabledSwitch = new Switch( "__switch" ); enabledSwitch->setup( inPlug() ); enabledSwitch->inPlugs()->getChild<ScenePlug>( 0 )->setInput( inPlug() ); enabledSwitch->inPlugs()->getChild<ScenePlug>( 1 )->setInput( defaultLightsSet->outPlug() ); enabledSwitch->indexPlug()->setValue( 1 ); enabledSwitch->enabledPlug()->setInput( enabledPlug() ); addChild( enabledSwitch ); outPlug()->setInput( enabledSwitch->outPlug() ); // We don't need to serialise the connection because we make // it upon construction. /// \todo Can we just do this in the SceneProcessor base class? outPlug()->setFlags( Plug::Serialisable, false ); }
// 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 buildGraphRecurse(UInt32 depth, UInt32 maxDepth, GeometryPtr pGeo) { if(depth == maxDepth) { NodePtr pNodeGeo = Node::create(); beginEditCP(pNodeGeo, Node::CoreFieldId); pNodeGeo->setCore(pGeo); endEditCP (pNodeGeo, Node::CoreFieldId); return pNodeGeo; } else { NodePtr pNodeGroup = Node ::create(); GroupPtr pGroup = Group ::create(); NodePtr pNodeSwitch = Node ::create(); SwitchPtr pSwitch = Switch::create(); beginEditCP(pNodeGroup, Node::CoreFieldId | Node::ChildrenFieldId); pNodeGroup->setCore (pGroup ); pNodeGroup->addChild(pNodeSwitch); endEditCP (pNodeGroup, Node::CoreFieldId | Node::ChildrenFieldId); beginEditCP(pSwitch, Switch::ChoiceFieldId); pSwitch->editChoice() = Switch::ALL; endEditCP (pSwitch, Switch::ChoiceFieldId); beginEditCP(pNodeSwitch, Node::CoreFieldId | Node::ChildrenFieldId); pNodeSwitch->setCore(pSwitch); for(UInt32 i = 0; i < 8; ++i) { NodePtr pNodeTrans = Node ::create(); TransformPtr pTrans = Transform::create(); Vec3f vecTrans; if(i & 0x01) { vecTrans[0] = 0.25 * X_SIZE * (1.0 / (depth + 1.0)) + 0.2 * (osgrand() - 0.5); } else { vecTrans[0] = -0.25 * X_SIZE * (1.0 / (depth + 1.0)) + 0.2 * (osgrand() - 0.5); } if(i & 0x02) { vecTrans[1] = 0.25 * Y_SIZE * (1.0 / (depth + 1.0)) + 0.2 * (osgrand() - 0.5); } else { vecTrans[1] = -0.25 * Y_SIZE * (1.0 / (depth + 1.0)) + 0.2 * (osgrand() - 0.5); } if(i & 0x04) { vecTrans[2] = 0.25 * Z_SIZE * (1.0 / (depth + 1.0)) + 0.2 * (osgrand() - 0.5); } else { vecTrans[2] = -0.25 * Z_SIZE * (1.0 / (depth + 1.0)) + 0.2 * (osgrand() - 0.5); } beginEditCP(pTrans, Transform::MatrixFieldId); pTrans->editMatrix().setTranslate(vecTrans); endEditCP (pTrans, Transform::MatrixFieldId); beginEditCP(pNodeTrans, Node::CoreFieldId | Node::ChildrenFieldId); pNodeTrans->setCore (pTrans ); pNodeTrans->addChild(buildGraphRecurse(depth + 1, maxDepth, pGeo)); endEditCP (pNodeTrans, Node::CoreFieldId | Node::ChildrenFieldId); pNodeSwitch->addChild(pNodeTrans); } endEditCP (pNodeSwitch, Node::CoreFieldId | Node::ChildrenFieldId); return pNodeGroup; } }
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; }