1

I am trying to get texture file name from an osg::Geometry I get the texture coordinates like this:

osg::Geometry* geom = dynamic_cast<osg::Geometry*> (drawable);
const osg::Geometry::ArrayList& texCoordArrayList = dynamic_cast<const osg::Geometry::ArrayList&>(geom->getTexCoordArrayList());
auto texCoordArrayListSize = texCoordArrayList.size();

auto sset = geom->getOrCreateStateSet();
processStateSet(sset);

for (size_t k = 0; k < texCoordArrayListSize; k++)
{
    const osg::Vec2Array* texCoordArray = dynamic_cast<const osg::Vec2Array*>(geom->getTexCoordArray(k));
    //doing sth with vertexarray, normalarray and texCoordArray
}

But I am not able to get texture file name in processStateSet() function. I take the processStateSet function code from OSG examples (specifically from osganalysis example). Even though there is a texture file, Sometimes it works and gets the name but sometimes not. Here is my processStateSet function

void processStateSet(osg::StateSet* stateset)
{
    if (!stateset) return;

    for (unsigned int ti = 0; ti < stateset->getNumTextureAttributeLists(); ++ti)
    {
        osg::StateAttribute* sa = stateset->getTextureAttribute(ti, osg::StateAttribute::TEXTURE);
        osg::Texture* texture = dynamic_cast<osg::Texture*>(sa);
        if (texture)
        {
            LOG("texture! ");

            //TODO: something with this.
            for (unsigned int i = 0; i < texture->getNumImages(); ++i)
            {
                auto img (texture->getImage(i));
                auto texturefname (img->getFileName());

                LOG("image ! image no: " + IntegerToStr(i) +  " file: " + texturefname);
            }
        }
    }
}

EDIT:

I just realized that: if the model that I load is ".3ds", texturefname is exist but if model is ".flt" there is not texture name.

Is it about loading different types? But I know that they both have textures. What is the difference? I confused.

Tuna
  • 113
  • 1
  • 11
  • 1
    That might depend on how the plugin that loads each 3D format and/or image type behaves: some of them might not set the filename in the image they read. Take a look at the relevant plugins code – rickyviking Apr 01 '21 at 08:11
  • Some 3D models don't even come with texture names – user253751 Apr 01 '21 at 12:53
  • @user253751 I know that but I do not know how OSG reads the texture. have any idea ? – Tuna Apr 19 '21 at 13:16

1 Answers1

0

Some 3D models don't have texture names. Your choices are to deal with it, or use model files that do. It also depends on the format. Some formats can't have texture names. Some Blender export scripts can't write texture names even though the format supports it. And so on.

3D model formats are not interchangeable - every one is different.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • I opened the "flt" model file and it contains texture name, actually I am not an expert on these topics – Tuna Sep 24 '21 at 13:51