////////////////////////////////////////////////////////////////////////// //! build a box ////////////////////////////////////////////////////////////////////////// void buildBox(Node* const spaceGroupNode, PhysicsWorld* const physicsWorld, PhysicsHashSpace* const physicsSpace) { Vec3f Lengths((Real32)(rand()%2)+1.0, (Real32)(rand()%2)+1.0, (Real32)(rand()%2)+1.0); Matrix m; //create OpenSG mesh GeometryRecPtr box; NodeRecPtr boxNode = makeBox(Lengths.x(), Lengths.y(), Lengths.z(), 1, 1, 1); box = dynamic_cast<Geometry*>(boxNode->getCore()); SimpleMaterialRecPtr box_mat = SimpleMaterial::create(); box_mat->setAmbient(Color3f(0.0,0.0,0.0)); box_mat->setDiffuse(Color3f(0.0,1.0 ,0.0)); box->setMaterial(box_mat); TransformRecPtr boxTrans; NodeRecPtr boxTransNode = makeCoredNode<Transform>(&boxTrans); m.setIdentity(); Real32 randX = (Real32)(rand()%10)-5.0; Real32 randY = (Real32)(rand()%10)-5.0; m.setTranslate(randX, randY, 10.0); boxTrans->setMatrix(m); //create ODE data PhysicsBodyRecPtr boxBody = PhysicsBody::create(physicsWorld); boxBody->setPosition(Vec3f(randX, randY, 10.0)); boxBody->setBoxMass(1.0, Lengths.x(), Lengths.y(), Lengths.z()); //std::cout << "mass: " << boxBody->getMass() << std::endl //<< "massCenterOfGravity: " << boxBody->getMassCenterOfGravity().x() << ", " << boxBody->getMassCenterOfGravity().y() << ", " << boxBody->getMassCenterOfGravity().z() << std::endl //<< "massInertiaTensor: " << std::endl //<< boxBody->getMassInertiaTensor()[0][0] << " "<< boxBody->getMassInertiaTensor()[0][1] << " "<< boxBody->getMassInertiaTensor()[0][2] << " " << boxBody->getMassInertiaTensor()[0][3] << std::endl //<< boxBody->getMassInertiaTensor()[1][0] << " "<< boxBody->getMassInertiaTensor()[1][1] << " "<< boxBody->getMassInertiaTensor()[1][2] << " " << boxBody->getMassInertiaTensor()[1][3] << std::endl //<< boxBody->getMassInertiaTensor()[2][0] << " "<< boxBody->getMassInertiaTensor()[2][1] << " "<< boxBody->getMassInertiaTensor()[2][2] << " " << boxBody->getMassInertiaTensor()[2][3] << std::endl //<< boxBody->getMassInertiaTensor()[3][0] << " "<< boxBody->getMassInertiaTensor()[3][1] << " "<< boxBody->getMassInertiaTensor()[3][2] << " " << boxBody->getMassInertiaTensor()[3][3] << std::endl //<< std::endl; PhysicsBoxGeomRecPtr boxGeom = PhysicsBoxGeom::create(); boxGeom->setBody(boxBody); boxGeom->setSpace(physicsSpace); boxGeom->setLengths(Lengths); //add attachments boxNode->addAttachment(boxGeom); boxTransNode->addAttachment(boxBody); boxTransNode->addChild(boxNode); //add to SceneGraph spaceGroupNode->addChild(boxTransNode); commitChanges(); }
////////////////////////////////////////////////////////////////////////// //! trimesh defined by filenode will be loaded ////////////////////////////////////////////////////////////////////////// void buildTriMesh(Node* const TriGeometryBase, Node* const spaceGroupNode, PhysicsWorld* const physicsWorld, PhysicsHashSpace* const physicsSpace) { NodeRecPtr tri = cloneTree(TriGeometryBase); if(tri!=NULL) { GeometryRecPtr triGeo = dynamic_cast<Geometry*>(tri->getCore()); Matrix m; SimpleMaterialRecPtr tri_mat = SimpleMaterial::create(); tri_mat->setAmbient(Color3f(0.1,0.1,0.2)); tri_mat->setDiffuse(Color3f(1.0,0.1,0.7)); triGeo->setMaterial(tri_mat); TransformRecPtr triTrans; NodeRecPtr triTransNode = makeCoredNode<Transform>(&triTrans); m.setIdentity(); Real32 randX = (Real32)(rand()%10)-5.0; Real32 randY = (Real32)(rand()%10)-5.0; m.setTranslate(randX, randY, 18.0); triTrans->setMatrix(m); //create ODE data Vec3f GeometryBounds(calcMinGeometryBounds(triGeo)); PhysicsBodyRecPtr triBody = PhysicsBody::create(physicsWorld); triBody->setPosition(Vec3f(randX, randY, 18.0)); triBody->setLinearDamping(0.0001); triBody->setAngularDamping(0.0001); triBody->setBoxMass(1.0,GeometryBounds.x(), GeometryBounds.y(), GeometryBounds.z()); PhysicsGeomRecPtr triGeom; if(true) { triGeom = PhysicsTriMeshGeom::create(); triGeom->setBody(triBody); //add geom to space for collision triGeom->setSpace(physicsSpace); //set the geometryNode to fill the ode-triMesh NodeRecPtr TorusGeometryNode(makeTorus(0.55, 1.05, 6, 6)); dynamic_pointer_cast<PhysicsTriMeshGeom>(triGeom)->setGeometryNode(TorusGeometryNode); } //add attachments tri->addAttachment(triGeom); triTransNode->addAttachment(triBody); //add to SceneGraph triTransNode->addChild(tri); spaceGroupNode->addChild(triTransNode); } else { SLOG << "Could not read MeshData!" << endLog; } commitChanges(); }
////////////////////////////////////////////////////////////////////////// //! build a sphere ////////////////////////////////////////////////////////////////////////// void buildSphere(Node* const spaceGroupNode, PhysicsWorld* const physicsWorld, PhysicsHashSpace* const physicsSpace) { Real32 Radius((Real32)(rand()%2)*0.5+0.5); Matrix m; //create OpenSG mesh GeometryRecPtr sphere; NodeRecPtr sphereNode = makeSphere(2, Radius); sphere = dynamic_cast<Geometry*>(sphereNode->getCore()); SimpleMaterialRecPtr sphere_mat = SimpleMaterial::create(); sphere_mat->setAmbient(Color3f(0.0,0.0,0.0)); sphere_mat->setDiffuse(Color3f(0.0,0.0,1.0)); sphere->setMaterial(sphere_mat); TransformRecPtr sphereTrans; NodeRecPtr sphereTransNode = makeCoredNode<Transform>(&sphereTrans); m.setIdentity(); Real32 randX = (Real32)(rand()%10)-5.0; Real32 randY = (Real32)(rand()%10)-5.0; m.setTranslate(randX, randY, 10.0); sphereTrans->setMatrix(m); //create ODE data PhysicsBodyRecPtr sphereBody = PhysicsBody::create(physicsWorld); sphereBody->setPosition(Vec3f(randX, randY, 10.0)); sphereBody->setAngularDamping(0.0001); sphereBody->setSphereMass(1.0,Radius); PhysicsSphereGeomRecPtr sphereGeom = PhysicsSphereGeom::create(); sphereGeom->setBody(sphereBody); sphereGeom->setSpace(physicsSpace); sphereGeom->setRadius(Radius); //add attachments sphereNode->addAttachment(sphereGeom); sphereTransNode->addAttachment(sphereBody); sphereTransNode->addChild(sphereNode); //add to SceneGraph spaceGroupNode->addChild(sphereTransNode); commitChanges(); }
NodeTransitPtr createTorus(void) { // Make Object Nodes NodeRecPtr ExampleTorusGeo = makeTorus(90, 270, 16, 16); // Preform transformations on them Matrix mat; // On Torus mat.setTranslate(0.0,100.0,-200.0); TransformRecPtr TorusTranCore = Transform::create(); TorusTranCore->setMatrix(mat); NodeRecPtr ExampleTorus = Node::create(); ExampleTorus->setCore(TorusTranCore); ExampleTorus->addChild(ExampleTorusGeo); ExampleTorus->setTravMask(0); return NodeTransitPtr(ExampleTorus); }
NodeTransitPtr createBox(void) { // Make Object Nodes NodeRecPtr ExampleBoxGeo = makeBox(100, 100, 100, 1, 1, 1); MaterialRecPtr GreenMaterial = createMaterial(Color4f(0.0f,1.0f,0.0f,1.0f)); dynamic_cast<Geometry*>(ExampleBoxGeo->getCore())->setMaterial(GreenMaterial); Matrix mat; mat.setTranslate(250.0,250.0,0.0); TransformRecPtr ExampleBoxTranCore = Transform::create(); ExampleBoxTranCore->setMatrix(mat); NodeRecPtr ExampleBox = Node::create(); ExampleBox->setCore(ExampleBoxTranCore); ExampleBox->addChild(ExampleBoxGeo); ExampleBox->setTravMask(0); return NodeTransitPtr(ExampleBox); }
NodeTransitPtr createSphere(void) { // Make Object Nodes NodeRecPtr ExampleSphereGeo = makeSphere(4, 100); MaterialRecPtr RedMaterial = createMaterial(Color4f(1.0f,0.0f,0.0f,1.0f)); dynamic_cast<Geometry*>(ExampleSphereGeo->getCore())->setMaterial(RedMaterial); Matrix mat; mat.setTranslate(250.0,0.0,0.0); TransformRecPtr SphereTranCore = Transform::create(); SphereTranCore->setMatrix(mat); NodeRecPtr ExampleSphere = Node::create(); ExampleSphere->setCore(SphereTranCore); ExampleSphere->addChild(ExampleSphereGeo); ExampleSphere->setTravMask(0); return NodeTransitPtr(ExampleSphere); }
NodeTransitPtr createCone(void) { // Make Object Nodes NodeRecPtr ExampleConeGeo = makeCone(150, 50, 16, true, true); MaterialRecPtr BlueMaterial = createMaterial(Color4f(0.0f,0.0f,1.0f,1.0f)); dynamic_cast<Geometry*>(ExampleConeGeo->getCore())->setMaterial(BlueMaterial); // Preform transformations on them Matrix mat; // On Cone mat.setTranslate(0.0,0.0,-250.0); TransformRecPtr ConeTranCore = Transform::create(); ConeTranCore->setMatrix(mat); NodeRecPtr ExampleCone = Node::create(); ExampleCone->setCore(ConeTranCore); ExampleCone->addChild(ExampleConeGeo); ExampleCone->setTravMask(0); return NodeTransitPtr(ExampleCone); }
int main(int argc, char **argv) { // OSG init osgInit(argc,argv); { // Set up Window WindowEventProducerRecPtr TutorialWindow = createNativeWindow(); TutorialWindow->initWindow(); // Create the SimpleSceneManager helper SimpleSceneManager sceneManager; TutorialWindow->setDisplayCallback(boost::bind(display, &sceneManager)); TutorialWindow->setReshapeCallback(boost::bind(reshape, _1, &sceneManager)); // Tell the Manager what to manage sceneManager.setWindow(TutorialWindow); TutorialWindow->connectKeyTyped(boost::bind(keyPressed, _1)); // Make Torus Node (creates Torus in background of scene) NodeRecPtr TorusGeometryNode = makeTorus(.5, 2, 16, 16); // Make Main Scene Node and add the Torus NodeRecPtr scene = Node::create(); scene->setCore(Group::create()); scene->addChild(TorusGeometryNode); // Create the Graphics GraphicsRecPtr TutorialGraphics = Graphics2D::create(); // Initialize the LookAndFeelManager to enable default settings LookAndFeelManager::the()->getLookAndFeel()->init(); //Create the nessicary parts for a viewport Matrix TransformMatrix; TransformMatrix.setTranslate(0.0f,0.0f, 0.0f); TransformRecPtr CameraBeaconTransform = Transform::create(); CameraBeaconTransform->setMatrix(TransformMatrix); NodeRecPtr CameraBeaconNode = Node::create(); CameraBeaconNode->setCore(CameraBeaconTransform); // Make Torus Node (creates Torus in background of scene) NodeRecPtr GeometryNode = makeTorus(.5, 2, 32, 32); //Make a light Node NodeRecPtr LightBeaconNode = makeCoredNode<Transform>(); DirectionalLightRecPtr SceneLight = DirectionalLight::create(); SceneLight->setAmbient(Color4f(0.3f,0.3f,0.3f,1.0f)); SceneLight->setDiffuse(Color4f(0.8f,0.8f,0.8f,1.0f)); SceneLight->setSpecular(Color4f(1.0f,1.0f,1.0f,1.0f)); SceneLight->setOn(true); SceneLight->setBeacon(LightBeaconNode); NodeRecPtr LightNode = makeNodeFor(SceneLight); LightNode->addChild(GeometryNode); // Make Main Scene Node and add the Torus NodeRecPtr DefaultRootNode = Node::create(); DefaultRootNode->setCore(Group::create()); DefaultRootNode->addChild(LightNode); DefaultRootNode->addChild(LightBeaconNode); DefaultRootNode->addChild(CameraBeaconNode); //Camera PerspectiveCameraRecPtr DefaultCamera = PerspectiveCamera::create(); DefaultCamera->setBeacon(CameraBeaconNode); DefaultCamera->setFov (osgDegree2Rad(60.f)); DefaultCamera->setNear (0.1f); DefaultCamera->setFar (100.f); //Background GradientBackgroundRecPtr DefaultBackground = GradientBackground::create(); DefaultBackground->addLine(Color3f(0.0f,0.0f,0.0f), 0.0f); DefaultBackground->addLine(Color3f(0.0f,0.0f,1.0f), 1.0f); //Viewport ViewportRecPtr DefaultViewport = Viewport::create(); DefaultViewport->setCamera (DefaultCamera); DefaultViewport->setRoot (DefaultRootNode); DefaultViewport->setSize (0.0f,0.0f, 1.0f,1.0f); DefaultViewport->setBackground (DefaultBackground); //GL Viewport Component LineBorderRecPtr TheGLViewportBorder = LineBorder::create(); TheGLViewportBorder->setColor(Color4f(1.0,0.0,0.0,1.0)); TheGLViewportBorder->setWidth(3.0); GLViewportRecPtr TheGLViewport = GLViewport::create(); TheGLViewport->setPort(DefaultViewport); TheGLViewport->setPreferredSize(Vec2f(400.0f,400.0f)); TheGLViewport->setBorders(TheGLViewportBorder); TheGLViewport->lookAt(Pnt3f(0.0f,0.0f,10.0f), //From Pnt3f(0.0f,0.0f,0.0f), //At Vec3f(0.0f,1.0f,0.0f)); //Up ButtonRecPtr ExampleButton = Button::create(); ExampleButton->setText("Example"); // Create The Main InternalWindow // Create Background to be used with the Main InternalWindow ColorLayerRecPtr MainInternalWindowBackground = ColorLayer::create(); MainInternalWindowBackground->setColor(Color4f(1.0,1.0,1.0,0.5)); InternalWindowRecPtr MainInternalWindow = InternalWindow::create(); LayoutRecPtr MainInternalWindowLayout = FlowLayout::create(); MainInternalWindow->pushToChildren(TheGLViewport); MainInternalWindow->pushToChildren(ExampleButton); MainInternalWindow->setLayout(MainInternalWindowLayout); MainInternalWindow->setBackgrounds(MainInternalWindowBackground); MainInternalWindow->setAlignmentInDrawingSurface(Vec2f(0.5f,0.5f)); MainInternalWindow->setScalingInDrawingSurface(Vec2f(0.95f,0.95f)); MainInternalWindow->setDrawTitlebar(false); MainInternalWindow->setResizable(false); // Create the Drawing Surface UIDrawingSurfaceRecPtr TutorialDrawingSurface = UIDrawingSurface::create(); TutorialDrawingSurface->setGraphics(TutorialGraphics); TutorialDrawingSurface->setEventProducer(TutorialWindow); TutorialDrawingSurface->openWindow(MainInternalWindow); // Create the UI Foreground Object UIForegroundRecPtr TutorialUIForeground = UIForeground::create(); TutorialUIForeground->setDrawingSurface(TutorialDrawingSurface); // Tell the Manager what to manage sceneManager.setRoot(scene); // Add the UI Foreground Object to the Scene ViewportRecPtr TutorialViewport = sceneManager.getWindow()->getPort(0); TutorialViewport->addForeground(TutorialUIForeground); //Create the Documentation Foreground and add it to the viewport SimpleScreenDoc TheSimpleScreenDoc(&sceneManager, TutorialWindow); // 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, "41GLViewportComponent"); //Enter 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); //SkeletonDrawer System Material LineChunkUnrecPtr ExampleLineChunk = LineChunk::create(); ExampleLineChunk->setWidth(4.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); //Skeleton Particle System Node NodeUnrecPtr SkeletonNode = Node::create(); SkeletonNode->setCore(ExampleSkeletonDrawable); // Make Main Scene Node and add the Torus NodeUnrecPtr scene = Node::create(); scene->setCore(Group::create()); scene->addChild(SkeletonNode); scene->addChild(ExampleRootJointNode); mgr->setRoot(scene); // 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, "10SkeletonDrawer"); //Main Loop TutorialWindow->mainLoop(); osgExit(); return 0; }
// Initialize WIN32 & OpenSG and set up the scene int main(int argc, char **argv) { // OSG init osgInit(argc,argv); { // Set up Window WindowEventProducerRecPtr TutorialWindow = createNativeWindow(); TutorialWindow->initWindow(); // Create the SimpleSceneManager helper SimpleSceneManager sceneManager; TutorialWindow->setDisplayCallback(boost::bind(display, &sceneManager)); TutorialWindow->setReshapeCallback(boost::bind(reshape, _1, &sceneManager)); // 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)); //Sound Emitter Node SoundEmitterRecPtr TheEmitter = SoundEmitter::create(); TheEmitter->attachUpdateProducer(TutorialWindow); NodeUnrecPtr TheEmitterNode = Node::create(); TheEmitterNode->setCore(TheEmitter); //Sphere Transformation Node Matrix Translate; Translate.setTranslate(0.0,0.0,-5.0); Matrix Rotation; Rotation.setRotate(Quaternion(Vec3f(0.0,1.0,0.0), 0.0)); Matrix Total(Translate); Total.mult(Rotation); TransformRecPtr TheSphereTransform = Transform::create(); TheSphereTransform->setMatrix(Total); NodeUnrecPtr SphereTransformNode = Node::create(); SphereTransformNode->setCore(TheSphereTransform); SphereTransformNode->addChild(makeSphere(2, 1.0)); SphereTransformNode->addChild(TheEmitterNode); // create the scene NodeUnrecPtr scene = Node::create(); scene->setCore(Group::create()); scene->addChild(SphereTransformNode); // tell the manager what to manage sceneManager.setRoot (scene); CameraUnrecPtr TheCamera = sceneManager.getCamera(); TheCamera->setNear(0.1); TheCamera->setFar(100.0); //Initialize the Sound Manager SoundManager::the()->attachUpdateProducer(TutorialWindow); SoundManager::the()->setCamera(sceneManager.getCamera()); SoundRecPtr PopSound = SoundManager::the()->createSound(); PopSound->setFile(BoostPath("./Data/pop.wav")); PopSound->setVolume(1.0); PopSound->setStreaming(false); PopSound->setLooping(-1); PopSound->setEnable3D(true); PopSound->connectSoundPlayed (boost::bind(handleSoundPlayed, _1)); PopSound->connectSoundStopped (boost::bind(handleSoundStopped, _1)); PopSound->connectSoundPaused (boost::bind(handleSoundPaused, _1)); PopSound->connectSoundUnpaused(boost::bind(handleSoundUnpaused, _1)); PopSound->connectSoundLooped (boost::bind(handleSoundLooped, _1)); //Attach this sound to the emitter node TheEmitter->setSound(PopSound); TutorialWindow->connectKeyTyped(boost::bind(keyTyped, _1, TheEmitter.get())); TutorialWindow->connectUpdate(boost::bind(handleUpdate, _1, TheSphereTransform.get())); //Create the Documentation SimpleScreenDoc TheSimpleScreenDoc(&sceneManager, TutorialWindow); Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5); TutorialWindow->openWindow(WinPos, WinSize, "02 Sound3D Window"); //Enter main loop TutorialWindow->mainLoop(); } osgExit(); return 0; }
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; }
// Initialize GLUT & OpenSG and set up the rootNode int main(int argc, char **argv) { // OSG init osgInit(argc,argv); { // Set up Window WindowEventProducerRecPtr TutorialWindow = createNativeWindow(); TutorialWindow->initWindow(); SimpleSceneManager sceneManager; TutorialWindow->setDisplayCallback(boost::bind(display, &sceneManager)); TutorialWindow->setReshapeCallback(boost::bind(reshape, _1, &sceneManager)); //Attach to events TutorialWindow->connectMousePressed(boost::bind(mousePressed, _1, &sceneManager)); TutorialWindow->connectMouseReleased(boost::bind(mouseReleased, _1, &sceneManager)); TutorialWindow->connectMouseMoved(boost::bind(mouseMoved, _1, &sceneManager)); TutorialWindow->connectMouseDragged(boost::bind(mouseDragged, _1, &sceneManager)); TutorialWindow->connectKeyPressed(boost::bind(keyPressed,_1)); // Tell the Manager what to manage sceneManager.setWindow(TutorialWindow); UInt32 SceneMask(1), PathMask(4); BoostPath SceneFilePath(".//Data//CellParts.osb"); if(argc >= 2) { SceneFilePath = BoostPath(argv[1]); if(!boost::filesystem::exists(SceneFilePath)) { SceneFilePath = BoostPath(".//Data//CellParts.osb"); } } //Make Base Geometry Node NodeRecPtr SceneGeometryNode = SceneFileHandler::the()->read(SceneFilePath.string().c_str()); SceneGeometryNode->setTravMask(SceneMask); if(SceneGeometryNode == NULL) { SceneGeometryNode = makeTorus(1.0, 10.0, 24, 24); } //Construct the Root Node NodeRecPtr RootNode = makeCoredNode<Group>(); RootNode->addChild(SceneGeometryNode); commitChanges(); //Create the Octree SLOG << "Started Building Octree" << std::endl; SLOG << "This may take some time ..." << std::endl; Time StartTime; StartTime = getSystemTime(); OctreePtr TheOctree = Octree::buildTree(RootNode,SceneMask,6,1.5,true); SLOG << "Building Octree: " << getSystemTime() - StartTime << " s" << std::endl; Pnt3f Min,Max; TheOctree->getRoot()->getVolume(); SLOG << "Octree: "<< std::endl << " Depth: " << TheOctree->getDepth() << std::endl << " Bounds: " << TheOctree->getRoot()->getVolume().getMin() << " : " << TheOctree->getRoot()->getVolume().getMax() << std::endl << " NodeCount: " << TheOctree->getNodeCount() << std::endl << " LeafNodeCount: " << TheOctree->getLeafNodeCount() << std::endl << " BranchNodeCount: " << TheOctree->getBranchNodeCount() << std::endl << " IntersectingNodeCount: " << TheOctree->getIntersectingNodeCount() << std::endl << " IntersectingLeafNodeCount: " << TheOctree->getIntersectingLeafNodeCount() << std::endl; //Create the Path Geometry //Generate the Path OctreeAStarAlgorithm AlgorithmObj; SLOG << "Started AStar Search" << std::endl; StartTime = getSystemTime(); Pnt3f Start(-4.01f,1.01f,10.01f),Goal(-4.01f,-0.01f,-7.01f); std::vector<Pnt3f> Path = AlgorithmObj.search(TheOctree,Start,Goal); Path.front() = Start; Path.back() = Goal; SLOG << "Finished AStar Search: " << getSystemTime() - StartTime << " s" << std::endl; NodeRecPtr PathNode = createPathGeometry(Path); PathNode->setTravMask(PathMask); RootNode->addChild(PathNode); NodeRecPtr StartNode = makeSphere(1.0, 2); TransformRecPtr StartNodeTransform = Transform::create(); Matrix StartNodeMatrix; StartNodeMatrix.setTranslate(Start); StartNodeMatrix.setScale(0.1f); StartNodeTransform->setMatrix(StartNodeMatrix); NodeRecPtr StartNodeTransformNode = makeNodeFor(StartNodeTransform); StartNodeTransformNode->addChild(StartNode); StartNodeTransformNode->setTravMask(PathMask); RootNode->addChild(StartNodeTransformNode); NodeRecPtr GoalNode = makeSphere(1.0, 2); TransformRecPtr GoalNodeTransform = Transform::create(); Matrix GoalNodeMatrix; GoalNodeMatrix.setScale(0.1f); GoalNodeMatrix.setTranslate(Goal); GoalNodeTransform->setMatrix(GoalNodeMatrix); NodeRecPtr GoalNodeTransformNode = makeNodeFor(GoalNodeTransform); GoalNodeTransformNode->addChild(GoalNode); GoalNodeTransformNode->setTravMask(PathMask); RootNode->addChild(GoalNodeTransformNode); //Set the background SolidBackgroundRecPtr TheBackground = SolidBackground::create(); TheBackground->setColor(Color3f(0.0f,0.0f,0.0f)); // tell the manager what to manage sceneManager.setRoot (RootNode); sceneManager.getWindow()->getPort(0)->setBackground(TheBackground); // show the whole RootNode sceneManager.showAll(); Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5); TutorialWindow->openWindow(WinPos, WinSize, "01AStarPathing"); //Enter main Loop TutorialWindow->mainLoop(); } osgExit(); return 0; }