我目前可以围绕枢轴点旋转,方法是首先平移到枢轴点,然后执行旋转,最后平移回原点。在我的例子中,我很容易就肩部做到这一点。但是我不知道如何为前臂添加绕肘部的旋转。
我尝试了以下方法来使前臂围绕肘部旋转:
- 平移到肩膀、旋转、平移到原点、平移到前臂、旋转、平移到原点
- 平移到肩膀、旋转、平移到前臂、旋转、平移到肩膀、平移到原点
都不适合我。有什么建议么?我真的被这个问题困住了。
最佳答案
我在做一些骨骼动画时遇到了类似的问题。为此使用递归非常有帮助。另外,按层次结构构建骨骼(例如,肩膀是前臂的父级,前臂是手的父级,等等)。通过这样做,您可以编写代码如下:
void drawBone(Bone *root) {
if (!root) return;
glPushMatrix();
glTranslatef(root->x,root->y,0);
glRotatef(root->a,0,0,1);
// insert code to actually draw bone here
int i;
glTranslatef(root->l,0,0);
for (i=0; i<root->childCount; i++)
drawBone(root->child[i]);
glPopMatrix();
}
我的骨骼结构如下所示:
typedef struct _Bone {
float x,y,a,l; // position, angle, length of bone
int childCound; // number of children for this bone
struct _Bone *child[MAX_CHILD_COUNT], *parent;
} Bone;
这个website是骨骼动画的重要资源。
关于人类的 opengl 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5494644/