int main(int argc, char **argv) { // OSG init osgInit(argc,argv); // Set up Window TutorialWindow = createNativeWindow(); TutorialWindow->initWindow(); TutorialWindow->setDisplayCallback(display); TutorialWindow->setReshapeCallback(reshape); //Add Window Listener TutorialKeyListener TheKeyListener; TutorialWindow->addKeyListener(&TheKeyListener); TutorialMouseListener TheTutorialMouseListener; TutorialMouseMotionListener TheTutorialMouseMotionListener; TutorialWindow->addMouseListener(&TheTutorialMouseListener); TutorialWindow->addMouseMotionListener(&TheTutorialMouseMotionListener); // Create the SimpleSceneManager helper mgr = new SimpleSceneManager; // Tell the Manager what to manage mgr->setWindow(TutorialWindow); //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); //Joint JointUnrecPtr ExampleRootJoint = Joint::create(); JointUnrecPtr TempRootJoint; TempRootJoint = ExampleRootJoint; Matrix TempMat; //Create a set of randomly placed joints for (Real32 i = 1.0f; i < 9.0f; i++) { JointUnrecPtr ExampleChildJoint; TempMat.setTranslate(RandomPoolManager::getRandomReal32(0.0, 10.0f), RandomPoolManager::getRandomReal32(0.0f, 10.0f), RandomPoolManager::getRandomReal32(0.0f, 10.0f)); ExampleChildJoint = Joint::create(); //create a joint called ExampleChildJoint //Set ExampleChildJoint's current and bind transformations to TempMat (calculated above) ExampleChildJoint->setRelativeTransformation(TempMat); ExampleChildJoint->setBindRelativeTransformation(TempMat); ExampleChildJoint->setUseParentTranslation(true); //Add ExampleChildJoint as a child to the previously created joint TempRootJoint->pushToChildJoints(ExampleChildJoint);//add a Child to the previous joint //ExampleChildJoint will be the next parent joint TempRootJoint = TempRootJoint->getChildJoints(0); } //Skeleton SkeletonUnrecPtr ExampleSkeleton = Skeleton::create(); ExampleSkeleton->pushToRootJoints(ExampleRootJoint); //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 SkeletonNode = Node::create(); SkeletonNode->setCore(ExampleSkeletonDrawable); //Create scene NodeUnrecPtr scene = Node::create(); scene->setCore(Group::create()); scene->addChild(SkeletonNode); mgr->setRoot(scene); //Setup the Animation setupAnimation(ExampleRootJoint, ExampleRootJoint->getChildJoints(0)); //Set the currently playing animation to TheJointAnimation (can be switched at runtime via a key command) TheCurrentAnimation = TheJointAnimation; // Show the whole Scene mgr->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; }
int main(int argc, char **argv) { // OSG init osgInit(argc,argv); // Set up Window TutorialWindow = createNativeWindow(); TutorialWindow->initWindow(); TutorialWindow->setDisplayCallback(display); TutorialWindow->setReshapeCallback(reshape); //Add Window Listener TutorialKeyListener TheKeyListener; TutorialWindow->addKeyListener(&TheKeyListener); TutorialMouseListener TheTutorialMouseListener; TutorialMouseMotionListener TheTutorialMouseMotionListener; TutorialWindow->addMouseListener(&TheTutorialMouseListener); TutorialWindow->addMouseMotionListener(&TheTutorialMouseMotionListener); // Create the SimpleSceneManager helper mgr = new SimpleSceneManager; // Tell the Manager what to manage mgr->setWindow(TutorialWindow); //Print key command info std::cout << "\n\nKEY COMMANDS:" << std::endl << "space Play/Pause the animation" << std::endl << "B Show/Hide the bind pose skeleton" << std::endl << "P Show/Hide the current pose skeleton" << std::endl << "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); //===========================================Joints================================================================== Matrix TempMat; /*================================================================================================*/ /* Left Fingers */ LeftFingers = Joint::create(); //create a joint called LeftFingers TempMat.setTranslate(1.0,0.0,0.0); LeftFingers->setRelativeTransformation(TempMat); LeftFingers->setBindRelativeTransformation(TempMat); /*================================================================================================*/ /* Right Fingers */ RightFingers = Joint::create(); //create a joint called RightFingers TempMat.setTranslate(-1.0,0.0,0.0); RightFingers->setRelativeTransformation(TempMat); RightFingers->setBindRelativeTransformation(TempMat); /*================================================================================================*/ /* Left Hand */ LeftHand = Joint::create(); //create a joint called LeftHand TempMat.setTranslate(2.0,0.0,0.0); LeftHand->setRelativeTransformation(TempMat); LeftHand->setBindRelativeTransformation(TempMat); LeftHand->pushToChildJoints(LeftFingers); /*================================================================================================*/ /* Right Hand */ RightHand = Joint::create(); //create a joint called RightHand TempMat.setTranslate(-2.0,0.0,0.0); RightHand->setRelativeTransformation(TempMat); RightHand->setBindRelativeTransformation(TempMat); RightHand->pushToChildJoints(RightFingers); /*================================================================================================*/ /* Left Elbow */ LeftElbow = Joint::create(); //create a joint called LeftElbow TempMat.setTranslate(2.0,0.0,0.0); LeftElbow->setRelativeTransformation(TempMat); LeftElbow->setBindRelativeTransformation(TempMat); LeftElbow->pushToChildJoints(LeftHand); /*================================================================================================*/ /* Right Elbow */ RightElbow = Joint::create(); //create a joint called RightElbow TempMat.setTranslate(-2.0,0.0,0.0); RightElbow->setRelativeTransformation(TempMat); RightElbow->setBindRelativeTransformation(TempMat); RightElbow->pushToChildJoints(RightHand); /*================================================================================================*/ /* Left Shoulder */ LeftShoulder = Joint::create(); //create a joint called LeftShoulder TempMat.setTranslate(1.0,-0.5,0.0); LeftShoulder->setRelativeTransformation(TempMat); LeftShoulder->setBindRelativeTransformation(TempMat); LeftShoulder->pushToChildJoints(LeftElbow); /*================================================================================================*/ /* Right Shoulder */ RightShoulder = Joint::create(); //create a joint called RightShoulder TempMat.setTranslate(-1.0,-0.5,0.0); RightShoulder->setRelativeTransformation(TempMat); RightShoulder->setBindRelativeTransformation(TempMat); RightShoulder->pushToChildJoints(RightElbow); /*================================================================================================*/ /* Head */ Head = Joint::create(); //create a joint called Head TempMat.setTranslate(0.0,1.0,0.0); Head->setRelativeTransformation(TempMat); Head->setBindRelativeTransformation(TempMat); /*================================================================================================*/ /* Clavicle */ Clavicle = Joint::create(); //create a joint called Clavicle TempMat.setTranslate(0.0,5.0,0.0); Clavicle->setRelativeTransformation(TempMat); Clavicle->setBindRelativeTransformation(TempMat); Clavicle->pushToChildJoints(LeftShoulder); Clavicle->pushToChildJoints(RightShoulder); Clavicle->pushToChildJoints(Head); /*================================================================================================*/ /* Left Toes */ LeftToes = Joint::create(); //create a bone called ExampleChildbone TempMat.setTranslate(0.0,0.0,1.0); LeftToes->setRelativeTransformation(TempMat); LeftToes->setBindRelativeTransformation(TempMat); /*================================================================================================*/ /* Right Toes */ RightToes = Joint::create(); //create a joint called RightToes TempMat.setTranslate(0.0,0.0,1.0); RightToes->setRelativeTransformation(TempMat); RightToes->setBindRelativeTransformation(TempMat); /*================================================================================================*/ /* Left Foot */ LeftFoot = Joint::create(); //create a joint called LeftFoot TempMat.setTranslate(0.0,-3.0,0.0); LeftFoot->setRelativeTransformation(TempMat); LeftFoot->setBindRelativeTransformation(TempMat); LeftFoot->pushToChildJoints(LeftToes); /*================================================================================================*/ /* Right Foot */ RightFoot = Joint::create(); //create a joint called RightFoot TempMat.setTranslate(0.0,-3.0,0.0); RightFoot->setRelativeTransformation(TempMat); RightFoot->setBindRelativeTransformation(TempMat); RightFoot->pushToChildJoints(RightToes); /*================================================================================================*/ /* Left Knee */ LeftKnee = Joint::create(); //create a joint called LeftKnee TempMat.setTranslate(0.0,-3.0,0.0); LeftKnee->setRelativeTransformation(TempMat); LeftKnee->setBindRelativeTransformation(TempMat); LeftKnee->pushToChildJoints(LeftFoot); /*================================================================================================*/ /* Right Knee */ RightKnee = Joint::create(); //create a joint called RightKnee TempMat.setTranslate(0.0,-3.0,0.0); RightKnee->setRelativeTransformation(TempMat); RightKnee->setBindRelativeTransformation(TempMat); RightKnee->pushToChildJoints(RightFoot); /*================================================================================================*/ /* Left Hip */ LeftHip = Joint::create(); //create a joint called LeftHip TempMat.setTranslate(1.0,-1.0,0.0); LeftHip->setRelativeTransformation(TempMat); LeftHip->setBindRelativeTransformation(TempMat); LeftHip->pushToChildJoints(LeftKnee); /*================================================================================================*/ /* Right Hip */ RightHip = Joint::create(); //create a joint called RightHip TempMat.setTranslate(-1.0,-1.0,0.0); RightHip->setRelativeTransformation(TempMat); RightHip->setBindRelativeTransformation(TempMat); RightHip->pushToChildJoints(RightKnee); /*================================================================================================*/ /* Pelvis */ Pelvis = Joint::create(); //create a joint called Pelvis TempMat.setTranslate(0.0,7.0,0.0); Pelvis->setRelativeTransformation(TempMat); Pelvis->setBindRelativeTransformation(TempMat); Pelvis->pushToChildJoints(LeftHip); Pelvis->pushToChildJoints(RightHip); Pelvis->pushToChildJoints(Clavicle); //Skeleton ExampleSkeleton = Skeleton::create(); ExampleSkeleton->pushToRootJoints(Pelvis); //Set Pelvis as root joint of skeleton //SkeletonDrawer SkeletonDrawableUnrecPtr ExampleSkeletonDrawable = SkeletonDrawable::create(); ExampleSkeletonDrawable->setSkeleton(ExampleSkeleton); ExampleSkeletonDrawable->setMaterial(ExampleMaterial); ExampleSkeletonDrawable->setDrawBindPose(false); //Be default, we won't draw the skeleton's bind pose ExampleSkeletonDrawable->setBindPoseColor(Color4f(0.0, 1.0, 0.0, 1.0)); //When drawn, the skeleton's bind pose renders green ExampleSkeletonDrawable->setDrawPose(true); //Be 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 renders blue //Skeleton Node SkeletonNode = Node::create(); SkeletonNode->setCore(ExampleSkeletonDrawable); //Create scene NodeUnrecPtr scene = Node::create(); scene->setCore(Group::create()); scene->addChild(SkeletonNode); mgr->setRoot(scene); //Setup the Animation setupAnimation(); // Show the whole Scene mgr->showAll(); //Open Window Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5); TutorialWindow->openWindow(WinPos, WinSize, "12SkeletonAnimation"); //Main Loop TutorialWindow->mainLoop(); osgExit(); return 0; }