void renderSceneToImage(::osg::Node* node, const ::std::string& sFileName_,double position[3],double target[3],double up[3]) { osg::Group* root = new osg::Group(); // Declare transform, initialize with defaults. osg::PositionAttitudeTransform* nodeXform = new osg::PositionAttitudeTransform(); // Use the 'addChild' method of the osg::Group class to // add the transform as a child of the root node and the // node node as a child of the transform. root->addChild(nodeXform); { Moby::CcolorVisitor newColor; newColor.setColor(0,0,0,0); node->accept( newColor ); } nodeXform->addChild(node); if(!sceneFile.empty()){ ::osg::Node* sceneNode = osgDB::readNodeFile(sceneFile); nodeXform->addChild(sceneNode); } // Declare and initialize a Vec3 instance to change the // position of the node model in the scene osg::Vec3 nodePosit(0,0,0); nodeXform->setPosition( nodePosit ); // Declare a 'viewer' osgViewer::Viewer viewer; // Next we will need to assign the scene graph we created // above to this viewer: viewer.setSceneData( root ); viewer.setCameraManipulator(new osgGA::TrackballManipulator()); viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet())); osg::Vec3d position_osg(position[0], position[1], position[2]); osg::Vec3d target_osg(target[0], target[1], target[2]); osg::Vec3d up_osg(up[0], up[1], up[2]); osg::Vec3d view = target_osg - position_osg; // compute the up and normal vectors //osg::Quat rot; // compute the rotation from the view vector to the world up //rot.makeRotate( up_osg, view ); // #unused // find the normal vector by crossing the view and world up vectors osg::Vec3d n = view^up_osg; // find desired up vector by crossing the normal vector with the view vector osg::Vec3d up_desired = n^view; //osg::Vec3d up_new = rot * up_osg; // #unused // replace the up vector with the desired up up_osg = up_desired; // set the camera view osg::Camera* camera = viewer.getCamera(); camera->setViewMatrixAsLookAt(position_osg, target_osg, up_osg); // setup the manipulator using the camera, if necessary viewer.getCameraManipulator()->setHomePosition(position_osg, target_osg, up_osg); ::osg::ref_ptr<SnapImageDrawCallback> snapImageDrawCallback = new SnapImageDrawCallback(); camera->setPostDrawCallback (snapImageDrawCallback.get()); snapImageDrawCallback->setFileName(sFileName_); snapImageDrawCallback->setSnapImageOnNextFrame(true); // Add a Light to the scene osg::ref_ptr<osg::Group> lightGroup (new osg::Group); osg::ref_ptr<osg::StateSet> lightSS (root->getOrCreateStateSet()); osg::ref_ptr<osg::LightSource> lightSource1 = new osg::LightSource; double xCenter = 10, yCenter=10; osg::Vec4f lightPosition (osg::Vec4f(xCenter, yCenter,75,1.0f)); osg::ref_ptr<osg::Light> light = new osg::Light; light->setLightNum(1); light->setPosition(lightPosition); light->setAmbient(osg::Vec4(0.3f,0.3f,0.3f,0.4f)); light->setDiffuse(osg::Vec4(0.2f,0.2f,0.2f,0.5f)); // light->setSpecular(osg::Vec4(0.1,0.1,0.1,0.3)); // light->setConstantAttenuation(0.5f); light->setDirection(osg::Vec3(0.1f, 0.1f, -1.0f)); lightSource1->setLight(light.get()); lightSource1->setLocalStateSetModes(osg::StateAttribute::ON); lightSource1->setStateSetModes(*lightSS,osg::StateAttribute::ON); //osg::StateSet* lightSS (lightGroup->getOrCreateStateSet()); lightGroup->addChild(lightSource1.get()); //Light markers: small spheres osg::ref_ptr<osg::Geode> lightMarkerGeode (new osg::Geode); lightMarkerGeode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3f(xCenter,yCenter,75),10.0f))); //Tuto 9: lighting code // root->addChild(lightGroup.get()); //Tuto 9: Adding the light marker geode // root->addChild(lightMarkerGeode.get()); viewer.realize(); int x,y,width,height; x = camera->getViewport()->x(); y = camera->getViewport()->y(); width = (WIDTH != 0)? WIDTH : camera->getViewport()->width(); height = (HEIGHT != 0)? HEIGHT : camera->getViewport()->height(); // ::osg::notify(::osg::NOTICE) << "Capturing image from: (" << x << ", " << y<< ") " <<width<< " x "<< height << std::endl; // Prevent this from opening a window by making pbuffer context // osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; // traits->x = 0; // traits->y = 0; // traits->width = width; // traits->height = height; // traits->red = 8; // traits->green = 8; // traits->blue = 8; // traits->alpha = 8; // traits->windowDecoration = false; // traits->pbuffer = true; // traits->doubleBuffer = true; // traits->sharedContext = 0; // osg::ref_ptr<osg::GraphicsContext> pbuffer; // pbuffer = ::osg::GraphicsContext::createGraphicsContext(traits.get()); // if (pbuffer.valid()) // { // ::osg::notify(osg::NOTICE)<<"Pixel buffer has been created successfully."<<std::endl; // } // else // { // ::osg::notify(osg::NOTICE)<<"Pixel buffer has not been created successfully."<<std::endl; // } // if (pbuffer.valid()) // { // osg::ref_ptr<osg::Camera> camera = new osg::Camera; // camera->setGraphicsContext(pbuffer.get()); // camera->setViewport(new osg::Viewport(0,0,width,height)); // GLenum buffer = pbuffer->getTraits()->doubleBuffer ? GL_BACK : GL_FRONT; // camera->setDrawBuffer(buffer); // camera->setReadBuffer(buffer); //// camera->setFinalDrawCallback(new WindowCaptureCallback(mode, position, readBuffer)); // camera->setFinalDrawCallback(snapImageDrawCallback.get()); // viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); viewer.realize(); viewer.frame(); // } }
int main() { osgViewer::Viewer viewer; osg::ref_ptr<osg::Group> root (new osg::Group); osg::ref_ptr<osg::PositionAttitudeTransform> objectPat (new osg::PositionAttitudeTransform); osg::ref_ptr<osg::PositionAttitudeTransform> quadPat (new osg::PositionAttitudeTransform); osg::ref_ptr<osg::MatrixTransform> terrainScaleMAT (new osg::MatrixTransform); osg::Matrix terrainScaleMatrix; terrainScaleMatrix.makeScale(0.05f,0.05f,0.03f); osg::Vec3f objectPosTrans = osg::Vec3f(-1,3,5); osg::Vec3f quadPos = osg::Vec3f(5,0,0.5f); osg::Vec3f quadPos2 = osg::Vec3f(-5,0,0); //osg::Vec3f terrainScale = osg::Vec3f(0.5f,0.5f,0.5f); //Tuto9: Lighting code osg::ref_ptr<osg::Group> lightGroup (new osg::Group); osg::ref_ptr<osg::StateSet> lightSS (root->getOrCreateStateSet()); osg::ref_ptr<osg::LightSource> lightSource1 = new osg::LightSource; osg::ref_ptr<osg::LightSource> lightSource2 = new osg::LightSource; // create a local light. osg::Vec4f lightPosition (osg::Vec4f(-5.0,-2.0,3.0,1.0f)); osg::ref_ptr<osg::Light> myLight = new osg::Light; myLight->setLightNum(1); myLight->setPosition(lightPosition); myLight->setAmbient(osg::Vec4(0.2f,0.2f,0.2f,1.0f)); myLight->setDiffuse(osg::Vec4(0.8f,0.8f,0.8f,1.0f)); myLight->setConstantAttenuation(1.0f); lightSource1->setLight(myLight.get()); lightSource1->setLocalStateSetModes(osg::StateAttribute::ON); lightSource1->setStateSetModes(*lightSS,osg::StateAttribute::ON); //osg::StateSet* lightSS (lightGroup->getOrCreateStateSet()); // create a local light. osg::Vec4f lightPosition2 (osg::Vec4f(2.0,-1.0,3.0,1.0f)); osg::ref_ptr<osg::Light> myLight2 = new osg::Light; myLight2->setLightNum(0); myLight2->setPosition(lightPosition2); myLight2->setAmbient(osg::Vec4(0.2f,0.2f,0.2f,1.0f)); myLight2->setDiffuse(osg::Vec4(0.8f,0.1f,0.1f,1.0f)); myLight2->setConstantAttenuation(1.0f); lightSource2->setLight(myLight2.get()); lightSource2->setLocalStateSetModes(osg::StateAttribute::ON); lightSource2->setStateSetModes(*lightSS,osg::StateAttribute::ON); lightGroup->addChild(lightSource1.get()); lightGroup->addChild(lightSource2.get()); //Light markers: small spheres osg::ref_ptr<osg::Geode> lightMarkerGeode (new osg::Geode); lightMarkerGeode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3f(-5.0f,-2.0f,3.0f),0.5f))); //lightMarkerGeode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF); //Second light marker lightMarkerGeode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3f(2.0f,-1.0f,3.0f),0.5f))); //The geode of the capsule osg::ref_ptr<osg::Geode> myshapegeode (new osg::Geode); objectPat->addChild(myshapegeode.get()); objectPat->setPosition(objectPosTrans); //quadPat->addChild(myQuad().get()); quadPat->setPosition(quadPos); myshapegeode->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3f(),1,2))); //Getting the state set of the geode osg::ref_ptr<osg::StateSet> nodess (myshapegeode->getOrCreateStateSet()); //loading texture image object osg::ref_ptr<osg::Image> image (osgDB::readImageFile("Fieldstone.png")); //Bind the image to a 2D texture object osg::ref_ptr<osg::Texture2D> tex (new osg::Texture2D); tex->setImage(image.get()); //Release the image memory on the GPU after using it! //tex->setUnRefImageDataAfterApply(true); //Applying texture on the object nodess->setTextureAttributeAndModes(0,tex.get(),osg::StateAttribute::ON); //Loading the terrain node osg::ref_ptr<osg::Node> terrainnode (osgDB::readNodeFile("JoeDirt.flt")); //osg::ref_ptr<osg::Node> terrainnode (osgDB::readNodeFile("Terrain2.3ds")); terrainScaleMAT->addChild(terrainnode.get()); terrainScaleMAT->setMatrix(terrainScaleMatrix); //Tutorial 11: Billboarding stuff osg::ref_ptr<osg::Billboard> quadBillBoard = new osg::Billboard(); osg::ref_ptr<osg::StateSet> billSS (quadBillBoard->getOrCreateStateSet()); //Adding texture to the billboards osg::ref_ptr<osg::Image> image1 (osgDB::readImageFile("foo.png")); if (image1.get() == 0) { std::cerr << "Error loading 'foo.png'.\n"; exit (EXIT_FAILURE); } osg::ref_ptr<osg::Texture2D> texture (new osg::Texture2D); texture->setImage (image1.get()); billSS->setTextureAttributeAndModes (0, // unit texture.get(), osg::StateAttribute::ON); root->addChild(quadBillBoard.get()); quadBillBoard->setMode(osg::Billboard::AXIAL_ROT); quadBillBoard->setAxis(osg::Vec3(0.0f,0.0f,1.0f)); quadBillBoard->setNormal(osg::Vec3(0.0f,-1.0f,0.0f)); quadBillBoard->addDrawable(myQuad().get(),quadPos); quadBillBoard->addDrawable(myQuad().get(),quadPos2); //adding the terrain node to the root node //root->addChild(myQuad().get()); //root->addChild(quadPat.get()); root->addChild(objectPat.get()); root->addChild(terrainScaleMAT.get()); //Tuto 9: lighting code root->addChild(lightGroup.get()); //Tuto 9: Adding the light marker geode root->addChild(lightMarkerGeode.get()); //Adding the fog to the root node //root->setStateSet(setFogState().get()); // add the state manipulator viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); //Stats Event Handler s key viewer.addEventHandler(new osgViewer::StatsHandler); //Windows size handler viewer.addEventHandler(new osgViewer::WindowSizeHandler); //Threading Handler activate with the 'm' key viewer.addEventHandler(new osgViewer::ThreadingHandler); // run optimization over the scene graph osgUtil::Optimizer optimzer; optimzer.optimize(root.get()); viewer.setSceneData( root.get() ); return (viewer.run()); }
osg::ref_ptr<osg::Node> creerLaScene() { /* OBJECTS CREATION */ //Creating the root node osg::ref_ptr<osg::Group> root (new osg::Group); /*PION*/ //The geode containing our shape //osg::ref_ptr<osg::Geode> geodePion (new osg::Geode); //La shape du Pion principal osg::ref_ptr<osg::Cylinder> monPion (new osg::Cylinder(osg::Vec3f(0,0,1),2,1)); //std::vector<osg::ref_ptr<osg::Geode>> geodePions; //std::vector<osg::ref_ptr<osg::PositionAttitudeTransform>> PATPions; for (int i=0; i<13; i++) { //The geode containing our shape osg::ref_ptr<osg::Geode> geodePion (new osg::Geode); //La shape drawable du pion osg::ref_ptr<osg::ShapeDrawable> pionDrawable (new osg::ShapeDrawable(monPion.get())); geodePion->addDrawable(pionDrawable.get()); osg::ref_ptr<osg::PositionAttitudeTransform> PATPion (new osg::PositionAttitudeTransform); osg::ref_ptr<osg::StateSet> pionSS (geodePion->getOrCreateStateSet()); /*Materiaux des pions*/ osg::ref_ptr<osg::Texture2D> bois_blanc (new osg::Texture2D); // load an image by reading a file: osg::ref_ptr<osg::Image> img_bois_blanc (osgDB::readImageFile("/Users/willispinaud/Desktop/kch-w.gif")); // protect from being optimized away as static state: bois_blanc->setDataVariance(osg::Object::DYNAMIC); // Assign the texture to the image we read from file: bois_blanc->setImage(img_bois_blanc); // Assign texture unit 0 of our new StateSet to the texture // we just created and enable the texture. pionSS->setTextureAttributeAndModes(0, bois_blanc,osg::StateAttribute::ON); // Associate this state set geodePion->setStateSet(pionSS); if (i<4) { osg::Vec3f pionPosTrans = osg::Vec3f(i*965%(965*4)/100,0.0,0.0); PATPion->setPosition(pionPosTrans); PATPion->addChild(geodePion.get()); root->addChild(PATPion.get()); } else if (i>=9) { osg::Vec3f pionPosTrans = osg::Vec3f(5.0f+i*965%(965*4)/100,5.0,0.0); PATPion->setPosition(pionPosTrans); PATPion->addChild(geodePion.get()); root->addChild(PATPion.get()); } else { osg::Vec3f pionPosTrans = osg::Vec3f(i*965%(965*4)/100,10,0.0); PATPion->setPosition(pionPosTrans); PATPion->addChild(geodePion.get()); root->addChild(PATPion.get()); } } for (int i=0; i<13; i++) { //The geode containing our shape osg::ref_ptr<osg::Geode> geodePion (new osg::Geode); //La shape drawable du pion osg::ref_ptr<osg::ShapeDrawable> pionDrawable (new osg::ShapeDrawable(monPion.get())); geodePion->addDrawable(pionDrawable.get()); osg::ref_ptr<osg::PositionAttitudeTransform> PATPion (new osg::PositionAttitudeTransform); osg::ref_ptr<osg::StateSet> pionSS (geodePion->getOrCreateStateSet()); /*Materiaux des pions*/ osg::ref_ptr<osg::Texture2D> bois_noir (new osg::Texture2D); // load an image by reading a file: osg::ref_ptr<osg::Image> img_bois_noir (osgDB::readImageFile("/Users/willispinaud/Desktop/kch-clr.gif")); // protect from being optimized away as static state: bois_noir->setDataVariance(osg::Object::DYNAMIC); // Assign the texture to the image we read from file: bois_noir->setImage(img_bois_noir); // Assign texture unit 0 of our new StateSet to the texture // we just created and enable the texture. pionSS->setTextureAttributeAndModes(0, bois_noir,osg::StateAttribute::ON); // Associate this state set geodePion->setStateSet(pionSS); if (i<4) { osg::Vec3f pionPosTrans = osg::Vec3f(5.0f+i*965%(965*4)/100,25.0,0.0); PATPion->setPosition(pionPosTrans); PATPion->addChild(geodePion.get()); root->addChild(PATPion.get()); } else if (i>=9) { osg::Vec3f pionPosTrans = osg::Vec3f(i*965%(965*4)/100,30.0,0.0); PATPion->setPosition(pionPosTrans); PATPion->addChild(geodePion.get()); root->addChild(PATPion.get()); } else { osg::Vec3f pionPosTrans = osg::Vec3f(5.0f+i*965%(965*4)/100,35,0.0); PATPion->setPosition(pionPosTrans); PATPion->addChild(geodePion.get()); root->addChild(PATPion.get()); } } /*TABLE*/ //The geode containing our shape osg::ref_ptr<osg::Geode> geodeTable (new osg::Geode); //La shape de la table de jeu principale osg::ref_ptr<osg::Box> maTable (new osg::Box(osg::Vec3f(17.5,17.5,0),40,40,1)); //La shape drawable de la table osg::ref_ptr<osg::ShapeDrawable> tableDrawable (new osg::ShapeDrawable(maTable.get())); // /*State Set*/ // // Create a new StateSet with default settings: // //osg::ref_ptr<osg::StateSet> pionSS (geodePion->getOrCreateStateSet()); osg::ref_ptr<osg::StateSet> tableSS (geodeTable->getOrCreateStateSet()); // // // /*POSITION*/ // osg::ref_ptr<osg::PositionAttitudeTransform> objectPAT (new osg::PositionAttitudeTransform); // // osg::Vec3f objectPosTrans = osg::Vec3f(2.0,2.0,2.0); // objectPAT->setPosition(objectPosTrans); // objectPAT->addChild(geodePion.get()); // // objectPAT->setPosition(osg::Vec3f(4.0,4.0,0.0)); // objectPAT->addChild(geodePion.get()); // // /* IMPORT*/ //Create transformation node osg::ref_ptr<osg::MatrixTransform> terrainScaleMAT (new osg::MatrixTransform); // Scale matrix osg::Matrix terrainScaleMatrix; terrainScaleMatrix.makeScale(osg::Vec3f(0.1f,0.1f,0.1f)); //Loading the terrain node osg::ref_ptr<osg::Node> terrainnode (osgDB::readNodeFile("/Users/willispinaud/Documents/Unzip/273/modell.3DS")); //Set transformation node parameters terrainScaleMAT->addChild(terrainnode); terrainScaleMAT->setMatrix(terrainScaleMatrix); osg::ref_ptr<osg::PositionAttitudeTransform> envPAT (new osg::PositionAttitudeTransform); envPAT->addChild(terrainScaleMAT); envPAT->setPosition(osg::Vec3(-20,210,-37)); root->addChild(envPAT); // /* Matériaux */ // osg::ref_ptr<osg::Material> myMaterial (new osg::Material()); // //myMaterial->setEmission(osg::Material::FRONT,osg::Vec4f(0.93f,0.12f,0.031f,1.0f)); // //myMaterial->setAmbient(osg::Material::FRONT,osg::Vec4f(0.0f,1.0f,1.0f,1.0f)); // //myMaterial->setShininess(osg::Material::FRONT,0.0); // //nodeStateSet->setAttribute(myMaterial); // // /*Materiaux de la table*/ osg::ref_ptr<osg::Texture2D> table (new osg::Texture2D); // load an image by reading a file: osg::ref_ptr<osg::Image> img_table (osgDB::readImageFile("/Users/willispinaud/Desktop/damier.jpg")); // protect from being optimized away as static state: table->setDataVariance(osg::Object::DYNAMIC); // Assign the texture to the image we read from file: table->setImage(img_table); //nodeStateSet->setAttribute(myMaterial); // Assign texture unit 0 of our new StateSet to the texture // we just created and enable the texture. tableSS->setTextureAttributeAndModes(0, table,osg::StateAttribute::ON); // Associate this state set geodeTable->setStateSet(tableSS); /*LIGHTING*/ //Create nodes osg::ref_ptr<osg::Group> lightGroup (new osg::Group()); osg::ref_ptr<osg::StateSet> lightSS (lightGroup->getOrCreateStateSet()); osg::ref_ptr<osg::LightSource> lightSource1 = new osg::LightSource; //Create a local light osg::Vec4f lightPosition = osg::Vec4f(10.0,1.0,1.0,1.0); osg::ref_ptr<osg::Light> myLight = new osg::Light; myLight->setLightNum(0); myLight->setPosition(lightPosition); myLight->setAmbient(osg::Vec4f(0.4f,0.4f,0.4f,0.4f)); myLight->setDiffuse(osg::Vec4f(1,0.4f,1,1)); myLight->setConstantAttenuation(0.3); //Set light source parameters lightSource1->setLight(myLight); lightSource1->setStateSetModes(*lightSS.get(), osg::StateAttribute::ON); //Add to light source group lightGroup->addChild(lightSource1); root->addChild(lightGroup); //Light markers: small spheres // /* SCENE GRAPH*/ // // Add the shape drawable to the geode //geodePion->addDrawable(pionDrawable.get()); osg::ref_ptr<osg::Geode> animPion (new osg::Geode); osg::ref_ptr<osg::ShapeDrawable> pionAnimDrawable (new osg::ShapeDrawable(monPion.get())); animPion->addDrawable(pionAnimDrawable.get()); // Ajout d'un pion dans un groupe MatrixTransform osg::MatrixTransform* pMatTrans = new osg::MatrixTransform; pMatTrans->addChild( animPion ); // Création d'un Animation Path osg::ref_ptr<osg::AnimationPath> rPath = new osg::AnimationPath; // Définition du mode de boucle rPath->setLoopMode( osg::AnimationPath::SWING ); // Création de point de contrôle osg::AnimationPath::ControlPoint c0(osg::Vec3(0,0,0)); osg::AnimationPath::ControlPoint c1(osg::Vec3( 0,10,0)); osg::AnimationPath::ControlPoint c2(osg::Vec3( 0,9,4)); osg::AnimationPath::ControlPoint c3(osg::Vec3( 0,0,4)); rPath->insert( 0.0f, c0 ); //rPath->insert( 2.0f, c2 ); rPath->insert( 1.0f, c1 ); // rPath->insert( 6.0f, c3 ); // rPath->insert( 8.0f, c0 ); // Mise en place du callback (rappel) osg::ref_ptr<osg::AnimationPathCallback> rAniCallback = new osg::AnimationPathCallback( rPath.get() ); pMatTrans->setUpdateCallback( rAniCallback.get() ); // // Add the geode to the scene graph root (Group) // root->addChild(geodePion.get()); geodeTable->addDrawable(tableDrawable); osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform; mt->addChild( geodeTable.get() ); osg::ref_ptr<ModelController> ctrler = new ModelController( mt.get() ); root->addChild(pMatTrans); root->addChild(geodeTable); // // // // Add the PAT to the scene graph root (Group) // root->addChild(objectPAT.get()); // // //root->addChild(terrainScaleMAT.get()); //For the import // root->addChild(lightGroup.get()); return root; }