在不同方向旋转相同的立方体

时间:2016-04-19 08:55:41

标签: c++ opengl rotation

我试图在不同方向旋转立方体。但无论何时旋转,都会绘制一个新的立方体。但我希望同一个立方体在不同的方向旋转。怎么做?

这就是我现在正在做的事情。

static GLfloat theta[]={0.0,0.0,0.0};
GLint axis =0;

GLubyte cubeIndices[]={0,3,2,1,
                    2,3,7,6,
                    0,4,7,3,
                    1,2,6,5,
                    4,5,6,7,
                    0,1,5,4};

void displayCallBack()
{


glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();


if(ch=='u')
gluLookAt(0.0,1.0,1.0,0.0,1.0,0.0,0.0,-1.0,0.0);//- vertical
else if(ch=='r') 
gluLookAt(0.0,0.0,0.0,0.0,1.0,0.0,1.0,1.0,0.0);//-right
else if(ch=='l')
gluLookAt(0.0,0.0,0.0,0.0,1.0,0.0,-1.0,1.0,0.0);//-left
else if(ch=='d')
gluLookAt(0.0,-1.0,1.0,0.0,-1.0,0.0,0.0,1.0,0.0);//-downward

glTranslatef(0.0,0.0,0.0);
glRotatef(theta[0],1.0,0.0,0.0);
glRotatef(theta[1],0.0,1.0,0.0);
glRotatef(theta[2],0.0,0.0,1.0);
glColorPointer(3,GL_FLOAT,0,colors);
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,cubeIndices);
glFlush();
glutSwapBuffers();
}

void myKeyboard(unsigned char key ,int x,int y)
{
switch(key)
{
case 'l': ch='l'; theta[axis]+=90; glutPostRedisplay(); break;
case 'r': ch='r'; theta[axis]+=90; glutPostRedisplay(); break;
case 'u': ch='u'; theta[axis]+=90; glutPostRedisplay(); break;
case 'd': ch='d'; theta[axis]+=90; glutPostRedisplay(); break;
}

}

0 个答案:

没有答案