Search code examples
c++openglopengl-compatglrotate

How to rotate only a specific object without affecting the others in opengl?


I'm making a fan in OpenGL, and I have in it the pole and the things that rotate, when I try to rotate them, all the fan rotate with the pole, how im supposed to fix it, I used glutPostRedisplay(), also when I use push and pup matrix in rotation, it doesn't rotate at all, it rotate only once with the angle I have written, any advices help??


Solution

  • I use push and pup matrix in rotation, it doesnt rotate at all, it rotate only once

    I f you use glPushMatrix / glPopMatrix, then the current matrix is stored on the matrix stack by glPushMatrix and restored when glPopMatrix is called. All matrix transformations which are applied in between are lost.

    To achieve a continuously rotation, you've to use an increasing rotation angle. Create a global variable angle and increment it. e.g:

    float angle = 0.0f;
    
    void draw()
    {
        // ...
    
        glPushMatrix();
    
        // apply rotation and increase angle
        glRotatef(angle, 1.0f, 0.0f, 0.0f);
        angle += 1.0f;
    
        // draw the object
        // ...
    
        glPopMatrix();
    
        // ...
    }