Akari
Akari

Reputation: 856

How to let a wheel to rotate around its center in (GLUT) openGL without pressing any key or clicking the mouse button

I need to make this wheel to make a rotation animation around its center in openGL continuously without clicking the mouse,because the wheel makes its rotation if the left button of the mouse is clicked!!, this is the wheel:

enter image description here

This is the code I used to draw the wheel and to make the rotation:

#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;
   }

I want this wheel to rotate by itself without clicking the left button of the mouse, how can I perform this?

Upvotes: 1

Views: 5628

Answers (1)

Lokno
Lokno

Reputation: 624

Here is a new draw_wheel() with the desired motion. Notably, you forgot glutPostRedisplay() at the end of the draw method; this function tells glut to redraw the window. Also you were not resetting your first call to glTranslatef(), so every time you clicked the window the object got further away from its original position.

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();
}

Upvotes: 1

Related Questions