教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 资格考试 >

opengl es 从零开始系列9动画基础和关键帧动画(4)

来源:网络收集 时间:2026-08-27
导读: 不过是比Vector 3D多加了一个GLfloat,经常把它当成w字段。所以对我们来说一个四元数就象这样:typedef struct { GLfloat x; GLfloat y; GLfloat z; GLfloat w;} Quaternion3D;Normalizing a Quaternion 四元数归一

不过是比Vector 3D多加了一个GLfloat,经常把它当成w字段。所以对我们来说一个四元数就象这样:typedef struct { GLfloat x; GLfloat y; GLfloat z; GLfloat w;} Quaternion3D;Normalizing a Quaternion 四元数归一化这个十分简单。四元数代表空间里的一个方向,就像Vector3Ds,实际距离的值并不在意,并且在进行一些计算之前/之后使他们正常下降到1.0。完成这些我们可以这样做:static inline void Quaternion3DNormalize(Quaternion3D

*quaternion){ GLfloat magnitude; magnitude =

sqrtf((quaternion->x * quaternion->x) + (quaternion->y * quaternion->y) + (quaternion->z * quaternion->z) + (quaternion->w * quaternion->w)); quaternion->x /= magnitude; quaternion->y /= magnitude; quaternion->z /= magnitude;

quaternion->w /= magnitude;}Creating a Quaternion from a Rotation matrix 从一个旋转矩阵中创建一个四元数 如果我们没法在四元数同其他的对象转换过程中保存旋转角度信息的话,四元数对我们来说还是没用的。首先我们从一个旋转矩阵中创建一个四元数。代码如下: static inline Quaternion3D Quaternion3DMakeWithMatrix3D(Matrix3D matrix){ Quaternion3D quat; GLfloat trace, s; trace

= matrix[0] + matrix[5] + matrix[10]; if (trace > 0.0f) { s = sqrtf(trace + 1.0f); quat.w = s * 0.5f; s = 0.5f / s; quat.x = (matrix[9] - matrix[6]) * s; quat.y = (matrix[2] - matrix[8]) * s; quat.z = (matrix[4] - matrix[1]) * s; } else { NSInteger biggest; enum {A,E,I}; if (matrix[0] > matrix[5]) if (matrix[10] > matrix[0]) biggest = I; else biggest = A; else if (matrix[10] > matrix[0]) biggest = I; else biggest = E; switch (biggest) { case A: s = sqrtf(matrix[0] - (matrix[5] + matrix[10]) + 1.0f);

if (s > QUATERNION_TRACE_ZERO_TOLERANCE) { quat.x = s * 0.5f; s = 0.5f / s; quat.w = (matrix[9] - matrix[6]) * s; quat.y = (matrix[1] + matrix[4]) * s; quat.z = (matrix[2] +

matrix[8]) * s; break; } s = sqrtf(matrix[10] - (matrix[0] + matrix[5]) + 1.0f); if (s > QUATERNION_TRACE_ZERO_TOLERANCE) { quat.z = s * 0.5f; s = 0.5f / s; quat.w = (matrix[4] -

matrix[1]) * s; quat.x = (matrix[8] + matrix[2]) * s; quat.y = (matrix[9] +

matrix[6]) * s; break; } s = sqrtf(matrix[5] - (matrix[10] + matrix[0]) + 1.0f); if (s > QUATERNION_TRACE_ZERO_TOLERANCE) { quat.y = s * 0.5f; s = 0.5f / s; quat.w = (matrix[2] - matrix[8]) * s; quat.z = (matrix[6] + matrix[9]) * s; quat.x = (matrix[4] +

matrix[1]) * s; break; break; case E: = sqrtf(matrix[5] - (matrix[10] + matrix[0]) + 1.0f); if (s > QUATERNION_TRACE_ZERO_TOLERANCE) { quat.y = s * 0.5f; s = 0.5f / s; quat.w = (matrix[2] - matrix[8]) * s; quat.z = (matrix[6] + matrix[9]) * s; quat.x = (matrix[4] +

matrix[1]) * s; break; s = sqrtf(matrix[10] - (matrix[0] + matrix[5]) + 1.0f); if (s > QUATERNION_TRACE_ZERO_TOLERANCE) { quat.z = s * 0.5f; s = 0.5f / s; quat.w = (matrix[4] -

} s } matrix[1]) * s; quat.x = (matrix[8] + matrix[2]) * s; quat.y = (matrix[9] +

matrix[6]) * s; break; } s = sqrtf(matrix[0] - (matrix[5] + matrix[10]) + 1.0f); if (s > QUATERNION_TRACE_ZERO_TOLERANCE) { quat.x = s * 0.5f; s = 0.5f / s; quat.w = (matrix[9] - matrix[6]) * s; quat.y = (matrix[1] + matrix[4]) * s; quat.z = (matrix[2] +

matrix[8]) * s; break; break; case I: = sqrtf(matrix[10] - (matrix[0] + matrix[5]) + 1.0f); if (s > QUATERNION_TRACE_ZERO_TOLERANCE) { quat.z = s * 0.5f; s = 0.5f / s; quat.w = (matrix[4] - matrix[1]) * s; quat.x = (matrix[8] + matrix[2]) * s; quat.y = (matrix[9] +

matrix[6]) * s; break; s = sqrtf(matrix[0] - (matrix[5] + matrix[10]) + 1.0f); if (s > QUATERNION_TRACE_ZERO_TOLERANCE) { quat.x = s * 0.5f; s = 0.5f / s; quat.w = (matrix[9] -

} s } matrix[6]) * s; quat.y = (matrix[1] + matrix[4]) * s; quat.z = (matrix[2] +

matrix[8]) * s; break; } s = sqrtf(matrix[5] - (matrix[10] + matrix[0]) + 1.0f); if (s > QUATERNION_TRACE_ZERO_TOLERANCE) { quat.y = s * 0.5f; s = 0.5f / s; quat.w = (matrix[2] - matrix[8]) * s; quat.z = (matrix[6] + matrix[9]) * s; quat.x = (matrix[4] +

matrix[1]) * s; break; } break; default: break; } } return quat;}好的,如果你想真正知道这里 …… 此处隐藏:2077字,全部文档内容请下载后查看。喜欢就下载吧 ……

opengl es 从零开始系列9动画基础和关键帧动画(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/413701.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)