NSTimeInterval timeSinceLastKeyFrame = [NSDate
timeIntervalSinceReferenceDate] - lastKeyframeTime;
if (timeSinceLastKeyFrame > kAnimationDuration) { direction = !direction;
timeSinceLastKeyFrame = timeSinceLastKeyFrame - kAnimationDuration;
lastKeyframeTime = [NSDate timeIntervalSinceReferenceDate]; }
NSTimeInterval percentDone = timeSinceLastKeyFrame / kAnimationDuration; VertexData3D *source, *dest;
if (direction == kAnimationDirectionForward) {
source = (VertexData3D *)Ball1VertexData; dest = (VertexData3D *)Ball2VertexData; } else {
source = (VertexData3D *)Ball2VertexData; dest = (VertexData3D *)Ball1VertexData; }
for (int i = 0; i < kBall1NumberOfVertices; i++) {
GLfloat diffX = dest[i].vertex.x - source[i].vertex.x; GLfloat diffY = dest[i].vertex.y - source[i].vertex.y; GLfloat diffZ = dest[i].vertex.z - source[i].vertex.z; GLfloat diffNormalX = dest[i].normal.x - source[i].normal.x; GLfloat diffNormalY = dest[i].normal.y - source[i].normal.y; GLfloat diffNormalZ = dest[i].normal.z - source[i].normal.z; ballVertexData[i].vertex.x = source[i].vertex.x + (percentDone * diffX);
ballVertexData[i].vertex.y = source[i].vertex.y + (percentDone * diffY);
ballVertexData[i].vertex.z = source[i].vertex.z + (percentDone * diffZ);
ballVertexData[i].normal.x = source[i].normal.x + (percentDone * diffNormalX);
ballVertexData[i].normal.y = source[i].normal.y + (percentDone * diffNormalY);
ballVertexData[i].normal.z = source[i].normal.z + (percentDone * diffNormalZ); }
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY); glVertexPointer(3, GL_FLOAT, sizeof(VertexData3D), &Ball2VertexData[0].vertex);
glNormalPointer(GL_FLOAT, sizeof(VertexData3D), &Ball2VertexData[0].normal);
glDrawArrays(GL_TRIANGLES, 0, kBall1NumberOfVertices); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); }
首先,有一些初始化设置。我创建了一个静态变量来追踪当前帧是否是最后一帧,这用来判定当前流逝的时间。首先我们初始化当前的时间,然后声明变量来追踪我们的动画是向前还是向后的。
static NSTimeInterval lastKeyframeTime = 0.0; if (lastKeyframeTime == 0.0)
lastKeyframeTime = [NSDate timeIntervalSinceReferenceDate]; static AnimationDirection direction = kAnimationDirectionForward;
然后是一些OpenGL ES一般设置。唯一需要注意的是我把x
轴旋转了-90°。我们知道OpenGL ES使用Y轴向上的坐标体系,同样的我们旋转为Z轴向上。
glClearColor(1.0, 1.0, 1.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();
glTranslatef(0.0f,2.2f,-6.0f);
glRotatef(-90.0, 1.0, 0.0, 0.0); // Blender uses Z-up, not Y-up like OpenGL ES
接下来,声明一个静态数组来存储插值数据:
static VertexData3D
ballVertexData[kBall1NumberOfVertices];
为了简单,我设置了一个颜色并且开启了color materials。我不想使使用texture(纹理)或者materials(材质)使这个例子变得更加混乱。
glColor4f(0.0, 0.3, 1.0, 1.0); glEnable(GL_COLOR_MATERIAL);
现在我计算出上一个帧过去到现在的时间,如果这个时间大于动画时长,改变动画的运动方向。
NSTimeInterval timeSinceLastKeyFrame = [NSDate
timeIntervalSinceReferenceDate] - lastKeyframeTime;
if (timeSinceLastKeyFrame > kAnimationDuration) { direction = !direction;
timeSinceLastKeyFrame = timeSinceLastKeyFrame - kAnimationDuration;
lastKeyframeTime = [NSDate timeIntervalSinceReferenceDate]; }
NSTimeInterval percentDone = timeSinceLastKeyFrame / kAnimationDuration;
为了适应双向动画,我声明了两个指针指向源帧和目的帧的数据,并且根据当前的方向指向适当的数据数组。
VertexData3D *source, *dest;
if (direction == kAnimationDirectionForward) {