OSG:为什么有纹理坐标数组而不是纹理本身?

问题描述

我正在尝试从 osg::Geometry 获取纹理文件名我得到的纹理坐标如下:

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
}

但是我无法在 processstateSet() 函数获取纹理文件名。我从 OSG 示例(特别是 osganalysis 示例)中获取 processstateSet 函数代码。即使有纹理文件,有时它也能工作并获得名称,但有时却不能。这是我的 processstateSet 函数

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);
            }
        }
    }
}

编辑:

我刚刚意识到:如果我加载的模型是“.3ds”,则 texturefname 存在但如果模型是“.flt”,则没有纹理名称

是关于加载不同类型吗?但我知道它们都有纹理。有什么区别?我糊涂了。

解决方法

某些 3D 模型没有纹理名称。您的选择是处理它,或者使用这样做的模型文件。这也取决于格式。某些格式不能有纹理名称。即使格式支持,某些 Blender 导出脚本也无法写入纹理名称。等等。

3D 模型格式不可互换 - 每个格式都不同。