osg::Group* group = new osg::Group;
osg::Node* node = osgDB::readNodeFile("D:\\cow.osg");
osg::Camera* camera = new osg::Camera;
camera->setViewport(0, 0, 256, 256);
osg::Image* img = new osg::Image;
img->allocateImage(256, 256, 1, GL_RGBA, GL_FLOAT);
osg::Texture2D* texture2D = new osg::Texture2D;
texture2D->setTextureSize(256, 256);
texture2D->setInternalFormat(GL_RGBA);
//camera->attach(Camera::COLOR_BUFFER, texture2D, 0, 0);
camera->attach(Camera::COLOR_BUFFER, img, 0, 0);
texture2D->setImage(0, img);
const osg::BoundingSphere& bs = node->getBound();//osg::Group = new osg::Group
float znear = 1.0f*bs.radius();
float zfar = 3.0f*bs.radius();
float proj_top = 0.25f*znear;
float proj_right = 0.5f*znear;
znear *= 0.9f;
zfar *= 1.1f;
// set up projection.
camera->setProjectionMatrixAsFrustum(-proj_right,proj_right,-proj_top,proj_top,znear,zfar);
// set view //set up projection
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setViewMatrixAsLookAt(bs.center()-osg::Vec3(0.0f,2.0f,0.0f)*bs.radius(),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));
camera->setRenderOrder(osg::Camera::PRE_RENDER);
camera->setRenderTargetImplementation(Camera::FRAME_BUFFER_OBJECT);
camera->addChild(node);
group->addChild(camera);
osg::Geometry* geometry = new osg::Geometry;
osg::Vec3Array* vertex = new osg::Vec3Array;
osg::Vec2Array* coord = new osg::Vec2Array;
vertex->push_back(osg::Vec3(0.0, 0.0, 0.0));
vertex->push_back(osg::Vec3(10.0, 0.0, 0.0));
vertex->push_back(osg::Vec3(10.0, 10.0, 0.0));
vertex->push_back(osg::Vec3(0.0, 10.0, 0.0));
coord->push_back(osg::Vec2(0.0, 0.0));
coord->push_back(osg::Vec2(1.0, 0.0));
coord->push_back(osg::Vec2(1.0, 1.0));
coord->push_back(osg::Vec2(0.0, 1.0));
geometry->setTexCoordArray(0,coord);
geometry->setVertexArray(vertex);
geometry->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::QUADS, 0, vertex->size() ));
osg::Geode* geode = new osg::Geode;
geode->addDrawable(geometry);
osg::StateSet* statsset = new osg::StateSet;
statsset->setTextureAttributeAndModes(0, texture2D,osg::StateAttribute::ON);
geometry->setStateSet(statsset);
group->addChild(geode);
osgViewer::Viewer viewer;
viewer.setSceneData(group);
return viewer.run();