opengl matrix rotation quaternions

后端 未结 4 961
忘掉有多难
忘掉有多难 2021-01-18 03:20

Im trying to do a simple rotation of a cube about the x and y axis:

I want to always rotate the cube over the x axis by an amount x and rotate the cube over the yax

4条回答
  •  走了就别回头了
    2021-01-18 03:51

    I got this to work correctly using quaternions: Im sure there are other ways, but afeter some reseatch , this worked perfectly for me. I posted a similar version on another forum. http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=280859&#Post280859

    first create the quaternion representation of the angles of change x/y then each frame multiply the changing angles quaternions to an accumulating quaternion , then finally convert that quaternion to matrix form to multiply the current matrix. Here is the main code of the loop:

    Quaternion3D Rotation1=Quaternion3DMakeWithAxisAndAngle(Vector3DMake(-1.0f,0,0), DEGREES_TO_RADIANS(globalRotateX));
    Quaternion3DNormalize(&Rotation1);
    
    Quaternion3D Rotation2=Quaternion3DMakeWithAxisAndAngle(Vector3DMake(0.0f,-1.0f,0), DEGREES_TO_RADIANS(globalRotateY));
    Quaternion3DNormalize(&Rotation2);
    
    
    Matrix3D Mat;
    Matrix3DSetIdentity(Mat);
    Quaternion3DMultiply(&QAccum, &Rotation1);
    
    Quaternion3DMultiply(&QAccum, &Rotation2);
    
    Matrix3DSetUsingQuaternion3D(Mat, QAccum);
    globalRotateX=0;
    globalRotateY=0;
    
    glMultMatrixf(Mat);
    

    then draw cube

提交回复
热议问题