Hi,
using OSG 2.2

I'm currently rendering to a texture using a FBO with an attached texture:

Code:

_texture = new osg::Texture2D();
_texture->setTextureSize((int)_camera->getViewport()->width(),(int)_camera->getViewport()->height());
_texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
_texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
_camera->attach(osg::Camera::COLOR_BUFFER, _texture.get(), 0,0,false);
_camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
_camera->setRenderOrder(osg::Camera::PRE_RENDER);




A 3rd party lib processes the texture in a postCallBack call:


Code:

virtual void operator()(const osg::Camera &cam) const
{
if (cam.isRenderToTextureCamera())
{
osg::Camera::BufferAttachmentMap bam = cam.getBufferAttachmentMap();
osg::Camera::BufferAttachmentMap::iterator iter = 
bam.find(osg::Camera::COLOR_BUFFER);
if (iter != bam.end())
{
osg::Texture *tex = iter->second._texture.get();
if (tex)
{
osg::Texture::TextureObject *texObj = ((osg::Texture2D 
*)tex)->getTextureObject(0);
int id = ((osg::Texture2D *)tex)->getTextureObject(0)->_id;

//do some processing here

copyToImage(cam, tex);
} 
}




My copyToImage() method creates a PBO to the texture and attempt to dump the 
memory into an associated osg::Image (really just a block of memory). This code 
was used to pull from the frame buffer using ReadPixels() which worked fine.


Code:


                void copyToImage(const osg::Camera &camera, osg::Texture* tex = 
NULL) const
                {

                        if (image.valid() && image->data())
                        {
                                int pixel_depth_bytes;
                                OpenThreads::ScopedLock<OpenThreads::Mutex> 
lock(_mutex);

                                const osg::GraphicsContext *gc = 
camera.getGraphicsContext();
                                osg::BufferObject::Extensions* ext = 
osg::BufferObject::getExtensions(gc->getState()->getContextID(), true);
                                if (ext->isPBOSupported())
                                {
                                        if (gc->getTraits())
                                        {
                                                int width = 
(int)camera.getViewport()->width();//gc->getTraits()->width;
                                                int height = 
(int)camera.getViewport()->height();//gc->getTraits()->height;
                                                int x = 
(int)camera.getViewport()->x();
                                                int y = 
(int)camera.getViewport()->y();
                                                pixel_depth_bytes = 
gc->getTraits()->depth / 8;

                                                if (width != _width || height 
!= _height || x != _x || y != _y)
                                                {
                                                        _width = width;
                                                        _height = height;
                                                        _x = x;
                                                        _y = y;
                                                        if (_pbo)
                                                        {
                                                                
ext->glDeleteBuffers(1,&_pbo);
                                                                _pbo = 0;
                                                        }
                                                }
                                        }

                                        //disable clamping
                                        osg::ClampColor::Extensions *clamp_ext 
= osg::ClampColor::getExtensions(gc->getState()->getContextID(), true);
                                        if (clamp_ext->isClampColorSupported())
                                        {
                                                clamp_ext->glClampColor( 
GL_CLAMP_READ_COLOR, GL_FALSE );
                                                clamp_ext->glClampColor( 
GL_CLAMP_VERTEX_COLOR, GL_FALSE );
                                                clamp_ext->glClampColor( 
GL_CLAMP_FRAGMENT_COLOR, GL_FALSE );
                                        }

                                        glReadBuffer(GL_BACK);

                                        //configure pbo
                                        unsigned int data_size = _width * 
_height * sizeof(float) * 4;
                                        if (_pbo)
                                        {
                                                //bind for writing (pack)
                                                
ext->glBindBuffer(GL_PIXEL_PACK_BUFFER, _pbo);
                                        } else
                                        {
                                                //create and bind for writing 
(pack)
                                                ext->glGenBuffers(1, &_pbo);
                                                
ext->glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, _pbo);
                                                
ext->glBufferData(GL_PIXEL_PACK_BUFFER_ARB, data_size, NULL, GL_STREAM_READ);
                                        }

                                        //read data
                                        if (tex) 
glGetTexImage(tex->getTextureTarget(), 0, 
osg::Image::computePixelFormat(tex->getInternalFormat()), 
osg::Image::computeNumComponents(tex->getInternalFormat()), NULL);
                                        else glReadPixels(_x,_y,_width, 
_height, image->getPixelFormat(), image->getDataType(), 0);


                                        //map and write data
                                        GLubyte* src = 
(GLubyte*)ext->glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
                                        if (src)
                                        {
                                                debug<<Debug::verbose<<"copy 
"<<_width<<"*"<<_height<<"*"<<pixel_depth_bytes<<" = 
"<<_width*_height*pixel_depth_bytes<<" bytes to image buffer\n'";
                                                memcpy(image->data(), src, 
_width*_height*pixel_depth_bytes);
                                                
ext->glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
                                        } 

                                        //reset buffer
                                        
ext->glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
                                }
                        }
                }





I've tried assigning the attached texture (without running the callback) to a 
quad in the scene and it is still blank.

I've tried just about everything I can think of and I cant seem to figure out 
what could be going wrong but I suspect its somewhere on the RTT side.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=15982#15982





_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to