查看: 781|回复: 1

为什么我不能将NODE渲染到纹理上?

[复制链接]

该用户从未签到

发表于 2014-5-14 17:14:03 | 显示全部楼层 |阅读模式
使用下面的代码,得到的纹理是个蓝色背景的纹理,但是里面没有结点的数据。请问为什么啊?
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);

}

该用户从未签到

发表于 2014-5-16 18:15:56 | 显示全部楼层
请参考例子osgprerender的做法,我没有细看,但是不知道您为什么要在callback中每帧都重新attach对象
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表