立方体旋转 - OpenGL

时间:2014-03-25 13:27:53

标签: c++ opengl rotation

我听取了关于openGL和旋转的帮助。这是我的代码:

#include <math.h>
#include <GL/glut.h>


float transZ=50;      
float rotateA=0;         

float rotateAspeed=0.0;

void cube (float dimX, float dimY, float dimZ)
{
   glMatrixMode(GL_MODELVIEW);
   glPushMatrix();


      glTranslatef(0,dimY/2,0);

      glScalef(dimX/2, dimY/2, dimZ/2);


  glBegin(GL_QUADS);        
    glColor3f(0.0, 1.0, 0.0);    // Color Green - TOP

    glVertex3f(1.0, 1.0, 1.0); // TOP-RIGHT-NEAR
    glVertex3f(-1.0, 1.0, 1.0); // TOP-LEFT-NEAR
    glVertex3f(-1.0, 1.0, -1.0); //TOP-LEFT-FAR
    glVertex3f(1.0, 1.0, -1.0); // TOP-RIGHT-FAR

    glColor3f(1.0, 0.0, 0.0); // Color RED - Bottom

    glVertex3f(1.0, -1.0, 1.0); //BOTTOM-RIGHT-NEAR
    glVertex3f(-1.0, -1.0, 1.0); //BOTTOM-LEFT-NEAR
    glVertex3f(-1.0, -1.0, -1.0); //BOTTOM-LEFT-FAR
    glVertex3f(1.0, -1.0, -1.0); //BOTTOM-RIGHT-FAR

    glColor3f(1.0, 1.0, 0.0); // Color Yellow - Back

    glVertex3f(1.0, 1.0, -1.0); //TOP-RIGHT-FAR
    glVertex3f(-1.0, 1.0, -1.0); //TOP-LEFT-FAR
    glVertex3f(-1.0, -1.0, -1.0); //BOTTOM-LEFT-FAR
    glVertex3f(1.0, -1.0, -1.0); //BOTTOM-RIGHT-FAR

    glColor3f(0.0, 0.0, 1.0); //Color Blue - RIGHT

    glVertex3f(1.0, 1.0, 1.0); //TOP-FRONT-NEAR
    glVertex3f(1.0, 1.0, -1.0); //TOP-BACK-FAR
    glVertex3f(1.0, -1.0, -1.0); //BOTTOM-BACK-FAR
    glVertex3f(1.0, -1.0, 1.0); //BOTTOM-FRONT-NEAR

    glColor3f(1.0, 0.5, 0.0); //Color Orange - Left

    glVertex3f(-1.0, 1.0, 1.0); //TOP-FRONT-NEAR
    glVertex3f(-1.0, 1.0, -1.0); //TOP-BACK-FAR
    glVertex3f(-1.0, -1.0, -1.0);//BOTTOM-BACK-FAR
    glVertex3f(-1.0, -1.0, 1.0); //BOTTOM-FRONT-NEAR




  glEnd();


   glPopMatrix();
}


void display(void)
{


   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );


   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();


   gluLookAt(transZ*cos(rotateA),10,transZ*sin(rotateA), 0,10,0, 0,1,0);


   cube(30,30,30);


   glFlush();            

   glutSwapBuffers();
}

void init (void)
{
   glClearColor(1.0, 1.0, 1.0, 1.0);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();

   glFrustum(-1, 1, -1, 1, 1, 1000);


   glEnable(GL_DEPTH_TEST); 
}


void keyboard(unsigned char key, int x, int y)
{
   switch (key) {
      case 27:                
         exit(0);
         break;
      case 'S':
         transZ+=1.0f;
         glutPostRedisplay();  
         break;
      case 'W':
         transZ-=1.0f;
         if (transZ<0) transZ=0;
         glutPostRedisplay();  
         break;
      case 's':
         transZ+=0.5f;
         glutPostRedisplay();  
         break;
      case 'w':
         transZ-=0.5f;
         if (transZ<0) transZ=0;
         glutPostRedisplay(); 
         break;
      case 'A':
         rotateAspeed+=0.001f;
         glutPostRedisplay();  
         break;
      case 'a':
         rotateAspeed+=0.001f;
         glutPostRedisplay();  
         break;
      case 'D':
         rotateAspeed-=0.001f;
         glutPostRedisplay();  
         break;
      case 'd':
         rotateAspeed-=0.001f;
         glutPostRedisplay();  
         break;

   }
}

void idle(void)
{
  rotateA=rotateA + rotateAspeed;
  glutPostRedisplay();    
}



int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize (500, 500);
   glutInitWindowPosition (100, 100);
   glutCreateWindow ("Cube");
   init ();

   glutDisplayFunc(display);
   glutIdleFunc(idle); 

   glutKeyboardFunc(keyboard); 
   glutMainLoop();
   return 0;
}

当我按A或D时,立方体水平旋转但我需要制作,当我按下时,例如J立方体开始垂直旋转。有关如何做到这一点的任何帮助吗?

1 个答案:

答案 0 :(得分:1)

您需要查看glRotate以及如何在openGL中完成转换。然后通过三个角度(称为Euler angles)或使用quaternions或其他合适的旋转表示来参数化旋转。从这里转换将正确旋转立方体,它可能如下所示:

glPushMatrix()
glRotatef(alpha, 1, 0, 0); //rotate alpha around the x axis
glRotatef(beta, 0, 1, 0); //rotate beta around the y axis
glRotatef(gamma, 0, 0, 1); //rotate gamma around the z axis

//dram my cube
glPopMatrix();