旋转多部分对象 [英] Rotating a multipart object

查看:89
本文介绍了旋转多部分对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个对象,该对象具有大约7个以上的部分,包括其主体和在不同位置附加"到其上的较小部分.我的目标是旋转整个对象.我试图在构造整个对象之前简单地调用glRotatef(angle, 0, 1, 0),但是我意识到,无论翻译如何,这似乎都围绕着原点旋转了一切".以下代码是尝试旋转主体本身并旋转其附加零件的尝试.

I've created an object that has about 7+ parts to it including its body and smaller parts that 'attach' to it in different places. My goal is to rotate the entire object. I tried to simply call glRotatef(angle, 0, 1, 0) before constructing the entire object, but I realize that this seems to rotate 'everything' around the origin, no matter the translation. The following code was an attempt to rotate the body itself and rotate the attached parts to it.

// glRotatef(angle, 0, 1, 0); //old way of rotating the object

// body
glPushMatrix();
    // movement
    glTranslatef(subx, suby + y, subz);
    //rotating the body itself
    glRotatef(angle, 0, 1, 0); 
    // starting position of the body
    glScalef(9.0, 1.75, 1.75);
    glTranslatef(-subx, -suby, -subz);
    glTranslatef(subx, suby, subz);
    glutSolidSphere(1.0, 50, 50);
glPopMatrix();

// attached part
glPushMatrix();
    // movement
    glTranslatef(rot1x, rot1y + y, rot1z); 
    // attempting to rotate the part while 'attached to' the body
    glRotatef(angle, 0, 1, 0);
    //placing the part on the object in starting position
    glRotatef(rot1angle, rot1xrot, rot1yrot, rot1zrot);
    glTranslatef(-rot1x, -rot1y, -rot1z);
    glTranslatef(rot1x, rot1y, rot1z);
    gluPartialDisk(gluNewQuadric(), 0, 1, 50, 1, 0, 100.0);
glPopMatrix();

为了使对象的较小部分随对象的身体正确旋转(固定点?),我似乎无法将头部缠绕在发生什么情况上.感谢您的帮助.

I can't seem to wrap my head around what needs to happen in order for the smaller parts of the object to rotate properly with the object's body (a fixed point?). Thanks for your help.

推荐答案

矩阵堆栈上的操作基于彼此.每个操作的参考系统是当前转换.如果要转换由一堆对象组成的对象,则必须知道每个子对象相对于对象并集的参考位置的相对位置.然后,您必须执行以下步骤:

The operations on the matrix stack are based on one another. The reference system of each operation is the current transformation. If you want to transform a object which consists of a bunch of objects, then you have to know the relative position of each sub object to a reference position of the object union. Then you have to do the following steps:

  • 将每个对象移动到世界上的共同位置(glTranslate).
  • 调整对象(glRotate)
  • 将每个对象移动到对象联合中的相对位置
  • Move each object to a common position in the world (glTranslate).
  • Orientate the objects (glRotate)
  • Move each object to its relative position in the object union

// dynamic position in the world
float refPosX, refPosY, refPosZ;

// dynamic orientation
float angle;

// constant positions of the sub object relative to the object union
float subPosX[], subPosY[], subPosZ[];


for ( int i = 0 i < noOfObj, ++i ) // for each object
{
  glPushMatrix();
  glTranslatef(refPosX, refPosY, refPosZ);
  glRotatef(angle, 0, 1, 0); 
  glTranslatef(subPosX[i], subPosY[i], subPosZ[i]);
  glScalef(9.0, 1.75, 1.75);

  ..... // draw the object here

  glPopMatrix();
}


请参见 glTranslate :


See the documentation of glTranslate:

glTranslate产生x y z的翻译.当前矩阵(请参见glMatrixMode)乘以该转换矩阵,乘积替换当前矩阵,

glTranslate produces a translation by x y z . The current matrix (see glMatrixMode) is multiplied by this translation matrix, with the product replacing the current matrix,

并参阅 glRotate :

glRotate围绕矢量x y z产生角度度的旋转.当前矩阵(参见glMatrixMode)乘以旋转矩阵,乘积替换当前矩阵,

glRotate produces a rotation of angle degrees around the vector x y z . The current matrix (see glMatrixMode) is multiplied by a rotation matrix with the product replacing the current matrix,


请注意,转换矩阵如下所示:


Note, the translation matrix looks like this:

Matrix4x4 translate;

translate[0] : ( 1,  0,  0,  0 )
translate[1] : ( 0,  1,  0,  0 )
translate[2] : ( 0,  0,  1,  0 )
translate[3] : ( tx, ty, tz, 1 )

围绕Y轴的旋转矩阵如下所示:

And the rotation matrix around Y-Axis looks like this:

Matrix4x4  rotate;
float      angle;

rotate[0] : ( cos(angle),  0, sin(angle), 0 )
rotate[1] : ( 0,           1, 0,          0 )
rotate[2] : ( -sin(angle), 0, cos(angle), 0 )
rotate[3] : ( 0,           0, 0,          1 ) 

矩阵乘法的工作原理如下:

A matrix multiplication works like this:

Matrix4x4 A, B, C;

// C = A * B
for ( int k = 0; k < 4; ++ k )
    for ( int l = 0; l < 4; ++ l )
        C[k][l] = A[0][l] * B[k][0] + A[1][l] * B[k][1] + A[2][l] * B[k][2] +  A[3][l] * B[k][3];


translate * rotate的结果是这样的:


The result of translate * rotate is this:

model[0] : ( cos(angle),  0,  sin(angle), 0 )
model[1] : ( 0,           1,  0,          0 )
model[2] : ( -sin(angle), 0,  cos(angle), 0 )
model[3] : ( tx,          ty, tz,         1 )


请注意,rotate * translate的结果将是:


Note, the result of rotate * translate would be:

model[0] : ( cos(angle),                     0,   sin(angle),                     0 )
model[1] : ( 0,                              1,   0,                              0 )
model[2] : ( -sin(angle),                    0,   cos(angle),                     0 )
model[3] : ( cos(angle)*tx - sin(angle)*tx,  ty,  sin(angle)*tz + cos(angle)*tz,  1 )

这篇关于旋转多部分对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆