|
使用下面的代码,得到的纹理是个蓝色背景的纹理,但是里面没有结点的数据。请问为什么啊?
main.cpp:
osg::Group *group=new osg::Group;
osg::Node *node1=osgDB::readnodefile("*******");
group->setUseData(node1);
group->addCullCallback(new MaskCallback);
osg::Group *root=new osg::Group;
root->addchild(group);
viewer.setSceneData(root);
..............
MaskCallback.cpp:
#include "MaskCallback.h"
MaskCallback::MaskCallback(void)
{
m_Camera=renderToTexturePass();
_isFirst=false;
m_Rtttex=NULL;
m_Camera->setRenderingCache(NULL);
}
MaskCallback::~MaskCallback(void)
{
}
osg::Texture2D*MaskCallback:: createTexture2D( const osg::Vec2s& size, GLint format )
{
osg::Texture2D* texture = new osg::Texture2D;
texture->setTextureSize(size.x(), size.y());
texture->setInternalFormat(format);
texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D:INEAR_MIPMAP_LINEAR);
texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE );
texture->setDataVariance(osg::Object:YNAMIC);
return texture;
}
osg::Camera *MaskCallback::renderToTexturePass()
{
osg::Camera* camera = new osg::Camera;
camera->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
camera->setClearColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.f) );
camera->setRenderOrder(osg::Camera:RE_RENDER);
camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);//设置为FBO相机,工作内容为将子场景渲染到指定的纹理对象
camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
return camera;
}
void MaskCallback:perator()(osg::Node* node, osg::NodeVisitor* nv)
{
if( nv->getVisitorType() == osg::NodeVisitor::CULL_VISITOR )
{
osg::ref_ptr<osgUtil::CullVisitor> cv = static_cast<osgUtil::CullVisitor*>(nv);
osg::ref_ptr<osgUtil::RenderStage> renderStage=cv->getRenderStage();
osg::ref_ptr<osg::Camera> camera=renderStage->getCamera();
osg::ref_ptr<osg::StateSet> nodeStateSet=node->getOrCreateStateSet();
osg::Vec3f centre,up,eye;
camera->getViewMatrixAsLookAt(eye,centre,up);
osg::Matrix localCoordMat = osg::computeLocalToWorld( cv->getNodePath());
m_Camera->setProjectionMatrix(camera->getProjectionMatrix());
m_Camera->setViewMatrix(localCoordMat*(camera->getViewMatrix()));
static osg::ref_ptr<osg::Viewport> viewport=camera->getViewport();
float width=renderStage->getViewport()->width();
float height=renderStage->getViewport()->height();
static int _viewportWidth=width;
static int _viewportHeight=height;
if (!m_Rtttex.valid())
{
static osg::ref_ptr<osg::Viewport> s_viewport=new osg::Viewport(0,0,_viewportWidth,_viewportHeight);
m_Camera->setViewport(s_viewport);
m_Rtttex=createTexture2D(osg::Vec2s(width,height),GL_RGBA);
m_Camera->attach(osg::Camera::COLOR_BUFFER,m_Rtttex);//指定纹理对象
/************************************************************************/
/* */
/************************************************************************/
node->getOrCreateStateSet()->setTextureAttributeAndModes(2,m_Rtttex,osg::StateAttribute::ON);
osg::Uniform* maskTexture=node->getOrCreateStateSet()->getUniform("MaskTexture");//看是否有maskTexture
if(maskTexture)
maskTexture->set(2);
else
node->getOrCreateStateSet()->addUniform(new osg::Uniform("MaskTexture",2));
}
if(!_isFirst)
{
osg::Node* maskData=dynamic_cast<osg::Node*>(node->getUserData());//water.ive
m_Camera->addChild(maskData);
osg::Group* group=dynamic_cast<osg::Group*>(node);
osg::Node::ParentList parents=m_Camera->getParents();
if(parents.size()==0)
group->addChild(m_Camera.get());
_isFirst=true;
}
}
traverse(node, nv);
} |
|