NodePtr createScenegraph(){ // the scene must be created here for (int x = 0; x < N; x++) for (int z = 0; z < N; z++) wMesh[x][z] = 0; // GeoPTypes will define the types of primitives to be used GeoPTypesPtr type = GeoPTypesUI8::create(); beginEditCP(type, GeoPTypesUI8::GeoPropDataFieldMask); // we want to use quads ONLY type->addValue(GL_QUADS); endEditCP(type, GeoPTypesUI8::GeoPropDataFieldMask); // GeoPLength will define the number of vertices of // the used primitives GeoPLengthsPtr length = GeoPLengthsUI32::create(); beginEditCP(length, GeoPLengthsUI32::GeoPropDataFieldMask); // the length of our quads is four ;-) length->addValue((N-1)*(N-1)*4); endEditCP(length, GeoPLengthsUI32::GeoPropDataFieldMask); // GeoPositions3f stores the positions of all vertices used in // this specific geometry core GeoPositions3fPtr pos = GeoPositions3f::create(); beginEditCP(pos, GeoPositions3f::GeoPropDataFieldMask); // here they all come for (int x = 0; x < N; x++) for (int z = 0; z < N; z++) pos->addValue(Pnt3f(x, wMesh[x][z], z)); endEditCP(pos, GeoPositions3f::GeoPropDataFieldMask); //GeoColors3f stores all color values that will be used GeoColors3fPtr colors = GeoColors3f::create(); beginEditCP(colors, GeoColors3f::GeoPropDataFieldMask); for (int x = 0; x < N; x++) for (int z = 0; z < N; z++) colors->addValue(Color3f(0,0,1)); endEditCP(colors, GeoColors3f::GeoPropDataFieldMask); GeoNormals3fPtr norms = GeoNormals3f::create(); beginEditCP(norms, GeoNormals3f::GeoPropDataFieldMask); for (int x = 0; x < N; x++) for (int z = 0; z < N; z++) // As initially all heights are set to zero thus yielding a plane, // we set all normals to (0,1,0) parallel to the y-axis norms->addValue(Vec3f(0,1,0)); endEditCP(norms, GeoNormals3f::GeoPropDataFieldMask); SimpleMaterialPtr mat = SimpleMaterial::create(); beginEditCP(mat); mat->setDiffuse(Color3f(0,0,1)); endEditCP(mat); // GeoIndicesUI32 points to all relevant data used by the // provided primitives GeoIndicesUI32Ptr indices = GeoIndicesUI32::create(); beginEditCP(indices, GeoIndicesUI32::GeoPropDataFieldMask); for (int x = 0; x < N-1; x++) for (int z = 0; z < N-1; z++){ // points to four vertices that will // define a single quad indices->addValue(z*N+x); indices->addValue((z+1)*N+x); indices->addValue((z+1)*N+x+1); indices->addValue(z*N+x+1); } endEditCP(indices, GeoIndicesUI32::GeoPropDataFieldMask); GeometryPtr geo = Geometry::create(); beginEditCP(geo, Geometry::TypesFieldMask | Geometry::LengthsFieldMask | Geometry::IndicesFieldMask | Geometry::PositionsFieldMask | Geometry::NormalsFieldMask | Geometry::MaterialFieldMask | Geometry::ColorsFieldMask | Geometry::DlistCacheFieldMask ); geo->setTypes(type); geo->setLengths(length); geo->setIndices(indices); geo->setPositions(pos); geo->setNormals(norms); geo->setMaterial(mat); //geo->setColors(colors); geo->setDlistCache(false); endEditCP(geo, Geometry::TypesFieldMask | Geometry::LengthsFieldMask | Geometry::IndicesFieldMask | Geometry::PositionsFieldMask | Geometry::NormalsFieldMask | Geometry::MaterialFieldMask | Geometry::ColorsFieldMask | Geometry::DlistCacheFieldMask ); PointLightPtr pLight = PointLight::create(); NodePtr root = Node::create(); NodePtr water = Node::create(); NodePtr pLightTransformNode = Node::create(); TransformPtr pLightTransform = Transform::create(); NodePtr pLightNode = Node::create(); beginEditCP(pLightNode); pLightNode->setCore(Group::create()); endEditCP(pLightNode); Matrix m; m.setIdentity(); m.setTranslate(50,25,50); beginEditCP(pLightTransform); pLightTransform->setMatrix(m); endEditCP(pLightTransform); //we add a little spehere that will represent the light source GeometryPtr sphere = makeSphereGeo(2,2); SimpleMaterialPtr sm = SimpleMaterial::create(); beginEditCP(sm, SimpleMaterial::DiffuseFieldMask | SimpleMaterial::LitFieldMask); { sm->setLit(false); sm->setDiffuse(Color3f(1,1,1)); } endEditCP (sm, SimpleMaterial::DiffuseFieldMask | SimpleMaterial::LitFieldMask); beginEditCP(sphere, Geometry::MaterialFieldMask); { sphere->setMaterial(sm); } endEditCP (sphere, Geometry::MaterialFieldMask); NodePtr sphereNode = Node::create(); beginEditCP(sphereNode); sphereNode->setCore(sphere); endEditCP(sphereNode); beginEditCP(pLightTransformNode); pLightTransformNode->setCore(pLightTransform); pLightTransformNode->addChild(pLightNode); pLightTransformNode->addChild(sphereNode); endEditCP(pLightTransformNode); beginEditCP(pLight); pLight->setPosition(Pnt3f(0,0,0)); //Attenuation parameters pLight->setConstantAttenuation(1); pLight->setLinearAttenuation(0); pLight->setQuadraticAttenuation(0); //color information pLight->setDiffuse(Color4f(1,1,1,1)); pLight->setAmbient(Color4f(0.2,0.2,0.2,1)); pLight->setSpecular(Color4f(1,1,1,1)); //set the beacon pLight->setBeacon(pLightNode); endEditCP (pLight); beginEditCP(water); water->setCore(geo); endEditCP(water); beginEditCP(root); root->setCore(pLight); root->addChild(water); root->addChild(pLightTransformNode); endEditCP(root); return root; }
// 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(){ //create sun, planet & moon geometry GeometryPtr sun = makeSphereGeo(3, 6); NodePtr planet = makeSphere(3, 3); NodePtr moon = makeSphere(2,1); //the root node will be the sun NodePtr root = Node::create(); beginEditCP(root, Node::CoreFieldMask); root->setCore(sun); endEditCP(root, Node::CoreFieldMask); NodePtr planetTransformNode = Node::create(); NodePtr moonTransformNode = Node::create(); // these were declared globally planetTransform = Transform::create(); moonTransform = Transform::create(); // Now we need to fill it with live // We want to have the planet some distance away from the sun, // but initial with no rotation. The same aplies to the moon Matrix m,n; m.setIdentity(); n.setIdentity(); m.setTranslate(20,0,0); n.setTranslate(8,0,0); beginEditCP(planetTransform, Transform::MatrixFieldMask); planetTransform->setMatrix(m); endEditCP(planetTransform, Transform::MatrixFieldMask); beginEditCP(moonTransform, Transform::MatrixFieldMask); moonTransform->setMatrix(n); endEditCP(moonTransform, Transform::MatrixFieldMask); //Insert the cores into the apropiate nodes and add the geometry beginEditCP(planetTransformNode, Node::CoreFieldMask | Node::ChildrenFieldMask); planetTransformNode->setCore(planetTransform); planetTransformNode->addChild(planet); endEditCP(planetTransformNode, Node::CoreFieldMask | Node::ChildrenFieldMask); beginEditCP(moonTransformNode, Node::CoreFieldMask | Node::ChildrenFieldMask); moonTransformNode->setCore(moonTransform); moonTransformNode->addChild(moon); endEditCP(moonTransformNode, Node::CoreFieldMask | Node::ChildrenFieldMask); //add the planet to the sun beginEditCP(root, Node::ChildrenFieldMask); root->addChild(planetTransformNode); endEditCP(root, Node::ChildrenFieldMask); //add the moon to the planet beginEditCP(planet, Node::ChildrenFieldMask); planet->addChild(moonTransformNode); endEditCP(planet, Node::ChildrenFieldMask); //now we are done return root; }
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; }
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); GeometryRefPtr SphereGeometry = makeSphereGeo(2, 0.25f); GeometryRefPtr BoxGeometry = makeBoxGeo(0.5f,0.5f,0.5f,1,1,1); //Skeleton SkeletonBlendedGeometryUnrecPtr ExampleSkeleton = SkeletonBlendedGeometry::create(); //Joint JointRecPtr ExampleRootJoint = Joint::create(); //Add this joint to the skeleton ExampleSkeleton->pushToJoints(ExampleRootJoint, Matrix()); NodeRecPtr ExampleRootJointNode = makeNodeFor(ExampleRootJoint); 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) { JointRecPtr ExampleChildJoint = Joint::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->setJointTransformation(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(ExampleChildJoint, 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 SkeletonNode = Node::create(); SkeletonNode->setCore(ExampleSkeletonDrawable); //Create scene NodeUnrecPtr scene = Node::create(); scene->setCore(Group::create()); scene->addChild(SkeletonNode); scene->addChild(ExampleRootJointNode); mgr->setRoot(scene); //Setup the Animation setupAnimation(ExampleRootJoint, dynamic_cast<Joint*>(ExampleRootJointNode->getChild(1)->getCore())); //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) { NodePtr node; osgInit(argc, argv); NodePtr pScene = Node::create(); GroupPtr pSceneGroup = Group::create(); pScene->setCore(pSceneGroup); setName(pScene, "foo"); setName(pSceneGroup, "bar"); NodePtr pSceneChild = Node::create(); // NodeCorePtr pSceneChildCore = Group::create(); NodeCorePtr pSceneChildCore = makeSphereGeo(3, 1); pSceneChild->setCore(pSceneChildCore); setName(pSceneChild, "foo1"); // setName(pSceneChildCore, "bar1"); pScene->addChild(pSceneChild); pSceneChild = Node::create(); // pSceneChildCore = Group::create(); // pSceneChildCore = makeSphereGeo(3, 1); pSceneChild->setCore(pSceneChildCore); // setName(pSceneChild, "foo2"); setName(pSceneChildCore, "bar2"); pScene->addChild(pSceneChild); pSceneChild = Node::create(); pSceneChildCore = Group::create(); pSceneChild->setCore(pSceneChildCore); setName(pSceneChild, "foo3"); setName(pSceneChildCore, "bar3"); pScene->addChild(pSceneChild); pSceneChild = Node::create(); pSceneChildCore = Group::create(); pSceneChild->setCore(pSceneChildCore); setName(pSceneChild, "foo4"); setName(pSceneChildCore, "bar4"); pScene->addChild(pSceneChild); pSceneChild = Node::create(); // pSceneChildCore = Group::create(); pSceneChild->setCore(pSceneChildCore); setName(pSceneChild, "foo4"); // setName(pSceneChildCore, "bar4"); pScene->addChild(pSceneChild); VRMLWriteAction *pWriter = VRMLWriteAction::create(); pWriter->addOptions(VRMLWriteAction::OSGNoNormals); //VRMLWriteAction::OSGNoIndent | pWriter->open((argc > 2) ? argv[2] : "test.wrl"); pWriter->write(pScene); pWriter->close(); return 0; }
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)); //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)); // Tell the Manager what to manage sceneManager.setWindow(TutorialWindow); VideoWrapperRecPtr TheVideo = getDefaultVideoManager()->createVideoWrapper(); TheVideo->attachUpdateProducer(TutorialWindow); TheVideo->connectStarted(boost::bind(handleVideoStarted, _1)); TheVideo->connectPaused(boost::bind(handleVideoPaused, _1)); TheVideo->connectUnpaused(boost::bind(handleVideoUnpaused, _1)); TheVideo->connectStopped(boost::bind(handleVideoStopped, _1)); TheVideo->connectClosed(boost::bind(handleVideoClosed, _1)); TheVideo->connectSeeked(boost::bind(handleVideoSeeked, _1)); TheVideo->connectCycled(boost::bind(handleVideoCycled, _1)); TheVideo->connectOpened(boost::bind(handleVideoOpened, _1)); TheVideo->connectEnded(boost::bind(handleVideoEnded, _1)); BoostPath VideoFilePath("./Data/ExampleVideo.avi"); if(argc >= 2) { VideoFilePath = BoostPath(argv[1]); if(!boost::filesystem::exists(VideoFilePath)) { std::cerr << "Could not load file: "<< VideoFilePath.string() << ", because no such files exists."<< std::endl; VideoFilePath = BoostPath("./Data/ExampleVideo.avi"); } } TheVideo->open(VideoFilePath, TutorialWindow); TheVideo->pause(); if(TheVideo->hasAudio()) { TheVideo->enableAudio(); TheVideo->setAudioVolume(0.5f); } //Wait for the video to initialize std::cout << "Dimensions: " << TheVideo->getWidth() << "x" << TheVideo->getHeight() << std::endl; Real32 AspectRatio(static_cast<Real32>(TheVideo->getWidth())/static_cast<Real32>(TheVideo->getHeight())); //Real32 AspectRatio(4.0f/3.0f); // Set filtering modes. LINEAR is cheap and good if the image size // changes very little (i.e. the window is about the same size as // the images). //TheVideo->setMinFilter(GL_LINEAR); TheVideo->setMinFilter(GL_NEAREST); TheVideo->setMagFilter(GL_LINEAR); //TheVideo->setMagFilter(GL_NEAREST); // Set the wrapping modes. We don't need repetition, it might actually // introduce artifactes at the borders, so switch it off. TheVideo->setWrapS(GL_CLAMP_TO_EDGE); TheVideo->setWrapT(GL_CLAMP_TO_EDGE); TheVideo->setScale(false); ChunkMaterialUnrecPtr VideoMaterial = ChunkMaterial::create(); VideoMaterial->addChunk(TheVideo); StateChunkUnrecPtr pMChunk = MaterialChunk::create(); VideoMaterial->addChunk(pMChunk); // create the scene //Plane Geometry GeometryRecPtr PlaneGeometry = makePlaneGeo(10.0*AspectRatio,10.0,10,10); PlaneGeometry->setMaterial(VideoMaterial); NodeRecPtr PlaneGeometryNode = Node::create(); PlaneGeometryNode->setCore(PlaneGeometry); //Box Geometry GeometryRecPtr BoxGeometry = makeBoxGeo(10.0*AspectRatio,10.0,10.0,2,2,2); BoxGeometry->setMaterial(VideoMaterial); NodeRecPtr BoxGeometryNode = Node::create(); BoxGeometryNode->setCore(BoxGeometry); //Sphere Geometry GeometryRecPtr SphereGeometry = makeSphereGeo(2,5.0); SphereGeometry->setMaterial(VideoMaterial); NodeRecPtr SphereGeometryNode = Node::create(); SphereGeometryNode->setCore(SphereGeometry); //Torus Geometry GeometryRecPtr TorusGeometry = makeTorusGeo(2.0,5.0,32,32); TorusGeometry->setMaterial(VideoMaterial); NodeRecPtr TorusGeometryNode = Node::create(); TorusGeometryNode->setCore(TorusGeometry); //Switch Node SwitchRecPtr GeometryNodeSwitch = Switch::create(); GeometryNodeSwitch->setChoice(0); NodeRecPtr SwitchNode = Node::create(); SwitchNode->setCore(GeometryNodeSwitch); SwitchNode->addChild(PlaneGeometryNode); SwitchNode->addChild(BoxGeometryNode); SwitchNode->addChild(SphereGeometryNode); SwitchNode->addChild(TorusGeometryNode); NodeRecPtr scene = Node::create(); TransformRecPtr trans = Transform::create(); scene->setCore(trans); scene->addChild(SwitchNode); TutorialWindow->connectKeyTyped(boost::bind(handleKeyTyped, _1, TheVideo.get(), GeometryNodeSwitch.get())); sceneManager.setRoot(scene); // 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, "01PlayVideo"); commitChanges(); //Enter main Loop TutorialWindow->mainLoop(); } osgExit(); return 0; }
// Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { // OSG init osgInit(argc,argv); // Set up Window TutorialWindowEventProducer = createDefaultWindowEventProducer(); WindowPtr MainWindow = TutorialWindowEventProducer->initWindow(); TutorialWindowEventProducer->setDisplayCallback(display); TutorialWindowEventProducer->setReshapeCallback(reshape); //Add Window Listener TutorialKeyListener TheKeyListener; TutorialWindowEventProducer->addKeyListener(&TheKeyListener); TutorialMouseListener TheTutorialMouseListener; TutorialMouseMotionListener TheTutorialMouseMotionListener; TutorialWindowEventProducer->addMouseListener(&TheTutorialMouseListener); TutorialWindowEventProducer->addMouseMotionListener(&TheTutorialMouseMotionListener); // Create the SimpleSceneManager helper mgr = new SimpleSceneManager; // Tell the Manager what to manage mgr->setWindow(TutorialWindowEventProducer->getWindow()); //Make a SphereNode for the point light LambertMaterialPtr TheLightMat = LambertMaterial::create(); beginEditCP(TheLightMat, LambertMaterial::IncandescenceFieldMask); TheLightMat->setIncandescence(Color3f(1.0,1.0,1.0)); endEditCP(TheLightMat, LambertMaterial::IncandescenceFieldMask); GeometryPtr LightSphereGeo = makeSphereGeo(2,2.0); beginEditCP(LightSphereGeo, Geometry::MaterialFieldMask); LightSphereGeo->setMaterial(TheLightMat); endEditCP (LightSphereGeo, Geometry::MaterialFieldMask); NodePtr LightSphereNode = Node::create(); beginEditCP(LightSphereNode, Node::CoreFieldMask); LightSphereNode->setCore(LightSphereGeo); endEditCP (LightSphereNode, Node::CoreFieldMask); //Create the beacon for the Point Light Matrix ThePointLightMat; ThePointLightMat.setTranslate(Vec3f(0.0,100.0,0.0)); ThePointLightBeaconTransform = Transform::create(); beginEditCP(ThePointLightBeaconTransform); ThePointLightBeaconTransform->setMatrix(ThePointLightMat); endEditCP(ThePointLightBeaconTransform); NodePtr ThePointLightBeaconNode = Node::create(); beginEditCP(ThePointLightBeaconNode); ThePointLightBeaconNode->setCore(ThePointLightBeaconTransform); ThePointLightBeaconNode->addChild(LightSphereNode); endEditCP(ThePointLightBeaconNode); //Set the light properties desired PointLightPtr ThePointLight = PointLight::create(); beginEditCP(ThePointLight); ThePointLight->setAmbient(0.3,0.3,0.3,0.3); ThePointLight->setDiffuse(1.0,1.0,1.0,1.0); ThePointLight->setSpecular(1.0,1.0,1.0,1.0); ThePointLight->setBeacon(ThePointLightBeaconNode); endEditCP(ThePointLight); NodePtr ThePointLightNode = Node::create(); beginEditCP(ThePointLightNode); ThePointLightNode->setCore(ThePointLight); endEditCP(ThePointLightNode); //Set the light properties desired SpotLightPtr TheSpotLight = SpotLight::create(); beginEditCP(TheSpotLight); TheSpotLight->setAmbient(0.3,0.3,0.3,0.3); TheSpotLight->setDiffuse(1.0,1.0,1.0,1.0); TheSpotLight->setSpecular(1.0,1.0,1.0,1.0); TheSpotLight->setBeacon(ThePointLightBeaconNode); TheSpotLight->setDirection(Vec3f(0.0,-1.0,0.0)); TheSpotLight->setSpotExponent(5.0); TheSpotLight->setSpotCutOff(1.1); endEditCP(TheSpotLight); NodePtr TheSpotLightNode = Node::create(); beginEditCP(TheSpotLightNode); TheSpotLightNode->setCore(TheSpotLight); endEditCP(TheSpotLightNode); //Load in the Heightmap Image ImagePtr PerlinNoiseImage = createPerlinImage(Vec2s(256,256), Vec2f(10.0f,10.0f),0.5f,1.0f,Vec2f(0.0f,0.0f),0.25f,6,PERLIN_INTERPOLATE_COSINE,false,Image::OSG_L_PF, Image::OSG_UINT8_IMAGEDATA); TextureChunkPtr TheTextureChunk = TextureChunk::create(); beginEditCP(TheTextureChunk); TheTextureChunk->setImage(PerlinNoiseImage); endEditCP(TheTextureChunk); //Lambert Material LambertMaterialPtr TheLambertMat = LambertMaterial::create(); beginEditCP(TheLambertMat, LambertMaterial::ColorFieldMask | LambertMaterial::AmbientColorFieldMask | LambertMaterial::DiffuseFieldMask | LambertMaterial::NumLightsFieldMask | LambertMaterial::DiffuseTextureFieldMask); TheLambertMat->setColor(Color3f(0.0,1.0,0.0)); TheLambertMat->setAmbientColor(Color3f(1.0,0.0,0.0)); TheLambertMat->setDiffuse(0.5); TheLambertMat->setNumLights(1); endEditCP(TheLambertMat, LambertMaterial::ColorFieldMask | LambertMaterial::AmbientColorFieldMask | LambertMaterial::DiffuseFieldMask | LambertMaterial::NumLightsFieldMask | LambertMaterial::DiffuseTextureFieldMask); //Blinn Material TheBlinnMat = BlinnMaterial::create(); beginEditCP(TheBlinnMat, BlinnMaterial::ColorFieldMask | BlinnMaterial::AmbientColorFieldMask | BlinnMaterial::DiffuseFieldMask | BlinnMaterial::SpecularColorFieldMask | BlinnMaterial::SpecularEccentricityFieldMask | BlinnMaterial::SpecularRolloffFieldMask | BlinnMaterial::DiffuseTextureFieldMask); TheBlinnMat->setColor(Color3f(1.0,0.0,0.0)); TheBlinnMat->setAmbientColor(Color3f(0.0,0.0,0.0)); TheBlinnMat->setSpecularColor(Color3f(0.0,0.0,1.0)); TheBlinnMat->setSpecularEccentricity(0.35); TheBlinnMat->setSpecularRolloff(0.85); TheBlinnMat->setDiffuse(0.65); TheBlinnMat->setDiffuseTexture(TheTextureChunk); endEditCP(TheBlinnMat, BlinnMaterial::ColorFieldMask | BlinnMaterial::AmbientColorFieldMask | BlinnMaterial::DiffuseFieldMask | BlinnMaterial::SpecularColorFieldMask | BlinnMaterial::SpecularEccentricityFieldMask | BlinnMaterial::SpecularRolloffFieldMask | BlinnMaterial::DiffuseTextureFieldMask); //Phong Material Phong2MaterialPtr ThePhongMat = Phong2Material::create(); beginEditCP(ThePhongMat, Phong2Material::ColorFieldMask | Phong2Material::AmbientColorFieldMask | Phong2Material::DiffuseFieldMask | Phong2Material::SpecularColorFieldMask | Phong2Material::SpecularCosinePowerFieldMask); ThePhongMat->setColor(Color3f(1.0,0.0,0.0)); ThePhongMat->setAmbientColor(Color3f(0.0,0.0,0.0)); ThePhongMat->setSpecularColor(Color3f(0.0,0.0,1.0)); ThePhongMat->setSpecularCosinePower(50.0); ThePhongMat->setDiffuse(0.65); endEditCP(ThePhongMat, Phong2Material::ColorFieldMask | Phong2Material::AmbientColorFieldMask | Phong2Material::DiffuseFieldMask | Phong2Material::SpecularColorFieldMask | Phong2Material::SpecularCosinePowerFieldMask); //Anisotropic Material AnisotropicMaterialPtr TheAnisotropicMat = AnisotropicMaterial::create(); beginEditCP(TheAnisotropicMat, AnisotropicMaterial::ColorFieldMask | AnisotropicMaterial::AmbientColorFieldMask | AnisotropicMaterial::DiffuseFieldMask | AnisotropicMaterial::SpecularColorFieldMask | AnisotropicMaterial::SpecularRoughnessFieldMask | AnisotropicMaterial::SpecularFresnelIndexFieldMask | AnisotropicMaterial::SpecularSpreadXFieldMask | AnisotropicMaterial::SpecularSpreadYFieldMask); TheAnisotropicMat->setColor(Color3f(1.0,0.0,0.0)); TheAnisotropicMat->setAmbientColor(Color3f(0.0,0.0,0.0)); TheAnisotropicMat->setDiffuse(0.65); TheAnisotropicMat->setSpecularColor(Color3f(0.0,0.0,1.0)); TheAnisotropicMat->setSpecularRoughness(32.0); TheAnisotropicMat->setSpecularFresnelIndex(0.85); TheAnisotropicMat->setSpecularSpreadX(1.0); TheAnisotropicMat->setSpecularSpreadY(1.0); endEditCP(TheAnisotropicMat, AnisotropicMaterial::ColorFieldMask | AnisotropicMaterial::AmbientColorFieldMask | AnisotropicMaterial::DiffuseFieldMask | AnisotropicMaterial::SpecularColorFieldMask | AnisotropicMaterial::SpecularRoughnessFieldMask | AnisotropicMaterial::SpecularFresnelIndexFieldMask | AnisotropicMaterial::SpecularSpreadXFieldMask | AnisotropicMaterial::SpecularSpreadYFieldMask); PointChunkPtr TempChunk = PointChunk::create(); //addRefCP(TempChunk); //Anisotropic Material TheRampMat = RampMaterial::create(); beginEditCP(TheRampMat); //Color TheRampMat->setRampSource(RampMaterial::RAMP_SOURCE_FACING_ANGLE); TheRampMat->getColors().push_back(Color3f(1.0,0.0,0.0)); TheRampMat->getColorPositions().push_back(0.4); TheRampMat->getColorInterpolations().push_back(RampMaterial::RAMP_INTERPOLATION_SMOOTH); TheRampMat->getColors().push_back(Color3f(0.0,1.0,0.0)); TheRampMat->getColorPositions().push_back(1.0); //Transparency TheRampMat->getTransparencies().push_back(Color3f(0.0,0.0,0.0)); TheRampMat->getTransparencyPositions().push_back(0.83); TheRampMat->getTransparencyInterpolations().push_back(RampMaterial::RAMP_INTERPOLATION_SMOOTH); TheRampMat->getTransparencies().push_back(Color3f(1.0,1.0,1.0)); TheRampMat->getTransparencyPositions().push_back(1.0); TheRampMat->setAmbientColor(Color3f(0.0,0.0,0.0)); TheRampMat->setSpecularity(1.0); TheRampMat->setSpecularEccentricity(0.8); TheRampMat->getSpecularColors().push_back(Color3f(1.0,1.0,1.0)); TheRampMat->getSpecularColorPositions().push_back(0.95); TheRampMat->getSpecularColorInterpolations().push_back(RampMaterial::RAMP_INTERPOLATION_SMOOTH); TheRampMat->getSpecularColors().push_back(Color3f(0.0,0.0,1.0)); TheRampMat->getSpecularColorPositions().push_back(1.0); TheRampMat->getSpecularRolloffs().push_back(1.0); TheRampMat->getExtraChunks().push_back(TempChunk); endEditCP(TheRampMat); //Make the Heightmap Geometry HeightmapGeometryPtr TutorialHeightmapGeo = HeightmapGeometry::create(); beginEditCP(TutorialHeightmapGeo, HeightmapGeometry::HeightImageFieldMask | HeightmapGeometry::DimensionsFieldMask | HeightmapGeometry::SegmentsFieldMask | HeightmapGeometry::ScaleFieldMask | HeightmapGeometry::OffsetFieldMask | HeightmapGeometry::MaterialFieldMask); TutorialHeightmapGeo->setHeightImage(PerlinNoiseImage); TutorialHeightmapGeo->setDimensions(Vec2f(200.0,200.0)); TutorialHeightmapGeo->setSegments(Vec2f(150.0,150.0)); TutorialHeightmapGeo->setScale(30.0); TutorialHeightmapGeo->setOffset(0.0); TutorialHeightmapGeo->setMaterial( TheBlinnMat ); endEditCP(TutorialHeightmapGeo, HeightmapGeometry::HeightImageFieldMask | HeightmapGeometry::DimensionsFieldMask | HeightmapGeometry::SegmentsFieldMask | HeightmapGeometry::ScaleFieldMask | HeightmapGeometry::OffsetFieldMask | HeightmapGeometry::MaterialFieldMask); calcVertexNormals(TutorialHeightmapGeo); calcVertexTangents(TutorialHeightmapGeo,0,Geometry::TexCoords7FieldId, Geometry::TexCoords6FieldId); //Make the Heightmap Node NodePtr TutorialHeightmapNode = Node::create(); beginEditCP(TutorialHeightmapNode, Node::CoreFieldMask); TutorialHeightmapNode->setCore(TutorialHeightmapGeo); endEditCP (TutorialHeightmapNode, Node::CoreFieldMask); //Make a SphereNode GeometryPtr SphereGeo = makeSphereGeo(2,50.0); //GeometryPtr SphereGeo = makeCylinderGeo(50,20.0, 16,true,true,true); beginEditCP(SphereGeo, Geometry::MaterialFieldMask); SphereGeo->setMaterial(TheLambertMat); endEditCP (SphereGeo, Geometry::MaterialFieldMask); calcVertexNormals(SphereGeo); calcVertexTangents(SphereGeo,0,Geometry::TexCoords7FieldId, Geometry::TexCoords6FieldId); NodePtr SphereNode = Node::create(); beginEditCP(SphereNode, Node::CoreFieldMask); SphereNode->setCore(SphereGeo); endEditCP (SphereNode, Node::CoreFieldMask); //Make Main Scene Node NodePtr scene = Node::create(); beginEditCP(scene, Node::CoreFieldMask | Node::ChildrenFieldMask); scene->setCore(Group::create()); // add the torus as a child scene->addChild(TutorialHeightmapNode); //scene->addChild(SphereNode); scene->addChild(ThePointLightBeaconNode); endEditCP (scene, Node::CoreFieldMask | Node::ChildrenFieldMask); //Add the scene to the Light Nodes //beginEditCP(ThePointLightNode, Node::ChildrenFieldMask); //ThePointLightNode->addChild(scene); //endEditCP(ThePointLightNode, Node::ChildrenFieldMask); //// tell the manager what to manage //mgr->setRoot (ThePointLightNode); beginEditCP(TheSpotLightNode, Node::ChildrenFieldMask); TheSpotLightNode->addChild(scene); endEditCP(TheSpotLightNode, Node::ChildrenFieldMask); // tell the manager what to manage mgr->setRoot (TheSpotLightNode); mgr->turnHeadlightOff(); // show the whole scene mgr->showAll(); //Open Window Vec2f WinSize(TutorialWindowEventProducer->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindowEventProducer->getDesktopSize() - WinSize) *0.5); TutorialWindowEventProducer->openWindow(WinPos, WinSize, "06Heightmap"); //Main Loop TutorialWindowEventProducer->mainLoop(); osgExit(); return 0; }