Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++/OpenGL - Rotating a rectangle

For my project i needed to rotate a rectangle. I thought, that would be easy but i'm getting an unpredictable behavior when running it..

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 that, is that my rectangle is making a translation somewhere in the window while rotating. In other words, the rectangle doesn't keep the position : vec_vehicle_position_.x and vec_vehicle_position_.y.

What's the problem ?

Thanks

like image 870
Amokrane Chentir Avatar asked Jun 17 '09 19:06

Amokrane Chentir


1 Answers

You need to flip the order of your transformations:

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);
like image 156
Andrew Garrison Avatar answered Oct 11 '22 20:10

Andrew Garrison