Rotating a multipart object

前端 未结 2 994
醉酒成梦
醉酒成梦 2021-01-16 10:53

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.

2条回答
  •  执念已碎
    2021-01-16 11:37

    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:

    • 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();
    }
    


    See the documentation of glTranslate:

    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,

    and see the documentation of glRotate:

    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 )
    

    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];
    


    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 )
    


    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 )
    

提交回复
热议问题