framework2
268 строк · 8.0 Кб
1#include "ofApp.h"2
3//--------------------------------------------------------------
4void ofApp::setup() {5
6ofSetBackgroundColor( 220 );7
8ofSetLogLevel( OF_LOG_VERBOSE );9
10ofSetFrameRate(60);11
12ofDisableArbTex();13if(!ofLoadImage(teddyTex, "tex.png" )) {14ofLogError("Could not load tex.png texture");15}16
17ofxFBXSource::Scene::Settings settings;18settings.importTextures = false;19settings.importMaterials = false;20// must use keyframes when blending animations //21settings.useKeyFrames = true; // on by default //22// model from https://www.turbosquid.com/FullPreview/Index.cfm/ID/61522723settings.filePath = "teddy.fbx";24// we don't want to manipulate the bones, so caching the meshes will reduce the bone transform calls25// and should speed it up with a lot of the same animated meshes that have bones //26settings.cacheMeshKeyframes = true;27
28cam.lookAt( ofVec3f(0,0,0) );29cam.setDistance( 550 );30cam.setFarClip(6000);31cam.setNearClip( .5f );32
33// fbxMan.setup( &scene );
34fbx.load( settings );35fbx.setAnimation(0);36fbx.setScale( 0.15 );37fbx.setPosition(-150, 0, 0 );38// fbx.cacheMeshKeyframes(true);
39
40// fbxManSmooth.setup( &scene );
41fbxSmooth.load( settings );42fbxSmooth.setAnimation(0);43fbxSmooth.setScale( 0.15 );44fbxSmooth.setPosition( 150, 0, 0 );45// fbxSmooth.cacheMeshKeyframes(true);
46
47for( int i = 0; i < fbx.getNumMeshes(); i++ ) {48cout << i << " - " << " num tex coords: " << fbx.getMeshes()[i]->getMesh().getNumTexCoords() << endl;49
50}51fbx.setMaterialsEnabled(false);52
53bRenderNormals = false;54}
55
56//--------------------------------------------------------------
57void ofApp::update() {58
59fbx.earlyUpdate();60
61// align the meshes with y axis //62for( int i = 0; i < fbx.getMeshes().size(); i++ ) {63fbx.getMeshes()[i]->panDeg(-90);64}65
66if( fbx.getCurrentAnimation().name == "walk" ) {67fbx.panDeg( 1 );68}69fbx.lateUpdate();70
71// update internally calls earlyUpdate and then lateUpdate72fbxSmooth.update();73
74// align the meshes with y axis //75for( int i = 0; i < fbxSmooth.getMeshes().size(); i++ ) {76fbxSmooth.getMeshes()[i]->panDeg(-90);77}78
79if( fbxSmooth.getCurrentAnimation().name == "walk" ) {80if(!fbxSmooth.isTransitioning()) fbxSmooth.panDeg( 1 );81}82
83}
84
85//--------------------------------------------------------------
86void ofApp::draw() {87
88ofSetColor(255, 255, 255);89// this causes the texture not to render on the models on iOS90#ifndef TARGET_OPENGLES91ofBackgroundGradient( ofColor::white, ofColor::gray );92#endif93
94ofEnableDepthTest();95cam.begin(); {96// this causes the texture not to render on the models on iOS97#ifndef TARGET_OPENGLES98ofSetColor( 160 );99ofPushMatrix(); {100ofTranslate( 0, -150, 0 );101ofRotateYDeg(90);102ofRotateZDeg(90);103// float stepSize = 1.25f, size_t numberOfSteps = 8, bool labels = false104ofDrawGridPlane(40, 50, false );105} ofPopMatrix();106#endif107
108ofEnableLighting();109light.enable();110
111ofSetColor( 200 );112teddyTex.bind();113fbx.draw();114fbxSmooth.draw();115teddyTex.unbind();116
117light.disable();118ofDisableLighting();119
120
121if( bRenderNormals ) {122ofSetColor( 255, 0, 255 );123fbx.drawMeshNormals( 0.1, false );124}125
126
127ofSetColor( light.getDiffuseColor() );128light.draw();129
130} cam.end();131
132ofDisableDepthTest();133
134int numBones = fbx.getNumBones();135
136ofSetColor( 60, 60, 60 );137stringstream ds;138ds << "Render normals (n): " << bRenderNormals << endl;139ds << "Render " << numBones << " bones " << endl;140if( fbx.areAnimationsEnabled() ) {141ds << "Toggle play/pause (spacebar): playing: " << fbx.getCurrentAnimation().isPlaying() << endl;142ds << "Previous/Next animation (up/down): " << fbx.getCurrentAnimation().name << endl;143}144// ds << "Scale is " << fbxMan.getScale() << endl;
145// if( fbxMan.getNumPoses() > 0 ) {
146// ds << "Pose: " << fbxMan.getCurrentPose()->getName() << " num poses: " << fbxMan.getNumPoses() << " enabled (p): " << fbxMan.arePosesEnabled() << endl;
147// }
148ofDrawBitmapString( ds.str(), 50, 30 );149
150
151for(int i = 0; i < fbx.getNumAnimations(); i++ ) {152stringstream ss;153ofxFBXAnimation& anim = fbx.getAnimation( i );154if( i == fbx.getCurrentAnimationIndex() ) {155ss << "- ";156}157ss << "name: " << anim.name << " " << ofToString(anim.getPositionSeconds(), 3) << " | " << ofToString(anim.getDurationSeconds(), 3) << " frame: " << anim.getFrameNum() << " / " << anim.getTotalNumFrames() << endl;158ofDrawBitmapString( ss.str(), 50, i * 30 + 650 );159}160
161stringstream fs;162fs << "TEDDY SMOOOOOTH" << endl;163fs << "transition: " << ofToString(fbxSmooth.getTransition().percent* 100.0, 0) << "% " << " is transitioning: " << fbxSmooth.isTransitioning() << endl;164ofDrawBitmapString( fs.str(), ofGetWidth()/2 + 200, 650 );165}
166
167#ifdef TARGET_OPENGLES168//--------------------------------------------------------------
169void ofApp::touchDown(ofTouchEventArgs & touch) {170
171}
172
173//--------------------------------------------------------------
174void ofApp::touchMoved(ofTouchEventArgs & touch) {175
176}
177
178//--------------------------------------------------------------
179void ofApp::touchUp(ofTouchEventArgs & touch) {180
181}
182
183//--------------------------------------------------------------
184void ofApp::touchDoubleTap(ofTouchEventArgs & touch) {185if(fbx.getNumAnimations() > 1) {186int newAnimIndex = fbx.getCurrentAnimationIndex()+1;187if(newAnimIndex > fbx.getNumAnimations()-1 ) {188newAnimIndex = 0;189}190fbx.setAnimation( newAnimIndex );191fbxSmooth.transition( newAnimIndex, 1.f );192}193}
194
195//--------------------------------------------------------------
196void ofApp::touchCancelled(ofTouchEventArgs & touch) {197
198}
199#else200
201//--------------------------------------------------------------
202void ofApp::keyPressed(int key) {203if(fbx.getNumAnimations() > 1) {204if(key == OF_KEY_DOWN ) {205int newAnimIndex = fbx.getCurrentAnimationIndex()+1;206if(newAnimIndex > fbx.getNumAnimations()-1 ) {207newAnimIndex = 0;208}209fbx.setAnimation( newAnimIndex );210fbxSmooth.transition( newAnimIndex, 1.f );211
212}213if(key == OF_KEY_UP ) {214int newAnimIndex = fbx.getCurrentAnimationIndex()-1;215if(newAnimIndex < 0 ) {216newAnimIndex = fbx.getNumAnimations()-1;217}218fbx.setAnimation( newAnimIndex );219fbxSmooth.transition( newAnimIndex, 1.f );220}221}222}
223
224//--------------------------------------------------------------
225void ofApp::keyReleased(int key) {226if(key == ' ') {227fbx.getCurrentAnimation().togglePlayPause();228}229if(key == 'n') {230bRenderNormals = !bRenderNormals;231}232}
233
234//--------------------------------------------------------------
235void ofApp::mouseMoved(int x, int y) {236
237}
238
239//--------------------------------------------------------------
240void ofApp::mouseDragged(int x, int y, int button) {241
242}
243
244//--------------------------------------------------------------
245void ofApp::mousePressed(int x, int y, int button) {246
247}
248
249//--------------------------------------------------------------
250void ofApp::mouseReleased(int x, int y, int button) {251
252}
253
254//--------------------------------------------------------------
255void ofApp::windowResized(int w, int h) {256
257}
258
259//--------------------------------------------------------------
260void ofApp::gotMessage(ofMessage msg) {261
262}
263
264//--------------------------------------------------------------
265void ofApp::dragEvent(ofDragInfo dragInfo) {266
267}
268#endif269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285