C++/OpenGL - Rotating a rectangle
c++, opengl, rotation
Solution
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);
Problem
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