简体   繁体   中英

Leveraging Core Animation for Animation in OpenGL

I'm interested in using Core Animation to drive custom animations in my OpenGL scene on the iPhone. I was hoping to find a mechanism that allowed you to generically animate a keypath of an NSObject or a delegate method for each animation tick. What I have come up with is to animate an arbitrary property of a CALayer and poll them from OpenGL to determine the progression of the animation. However, I'm convinced there's a better way to do it. What is the cleanest, safest way to tap into this great animation engine ?

I suspect what you really want are just the easing functions used by core animation. These are easy enough to find with a little googling. Here's one example:

GLfloat bounceEaseOut(GLfloat ratio) {
    if (ratio < 1.0f/2.75f) {
        return 7.5625f*ratio*ratio;
    } else if (ratio < 2.0f/2.75f) {
        return 7.5625f*(ratio-=1.5f/2.75f)*ratio+0.75f;
    } else if (ratio < 2.5f/2.75f) {
        return 7.5625f*(ratio-=2.25f/2.75f)*ratio+0.9375f;
    } else {
        return 7.5625f*(ratio-=2.625f/2.75f)*ratio+0.984375f;
    }
}

You pass in how far into your tween you are (elapsedTime/tweenDuration) and it tells you what percent of the way through the tween's "timeline" you should be.

I was trying to do something similar, update a gl view based on its position in its parent during a CA animation. I was getting similar results, choppy animation which wasn't in sync with the CA animation. I finally decided to stop using CA and use a different interpolation engine entirely. Now I'm using PRTween . It's working very well. I'm sure there are some draw backs (your animations are running on the main thread, the interface isn't as nice as CA, and it's a little buggy) but my animations are running smooth.

If you want to animate view itself try to read iPhone UIView Animation Best Practice . For animation inside OpenGL view it's better to use animated models or custom code but not Core Animation.

This is possible, to use Core Animation to drive whatever you wish. I am sure it can be used for sound, too! (Given a little subclassing of CALayer). UIKit and Core Animation can be mixed quite freely, with a little care, as there is CAEAGLLayer for us to play with as well.

The key here is -(id)presentationLayer and -(id)modelLayer for using pure/only OpenGL; of course even the hierarchy can be used for your OpenGL scene too. But, using CALayer for rendering as well gets even more "for free", if this is possible for your circumstances (but also see CATransformLayer), if this is what you seek! =)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM