Hi All,

I'm in the process of upgrading 2.2 ->2.6 and came across a bug in mipmap generation. For NPOT textures when the ResizeNonPowerOfTwoHint is false, I'm not getting textures applied correctly.

Sample program demonstrating the bug:

   #include <osg/Texture2D>
   #include <osg/ShapeDrawable>

   #include <osgViewer/Viewer>
   #include <osgText/Text>

   int main(int, char **)
   {
       osg::ref_ptr<osg::Texture2D> redTexture = new osg::Texture2D;
       osg::Image* image = new osg::Image;
       static unsigned char bytes[12] = { 0xff, 0x00, 0x00, 0xff, 0xff,
   0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff };
       image->setImage(3, 1, 1, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE,
   &bytes[0], osg::Image::NO_DELETE, 1);
       redTexture->setImage(image);
       redTexture->setResizeNonPowerOfTwoHint(false);
       redTexture->setFilter( osg::Texture::MIN_FILTER,
   osg::Texture::LINEAR_MIPMAP_LINEAR );

       osg::ref_ptr<osg::Geode> geode = new osg::Geode();
       osg::ref_ptr<osg::ShapeDrawable> sphere1 = new
   osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),10));
       geode->addDrawable(sphere1.get());

       osg::ref_ptr<osgText::Text> text = new osgText::Text();
       text->setText( L"Text" );
       text->setAxisAlignment(osgText::Text::XZ_PLANE);

       osgViewer::Viewer viewer;
       viewer.setThreadingModel(osgViewer::ViewerBase::SingleThreaded);

       viewer.setSceneData( geode.get() );

       osg::StateSet* stateset = new osg::StateSet();
       stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
       stateset->setTextureAttributeAndModes(0, redTexture.get(),
   osg::StateAttribute::ON);
       geode->setStateSet( stateset );

       viewer.run();

       return 0;
   }

The problem appears to be that in mipmapAfterTexImage, called by applyTexImage2D_load, getTextureObject(contextID) is returning NULL. Moving the assignment of the TextureObject to the _textureObjectBuffer before applyTexImage2D_load is called fixes the problem. The change to Texture2D::apply is shown below, controlled by #define MIPMAP_FIX. Other code (in the same function even) is assigning to _textureObjectBuffer immediately upon generation of the TextureObject so I figure this should be a safe change.

   void Texture2D::apply(State& state) const
   {

       //state.setReportGLErrors(true);

       // get the contextID (user defined ID of 0 upwards) for the
       // current OpenGL context.
       const unsigned int contextID = state.getContextID();

       // get the texture object for the current contextID.
       TextureObject* textureObject = getTextureObject(contextID);

       if (textureObject != 0)
       {
           textureObject->bind();
if (getTextureParameterDirty(state.getContextID()))
               applyTexParameters(GL_TEXTURE_2D,state);

           if (_subloadCallback.valid())
           {
               _subloadCallback->subload(*this,state);
           }
           else if (_image.valid() && getModifiedCount(contextID) !=
   _image->getModifiedCount())
           {
               applyTexImage2D_subload(state,GL_TEXTURE_2D,_image.get(),
                                       _textureWidth, _textureHeight,
   _internalFormat, _numMipmapLevels);
// update the modified tag to show that it is up to date.
               getModifiedCount(contextID) = _image->getModifiedCount();
}
           else if (_readPBuffer.valid())
           {
               _readPBuffer->bindPBufferToTexture(GL_FRONT);
           }

       }
       else if (_subloadCallback.valid())
       {

           _textureObjectBuffer[contextID] = textureObject =
   generateTextureObject(contextID,GL_TEXTURE_2D);

           textureObject->bind();

           applyTexParameters(GL_TEXTURE_2D,state);

           _subloadCallback->load(*this,state);
textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,0);

           // in theory the following line is redundent, but in practice
           // have found that the first frame drawn doesn't apply the
   textures
           // unless a second bind is called?!!
           // perhaps it is the first glBind which is not required...
           //glBindTexture( GL_TEXTURE_2D, handle );

       }
       else if (_image.valid() && _image->data())
       {

           // keep the image around at least till we go out of scope.
           osg::ref_ptr<osg::Image> image = _image;

           // compute the internal texture format, this set the
   _internalFormat to an appropriate value.
           computeInternalFormat();
// compute the dimensions of the texture.
           computeRequiredTextureDimensions(state,*image,_textureWidth,
   _textureHeight, _numMipmapLevels);
#define MIPMAP_FIX
   #if defined MIPMAP_FIX
           _textureObjectBuffer[contextID] = textureObject =
   generateTextureObject(
contextID,GL_TEXTURE_2D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,0);
   #else
            textureObject = generateTextureObject(
contextID,GL_TEXTURE_2D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,0);
   #endif
textureObject->bind();

           applyTexParameters(GL_TEXTURE_2D,state);

           if (textureObject->isAllocated())
           {
               //notify(NOTICE)<<"Reusing texture object"<<std::endl;
               applyTexImage2D_subload(state,GL_TEXTURE_2D,image.get(),
                                    _textureWidth, _textureHeight,
   _internalFormat, _numMipmapLevels);
           }
           else
           {
               //notify(NOTICE)<<"Creating new texture object"<<std::endl;
               applyTexImage2D_load(state,GL_TEXTURE_2D,image.get(),
                                    _textureWidth, _textureHeight,
   _numMipmapLevels);
textureObject->setAllocated(true);
           }
// update the modified tag to show that it is upto date.
           getModifiedCount(contextID) = image->getModifiedCount();

   #if !defined MIPMAP_FIX
           _textureObjectBuffer[contextID] = textureObject;
   #endif
           if (_unrefImageDataAfterApply &&
   areAllTextureObjectsLoaded() && image->getDataVariance()==STATIC)
           {
               Texture2D* non_const_this = const_cast<Texture2D*>(this);
               non_const_this->_image = 0;
           }

           // in theory the following line is redundent, but in practice
           // have found that the first frame drawn doesn't apply the
   textures
           // unless a second bind is called?!!
           // perhaps it is the first glBind which is not required...
           //glBindTexture( GL_TEXTURE_2D, handle );
}
       else if ( (_textureWidth!=0) && (_textureHeight!=0) &&
   (_internalFormat!=0) )
       {
           _textureObjectBuffer[contextID] = textureObject =
   generateTextureObject(
contextID,GL_TEXTURE_2D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,0); textureObject->bind();

           applyTexParameters(GL_TEXTURE_2D,state);

           // no image present, but dimensions at set so lets create
   the texture
           glTexImage2D( GL_TEXTURE_2D, 0, _internalFormat,
                        _textureWidth, _textureHeight, _borderWidth,
                        _sourceFormat ? _sourceFormat : _internalFormat,
                        _sourceType ? _sourceType : GL_UNSIGNED_BYTE,
0); if (_readPBuffer.valid())
           {
               _readPBuffer->bindPBufferToTexture(GL_FRONT);
           }
}
       else
       {
           glBindTexture( GL_TEXTURE_2D, 0 );
       }

       // if texture object is now valid and we have to allocate mipmap
   levels, then
       if (textureObject != 0 && _texMipmapGenerationDirtyList[contextID])
       {
           generateMipmap(state);
       }
   }


Regards,

Mark


_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to