framework2

Форк
0
277 строк · 10.7 Кб
1
//
2
//  ofxFBXMeshMaterial.cpp
3
//  ofxFBX-Example-Importer
4
//
5
//  Created by Nick Hardeman on 10/31/13.
6
//
7
//
8

9
#include "ofxFBXSrcMeshMaterial.h"
10

11
using namespace ofxFBXSource;
12

13
//--------------------------------------------------------------
14
MeshMaterial::MeshMaterial() {
15
//    texture = NULL;
16
    _name   = "default material";
17
    enableTextures();
18
    enableMaterials();
19
    enable();
20
}
21

22
//--------------------------------------------------------------
23
void MeshMaterial::setup( const FbxSurfaceMaterial * pMaterial ) {
24
    ofFloatColor tEmissive = getMaterialProperty( pMaterial, FbxSurfaceMaterial::sEmissive, FbxSurfaceMaterial::sEmissiveFactor );
25
    ofFloatColor tAmbient = getMaterialProperty( pMaterial, FbxSurfaceMaterial::sAmbient, FbxSurfaceMaterial::sAmbientFactor );
26
    ofFloatColor tDiffuse = getMaterialProperty( pMaterial, FbxSurfaceMaterial::sDiffuse, FbxSurfaceMaterial::sDiffuseFactor );
27
    ofFloatColor tSpecular = getMaterialProperty( pMaterial, FbxSurfaceMaterial::sSpecular, FbxSurfaceMaterial::sSpecularFactor );
28
    float shininess = 0;
29
    FbxProperty lShininessProperty = pMaterial->FindProperty(FbxSurfaceMaterial::sShininess);
30
    if (lShininessProperty.IsValid()) {
31
        double lShininess = lShininessProperty.Get<FbxFloat>();
32
        shininess = lShininess;
33
    }
34
    
35
    setEmissiveColor( tEmissive );
36
    setAmbientColor( tAmbient );
37
    setDiffuseColor( tDiffuse );
38
    setSpecularColor( tSpecular );
39
    setShininess( shininess );
40
    
41
    _name = pMaterial->GetName();
42
    
43
//    cout << "-- " << pMaterial->GetName() << " -- setup -------------------------" << endl;
44
//    cout << "emissive = " << tEmissive << endl;
45
//    cout << "ambient = " << tAmbient << endl;
46
//    cout << "diffuse = " << tDiffuse << endl;
47
//    cout << "specular = " << tSpecular << endl;
48
//    cout << "shininess = " << shininess << endl;
49
    
50
    const int lTextureCount = pMaterial->GetSrcObjectCount<FbxFileTexture>();
51
    ofLogVerbose("ofxFBXMeshMaterial::setup") << pMaterial->GetName() << " texture count = " << lTextureCount;
52
    
53
    // pMaterial->GetClassId().Is(KFbxSurfacePhong::ClassId)
54
    
55
    // search for a texture based on the material properties :)
56
	if (!hasTexture()) {
57
		findTextureForProperty(pMaterial, FbxSurfaceMaterial::sShadingModel);
58
	}
59
    if(!hasTexture()) {
60
        findTextureForProperty( pMaterial, FbxSurfaceMaterial::sEmissive );
61
    }
62
    if(!hasTexture()) {
63
        findTextureForProperty( pMaterial, FbxSurfaceMaterial::sAmbient );
64
    }
65
    if(!hasTexture()) {
66
        findTextureForProperty( pMaterial, FbxSurfaceMaterial::sDiffuse );
67
    }
68
    if(!hasTexture()) {
69
        findTextureForProperty( pMaterial, FbxSurfaceMaterial::sSpecular );
70
    }
71
    if(!hasTexture()) {
72
        findTextureForProperty( pMaterial, FbxSurfaceMaterial::sNormalMap );
73
    }
74
    if(!hasTexture()) {
75
        findTextureForProperty( pMaterial, FbxSurfaceMaterial::sBump );
76
    }
77
    if(!hasTexture()) {
78
        findTextureForProperty( pMaterial, FbxSurfaceMaterial::sDisplacementColor );
79
    }
80
    if(!hasTexture()) {
81
        findTextureForProperty( pMaterial, FbxSurfaceMaterial::sMultiLayer );
82
    }
83
    if(!hasTexture()) {
84
        findTextureForProperty( pMaterial, FbxSurfaceMaterial::sReflection );
85
    }
86
    if(!hasTexture()) {
87
        findTextureForProperty( pMaterial, FbxSurfaceMaterial::sShadingModel );
88
    }
89
    if(!hasTexture()) {
90
        findTextureForProperty( pMaterial, FbxSurfaceMaterial::sShininess );
91
    }
92
    if(!hasTexture()) {
93
        findTextureForProperty( pMaterial, FbxSurfaceMaterial::sTransparentColor );
94
    }
95
    if(!hasTexture()) {
96
        findTextureForProperty( pMaterial, FbxSurfaceMaterial::sVectorDisplacementColor );
97
    }
98
    if(!hasTexture()) {
99
        findTextureForProperty( pMaterial, FbxSurfaceMaterial::sMultiLayer );
100
    }
101
    
102
    
103
//    cout << "Do we have a texture for this material? " << hasTexture() << endl;
104
    
105
    if( hasTexture() ) enableTextures();
106
}
107

108
//--------------------------------------------------------------
109
void MeshMaterial::begin() {
110
    if( !isEnabled() ) return;
111
    if( areMaterialsEnabled() ) ofMaterial::begin();
112
    if( hasTexture() && areTexturesEnabled() ) {
113
//        cout << "ofxFBXMeshMaterial :: " << getName() << " binding the texture " << areMaterialsEnabled() << endl;
114
//        glEnable( GL_TEXTURE_2D );
115
//        glBindTexture(GL_TEXTURE_2D, texture->getTextureData().textureID);
116
        texture->bind();
117
    }
118
}
119

120
//--------------------------------------------------------------
121
void MeshMaterial::end() {
122
    if( !isEnabled() ) return;
123
    if( areMaterialsEnabled() ) ofMaterial::end();
124
    if( hasTexture() && areTexturesEnabled() ) {
125
        texture->unbind();
126
//        glBindTexture( GL_TEXTURE_2D, 0);
127
//        glDisable( GL_TEXTURE_2D );
128
    }
129
}
130

131
//--------------------------------------------------------------
132
bool MeshMaterial::hasTexture() {
133
//    return texture != NULL;
134
    if( texture ) return true;
135
    return false;//texture;// != NULL;
136
}
137

138
//--------------------------------------------------------------
139
//const MeshTexture* MeshMaterial::getTexturePtr() {
140
shared_ptr<MeshTexture> MeshMaterial::getTexturePtr() {
141
    return texture;
142
}
143

144
//--------------------------------------------------------------
145
void MeshMaterial::enableTextures() {
146
    _bTexturesEnabled = true;
147
}
148

149
//--------------------------------------------------------------
150
void MeshMaterial::disableTextures() {
151
    _bTexturesEnabled = false;
152
}
153

154
//--------------------------------------------------------------
155
bool MeshMaterial::areTexturesEnabled() {
156
    return _bTexturesEnabled;
157
}
158

159
//--------------------------------------------------------------
160
void MeshMaterial::enableMaterials() {
161
    _bMaterialsEnabled = true;
162
}
163

164
//--------------------------------------------------------------
165
void MeshMaterial::disableMaterials() {
166
    _bMaterialsEnabled = false;
167
}
168

169
//--------------------------------------------------------------
170
bool MeshMaterial::areMaterialsEnabled() {
171
    return _bMaterialsEnabled;
172
}
173

174
//--------------------------------------------------------------
175
void MeshMaterial::enable() {
176
    _bEnabled = true;
177
}
178

179
//--------------------------------------------------------------
180
void MeshMaterial::disable() {
181
    _bEnabled = false;
182
}
183

184
//--------------------------------------------------------------
185
bool MeshMaterial::isEnabled() {
186
    return _bEnabled;
187
}
188

189
//--------------------------------------------------------------
190
string MeshMaterial::getName() {
191
    return _name;
192
}
193

194
//--------------------------------------------------------------
195
string MeshMaterial::getInfoAsString() {
196
    stringstream ss;
197
    ss << "-- " << getName() << " Material enabled: " << isEnabled() << " material enabled: " << areMaterialsEnabled() << " ----------------------" << endl;
198
    ss << "emissive = " << getEmissiveColor() << endl;
199
    ss << "ambient = " << getAmbientColor() << endl;
200
    ss << "diffuse = " << getDiffuseColor() << endl;
201
    ss << "specular = " << getSpecularColor() << endl;
202
    ss << "shininess = " << getShininess() << endl;
203
    ss << "texture = " << _textureName << " enabled: " << areTexturesEnabled() << " has texture: " << hasTexture() << endl;
204
    
205
    return ss.str();
206
}
207

208
//--------------------------------------------------------------
209
ofFloatColor MeshMaterial::getMaterialProperty(const FbxSurfaceMaterial * pMaterial,
210
                                 const char * pPropertyName,
211
                                 const char * pFactorPropertyName ) {
212
    FbxDouble3 lResult(0, 0, 0);
213
    const FbxProperty lProperty = pMaterial->FindProperty(pPropertyName);
214
    const FbxProperty lFactorProperty = pMaterial->FindProperty(pFactorPropertyName);
215
    if (lProperty.IsValid() && lFactorProperty.IsValid()) {
216
        lResult = lProperty.Get<FbxDouble3>();
217
        double lFactor = lFactorProperty.Get<FbxDouble>();
218
        if (lFactor != 1) {
219
            lResult[0] *= lFactor;
220
            lResult[1] *= lFactor;
221
            lResult[2] *= lFactor;
222
        }
223
    }
224
    ofFloatColor tcolor;
225
    tcolor.set( lResult[0], lResult[1], lResult[2] );
226
    return tcolor;
227
}
228

229
//--------------------------------------------------------------
230
bool MeshMaterial::findTextureForProperty(const FbxSurfaceMaterial * pMaterial,
231
                                                const char * pPropertyName ) {
232
    
233
    const FbxProperty lProperty = pMaterial->FindProperty(pPropertyName);
234
    
235
    if (lProperty.IsValid()) {
236
        const int lTextureCount     = lProperty.GetSrcObjectCount<FbxFileTexture>();
237
        const int lLTextureCount    = lProperty.GetSrcObjectCount<FbxLayeredTexture>();
238
        ofLogVerbose("ofxFBXMeshMaterial::findTextureForProperty") << pPropertyName << " is a valid property with texture count of " << lTextureCount << " layered texture = " << lLTextureCount;
239
        if (lTextureCount) {
240
            const FbxFileTexture* lTexture = lProperty.GetSrcObject<FbxFileTexture>();
241
            if (lTexture && lTexture->GetUserDataPtr()) {
242
                _textureName = lTexture->GetName();
243
//                texture = static_cast<MeshTexture *>( lTexture->GetUserDataPtr() );
244
//                texture = *static_cast< shared_ptr<MeshTexture>* >(lTexture->GetUserDataPtr());
245
//                auto rtex = static_cast< shared_ptr<MeshTexture>* >(lTexture->GetUserDataPtr());
246
                auto ptex = static_cast<MeshTexture *>( lTexture->GetUserDataPtr() );
247
                texture = make_shared<MeshTexture>(*ptex);
248
                return true;
249
            }
250
        }
251
        
252
        if (lLTextureCount) {
253
            const FbxLayeredTexture* lLTexture = lProperty.GetSrcObject<FbxLayeredTexture>();
254
            if( lLTexture ) {
255
                const FbxFileTexture* lTexture = lLTexture->GetSrcObject<FbxFileTexture>();
256
                ofLogVerbose("ofxFBXMeshMaterial::findTextureForProperty") << "-------> Layered " << pPropertyName << " layered texture = " << lLTextureCount;
257
                if (lTexture && lTexture->GetUserDataPtr()) {
258
                    _textureName = lTexture->GetName();
259
                    // std::shared_ptr<disk_node> u_poi = * static_cast< std::shared_ptr<disk_node>* >( RayCallback.m_collisionObject->getUserPointer() );
260
//                    texture = static_cast<MeshTexture *>( lTexture->GetUserDataPtr() );
261
//                    texture = *static_cast< shared_ptr<MeshTexture>* >(lTexture->GetUserDataPtr());
262
//                    auto rtex = static_cast< shared_ptr<MeshTexture>* >(lTexture->GetUserDataPtr());
263
//                    texture = *rtex;
264
//                    texture = make_shared<MeshTexture>(ptex);
265
                    auto ptex = static_cast<MeshTexture *>( lTexture->GetUserDataPtr() );
266
                    texture = make_shared<MeshTexture>(*ptex);
267
                    return true;
268
                }
269
            }
270
        }
271
        
272
    } else {
273
//        cout << pPropertyName << " is NOT a valid property " << endl;
274
    }
275
    
276
    return false;
277
}
278

279

280

281

282

283

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.