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()); }
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(); // } }