GLUT矩阵乘法在不使用glRotate的情况下在自己的轴上旋转对象

时间:2010-08-29 17:23:55

标签: c++ visual-c++

*我说在自己的轴上旋转一个物体,但我认为我的术语是错的......我的意思就是从对象中心旋转我的物体而不是原点

我正在尝试制作一个程序,在不使用glRotate或glTranslate的情况下,在我自己的轴上旋转我的对象(通过使用点/顶点矩阵) 我所做的是制作一个MatrixMultiplication函数和3个单独的函数,用于在X,Y和Z轴上旋转对象,以及另一个使用矩阵和矩阵乘法函数转换它们的函数:

void MMMultiplication(float fVector[], float fMatrix[])
{
    float tmpFloat;
for(int a = 0; a <= 72; a+=4)//this is 72 because the elements of my object's matrix 
                               is also 72
{
        for(int indx2= 0; indx2 < 4; indx2++) 
        {
            tmpFloat = 0.0f;
            for(int indx = 0; indx < 4; indx++)
    {
         tmpFloat = tmpFloat +
         (fVector[indx+a] * fMatrix[(indx*4) + indx2]);
    }
    fVector[indx2+a] = tmpFloat;
}
}

}

和另外3个函数,它们代表x y和z旋转矩阵以及平移矩阵。

程序会要求x,y,z坐标转换那里的对象然后你可以使用z,x,c键盘按钮分别在x,y,z轴上旋转它,它会旋转对象它自己的轴

我所做的是调用一个在RenderScene中绘制对象的函数,并使用glutKeyboard函数在x,y,z轴上旋转我的对象......

到目前为止,我的程序只会在原点中正确旋转我的对象。如果我尝试在其他地方翻译这个项目,那么每次按z,x或c时它都会“添加”到翻译中...我明白我需要先将对象翻译成原点然后将其翻译回来但是作为我说它最终会翻译越来越远

无论如何,这里是我迄今为止使用的代码...

void main(int argc, char **argv)
{

cout<<"Input:(x,y,z)";
cin>>arbitrary[0];
cin>>arbitrary[1];
cin>>arbitrary[2];
arbitrary[3] = 1.0f;
TranslateMatrix(initalpos,object,arbitrary[0],arbitrary[1],arbitrary[2]);




glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(1024,768);
glutCreateWindow("Rotation");

glutReshapeFunc(changeSize);

Lighting(false);
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(inputKey);

glutDisplayFunc(renderScene);
glutTimerFunc(0,Timer,0);

// Enable Depth Testing
glEnable(GL_DEPTH_TEST);    

glutMainLoop();

getch();
return;



_getch();
return;
}

void renderScene()
{
    glClear(GL_COLOR_BUFFER_BIT |
    GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);






    glPushMatrix(); 
    DrawObject(object);
    glPopMatrix();


    glutSwapBuffers();

}

void rotatex(float xangle)
{
    glPushMatrix();
    TranslateVector(object,initalpos,arbitrary[0],arbitrary[1],arbitrary[2]);
    RotateX(object, xangle);//this is my x-axis rotation
    TranslateMatrix(object,initalpos,arbitrary[0], arbitrary[1], arbitrary[2]);
    glPopMatrix();
    return;
}

void rotatey(float yangle)
{
    glPushMatrix();
    TranslateVector(object,initalpos,arbitrary[0],arbitrary[1],arbitrary[2]);
    RotateY(object, yangle);//this is my y-axis rotation matrix
    TranslateMatrix(object,initalpos,arbitrary[0] ,arbitrary[1],arbitrary[2]);
    glPopMatrix();
    return;
}

void rotatez(float zangle)
{
    glPushMatrix();
    TranslateVector(object,initalpos,arbitrary[0],arbitrary[1],arbitrary[2]);
    RotateZ(object, zangle);//this is my z-axis rotation matrix matrix
    TranslateMatrix(object,initalpos,arbitrary[0],arbitrary[1],arbitrary[2]);
    glPopMatrix();
    return;
}

void TranslateMatrix
  (float fSVector[],float fEVector[], float fX, float fY, float fZ)
{
  float fTranslationMatrix[16];

  for (int indx = 0; indx < 16; indx++)
  {
    fTranslationMatrix[indx] = 0;
  }  
  fTranslationMatrix[0] = 1;
  fTranslationMatrix[5] = 1;
  fTranslationMatrix[10] = 1;
  fTranslationMatrix[15] = 1;

  fTranslationMatrix[12] = fX;
  fTranslationMatrix[13] = fY;
  fTranslationMatrix[14] = fZ;

  MMMultiplication(fSVector, fTranslationMatrix);
  MMMultiplication(fEVector, fTranslationMatrix);
}

void processNormalKeys(unsigned char key, int x, int y) 
{

    switch(key)
    {
    case 27:
        exit(0);
    case 'X':
    case 'x':
        {
          rotatex(0.5f);break;

        }
    case 'c':
    case 'C':
        {
          rotatey(0.5f);break;

        }
    case 'Z':
    case 'z':
        {
            rotatez(0.5f);break;

        }
    }   
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

发现解决方案哈哈哈花了我一段时间才意识到原因是这个......我刚刚改变了这3个功能......

void rotatex(float xangle)
{
    glPushMatrix();
    TranslateMatrix(object,initalpos,-arbitrary[0],-arbitrary[1],-arbitrary[2]);
    RotateX(object, xangle);
    TranslateMatrix(object,initalpos,arbitrary[0], arbitrary[1], arbitrary[2]);
    glPopMatrix();
    return;
}
void rotatey(float yangle)
{
    glPushMatrix();
    TranslateMatrix(object,initalpos,-arbitrary[0],-arbitrary[1],-arbitrary[2]);
    RotateY(object, yangle);
    TranslateMatrix(object,initalpos,arbitrary[0],arbitrary[1],arbitrary[2]);
    glPopMatrix();
    return;
}
void rotatez(float zangle)
{
    glPushMatrix();
    TranslateMatrix(object,initalpos,-arbitrary[0],-arbitrary[1],-arbitrary[2]);
    RotateZ(object, zangle);
    TranslateMatrix(object,initalpos,arbitrary[0],arbitrary[1],arbitrary[2]);
    glPopMatrix();
    return;
}