GlRotatef does not work correctly

I am new to openGl. I am doing a very basic thing. Just want to rotate the object around the x axis by 20.0 degrees. But instead of spinning, it moves up.

Can someone help me where I am doing wrong.

Below is my code,

void drawScene(){

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glColor3f(1.0f, 0.0f, 0.0f);
    glPushMatrix();
    //glTranslatef(1.0f,0.0f,0.0f);
    glRotatef(20.0f,1.0f,0.0f,0.0f);

    glBegin(GL_QUADS);
    glVertex3f(-0.7f, -0.5f, -5.0f);
    glVertex3f(0.7f, -0.5f, -5.0f);
    glVertex3f(0.4f, 0.5f, -5.0f);
    glVertex3f(-0.4f, 0.5f, -5.0f);
    glEnd();
    glPopMatrix();

        glutSwapBuffers();
    }
+3
source share
2 answers

You can see the effect of your rotation if you center your object on the coordinate axes. In each glVertex3f call, change the Z value from -0.5f to 0.f, and you will see the rotation around the X axis that you expected.

0
source

, . , -5.0 Z. (0, 0, 0) . , .

- , , . :

glTranslatef(0.0f, 0.0f, -5.0f); //Translate back to the original location.
glRotatef(...);                  //Rotate.
glTranslatef(0.0f, 0.0, 5.0f);   //Translate to the origin

, .

Update

, -. , . " ".

, , . , , , , .

+10

All Articles