| 1 | #ifndef FBXMATERIALTOOSGSTATESET_H |
|---|
| 2 | #define FBXMATERIALTOOSGSTATESET_H |
|---|
| 3 | |
|---|
| 4 | #include <map> |
|---|
| 5 | #include <memory> |
|---|
| 6 | #include <osg/Material> |
|---|
| 7 | #include <osg/StateSet> |
|---|
| 8 | #include <osgDB/ReaderWriter> |
|---|
| 9 | #include <osg/Texture2D> |
|---|
| 10 | |
|---|
| 11 | #if defined(_MSC_VER) |
|---|
| 12 | #pragma warning( disable : 4505 ) |
|---|
| 13 | #endif |
|---|
| 14 | #include <fbxsdk.h> |
|---|
| 15 | |
|---|
| 16 | //The only things we need to create a new StateSet are texture and materials. So we store that in a pair. |
|---|
| 17 | //We Don't store directly in stateSet because getOrCreateStateSet function set some parameters. |
|---|
| 18 | typedef std::pair<osg::Material *, osg::Texture2D *> StateSetContent; |
|---|
| 19 | |
|---|
| 20 | //We use the pointers setted by the importer to not duplicate materials and textures. |
|---|
| 21 | typedef std::map<const KFbxSurfaceMaterial *, StateSetContent> KFbxMaterialMap; |
|---|
| 22 | |
|---|
| 23 | //This map is used to not load the same image more than 1 time. |
|---|
| 24 | typedef std::map<std::string, osg::Texture2D *> ImageMap; |
|---|
| 25 | |
|---|
| 26 | class FbxMaterialToOsgStateSet |
|---|
| 27 | { |
|---|
| 28 | public: |
|---|
| 29 | //Convert a KfbxSurfaceMaterial to a osgMaterial and an osgTexture. |
|---|
| 30 | StateSetContent convert(const KFbxSurfaceMaterial* pFbxMat); |
|---|
| 31 | |
|---|
| 32 | //dir is the directory where fbx is stored (for relative path). |
|---|
| 33 | FbxMaterialToOsgStateSet::FbxMaterialToOsgStateSet(const std::string& dir, const osgDB::ReaderWriter::Options* options) : |
|---|
| 34 | _options(options), |
|---|
| 35 | _dir(dir) {} |
|---|
| 36 | |
|---|
| 37 | void checkInvertTransparency(); |
|---|
| 38 | private: |
|---|
| 39 | //Convert a texture fbx to an osg texture. |
|---|
| 40 | osg::ref_ptr<osg::Texture2D> |
|---|
| 41 | fbxTextureToOsgTexture(const KFbxTexture* pOsgTex); |
|---|
| 42 | KFbxMaterialMap _kFbxMaterialMap; |
|---|
| 43 | ImageMap _imageMap; |
|---|
| 44 | const osgDB::ReaderWriter::Options* _options; |
|---|
| 45 | const std::string _dir; |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | #endif //FBXMATERIALTOOSGSTATESET_H |
|---|