framework2
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
11using namespace ofxFBXSource;
12
13//--------------------------------------------------------------
14MeshMaterial::MeshMaterial() {
15// texture = NULL;
16_name = "default material";
17enableTextures();
18enableMaterials();
19enable();
20}
21
22//--------------------------------------------------------------
23void MeshMaterial::setup( const FbxSurfaceMaterial * pMaterial ) {
24ofFloatColor tEmissive = getMaterialProperty( pMaterial, FbxSurfaceMaterial::sEmissive, FbxSurfaceMaterial::sEmissiveFactor );
25ofFloatColor tAmbient = getMaterialProperty( pMaterial, FbxSurfaceMaterial::sAmbient, FbxSurfaceMaterial::sAmbientFactor );
26ofFloatColor tDiffuse = getMaterialProperty( pMaterial, FbxSurfaceMaterial::sDiffuse, FbxSurfaceMaterial::sDiffuseFactor );
27ofFloatColor tSpecular = getMaterialProperty( pMaterial, FbxSurfaceMaterial::sSpecular, FbxSurfaceMaterial::sSpecularFactor );
28float shininess = 0;
29FbxProperty lShininessProperty = pMaterial->FindProperty(FbxSurfaceMaterial::sShininess);
30if (lShininessProperty.IsValid()) {
31double lShininess = lShininessProperty.Get<FbxFloat>();
32shininess = lShininess;
33}
34
35setEmissiveColor( tEmissive );
36setAmbientColor( tAmbient );
37setDiffuseColor( tDiffuse );
38setSpecularColor( tSpecular );
39setShininess( 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
50const int lTextureCount = pMaterial->GetSrcObjectCount<FbxFileTexture>();
51ofLogVerbose("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 :)
56if (!hasTexture()) {
57findTextureForProperty(pMaterial, FbxSurfaceMaterial::sShadingModel);
58}
59if(!hasTexture()) {
60findTextureForProperty( pMaterial, FbxSurfaceMaterial::sEmissive );
61}
62if(!hasTexture()) {
63findTextureForProperty( pMaterial, FbxSurfaceMaterial::sAmbient );
64}
65if(!hasTexture()) {
66findTextureForProperty( pMaterial, FbxSurfaceMaterial::sDiffuse );
67}
68if(!hasTexture()) {
69findTextureForProperty( pMaterial, FbxSurfaceMaterial::sSpecular );
70}
71if(!hasTexture()) {
72findTextureForProperty( pMaterial, FbxSurfaceMaterial::sNormalMap );
73}
74if(!hasTexture()) {
75findTextureForProperty( pMaterial, FbxSurfaceMaterial::sBump );
76}
77if(!hasTexture()) {
78findTextureForProperty( pMaterial, FbxSurfaceMaterial::sDisplacementColor );
79}
80if(!hasTexture()) {
81findTextureForProperty( pMaterial, FbxSurfaceMaterial::sMultiLayer );
82}
83if(!hasTexture()) {
84findTextureForProperty( pMaterial, FbxSurfaceMaterial::sReflection );
85}
86if(!hasTexture()) {
87findTextureForProperty( pMaterial, FbxSurfaceMaterial::sShadingModel );
88}
89if(!hasTexture()) {
90findTextureForProperty( pMaterial, FbxSurfaceMaterial::sShininess );
91}
92if(!hasTexture()) {
93findTextureForProperty( pMaterial, FbxSurfaceMaterial::sTransparentColor );
94}
95if(!hasTexture()) {
96findTextureForProperty( pMaterial, FbxSurfaceMaterial::sVectorDisplacementColor );
97}
98if(!hasTexture()) {
99findTextureForProperty( pMaterial, FbxSurfaceMaterial::sMultiLayer );
100}
101
102
103// cout << "Do we have a texture for this material? " << hasTexture() << endl;
104
105if( hasTexture() ) enableTextures();
106}
107
108//--------------------------------------------------------------
109void MeshMaterial::begin() {
110if( !isEnabled() ) return;
111if( areMaterialsEnabled() ) ofMaterial::begin();
112if( hasTexture() && areTexturesEnabled() ) {
113// cout << "ofxFBXMeshMaterial :: " << getName() << " binding the texture " << areMaterialsEnabled() << endl;
114// glEnable( GL_TEXTURE_2D );
115// glBindTexture(GL_TEXTURE_2D, texture->getTextureData().textureID);
116texture->bind();
117}
118}
119
120//--------------------------------------------------------------
121void MeshMaterial::end() {
122if( !isEnabled() ) return;
123if( areMaterialsEnabled() ) ofMaterial::end();
124if( hasTexture() && areTexturesEnabled() ) {
125texture->unbind();
126// glBindTexture( GL_TEXTURE_2D, 0);
127// glDisable( GL_TEXTURE_2D );
128}
129}
130
131//--------------------------------------------------------------
132bool MeshMaterial::hasTexture() {
133// return texture != NULL;
134if( texture ) return true;
135return false;//texture;// != NULL;
136}
137
138//--------------------------------------------------------------
139//const MeshTexture* MeshMaterial::getTexturePtr() {
140shared_ptr<MeshTexture> MeshMaterial::getTexturePtr() {
141return texture;
142}
143
144//--------------------------------------------------------------
145void MeshMaterial::enableTextures() {
146_bTexturesEnabled = true;
147}
148
149//--------------------------------------------------------------
150void MeshMaterial::disableTextures() {
151_bTexturesEnabled = false;
152}
153
154//--------------------------------------------------------------
155bool MeshMaterial::areTexturesEnabled() {
156return _bTexturesEnabled;
157}
158
159//--------------------------------------------------------------
160void MeshMaterial::enableMaterials() {
161_bMaterialsEnabled = true;
162}
163
164//--------------------------------------------------------------
165void MeshMaterial::disableMaterials() {
166_bMaterialsEnabled = false;
167}
168
169//--------------------------------------------------------------
170bool MeshMaterial::areMaterialsEnabled() {
171return _bMaterialsEnabled;
172}
173
174//--------------------------------------------------------------
175void MeshMaterial::enable() {
176_bEnabled = true;
177}
178
179//--------------------------------------------------------------
180void MeshMaterial::disable() {
181_bEnabled = false;
182}
183
184//--------------------------------------------------------------
185bool MeshMaterial::isEnabled() {
186return _bEnabled;
187}
188
189//--------------------------------------------------------------
190string MeshMaterial::getName() {
191return _name;
192}
193
194//--------------------------------------------------------------
195string MeshMaterial::getInfoAsString() {
196stringstream ss;
197ss << "-- " << getName() << " Material enabled: " << isEnabled() << " material enabled: " << areMaterialsEnabled() << " ----------------------" << endl;
198ss << "emissive = " << getEmissiveColor() << endl;
199ss << "ambient = " << getAmbientColor() << endl;
200ss << "diffuse = " << getDiffuseColor() << endl;
201ss << "specular = " << getSpecularColor() << endl;
202ss << "shininess = " << getShininess() << endl;
203ss << "texture = " << _textureName << " enabled: " << areTexturesEnabled() << " has texture: " << hasTexture() << endl;
204
205return ss.str();
206}
207
208//--------------------------------------------------------------
209ofFloatColor MeshMaterial::getMaterialProperty(const FbxSurfaceMaterial * pMaterial,
210const char * pPropertyName,
211const char * pFactorPropertyName ) {
212FbxDouble3 lResult(0, 0, 0);
213const FbxProperty lProperty = pMaterial->FindProperty(pPropertyName);
214const FbxProperty lFactorProperty = pMaterial->FindProperty(pFactorPropertyName);
215if (lProperty.IsValid() && lFactorProperty.IsValid()) {
216lResult = lProperty.Get<FbxDouble3>();
217double lFactor = lFactorProperty.Get<FbxDouble>();
218if (lFactor != 1) {
219lResult[0] *= lFactor;
220lResult[1] *= lFactor;
221lResult[2] *= lFactor;
222}
223}
224ofFloatColor tcolor;
225tcolor.set( lResult[0], lResult[1], lResult[2] );
226return tcolor;
227}
228
229//--------------------------------------------------------------
230bool MeshMaterial::findTextureForProperty(const FbxSurfaceMaterial * pMaterial,
231const char * pPropertyName ) {
232
233const FbxProperty lProperty = pMaterial->FindProperty(pPropertyName);
234
235if (lProperty.IsValid()) {
236const int lTextureCount = lProperty.GetSrcObjectCount<FbxFileTexture>();
237const int lLTextureCount = lProperty.GetSrcObjectCount<FbxLayeredTexture>();
238ofLogVerbose("ofxFBXMeshMaterial::findTextureForProperty") << pPropertyName << " is a valid property with texture count of " << lTextureCount << " layered texture = " << lLTextureCount;
239if (lTextureCount) {
240const FbxFileTexture* lTexture = lProperty.GetSrcObject<FbxFileTexture>();
241if (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());
246auto ptex = static_cast<MeshTexture *>( lTexture->GetUserDataPtr() );
247texture = make_shared<MeshTexture>(*ptex);
248return true;
249}
250}
251
252if (lLTextureCount) {
253const FbxLayeredTexture* lLTexture = lProperty.GetSrcObject<FbxLayeredTexture>();
254if( lLTexture ) {
255const FbxFileTexture* lTexture = lLTexture->GetSrcObject<FbxFileTexture>();
256ofLogVerbose("ofxFBXMeshMaterial::findTextureForProperty") << "-------> Layered " << pPropertyName << " layered texture = " << lLTextureCount;
257if (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);
265auto ptex = static_cast<MeshTexture *>( lTexture->GetUserDataPtr() );
266texture = make_shared<MeshTexture>(*ptex);
267return true;
268}
269}
270}
271
272} else {
273// cout << pPropertyName << " is NOT a valid property " << endl;
274}
275
276return false;
277}
278
279
280
281
282
283