简体   繁体   中英

OSG: Why there is texture coordinate array but not texture itself?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM