[英]UIImageView Rotation Animation using CADisplayLink - Tracking Rotation
[英]Constant-velocity rotation in OpenGL ES view using CADisplayLink on iPhone
我的OpenGL ES類和代碼均來自Apple的GLES2Sample代碼示例 。 我用它們來顯示3D對象繞一個軸平穩地旋轉,並以恆定的旋轉速度實現。 目前,該應用使用的幀間隔為1,並且每次繪制OpenGL視圖(在EAGLView的drawView
方法中)時,我都會將模型旋轉一定角度。
實際上,這給出了不錯的結果,但並不是完美的結果:在旋轉過程中,當物體的大部分不可見時,渲染變得更快,因此旋轉沒有恆定的角速度。 我的問題是: 如何使它更平滑?
盡管我歡迎所有建議,但我已經有了一個主意:每半秒測量一次渲染FPS,並在此基礎上調整每次重繪的旋轉角度。 但是,這聽起來不太好:您對此有何看法,您將如何處理該問題?
我傾向於使用CADisplayLink觸發新幀,並在幀請求中進行簡單的時間計算,以弄清楚我的邏輯前進了多少。
假設您有一個NSTimeInterval類型的成員變量timeOfLastDraw。 您希望您的邏輯以每秒60次的速度跳動。 然后(使用一堆變量使代碼更清晰):
- (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);
而不是保留自己的變量。 但是,在最瑣碎的情況下,您通常希望每個時間段執行一堆復雜的事情。
如果您不希望渲染速度變化,並且不希望通過CADisplayLink或其他動畫計時器運行開環(即全傾斜),則可以執行以下兩項操作:
1)優化您的代碼,使其永遠不會低於60 FPS(在任何情況下使用您的型號設備的最大幀速率)。
2)在運行時,通過幾個完整的周期來測量應用程序的幀速率,並設置繪制速率,以使其永遠不會超過最低的測量繪制性能。
我認為,調整旋轉角度不是解決此問題的正確方法,因為您現在嘗試使兩個參數彼此保持同步(繪制速率和旋轉速率),而不是簡單地固定一個參數:繪制速率。
干杯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.