void ofxTimeline::update(ofEventArgs& updateArgs){ if(isFrameBased){ currentFrame = ofGetFrameNum() - playbackStartFrame; if(currentFrame >= durationInFrames){ if(loopType == OF_LOOP_NONE){ currentFrame = durationInFrames; stop(); } else if(loopType == OF_LOOP_NORMAL) { playbackStartFrame += getDurationInFrames(); currentFrame %= durationInFrames; ofxTLPlaybackEventArgs args = createPlaybackEvent(); ofNotifyEvent(ofxTLEvents.playbackLooped, args); } } } else{ currentTime = ofGetElapsedTimef() - playbackStartTime; if(currentTime >= durationInSeconds){ if(loopType == OF_LOOP_NONE){ currentTime = durationInSeconds; stop(); } else if(loopType == OF_LOOP_NORMAL) { currentTime = fmod(currentTime, durationInSeconds); playbackStartTime += getDurationInSeconds(); ofxTLPlaybackEventArgs args = createPlaybackEvent(); ofNotifyEvent(ofxTLEvents.playbackLooped, args); } } } }
void ofxAssimpAnimation::update() { animationPrevTime = animationCurrTime; animationCurrTime = ofGetElapsedTimef(); if(!bPlay || bPause) { return; } float duration = getDurationInSeconds(); float timeStep = animationCurrTime - animationPrevTime; float positionStep = timeStep / (float)duration; float position = getPosition() + positionStep; if(position > 1.0 && loopType == OF_LOOP_NONE) { position = 1.0; stop(); } else if(position > 1.0 && loopType == OF_LOOP_NORMAL) { position = fmod(position, 1.0f); } else if(position > 1.0 && loopType == OF_LOOP_PALINDROME) { // TODO. } else if(position < 0.0 && loopType == OF_LOOP_PALINDROME) { // TODO. } setPosition(position); }
void ofxAssimpAnimation::setPosition(float position) { position = ofClamp(position, 0.0f, 1.0f); if(progress == position) { return; } progress = position; progressInSeconds = progress * getDurationInSeconds(); progressInMilliSeconds = progress * getDurationInMilliSeconds(); updateAnimationNodes(); }
float AVFile::getPositionInPercents() { return getPositionInSeconds() / getDurationInSeconds(); }
void ofxAssimpAnimation::updateAnimationNodes() { for(unsigned int i=0; i<animation->mNumChannels; i++) { const aiNodeAnim * channel = animation->mChannels[i]; aiNode * targetNode = scene->mRootNode->FindNode(channel->mNodeName); aiVector3D presentPosition(0, 0, 0); if(channel->mNumPositionKeys > 0) { unsigned int frame = 0; while(frame < channel->mNumPositionKeys - 1) { if(progressInSeconds < channel->mPositionKeys[frame+1].mTime) { break; } frame++; } unsigned int nextFrame = (frame + 1) % channel->mNumPositionKeys; const aiVectorKey & key = channel->mPositionKeys[frame]; const aiVectorKey & nextKey = channel->mPositionKeys[nextFrame]; double diffTime = nextKey.mTime - key.mTime; if(diffTime < 0.0) { diffTime += getDurationInSeconds(); } if(diffTime > 0) { float factor = float((progressInSeconds - key.mTime) / diffTime); presentPosition = key.mValue + (nextKey.mValue - key.mValue) * factor; } else { presentPosition = key.mValue; } } aiQuaternion presentRotation(1, 0, 0, 0); if(channel->mNumRotationKeys > 0) { unsigned int frame = 0; while(frame < channel->mNumRotationKeys - 1) { if(progressInSeconds < channel->mRotationKeys[frame+1].mTime) { break; } frame++; } unsigned int nextFrame = (frame + 1) % channel->mNumRotationKeys; const aiQuatKey& key = channel->mRotationKeys[frame]; const aiQuatKey& nextKey = channel->mRotationKeys[nextFrame]; double diffTime = nextKey.mTime - key.mTime; if(diffTime < 0.0) { diffTime += getDurationInSeconds(); } if(diffTime > 0) { float factor = float((progressInSeconds - key.mTime) / diffTime); aiQuaternion::Interpolate(presentRotation, key.mValue, nextKey.mValue, factor); } else { presentRotation = key.mValue; } } aiVector3D presentScaling(1, 1, 1); if(channel->mNumScalingKeys > 0) { unsigned int frame = 0; while(frame < channel->mNumScalingKeys - 1){ if(progressInSeconds < channel->mScalingKeys[frame+1].mTime) { break; } frame++; } presentScaling = channel->mScalingKeys[frame].mValue; } aiMatrix4x4 mat = aiMatrix4x4(presentRotation.GetMatrix()); mat.a1 *= presentScaling.x; mat.b1 *= presentScaling.x; mat.c1 *= presentScaling.x; mat.a2 *= presentScaling.y; mat.b2 *= presentScaling.y; mat.c2 *= presentScaling.y; mat.a3 *= presentScaling.z; mat.b3 *= presentScaling.z; mat.c3 *= presentScaling.z; mat.a4 = presentPosition.x; mat.b4 = presentPosition.y; mat.c4 = presentPosition.z; targetNode->mTransformation = mat; } }