代码之家  ›  专栏  ›  技术社区  ›  F'x

在iPhone上使用CADisplayLink在OpenGL ES视图中实现匀速旋转

  •  0
  • F'x  · 技术社区  · 14 年前

    GLES2Sample code sample . 我用它们来显示3D物体在一个轴上平滑地旋转,这是我期望的恒定旋转速度。目前,应用程序使用1的帧间隔,每次绘制OpenGL视图(在eagleview的 drawView 方法),将模型旋转一定角度。

    在实践中,这给出了不错的结果,但并不完美:在旋转过程中,当对象的大部分离开视线时,渲染会变得更快,因此旋转不具有恒定的角速度。我的问题是:

    尽管我欢迎所有的建议,但我已经有了一个想法:每半秒测量一次渲染FPS,并在此基础上调整每次重画时的旋转角度。不过,这听起来不太好,所以:你对此有何看法,你将如何处理这个问题?

    2 回复  |  直到 14 年前
        1
  •  3
  •   Tommy    14 年前

    我倾向于使用一个CADisplayLink来触发新的帧,并在帧请求中使用一个简单的时间计算来计算出在多大程度上提升我的逻辑。

    - (void)displayLinkDidTick
    {
        // get the time now
        NSTimeInterval timeNow = [NSDate timeIntervalSinceReferenceDate];
    
        // work out how many quantums (rounded down to the nearest integer) of
        // time have elapsed since last we drew
        NSTimeInterval timeSinceLastDraw = timeNow - timeOfLastDraw;
        NSTimeInterval desiredBeatsPerSecond = 60.0;
        NSTimeInterval desiredTimeInterval = 1.0 / desiredBeatsPerSecond;
    
        NSUInteger numberOfTicks = (NSUInteger)(timeSinceLastDraw / desiredTimeInterval);
    
        if(numberOfTicks > 8)
        {
            // if we're more than 8 ticks behind then just do 8 and catch up
            // instantly to the correct time
            numberOfTicks = 8;
            timeOfLastDraw = timeNow;
        }
        else
        {
            // otherwise, advance timeOfLastDraw according to the number of quantums
            // we're about to apply. Don't update it all the way to now, or we'll lose
            // part quantums
            timeOfLastDraw += numberOfTicks * desiredTimeInterval;
        }
    
        // do the number of updates
        while(numberOfTicks--)
            [self updateLogic];
    
        // and draw
        [self draw];
    }
    

    在您的情况下,updateLogic将应用固定的旋转量。如果您真正想要的是恒定旋转,那么您可以将旋转常数乘以numberOfTicks,甚至跳过整个方法,执行如下操作:

    glRotatef([NSDate timeIntervalSinceReferenceData] * rotationsPerSecond, 0, 0, 1);
    

    而不是保留自己的变量。不过,在除了最琐碎的情况之外的任何情况下,您通常都希望在每个时间量中做一大堆复杂的事情。

        2
  •  1
  •   user487158    14 年前

    1)优化你的代码,这样它就不会徘徊在60 fps以下——你的模型在任何情况下的设备的最大帧速率。

    2) 在运行时,通过几个完整的周期测量应用程序的帧速率,并设置绘制速率,使其永远不会超过最低的测量绘制性能。

    干杯。

    推荐文章