如何让轮子在(GLUT)openGL中绕其中心旋转,而无需按任何键或单击鼠标按钮

时间:2013-12-31 02:33:48

标签: opengl

我需要让这个轮子在openGL的中心周围连续旋转动画而不点击鼠标,因为如果单击鼠标的左键,轮子就会旋转!!这就是轮子:

enter image description here

这是我用来绘制轮子并进行旋转的代码:

#include <freeglut.h>

#include <glaux.h>


void whiteStud()

{


        glColor3f(1.0, 1.0, 1.0);
        glutSolidSphere(0.01, 16, 16);


}

void blackWheel() 

{


       glColor3f(0.129412, 0.129412, 0.129412);
       glutSolidSphere(0.1, 16, 16);


}


void wheelWithStuds()

   {  
       /**********************************/
        int iTimeElapsed = glutGet(GLUT_ELAPSED_TIME);
        float fScale= 0.5f;
        long i;
       /**********************************/
       /* clear all pixels */
       glClear(GL_COLOR_BUFFER_BIT);
       /**********************************/
       glPushMatrix();
       glTranslatef(0.25, 0.25, 0.0);
       glRotatef(iTimeElapsed * fScale,0.0,0.0,1.0);
       blackWheel(); // draw the wheel without studs.
       /**********************************/
       // five studs, step 72 degree (72*5=360 degree).
       for (i=0; i<5; i++) {
            glPushMatrix();
            glRotatef(72*i,0.0,0.0,1.0); // rotate coordinate 72 degree.
            glTranslatef(0.04, 0.0, 0.0);// translate on the rotated coordinate.
            whiteStud(); // draw the stud.
            glPopMatrix();
            } 
            glTranslatef(0.0, 0.0, 0.0);// translate in order to draw a stud at the center of the wheel.
            whiteStud();// draw the stud at the center of the wheel.
            /**********************************/
            /* don't wait! start processing buffered OpenGL routines */
            glFlush();
            glPopMatrix();
            /**********************************/

   }


int main(int argc, char** argv)

   {
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
      glutInitWindowSize(400, 400);
      glutInitWindowPosition(10, 10);
      glutCreateWindow("(-Rotating Car Wheel-)");
      /* select clearing (background) color */
      glClearColor(1.0, 1.0,1.0, 1.0);
      /* initialize viewing values */
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      glutDisplayFunc(wheelWithStuds);
      glutMainLoop();
      return 0;
   }

我想让这个轮子单独旋转而不点击鼠标的左键,我该怎么办呢?

1 个答案:

答案 0 :(得分:1)

这是一个具有所需动作的新draw_wheel()。值得注意的是,您在绘制方法结束时忘记了glutPostRedisplay();这个函数告诉glut重绘窗口。此外,您没有重置第一次拨打glTranslatef(),因此每次点击窗口时,对象都会远离其原始位置。

void draw_wheel()
{  
    int iTimeElapsed = glutGet(GLUT_ELAPSED_TIME);
    float fRevolveScale1 = 0.2f;
    float fRevolveScale2 = 0.4f;
    long i;

    // clear all pixels
    glClear(GL_COLOR_BUFFER_BIT);

    // push temp state
    glPushMatrix();

    // translate to center
    glTranslatef(0.5f, 0.5f, 0.0); 

    // rotate around pivot
    glRotatef(iTimeElapsed * fRevolveScale1,0.0,0.0,1.0);

    // translate to planet location
    glTranslatef(0.25f, 0.25f, 0.0); 

    glRotatef(iTimeElapsed * fRevolveScale2,0.0,0.0,1.0);

    glColor3f(0.129412f, 0.129412f, 0.129412f);
    glutSolidSphere(0.1f, 16, 16);

    // five bolts, step 72 degree (72*5=360 degree)
    glColor3f(1.0, 1.0, 1.0);
    for(i=0; i<5; ++i) 
    {
        glPushMatrix();
        glRotatef(72.0f*i,0.0,0.0,1.0); // rotate coordinate 72 degree
        glTranslatef(0.04f, 0.0, 0.0);// translate on the rotated coordinate
        glutSolidSphere(0.01f, 16, 16);
        glPopMatrix();
    }

    glTranslatef(0.0f, 0.0f, 0.0f);// translate on the rotated coordinate
    glutSolidSphere(0.01, 16, 16);

    // pop temp state
    glPopMatrix();

    glFlush();
    glutPostRedisplay();
}