[osg-users] 回复: Is it possible to load texture without applying to adrawable

Garfield Pig yuqicxy at foxmail.com
Thu May 9 09:35:50 PDT 2019


Hi Steve,
I meet the similar question yesterday,how about use osg::texture::apply method with a new state under a new GraphicContext.


this is the source of my idea.


In osg3.6.3 osgConv application.
osgConv.cpp line 152


void compress()
    {
        MyGraphicsContext context;
        if (!context.valid())
        {
            osg::notify(osg::NOTICE)<<"Error: Unable to create graphis context, problem with running osgViewer-"<<osgViewerGetVersion()<<", cannot run compression."<<std::endl;
            return;
        }


        osg::ref_ptr<osg::State> state = new osg::State;
        state->initializeExtensionProcs();


        for(TextureSet::iterator itr=_textureSet.begin();
            itr!=_textureSet.end();
            ++itr)
        {
            osg::Texture* texture = const_cast<osg::Texture*>(itr->get());


            osg::Texture2D* texture2D = dynamic_cast<osg::Texture2D*>(texture);
            osg::Texture3D* texture3D = dynamic_cast<osg::Texture3D*>(texture);


            osg::ref_ptr<osg::Image> image = texture2D ? texture2D->getImage() : (texture3D ? texture3D->getImage() : 0);
            if (image.valid() &&
                (image->getPixelFormat()==GL_RGB || image->getPixelFormat()==GL_RGBA) &&
                (image->s()>=32 && image->t()>=32))
            {
                texture->setInternalFormatMode(_internalFormatMode);


                // need to disable the unref after apply, otherwise the image could go out of scope.
                bool unrefImageDataAfterApply = texture->getUnRefImageDataAfterApply();
                texture->setUnRefImageDataAfterApply(false);


                // get OpenGL driver to create texture from image.
                texture->apply(*state);


                // restore the original setting
                texture->setUnRefImageDataAfterApply(unrefImageDataAfterApply);


                image->readImageFromCurrentTexture(0,true);


                texture->setInternalFormatMode(osg::Texture::USE_IMAGE_DATA_FORMAT);
            }
        }
    }





Thank you!


Cheers,
Qi Yu



------------------ 原始邮件 ------------------
发件人: "Steve Powers"<stevenapowers at gmail.com>;
发送时间: 2019年5月9日(星期四) 晚上11:33
收件人: "osg-users"<osg-users at lists.openscenegraph.org>;

主题: [osg-users] Is it possible to load texture without applying to adrawable



I am attempting to load a Texture2D into memory in order to pass the texture ID to a 3rd party OpenGL object that will use the ID within it's own drawable class.


The issue is that the TextureObject attached to the texture is not getting instantiated and I think this is because it is not assigned to a drawable object and thus not getting visited.




Instead, I set the texture to a stateset on an osg::Group node 




stateset = group->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);




Is there a way to have this texture object instantiate without physically using the texture on a drawable?


Thank you!


Cheers,
Steven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20190510/70b65c2a/attachment.html>


More information about the osg-users mailing list