|
请大家帮忙看下,同样的两个RTT,显示同一个模型时,只能显示第一个,第二个不能正确显示(只是一片蓝)。而两个显示不同的模型时则都能显示。
class TextureVisitor : public osg::NodeVisitor
{
public:
TextureVisitor():
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
{
tex2D =NULL ;
}
virtual void apply(osg::Node& node)
{
if(node.getStateSet())
{
apply(node.getStateSet());
}
traverse(node);
}
virtual void apply(osg::Geode& geode)
{
if(geode.getStateSet())
{
apply(geode.getStateSet());
}
unsigned int cnt = geode.getNumDrawables();
for(unsigned int i =0;i<cnt ;i++)
{
apply(geode.getDrawable(i)->getStateSet());
}
traverse(geode);
}
void apply(osg::StateSet* state)
{
osg::StateSet::TextureAttributeList& texAttribList =
state->getTextureAttributeList();
tex2D =dynamic_cast<osg::Texture2D*>(state->getTextureAttribute(0,osg::StateAttribute::TEXTURE));
}
}
std::map<std::string ,osg::Image*>& getImage(void)
{
return _imageList;
}
osg::Texture2D* getTexture2D(void)
{
return tex2D;
}
protected:
std::map<std::string ,osg::Image*> _imageList;
osg::Texture2D* tex2D ;
};
osg::ref_ptr<osg::Camera> camera[10] ;
osg::Camera* createRttCamera0(const osg::BoundingSphere& bs)
{
camera[0] =new osg::Camera;
camera[0]->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera[0]->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera[0]->setViewport(0,0,256,256);
camera[0]->setRenderOrder(osg::Camera:RE_RENDER);
camera[0]->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
double viewDistance =2.0* bs.radius();
double znear =viewDistance -bs.radius();
double zfar =viewDistance + bs.radius();
float top =0.6 *znear ;
float right =0.8* znear;
camera[0]->setProjectionMatrixAsFrustum(-right,right,-top,top,znear,zfar);
osg::Vec3d upDirection(0.0,0.0,1.0);
osg::Vec3d viewDirection(0.0,-1.0,0.0);
osg::Vec3d center =bs.center();
osg::Vec3d eyePoint =center +viewDirection * viewDistance ;
camera[0]->setViewMatrixAsLookAt(eyePoint ,center ,upDirection);
return camera[0].release();
}
osg::Camera* createRttCamera1(const osg::BoundingSphere& bs)
{
camera[1] =new osg::Camera;
camera[1]->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera[1]->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera[1]->setViewport(0,0,256,256);
camera[1]->setRenderOrder(osg::Camera::PRE_RENDER);
camera[1]->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
double viewDistance =2.0* bs.radius();
double znear =viewDistance -bs.radius();
double zfar =viewDistance + bs.radius();
float top =0.6 *znear ;
float right =0.8* znear;
camera[1]->setProjectionMatrixAsFrustum(-right,right,-top,top,znear,zfar);
osg::Vec3d upDirection(0.0,0.0,1.0);
osg::Vec3d viewDirection(0.0,-1.0,0.0);
osg::Vec3d center =bs.center();
osg::Vec3d eyePoint =center +viewDirection * viewDistance ;
camera[1]->setViewMatrixAsLookAt(eyePoint ,center ,upDirection);
return camera[1].release();
}
int main()
{
osgViewer::Viewer* viewer =new osgViewer::Viewer();
osg::Group* root =new osg::Group();
osg::Node* node =new osg::Node();
node =osgDB::readNodeFile("aa.ive");//我自己的模型,RTT相机纹理帖子该模型的两块地方。
TextureVisitor textureTV1;
node->asGroup()->getChild(0)->accept(textureTV1);
TextureVisitor textureTV2;
node->asGroup()->getChild(1)->accept(textureTV2);
osg::Texture2D* texture1 = dynamic_cast<osg::Texture2D*>(textureTV1.getTexture2D());
osg::Texture2D* texture2 = dynamic_cast<osg::Texture2D*>(textureTV2.getTexture2D());
osg::Node* cownode1 =osgDB::readNodeFile("cow.osg");//如果再定义个节点,读取飞机模型,然后给camera[1]。则两个RTT都能正确显示。
cownode1->getOrCreateStateSet()->setMode(GL_LIGHTING ,osg::StateAttribute::OFF);
camera[0]=createRttCamera0(cownode1->getBound());
camera[1] =createRttCamera1(cownode1->getBound());
camera[0]->addChild(cownode1);
camera[0]->attach(osg::Camera::COLOR_BUFFER ,texture1);
camera[1]->addChild(cownode1);
camera[1]->attach(osg::Camera::COLOR_BUFFER ,texture2);
root->addChild(camera[0]);
root->addChild(camera[1]);
root->addChild(node);
osgUtil::Optimizer optimizer;
optimizer.optimize(root);
viewer->setSceneData(root);
viewer->realize();
viewer->run();
return 0;
}
|
|