opengl es 从零开始系列9动画基础和关键帧动画(2)
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) {
…… 此处隐藏:1246字,全部文档内容请下载后查看。喜欢就下载吧 ……相关推荐:
- [资格考试]机械振动与噪声学部分答案
- [资格考试]空调工程课后思考题部分整合版
- [资格考试]电信登高模拟试题
- [资格考试]2018年上海市徐汇区中考物理二模试卷(
- [资格考试]坐标转换及方里网的相关问题(椭球体、
- [资格考试]语文教研组活动记录表
- [资格考试]广东省2006年高应变考试试题
- [资格考试]LTE学习总结—后台操作-数据配置步骤很
- [资格考试]北京市医疗美容主诊医师和外籍整形外科
- [资格考试]中学生广播稿400字3篇
- [资格考试]CL800双模站点CDMA主分集RSSI差异过大
- [资格考试]泵与泵站考试复习题
- [资格考试]4个万能和弦搞定尤克里里即兴弹唱(入
- [资格考试]咽喉与经络的关系
- [资格考试]《云南省国家通用语言文字条例》学习心
- [资格考试]标准化第三范式
- [资格考试]GB-50016-2014-建筑设计防火规范2018修
- [资格考试]五年级上册品社复习资料(第二单元)
- [资格考试]2.对XX公司领导班子和班子成员意见建议
- [资格考试]关于市区违法建设情况的调研报告
- 二0一五年下半年经营管理目标考核方案
- 2014年春八年级英语下第三次月考
- 北师大版语文二年级上册第十五单元《松
- 2016国网江苏省电力公司招聘高校毕业生
- 多渠道促家长督导家长共育和谐 - 图文
- 2018 - 2019学年高中数学第2章圆锥曲线
- 竞争比合作更重要( - 辩论准备稿)课
- “案例积淀式”校本研训的实践与探索
- 新闻必须客观vs新闻不必客观一辩稿
- 福师大作业 比较视野下的外国文学
- 新编大学英语第二册1-7单元课文翻译及
- 年产13万吨天然气蛋白项目可行性研究报
- 河南省洛阳市2018届高三第二次统一考试
- 地下车库建筑设计探讨
- 南京大学应用学科教授研究方向汇编
- 2018年八年级物理全册 第6章 第4节 来
- 毕业论文-浅析余华小说的悲悯性 - 以《
- 2019年整理乡镇城乡环境综合治理工作总
- 广西民族大学留学生招生简章越南语版本
- 故宫旧称紫禁城简介




