bool OSGSceneItem::init(UInt32 uiInitPhase, App *pApp) { fprintf(stderr, "OSGSceneItem::init %s (%x)\n", getName(this), uiInitPhase); if(0x0000 != (uiInitPhase & ::OSG::InitPhase::LoadReferences)) { if(_sfMatchedUrl.getValue().empty() == false) { fprintf(stderr, "Loading %s\n", _sfMatchedUrl.getValue().c_str()); NodeUnrecPtr pRoot = SceneFileHandler::the()->read( _sfMatchedUrl.getValue().c_str(), NULL, NULL, false); fprintf(stderr, "got %p\n", pRoot.get()); if(pRoot != NULL) { setRoot(pRoot); } else { fprintf(stderr, " failed\n"); } } if(_sfMatchedGlobalUrl.getValue().empty() == false) { fprintf(stderr, "Loading global %s\n", _sfMatchedGlobalUrl.getValue().c_str()); FieldContainerUnrecPtr pRes(NULL); _oPathHandler.pushState(); _oPathHandler.setBaseFile(_sfMatchedGlobalUrl.getValue().c_str()); ImageFileHandler::the()->setPathHandler(&_oPathHandler); fprintf(stderr, "loading osg file %s ...\n", _sfMatchedGlobalUrl.getValue().c_str()); pRes = OSG::OSGSceneFileType::the().readContainer( _sfMatchedGlobalUrl.getValue().c_str(), NULL); ImageFileHandler::the()->setPathHandler(NULL); _oPathHandler.popState(); fprintf(stderr, "got global %p\n", pRes.get()); if(pRes != NULL) { ContainerCollectionUnrecPtr pColl = dynamic_pointer_cast<ContainerCollection>(pRes); if(pColl != NULL) { MFUnrecFieldContainerPtr::const_iterator fIt = pColl->getMFContainers()->begin(); MFUnrecFieldContainerPtr::const_iterator fEnd = pColl->getMFContainers()->end(); while(fIt != fEnd) { this->pushToGlobals(*fIt); ++fIt; } } } else { fprintf(stderr, " failed\n"); } fprintf(stderr, "got %ld globals\n", _mfGlobals.size()); if(_sfActiveCamera.getValue().empty() == false) { FieldContainer *pCnt = findNamedComponent( _sfActiveCamera.getValue().c_str()); Camera *pCam = dynamic_cast<Camera *>(pCnt); fprintf(stderr, "found camera %p\n", pCam); setCamera(pCam); } } } return true; }
int main(int argc, char **argv) { // OSG init osgInit(argc,argv); { // Set up Window WindowEventProducerRecPtr TutorialWindow = createNativeWindow(); SimpleSceneManager sceneManager; TutorialWindow->setDisplayCallback(boost::bind(display, &sceneManager)); TutorialWindow->setReshapeCallback(boost::bind(reshape, _1, &sceneManager)); //Initialize Window TutorialWindow->initWindow(); // Tell the Manager what to manage sceneManager.setWindow(TutorialWindow); //Attach to events TutorialWindow->connectMousePressed(boost::bind(mousePressed, _1, &sceneManager)); TutorialWindow->connectMouseReleased(boost::bind(mouseReleased, _1, &sceneManager)); TutorialWindow->connectMouseDragged(boost::bind(mouseDragged, _1, &sceneManager)); TutorialWindow->connectMouseWheelMoved(boost::bind(mouseWheelMoved, _1, &sceneManager)); //Print key command info std::cout << "\n\nKEY COMMANDS:" << std::endl; std::cout << "space Play/Pause the animation" << std::endl; std::cout << "B Show/Hide the bind pose skeleton" << std::endl; std::cout << "P Show/Hide the current pose skeleton" << std::endl; //std::cout << "1 Play first example animation" << std::endl; //std::cout << "2 Play second example animation" << std::endl; std::cout << "CTRL-Q Exit\n\n" << std::endl; //SkeletonDrawer System Material LineChunkUnrecPtr ExampleLineChunk = LineChunk::create(); ExampleLineChunk->setWidth(2.0f); ExampleLineChunk->setSmooth(true); BlendChunkUnrecPtr ExampleBlendChunk = BlendChunk::create(); ExampleBlendChunk->setSrcFactor(GL_SRC_ALPHA); ExampleBlendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA); MaterialChunkUnrecPtr ExampleMaterialChunk = MaterialChunk::create(); ExampleMaterialChunk->setAmbient(Color4f(1.0f,1.0f,1.0f,1.0f)); ExampleMaterialChunk->setDiffuse(Color4f(0.0f,0.0f,0.0f,1.0f)); ExampleMaterialChunk->setSpecular(Color4f(0.0f,0.0f,0.0f,1.0f)); ChunkMaterialUnrecPtr ExampleMaterial = ChunkMaterial::create(); ExampleMaterial->addChunk(ExampleLineChunk); ExampleMaterial->addChunk(ExampleMaterialChunk); ExampleMaterial->addChunk(ExampleBlendChunk); GeometryRefPtr SphereGeometry = makeSphereGeo(2, 0.25f); GeometryRefPtr BoxGeometry = makeBoxGeo(0.5f,0.5f,0.5f,1,1,1); //Skeleton SkeletonBlendedGeometryUnrecPtr ExampleSkeleton = SkeletonBlendedGeometry::create(); //Joint TransformRecPtr ExampleRootJoint = Transform::create(); NodeRecPtr ExampleRootJointNode = makeNodeFor(ExampleRootJoint); //Add this joint to the skeleton ExampleSkeleton->pushToJoints(ExampleRootJointNode, Matrix()); NodeRecPtr TempRootJointNode = ExampleRootJointNode; NodeRefPtr GeoNode = makeNodeFor(BoxGeometry); TempRootJointNode->addChild(GeoNode); Matrix TempMat; //Create a set of randomly placed child joints for (Real32 i = 0.0f; i < 5.0f; ++i) { TransformRecPtr ExampleChildJoint = Transform::create(); NodeRecPtr ExampleChildJointNode = makeNodeFor(ExampleChildJoint); GeoNode = makeNodeFor(SphereGeometry); ExampleChildJointNode->addChild(GeoNode); //TempMat.setTranslate(RandomPoolManager::getRandomReal32(0.0, 10.0f), RandomPoolManager::getRandomReal32(0.0f, 10.0f), RandomPoolManager::getRandomReal32(0.0f, 10.0f)); switch((static_cast<UInt32>(i) % 3)) { case 0: TempMat.setTranslate(2.0f,0.0f,0.0f); break; case 1: TempMat.setTranslate(0.0f,2.0f,0.0f); break; case 2: TempMat.setTranslate(0.0f,0.0f,2.0f); break; } //Set bind and current transformations to TempMat (calculated above) ExampleChildJoint->setMatrix(TempMat); //Add ExampleChildJoint as a child to the previous joint TempRootJointNode->addChild(ExampleChildJointNode);//add a Child to the root joint //ExampleChildJoint will be the next parent joint TempRootJointNode = ExampleChildJointNode; //Add this joint to the skeleton Matrix InvBind(TempRootJointNode->getToWorld()); InvBind.invert(); ExampleSkeleton->pushToJoints(ExampleChildJointNode, InvBind); } //SkeletonDrawer SkeletonDrawableUnrecPtr ExampleSkeletonDrawable = SkeletonDrawable::create(); ExampleSkeletonDrawable->setSkeleton(ExampleSkeleton); ExampleSkeletonDrawable->setMaterial(ExampleMaterial); ExampleSkeletonDrawable->setDrawBindPose(false); //By default, we won't draw the skeleton's bind pose ExampleSkeletonDrawable->setBindPoseColor(Color4f(0.0, 1.0, 0.0, 1.0)); //When the skeleton's bind pose is rendered, it will be green ExampleSkeletonDrawable->setDrawPose(true); //By default, we do draw the skeleton's current pose ExampleSkeletonDrawable->setPoseColor(Color4f(0.0, 0.0, 1.0, 1.0)); //The skeleton's current pose is rendered in blue //Skeleton Node NodeUnrecPtr SkeletonNode = Node::create(); SkeletonNode->setCore(ExampleSkeletonDrawable); //Create scene NodeUnrecPtr scene = Node::create(); scene->setCore(Group::create()); scene->addChild(SkeletonNode); scene->addChild(ExampleRootJointNode); sceneManager.setRoot(scene); //Create the Documentation SimpleScreenDoc TheSimpleScreenDoc(&sceneManager, TutorialWindow); //Setup the Animation AnimationUnrecPtr TheAnimation = setupAnimation(ExampleRootJoint, dynamic_cast<Transform*>(ExampleRootJointNode->getChild(1)->getCore())); TheAnimation->attachUpdateProducer(TutorialWindow); TheAnimation->start(); TutorialWindow->connectKeyPressed(boost::bind(keyPressed, _1, TheAnimation.get(), SkeletonNode.get(), TutorialWindow.get())); // Show the whole Scene sceneManager.showAll(); //Open Window Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5); TutorialWindow->openWindow(WinPos, WinSize, "11BoneAnimation"); //Main Loop TutorialWindow->mainLoop(); } osgExit(); return 0; }