rotate 2D texture/stack of 2D textures

I’m trying to apply rotations to stack of textures (working on volume rendering project). My rendering code looks like that:

static void RenderSceneCB()


    float viewPortSize = 1.0;

    for (int i = 0; i < z; ++i)
        glRotatef(angleX, 1.0, 0.0, 0.0);
        glRotatef(angleY, 0.0, 1.0, 0.0);
        glRotatef(angleZ, 0.0, 0.0, 1.0);


        glBindTexture(GL_TEXTURE_2D, texturesObjects[i]);


        glTexCoord2f(0.0f, 0.0f);
        glVertex3f(-viewPortSize, -viewPortSize, 2 * i / static_cast<float>(z)-1);
        glTexCoord2f(1.0f, 0.0f);
        glVertex3f(viewPortSize, -viewPortSize, 2 * i / static_cast<float>(z)-1);
        glTexCoord2f(1.0f, 1.0f);
        glVertex3f(viewPortSize, viewPortSize, 2 * i / static_cast<float>(z)-1);
        glTexCoord2f(0.0f, 1.0f);
        glVertex3f(-viewPortSize, viewPortSize, 2 * i / static_cast<float>(z)-1);

        glBindTexture(GL_TEXTURE_2D, 0);


Those rotations seem to even work, however while rotating along X axis the object after 90 degrees flips and starts to rotate the other way. Secondly, the object magically stretches and does not fit in viewport sometimes. Additionally, I drew 3 vectors imitating axises, and I see that while rotating along X axis or Y axis all of them are floating. Only while rotating along Z axis that vector representing Z axis stands still like it should. I cannot figure out why is this happening.

What you are experiencing is called “Gimbal Lock” since you are doing “roll/pitch/yaw” with Euler Angles. Because the rotations are done in turn, when one axis causes a rotation which aligns itself parallel another axes you lose control along the second axis. For other rotation options, there’s a lot of good work on quaternions you can use if you google for it. I would guess in your project you probably want user interaction, so researching “track ball rotations” might be the best fit for you.

If the object stretches near the edge of the frame, your perspective is experiencing bending due to effects of the field of view angle. Lowering your field of view angle for your camera can compensate for this. If you post a picture you can help us a lot here.