C ++ / OpenGL - rectangle rotation

For my project, I needed to rotate the rectangle. I thought it would be easy, but I get unpredictable behavior when it starts.

Here is the code:

    glPushMatrix();
    glRotatef(30.0f, 0.0f, 0.0f, 1.0f);
    glTranslatef(vec_vehicle_position_.x, vec_vehicle_position_.y, 0);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glBegin(GL_QUADS);

        glTexCoord2f(0.0f, 0.0f);
        glVertex2f(0, 0);
        glTexCoord2f(1.0f, 0.0f);
        glVertex2f(width_sprite_, 0);
        glTexCoord2f(1.0f, 1.0f);
        glVertex2f(width_sprite_, height_sprite_);
        glTexCoord2f(0.0f, 1.0f);
        glVertex2f(0, height_sprite_);

    glEnd();
    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
    glPopMatrix();

The problem with this is that my rectangle is translating somewhere in the window during rotation. In other words, the rectangle does not retain the position: vec_vehicle_position_.xand vec_vehicle_position_.y.

What is the problem?

thanks

+5
source share
4 answers

You need to reverse the order of your conversions:

glRotatef(30.0f, 0.0f, 0.0f, 1.0f);
glTranslatef(vec_vehicle_position_.x, vec_vehicle_position_.y, 0);

becomes

glTranslatef(vec_vehicle_position_.x, vec_vehicle_position_.y, 0);
glRotatef(30.0f, 0.0f, 0.0f, 1.0f);
+10
source

Expand previous answers.

Conversions in OpenGL are performed using matrix multiplication. In your example, you have:

M_r_ -

M_t_ -

v -

:

M_r_ * M_t_ * v

:

(M_r_ * (M_t_ * v))

, , . , , , , . , , , , ( ).

, , , .

+9

. . , .

:
glRotate();
glTranslate();
glScale();
drawMyThing();

, , . " ", , . , , .

+3

, .

+1

All Articles