Thank you Gwaldron,
that line of code saved me 40MB. Better than nothing. Now the memory usage is 
around 110 MB.

The format of the image is JPG.
I tried to use PVR but I can't load the image.

This is the complete function (updated with your suggestion) that I use to 
create a sphere with 2 textures.
Then I use a shader to cross fade between the two textures.... that's why I 
need to have both of them in memory.


Code:

- (osg::ref_ptr<osg::Geode>)createSphereWithTodayImage:(NSString 
*)todayTextName pastImage:(NSString *)pastTextName andRay:(CGFloat)ray
{
    osg::ref_ptr<osg::Geode> sphere = new osg::Geode();
    sphere->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(), 
ray)));
    sphere->setName("BubbleNode");

    // load image for texture
    osg::ref_ptr<osg::Image> todayImage = 
osgDB::readImageFile(todayTextName.UTF8String);
    osg::ref_ptr<osg::Image> pastImage = 
osgDB::readImageFile(pastTextName.UTF8String);

    if (!todayImage || !pastImage)
    {
        std::cout << "Couldn't load texture." << std::endl;
    }
    else
    {
        osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
        texture->setDataVariance(osg::Object::STATIC);
        texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
        texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
        texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
        texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
        texture->setImage(todayImage.release());
        texture->setUnRefImageDataAfterApply(true);
        
sphere->getOrCreateStateSet()->setTextureAttributeAndModes(0,texture.get());
        
        osg::ref_ptr<osg::Texture2D> texture2 = new osg::Texture2D;
        texture2->setDataVariance(osg::Object::STATIC);
        texture2->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
        texture2->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
        texture2->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
        texture2->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
        texture2->setImage(pastImage.release());
        texture->setUnRefImageDataAfterApply(true); 
        
sphere->getOrCreateStateSet()->setTextureAttributeAndModes(1,texture2.get());
        
    }
    return sphere;
}



and then I call this to add the sphere

Code:

osg::ref_ptr<osg::Geode> sphere = [self 
createSphereWithTodayImage:"pathofimage1.jpg" pastImage:"pathofimage2.jpg" 
andRay:1.0f];
_root->addChild(sphere.get());
_root->setDataVariance( osg::Object::DYNAMIC);






gwaldron wrote:
> What's the data type of your image? Show the code where you allocate the 
> image data.
> 
> You can also call this, which will free the CPU memory once the texture gets 
> to the GPU:
> 
>    texture->setUnRefImageDataAfterApply(true); 
> 
> 
> Glenn Waldron / @glennwaldron
> 
> 
> On Tue, Nov 19, 2013 at 8:11 AM, John Moore < ()> wrote:
> 
> >  Hi,
> > 
> > In my app I am using 2 textures of size 4096x2048.
> > I have a serious problem of memory usage.
> > 
> > 
> > Code:
> > 
> > osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
> >         texture->setDataVariance(osg::Object::STATIC);
> >         texture->setFilter(osg::Texture::MIN_FILTER, 
> > osg::Texture::LINEAR_MIPMAP_LINEAR);
> >         texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
> >         texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
> >         texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
> >         texture->setImage(image.release());
> >         
> > sphere->getOrCreateStateSet()->setTextureAttributeAndModes(0,texture.get());
> > 
> >         osg::ref_ptr<osg::Texture2D> texture2 = new osg::Texture2D;
> >         texture2->setDataVariance(osg::Object::STATIC);
> >         texture2->setFilter(osg::Texture::MIN_FILTER, 
> > osg::Texture::LINEAR_MIPMAP_LINEAR);
> >         texture2->setFilter(osg::Texture::MAG_FILTER, osg::Texture:: 
> > LINEAR);
> >         texture2->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
> >         texture2->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
> >         texture2->setImage(image2.release());
> >         
> > sphere->getOrCreateStateSet()->setTextureAttributeAndModes(1,texture2.get());
> > 
> > 
> > 
> > 
> > 
> > using this code my app jumps from 20 MB of memory to 250 MB of memory usage
> > 
> > Since I am developing on mobile platform this is a huge waste of memory.
> > 
> > I partially solved the problem by changing the MIN_FILTER from 
> > LINEAR_MIPMAP_LINEAR to LINEAR
> > Now the memory usage is 150MB
> > 
> > It's still a lot!
> > 
> > Do you know a way to optimize the memory usage of these 2 textures?
> > 
> > Thank you,
> > Have a nice day (or night :) ),
> > 
> > John
> > 
> > ------------------
> > Read this topic online here:
> > http://forum.openscenegraph.org/viewtopic.php?p=57295#57295 
> > (http://forum.openscenegraph.org/viewtopic.php?p=57295#57295)
> > 
> > 
> > 
> > 
> > 
> > _______________________________________________
> > osg-users mailing list
> >  ()
> > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org 
> > (http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org)
> > 
> 
> 
>  ------------------
> Post generated by Mail2Forum


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





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

Reply via email to