framework2

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

9
#include "ofxFBXMesh.h"
10
#include "ofxFBX.h"
11

12
ofVbo ofxFBXMesh::dummyVbo;
13

14
//----------------------------------------
15
ofxFBXSource::Node::NodeType ofxFBXMesh::getType() {
16
    return ofxFBXSource::Node::OFX_FBX_MESH;
17
}
18

19
//--------------------------------------------------------------
20
void ofxFBXMesh::setup( shared_ptr<ofxFBXSource::Node> anode ) {
21
    ofxFBXNode::setup( anode );
22
    _checkSrcMesh();
23
    bHasTexture = false;
24
    if(mSrcMesh) {
25
        mSrcMesh->configureMesh( mesh );
26
        setTransform( anode );
27
        // populate the materials //
28
        auto srcMats = mSrcMesh->getMaterials();
29
        for( auto sm : srcMats ) {
30
            auto mat = make_shared<ofxFBXMeshMaterial>();
31
            mat->setup( sm );
32
            mMaterials.push_back( mat );
33
            if( mat->hasTexture() ) {
34
                bHasTexture = true;
35
            }
36
        }
37
    }
38
}
39

40
//--------------------------------------------------------------
41
void ofxFBXMesh::update( FbxTime& pTime, FbxPose* pPose ) {
42
    _checkSrcMesh();
43
    if(mSrcMesh) {
44
        mSrcMesh->update( pTime, pPose );
45
        bDrawMeshKeyframe=false;
46
    }
47
}
48

49
//--------------------------------------------------------------
50
void ofxFBXMesh::update( int aAnimIndex, signed long aMillis ) {
51
    _checkSrcMesh();
52
    if(mSrcMesh) {
53
        mSrcMesh->update( aAnimIndex, aMillis );
54
        
55
        if( mSrcMesh->isUsingCachedMeshes() && mSrcMesh->hasMeshKeyCollection(aAnimIndex)) {
56
            // now configure the mesh for the animation index and time //
57
            // but only if there are no vertices, since the src mesh has all of the meshes already in a vector //
58
            // the mesh gets blended in transition, so setup here, so there isn't a stutter on transition start //
59
            if( bBlendMeshFrames ) {
60
                mSrcMesh->updateMeshFromKeyframesBlended( &mesh, aAnimIndex, aMillis );
61
            } else {
62
                if( mesh.getNumVertices() < 1 ) {
63
                    mSrcMesh->updateMeshFromKeyframes( &mesh, aAnimIndex, aMillis );
64
                }
65
            }
66
            mLastAnimIndex = aAnimIndex;
67
            mLastFbxTimeMillis = aMillis;
68
            bDrawMeshKeyframe=true;
69
        }
70
    }
71
}
72

73
//--------------------------------------------------------------
74
void ofxFBXMesh::update( int aAnimIndex1, signed long aAnim1Millis, int aAnimIndex2, signed long aAnim2Millis, float aMixPct ) {
75
    _checkSrcMesh();
76
    if(mSrcMesh) {
77
        mSrcMesh->update( aAnimIndex1, aAnim1Millis, aAnimIndex2, aAnim2Millis, aMixPct );
78
        bDrawMeshKeyframe=false;
79
        if( mSrcMesh->isUsingCachedMeshes() && mSrcMesh->hasMeshKeyCollection(aAnimIndex1) && mSrcMesh->hasMeshKeyCollection(aAnimIndex2)) {
80
            mSrcMesh->updateMeshFromKeyframes( &mesh, aAnimIndex1, aAnim1Millis, aAnimIndex2, aAnim2Millis, aMixPct );
81
        }
82
    }
83
}
84

85
//--------------------------------------------------------------
86
void ofxFBXMesh::update() {
87
    _checkSrcMesh();
88
    if(mSrcMesh) {
89
        setPosition( mSrcMesh->getPosition() );
90
        setOrientation( mSrcMesh->getOrientationQuat() );
91
        setScale( mSrcMesh->getScale() );
92
    }
93
}
94

95
//--------------------------------------------------------------
96
void ofxFBXMesh::lateUpdate(FbxTime& pTime, FbxAnimLayer * pAnimLayer, FbxPose* pPose) {
97
    _checkSrcMesh();
98
    if(mSrcMesh) {
99
//        signed long ctime = (signed long)pTime.GetMilliSeconds();
100
//        if(mLastFbxTimeMillis != ctime ) {
101
        if(!mSrcMesh->isUsingCachedMeshes() ) {
102
            mSrcMesh->updateMesh( &mesh, pTime, pAnimLayer, pPose );
103
            bMeshDirty = true;
104
        } else {
105
            bMeshDirty = true;
106
        }
107
//        mLastFbxTimeMillis = ctime;
108
    }
109
}
110

111
//--------------------------------------------------------------
112
void ofxFBXMesh::draw() {
113
    _checkSrcMesh();
114
    if(!mSrcMesh) {
115
        ofLogError("ofxFBXMesh::draw : src mesh is invalid! ") << getName();
116
        return;
117
    }
118
    transformGL(); {
119
        if(bDrawMeshKeyframe) {
120
            mSrcMesh->drawMeshKeyframe( mLastAnimIndex, mLastFbxTimeMillis, mMaterials, bMeshDirty );
121
        } else {
122
            mSrcMesh->draw( &mesh, mMaterials, bMeshDirty );
123
        }
124
    } restoreTransformGL();
125
    
126
    bMeshDirty = false;
127
}
128

129
//--------------------------------------------------------------
130
void ofxFBXMesh::drawWireframe() {
131
    _checkSrcMesh();
132
    transformGL(); {
133
        mesh.drawWireframe();
134
    } restoreTransformGL();
135
}
136

137
//--------------------------------------------------------------
138
void ofxFBXMesh::drawNormals(float length, bool bFaceNormals ) {
139
    
140
    if( mesh.usingNormals()) {
141
        vector<glm::vec3>& normals    = mesh.getNormals();
142
        vector<glm::vec3>& vertices   = mesh.getVertices();
143
        glm::vec3 normal;
144
        glm::vec3 vert;
145
        
146
        // super inefficient, for debug only //
147
//        ofMesh normalsMesh;
148
        if( normalsMesh.getNumVertices() != normals.size() * 2 ) {
149
            normalsMesh.getVertices().resize( normals.size() * 2 );
150
            normalsMesh.setMode( OF_PRIMITIVE_LINES );
151
        }
152
        
153
//        normalsMesh.getVertices().resize( normals.size() * 2);
154
        
155
        if(bFaceNormals) {
156
            for(int i = 0; i < (int)normals.size(); i++ ) {
157
                if(i % 3 == 0) {
158
                    vert = (vertices[i]+vertices[i+1]+vertices[i+2]) / 3;
159
                } else if(i % 3 == 1) {
160
                    vert = (vertices[i-1]+vertices[i]+vertices[i+1]) / 3;
161
                } else if ( i % 3 == 2) {
162
                    vert = (vertices[i-2]+vertices[i-1]+vertices[i]) / 3;
163
                }
164
                normalsMesh.setVertex(i*2, vert);
165
                normal = glm::normalize(normals[i]);
166
                normal *= length;
167
                normalsMesh.setVertex(i*2+1, normal+vert);
168
            }
169
        } else {
170
            for(int i = 0; i < (int)normals.size(); i++) {
171
                vert = vertices[i];
172
                normal = glm::normalize(normals[i]);
173
                normalsMesh.setVertex( i*2, vert);
174
                normal *= length;
175
                normalsMesh.setVertex(i*2+1, normal+vert);
176
            }
177
        }
178
        transformGL(); {
179
            normalsMesh.draw();
180
        } restoreTransformGL();
181
    } else {
182
        ofLogWarning("ofxFBXMesh") << "drawNormals(): mesh normals are disabled for " << getName();
183
    }
184
}
185

186
//--------------------------------------------------------------
187
ofVbo& ofxFBXMesh::getVbo() {
188
    _checkSrcMesh();
189
    if( mSrcMesh ) {
190
        return mSrcMesh->getVbo();
191
    }
192
    ofLogWarning( "ofxFBXMesh::getVbo src node is not set!!" );
193
    return dummyVbo;
194
}
195

196
//--------------------------------------------------------------
197
ofMesh& ofxFBXMesh::getMesh() {
198
    return mesh;
199
}
200

201
//--------------------------------------------------------------
202
int ofxFBXMesh::getNumMaterials() {
203
    return mMaterials.size();
204
}
205

206
//--------------------------------------------------------------
207
vector< shared_ptr<ofxFBXMeshMaterial> > ofxFBXMesh::getMaterials() {
208
    return mMaterials;
209
}
210

211
//--------------------------------------------------------------
212
void ofxFBXMesh::setMaterialsEnabled(bool ab) {
213
    for( auto mat : mMaterials ) {
214
        if( ab ) {
215
            mat->enable();
216
        } else {
217
            mat->disable();
218
        }
219
    }
220
}
221

222
//--------------------------------------------------------------
223
vector< shared_ptr<ofxFBXSource::MeshTexture> > ofxFBXMesh::getTextures() {
224
    vector< shared_ptr<ofxFBXSource::MeshTexture> > ttexs;
225
    for( auto mat : mMaterials ) {
226
        if( mat->hasSourceTexture() ) {
227
            ttexs.push_back( mat->getSrcTexture() );
228
        }
229
    }
230
    return ttexs;
231
}
232

233
//--------------------------------------------------------------
234
bool ofxFBXMesh::hasTexture() {
235
    return bHasTexture;
236
//    for( auto mat : mMaterials ) {
237
//        if( mat->hasTexture() ) {
238
//            return true;
239
//        }
240
//    }
241
//    return false;
242
}
243

244
//--------------------------------------------------------------
245
ofMesh ofxFBXMesh::getGlobalMesh() {
246
    glm::mat4 gmat = getGlobalTransformMatrix();
247
    // transform points into global space //
248
    ofMesh tmesh = mesh;
249
    auto& tverts = tmesh.getVertices();
250
    
251
    for( int i = 0; i < tverts.size(); i++ ) {
252
        auto& mp = tverts[i];
253
        glm::vec4 v = gmat * glm::vec4(mp, 1.0);
254
        mp = glm::vec3(v.x, v.y, v.z);
255
    }
256
    
257
    glm::quat tq = getGlobalOrientation();
258
    // transform the normals from local to global space //
259
    auto& tnormals = tmesh.getNormals();
260
    for( auto& tn : tnormals ) {
261
        tn = tq * tn;
262
    }
263
    
264
    return tmesh;
265
}
266

267
//--------------------------------------------------------------
268
ofMesh ofxFBXMesh::getGlobalMeshAroundPosition() {
269
    glm::vec3 gpos = getGlobalPosition();
270
    auto tmesh = getGlobalMesh();
271
    for( auto& tv : tmesh.getVertices() ) {
272
        tv -= gpos;
273
    }
274
    return tmesh;
275
}
276

277
//--------------------------------------------------------------
278
ofMesh ofxFBXMesh::getMeshAroundPositionScaleApplied() {
279
    glm::vec3 gscale = getGlobalScale();
280
    glm::vec3 gpos = getGlobalPosition();
281
    // transform points into global space //
282
    ofMesh tmesh = mesh;
283
    auto& tverts = tmesh.getVertices();
284
    
285
    for( int i = 0; i < tverts.size(); i++ ) {
286
        auto& mp = tverts[i];// - gpos;
287
        mp -= gpos;
288
        mp = gscale * mp;//glm::vec4(mp, 1.0);
289
    }
290
    return tmesh;
291
}
292

293
#pragma mark private
294
//--------------------------------------------------------------
295
void ofxFBXMesh::_checkSrcMesh() {
296
    if( !mSrcMesh && mSrcNode && mSrcNode->getType() == ofxFBXSource::Node::OFX_FBX_MESH ) {
297
        mSrcMesh = dynamic_pointer_cast<ofxFBXSource::Mesh>(mSrcNode);
298
    }
299
}
300

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

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

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

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